Skip to content

Incorrect dead code range for null-aware method call with null target #60364

@stereotype441

Description

@stereotype441

Consider the following code:

void f(Null n, int i) {
  n?.foo(i);
  print('reached');
}

main() {
  f(null, 0);
}

When run through the analyzer using --format=json, the following warning is reported (I've added line breaks to make the output easier to read):

{
  "version":1,
  "diagnostics":[
    {
      "code":"dead_code",
      "severity":"WARNING",
      "type":"STATIC_WARNING",
      "location":{
	"file":"/home/paulberry/tmp/proj/test.dart",
	"range":{
	  "start":{"offset":32,"line":2,"column":9},
	  "end":{"offset":56,"line":3,"column":20}
	}
      },
      "problemMessage":"Dead code.",
      "correctionMessage":"Try removing the code, or fixing the code before it so that it can be reached.",
      "documentation":"https://dart.dev/diagnostics/dead_code"
    }
  ]
}

Note that the starting offset is at line 2 column 9 (the ( of the line n?.foo(i);), which isn't great, but it's not awful. (Column 6 would be better, since it would include the identifier foo as part of the unreachable code).

The ending offset is line 3 column 20 (the end of the line print('reached');). This is wholly incorrect; it implies that the line print('reached'); is dead code, which it definitely isn't.

Metadata

Metadata

Assignees

Labels

P3A lower priority bug or feature requestarea-dart-modelFor issues related to conformance to the language spec in the parser, compilers or the CLI analyzer.model-flowImplementation of flow analysis in analyzer/cfe

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions