Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hint for missing return when the function is guaranteed to throw #21741

Closed
DartBot opened this issue Nov 27, 2014 · 6 comments
Closed

Hint for missing return when the function is guaranteed to throw #21741

DartBot opened this issue Nov 27, 2014 · 6 comments
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Nov 27, 2014

This issue was originally filed by greg...@gmail.com


Ideally the analyzer would not generate a hint for the function below:

//Hint: This function declares the return type of 'int' but does not end with a return statement.

  int foo() {
    bar() => throw "oi";
    bar();
  }

Here's the actual function that I was writing when I noticed this.

  Duration _parseWithSuffix(String s) {
    fail(s) => throw new FormatException('Cannot parse string as duration: "$s".');
    
    parsePrefix(s, [int suffixLen = 1])
      => int.parse(s.substring(0, s.length-suffixLen), onError: fail);
    
    if (s.endsWith('h')) return new Duration(hours: parsePrefix(s));
    if (s.endsWith('m')) return new Duration(minutes: parsePrefix(s));
    if (s.endsWith('s')) return new Duration(seconds: parsePrefix(s));
    if (s.endsWith('ms')) return new Duration(milliseconds: parsePrefix(s, 2));
    if (s.endsWith('us')) return new Duration(microseconds: parsePrefix(s, 2));
    
    fail(s);
  }

@kasperl
Copy link

kasperl commented Nov 28, 2014

Added Area-Analyzer, Triaged labels.

@bwilkerson
Copy link
Member

While I agree that this would be nice, and potentially possible when dealing with locally defined functions, the full solution (being able to trace all possible execution paths to see whether they all throw an exception) is a bit more than we have resources to do at the moment.

I will note that as a workaround (albeit an ugly workaround) the analyzer recognizes a throw as an exit from the method, so if you inline final invocation of the local function 'fail' the hint will not be generated.


Removed Priority-Unassigned label.
Added Priority-Medium label.

@DartBot DartBot added Type-Defect area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. labels Nov 28, 2014
@nex3
Copy link
Member

nex3 commented Jun 4, 2015

Would it be possible to add an @alwaysThrows annotation that the analyzer understands, sort of like @deprecated? This would be really useful for stuff like the test package's fail() method.

@sethladd
Copy link
Contributor

sethladd commented Jun 4, 2015

Another case for Analyzer plugins! :)

@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed triaged labels Mar 1, 2016
@srawlins
Copy link
Member

CC #17999

@srawlins
Copy link
Member

@alwaysThrows has been implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

7 participants