tools icon indicating copy to clipboard operation
tools copied to clipboard

๐Ÿ› Analyzer: too many diagnostics for`js/noDeadCode`

Open Boshen opened this issue 2 years ago โ€ข 4 comments

Environment information

Playground

What happened?

Given dead code separated by control blocks, it outputs every single node/identifier/statement as a separate diagnostic.

function foo() {
  return

  if (x) {
    a
  }

  bar()

  while(y) {}

  b
}
warning[[js/noDeadCode](https://rome.tools/docs/lint/rules/noDeadCode/)]: This code is unreachable
  โ”Œโ”€ main.js:4:7
  โ”‚
2 โ”‚   return
  โ”‚   ------ This statement will return from the function ...
3 โ”‚ 
4 โ”‚   if (x) {
  โ”‚       - ... before it can reach this code

warning[[js/noDeadCode](https://rome.tools/docs/lint/rules/noDeadCode/)]: This code is unreachable
  โ”Œโ”€ main.js:5:5
  โ”‚
2 โ”‚   return
  โ”‚   ------ This statement will return from the function ...
  ยท
5 โ”‚     a
  โ”‚     - ... before it can reach this code

warning[[js/noDeadCode](https://rome.tools/docs/lint/rules/noDeadCode/)]: This code is unreachable
  โ”Œโ”€ main.js:8:3
  โ”‚
2 โ”‚   return
  โ”‚   ------ This statement will return from the function ...
  ยท
8 โ”‚   bar()
  โ”‚   ----- ... before it can reach this code

warning[[js/noDeadCode](https://rome.tools/docs/lint/rules/noDeadCode/)]: This code is unreachable
   โ”Œโ”€ main.js:10:9
   โ”‚
 2 โ”‚   return
   โ”‚   ------ This statement will return from the function ...
   ยท
10 โ”‚   while(y) {}
   โ”‚         - ... before it can reach this code

warning[[js/noDeadCode](https://rome.tools/docs/lint/rules/noDeadCode/)]: This code is unreachable
   โ”Œโ”€ main.js:12:3
   โ”‚
 2 โ”‚   return
   โ”‚   ------ This statement will return from the function ...
   ยท
12 โ”‚   b
   โ”‚   - ... before it can reach this code

Expected result

Preferably a single diagnostic for a block of continuous statements.

Let me shamefully show the result of my implementation ...

image

Code of Conduct

  • [X] I agree to follow Rome's Code of Conduct

Boshen avatar Jul 29 '22 06:07 Boshen

This is more likely a suggestion rather than a bug, @ematipico, what do you think? Do we need to add another issue type?

IWANABETHATGUY avatar Jul 29 '22 06:07 IWANABETHATGUY

This is more likely a suggestion rather than a bug, @ematipico, what do you think? Do we need to add another issue type?

For suggestions, we should use GitHub discussions. We have a "suggestions" category.

@Boshen

While I might agree that the messaging can be verbose, I would argue that the counter proposal is too slim for Rome's standards. We pledge to always explain the reason of an error, trying to point to the source of the error.

ematipico avatar Jul 29 '22 07:07 ematipico

It's actually not about the explanation.

If you look closely, Rome is currently reporting a separate "before it can reach this code" for every single node/identifier/statement. My suggestion is to combine these into a single reporting span.

I found this from a real case here in lodash, where we get a screenful of diagnostics ...

Boshen avatar Jul 29 '22 07:07 Boshen

This is partially due to a technical limitation in the way the Language Server Protocol represents diagnostics: in order to mark code as "useless" in the editor we need to emit a diagnostic covering that range, but a single diagnostic may only cover one continuous range at once. So when multiple statement or expressions are marked as unreachable in a function, the noDeadCode rule will merge contiguous diagnostics but if two unreachable ranges do not immediately follow each other the rule needs to emit multiple diagnostics instead. Now in this case I agree that the logical range of code that's unreachable is single unit covering everything after the return statement and we should try to improve the range merging algorithm to support this by propagating unreachable ranges to the outer node. This would need to be implemented on a per-node basis, but the corresponding logic for JsWhileStatement for instance would be that if both the test expression and the entire body are covered by an unreachable range, these two ranges get merged into a single one covering the entire while loop (and running this logic in a loop so this new range recursively gets merged with the unreachable range covering the rest of the function body)

leops avatar Jul 29 '22 07:07 leops

This issue is stale because it has been open 14 days with no activity.

github-actions[bot] avatar Sep 08 '22 12:09 github-actions[bot]

@leops is there something we can do to close this possible bug? If not a bug, then we should just close it

ematipico avatar Sep 08 '22 12:09 ematipico

@leops is there something we can do to close this possible bug? If not a bug, then we should just close it

I tried to come up with a solution to this a few weeks ago, and I think it would require more time to finish it. I still think this is important though, given how much work is required to implement the solution I would probably re-qualify this from a bug to a missing feature

leops avatar Sep 08 '22 13:09 leops

This issue is stale because it has been open 14 days with no activity.

github-actions[bot] avatar Sep 23 '22 12:09 github-actions[bot]