google / closure-compiler

A JavaScript checker and optimizer.
https://developers.google.com/closure/compiler/
Apache License 2.0
7.4k stars 1.15k forks source link

Validate returned value usage for functions not annotated with @return #466

Open crdev opened 10 years ago

crdev commented 10 years ago

Here's the case:

function foo()
{
  // do something
  // do not return any value
}

function bar()
{
  console.log(1 + foo());
}

In this case, the foo() function has no @return annotation, since it does not return anything. However, it used to @return {number}, and bar() contains code relying on this. Now that foo() has been modified to not return anything, the compiler considers its return type as ? (unchecked), and hence does not detect the error in bar().

nicks commented 10 years ago

i think the real bug here is just that 1 + undefined should be a type error. the compiler is pretty good about figuring out when a function doesn't return anything

crdev commented 10 years ago

Yeah, sounds about right, but the compiler seems to skip this check...

concavelenz commented 10 years ago

1 - foo() produces a type error. Clearly it is being to conservation about warning about string concats.