Summary
Refine the -Werror
flag to support lint categories just like -Xlint
does. So, for example -Werror:all,-preview
would cause an error if any warning other than a preview
warning occurred.
Problem
-Xlint
allows one to customize which warnings should appear and which should be hidden. This caters to different needs in different scenarios - a "one size fits all" approach is too limiting.
The same logic applies to -Werror
: there are scenarios where some warnings are more salient than others to whether a build should fail. Allowing -Werror
to function like -Xlint
with per-lint category customizations would accomodate this.
Solution
First, preserve the existing behavior:
- If no
-Werror
flag of any kind appears at all, then no warning will trigger an error. - If only the
-Werror
flag is given, then any warning will trigger an error.
Next, add support for "custom" flags like -Werror:all
, -Werror:none
, and -Werror:foo,-bar
.
These flags will be parsed and interpreted exactly like the corresponding -Xlint
flags, with one exception: If no -Xlint
flag appears at all, then a non-empty set of default lint warning categories is enabled, whereas if no -Werror
flag appears at all, then the default behavior is as it is today, namely, no warning will trigger an error (i.e., the default set is empty).
Note that the existing -Xlint
flag has a particular behavior when conflicting flags are combined. For example, -Xlint:all -Xlint:none -Xlint:foo -Xlint:-foo
equals -Xlint:all
. This same behavior has been preserved and carried over to -Werror
.
Corner Case #1
Currently -Werror
turns every warning into an error, even warnings that have no associated lint category. So that raises a question, does -Werror:all
also turn every warning into an error, or only those warnings that have an associated lint category?
The conservative choice here (i.e., more errors) is the former choice, i.e., make -Werror:all
behave just like -Werror
, so that's how we implement it. This is a slight breach of orthogonality; however, it addresses the likely and reasonable developer belief that -Werror:all
should be "at least as strong as" -Werror
. In particular, when a developer discovers one particular lint category foo
is causing build failures and they want to disable that category, they are likely going to replace their existing -Werror
flag with -Werror:all,-foo
. If they do, we don't want them to unknowingly disable all non-lint warnings from generating errors.
Corner Case #2
What about a seemingly contradictory combination like -Werror:serial -Xlint:-serial
? This means what it says, i.e., disable serial
warnings but error out if any serial
warnings occur: but the "error out if any serial
warnings occur" will never apply because that will never happen.
Specification
Here is the diff of the output of the command javac --help && javac --help-extra && javac --help-lint
:
--- x0 2025-02-13 15:32:33.390676169 -0600
+++ x1 2025-02-13 15:32:57.119988923 -0600
@@ -56,24 +56,28 @@
Override location of upgradeable modules
-verbose Output messages about what the compiler is doing
--version, -version Version information
- -Werror Terminate compilation if warnings occur
+ -Werror Terminate compilation if any warnings occur
+ -Werror:<key>(,<key>)*
+ Specify warnings that should terminate compilation, separated by comma.
+ Precede a key by '-' to exclude the specified warning.
+ Use --help-lint to see the supported keys.
--add-exports <module>/<package>=<other-module>(,<other-module>)*
Specify a package to be considered as exported from its
@@ -87,10 +91,10 @@
Disable support for documentation comments with lines beginning '///'
-Djava.endorsed.dirs=<dirs> Override location of endorsed standards path
-Djava.ext.dirs=<dirs> Override location of installed extensions
- --help-lint Print the supported keys for -Xlint
+ --help-lint Print the supported keys for -Xlint and -Werror
--patch-module <module>=<file>(:<file>)*
Override or augment a module with classes and resources
in JAR files or directories
@@ -134,7 +138,7 @@
-Xstdout <filename> Redirect standard output
These extra options are subject to change without notice.
-The supported keys for -Xlint are:
+The supported keys for -Xlint and -Werror are:
all Enable all warnings
auxiliaryclass Warn about an auxiliary class that is hidden in a source file, and is used from other files.
cast Warn about use of unnecessary casts.
Here's the man page diff (source):
@@ -448,7 +448,12 @@ ### Standard Options
: Prints version information.
<a id="option-Werror">`-Werror`</a>
-: Terminates compilation when warnings occur.
+: Terminates compilation when any warnings occur; this includes warnings in all lint
+ categories, as well as non-lint warnings.
+
+<a id="option-Werror-custom">`-Werror:`\[`-`\]*key*(`,`\[`-`\]*key*)\*</a>
+: Enables and/or disables specific categories of lint warnings that should terminate compilation.
+ See [`-Xlint`](#option-Xlint-custom) below for the list of lint category keys.
### Extra Options
Here's the man page diff (troff):
--- x0 2025-05-12 20:47:27.668158862 -0500
+++ x1 2025-05-12 20:47:21.547802454 -0500
@@ -7,7 +7,13 @@
Prints version information.
‐Werror
- Terminates compilation when warnings occur.
+ Terminates compilation when any warnings occur; this includes
+ warnings in all lint categories, as well as non‐lint warnings.
+
+ ‐Werror:[‐]key(,[‐]key)*
+ Enables and/or disables specific categories of lint warnings
+ that should terminate compilation. See ‐Xlint below for the
+ list of lint category keys.
Extra Options
‐‐add‐exports module/package=other‐module(,other‐module)*
- csr of
-
JDK-8349847 Support configuring individual lint categories as errors
-
- Open
-