SonarJS icon indicating copy to clipboard operation
SonarJS copied to clipboard

Fix FP S3403 (`different-types-comparison`): Ignore when comparing to `null` or `undefined`

Open ilia-kebets-sonarsource opened this issue 1 year ago • 1 comments

Examples:

  • https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYfLCI8V8rsgv4ohPCTc
  • https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYfLCwbC8rsgv4ohPC25
  • https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYfLCwbD8rsgv4ohPC26
  • https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYfLDJOU8rsgv4ohPPir
  • https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYpEPL6JA8vuoRjT2MwJ
  • https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYpEPLyCA8vuoRjT2MUf
  • https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYpEPLyCA8vuoRjT2MUg
  • https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYpEPLsuA8vuoRjT2MCq
  • https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYpEPLsuA8vuoRjT2MCr
  • https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYtK6aGR4lU6anUcOmw8

ilia-kebets-sonarsource avatar Jan 24 '24 15:01 ilia-kebets-sonarsource

Can we re-discuss this issue just to make sure that it is legit to add an exception?

  • With types, if strictNullChecks is disabled, the rule never triggers because T is analogous to T | null | undefined in this case
  • With types, if strictNullChecks is enabled, the rule never triggers because the compiler would not accept a variable of type T to be assigned null or undefined rendering the comparison body not reachable anyway
  • Without types and without JSDoc annotations, the rule is never triggered because T is analogous to T | null | undefined in this case
  • With JSDoc annotations, the rule is triggered when the JSDoc-declared type does not intersect with null, when compared to null, or undefined when compared to undefined. Hence, in this case, the issue comes from an incorrect JSDoc annotation instead of a rule false positive (for example, about https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYpEPL6JA8vuoRjT2MwJ, see https://github.com/WordPress/gutenberg/blob/trunk/packages/rich-text/src/types.ts)
  • With pure JavaScript - no tsconfig.json file, the rule is never triggered.

ericmorand-sonarsource avatar Feb 05 '24 08:02 ericmorand-sonarsource

Migrated to Jira. New ticket number: JS-36

Wohops avatar Apr 24 '24 15:04 Wohops