liveBook · Manning

Static-analysis warning

As explained in 100 Java Mistakes and How to Avoid Them

A static analyzer warning is a diagnostic reported by static analysis that is irrelevant, excessive, or otherwise not useful to the developer.

A static-analysis warning is a diagnostic that is irrelevant, excessive, or otherwise unhelpful in a particular context. Static analyzers can prevent common mistakes, but their findings require judgment rather than automatic acceptance. section 1.3.4 section 1.8

Intentional unused collection

An analyzer may report that debugData is written but never read. If the collection exists deliberately for debugging or memory-dump analysis, suppress the warning locally and document the reason. section 1.3.4

Reduce unwanted warnings without hiding useful diagnostics:

  • Configure an inspection or disable it selectively for a project or specific code area. section 1.3.4
  • Prefer a suppression over disabling an entire inspection when the exception is narrow and intentional. section 1.3.4
  • Apply suppressions to the narrowest possible scope; broad method-level annotations can hide later, more useful warnings. section 1.3.4
  • Rearrange code to make its intent easier for the analyzer to understand, and use code reviews, dynamic analysis, and tests alongside static analysis. section 1.8
//Collection is never read: intended (used for debug)
//noinspection MismatchedQueryAndUpdateOfCollection
List<String> debugData = new ArrayList<>();

Shows a narrow IntelliJ IDEA suppression for an intentionally unread collection. section 1.3.4

Table 1.1 Suppression of bad shift amount in different static analyzers
Static analyzer
Suppression annotation
IntelliJ IDEA
@SuppressWarnings("ShiftOutOfRange")
Error Prone
@SuppressWarnings("BadShiftAmount")
SonarLint
@SuppressWarnings("squid:S2183")
SpotBugs
@SuppressFBWarnings("ICAST_BAD_SHIFT_AMOUNT")

Compares suppression identifiers across IntelliJ IDEA, Error Prone, SonarLint, and SpotBugs. section 1.3.4

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains configuration, suppression scope, rationale, and analyzer-specific identifiers, but stops short of diagnostic implementation internals.
Examples
Uses an intentionally unread debug collection and compares bad-shift suppression identifiers across analyzers.
What you can do after
Operate/configure analyzers, choose narrow suppressions, document exceptions, and combine diagnostics with other quality techniques.
How it connects
Ties static analysis to code reviews, dynamic analysis, automated testing, unit testing, and test coverage.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.