RFR: 8345263: Make sure that lint categories are used correctly when logging lint warnings

Maurizio Cimadamore mcimadamore at openjdk.org
Wed Dec 4 19:13:33 UTC 2024


This PR tightens up the logic by which javac reports lint warnings. Currently, lint warnings are typically emitted using the following idiom:


if (lint.isEnabled(LintCategory.DIVZERO) {
    log.warning(LintCategory.DIVZERO, pos, Warnings.DIVZERO);
}

There are some issues with this approach:

* logging a lint warning has to be preceded by the correct `isEnabled` check
* the check and the `log::warning` call must share the same `LintCategory`
* the selected warning key in the `Warnings` class must also make sense for the selected `LintCategory`

This PR addresses these issues, so that the above code is now written as follows:


lint.logIfEnabled(pos, LintWarnings.DIVZERO);


The new idiom builds on a number of small improvements:

* the lint category is now tracked directly in the `compiler.properties` file;
* a new `LintWarning` class is added to `JCDiagnostic` to model a warning key that is also associated with a speicfic `LintCategory` enum constant;
* the `CompilerProperties` class has a new group of compiler keys, nested in the new `LintWarnings` class. This class defines the `LintWarning` objects for all the warning keys in `compiler.properties` that have a lint category set
* A new method `Lint::logIfEnabled(Position, LintWarning)` is added - which simplifies the logging of lint warnings in many common cases, by merging the `isEnabled` check together with the logging.

As bonus points, the signatures of some methods in `Check` and `MandatoryWarningHandler` have been tightened to accept not just a `Warning`, but a `LintWarning`. This makes it impossible, for instance, to use `Check::warnUnchecked` with a warning that is not a true lint warning.

Many thanks @archiecobbs for the useful discussions!

-------------

Commit messages:
 - Fix regression
 - Separate LintWarnings from Warnings in CompilerProperties
 - Simplify code
 - All langtools tests pass
 - All tests pass
 - Use diagnostic info line to declare lint category
 - Builds ok
 - Tweak warning keys in compiler.properties

Changes: https://git.openjdk.org/jdk/pull/22553/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=22553&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8345263
  Stats: 598 lines in 24 files changed: 234 ins; 110 del; 254 mod
  Patch: https://git.openjdk.org/jdk/pull/22553.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/22553/head:pull/22553

PR: https://git.openjdk.org/jdk/pull/22553


More information about the compiler-dev mailing list