diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java index 269d2f5de62..56dfd395aed 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java @@ -440,7 +440,7 @@ public class JavaCompiler { options.isSet(G_CUSTOM, "lines"); devVerbose = options.isSet("dev"); processPcks = options.isSet("process.packages"); - werrorAny = options.isSet(WERROR) || options.isSet(WERROR_CUSTOM, Option.LINT_CUSTOM_ALL); + werrorNonLint = options.isSet(WERROR) || options.isSet(WERROR_CUSTOM, Option.LINT_CUSTOM_ALL); werrorLint = options.getLintCategoriesOf(WERROR, LintCategory::newEmptySet); verboseCompilePolicy = options.isSet("verboseCompilePolicy"); @@ -510,9 +510,9 @@ public class JavaCompiler { */ protected boolean processPcks; - /** Switch: treat any kind of warning (lint or non-lint) as an error. + /** Switch: treat non-lint warnings as errors. Set by either "-Werror" or "-Werror:all". */ - protected boolean werrorAny; + protected boolean werrorNonLint; /** Switch: treat lint warnings in the specified {@link LintCategory}s as errors. */ @@ -583,7 +583,7 @@ public class JavaCompiler { public int errorCount() { log.reportOutstandingWarnings(); if (log.nerrors == 0 && log.nwarnings > 0 && - (werrorAny || werrorLint.clone().removeAll(log.lintWarnings))) { + ((werrorNonLint && log.nonLintWarnings > 0) || werrorLint.clone().removeAll(log.lintWarnings))) { log.error(Errors.WarningsAndWerror); } return log.nerrors; @@ -593,7 +593,7 @@ public class JavaCompiler { * Should warnings in the given lint category be treated as errors due to a {@code -Werror} flag? */ public boolean isWerror(LintCategory lc) { - return werrorAny || werrorLint.contains(lc); + return werrorLint.contains(lc); } protected final Queue stopIfError(CompileState cs, Queue queue) { diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java index 99f71e87df1..dd4e14d373d 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java @@ -558,6 +558,10 @@ public class Log extends AbstractLog { */ public int nwarnings = 0; + /** The total number of non-lint warnings encountered so far. + */ + public int nonLintWarnings = 0; + /** Tracks whether any warnings have been encountered in each {@link LintCategory}. */ public final EnumSet lintWarnings = LintCategory.newEmptySet(); @@ -888,6 +892,7 @@ public class Log extends AbstractLog { lintWarnings.clear(); nerrors = 0; nwarnings = 0; + nonLintWarnings = 0; nsuppressederrors = 0; nsuppressedwarns = 0; while (diagnosticHandler.prev != null) @@ -981,7 +986,7 @@ public class Log extends AbstractLog { nwarnings++; Optional.of(diag) .map(JCDiagnostic::getLintCategory) - .ifPresent(lintWarnings::add); + .ifPresentOrElse(lintWarnings::add, () -> nonLintWarnings++); break; case ERROR: nerrors++; @@ -1121,6 +1126,7 @@ public class Log extends AbstractLog { } prompt(); nwarnings++; + nonLintWarnings++; warnWriter.flush(); } diff --git a/test/langtools/tools/javac/warnings/WerrorLint2.fail1.out b/test/langtools/tools/javac/warnings/WerrorLint2.fail1.out new file mode 100644 index 00000000000..1b0497dfcb0 --- /dev/null +++ b/test/langtools/tools/javac/warnings/WerrorLint2.fail1.out @@ -0,0 +1,4 @@ +WerrorLint2.java:16:30: compiler.warn.empty.if +- compiler.err.warnings.and.werror +1 error +1 warning diff --git a/test/langtools/tools/javac/warnings/WerrorLint2.fail2.out b/test/langtools/tools/javac/warnings/WerrorLint2.fail2.out new file mode 100644 index 00000000000..3977e6318ef --- /dev/null +++ b/test/langtools/tools/javac/warnings/WerrorLint2.fail2.out @@ -0,0 +1,5 @@ +WerrorLint2.java:16:30: compiler.warn.empty.if +WerrorLint2.java:17:28: compiler.warn.diamond.redundant.args +- compiler.err.warnings.and.werror +1 error +2 warnings diff --git a/test/langtools/tools/javac/warnings/WerrorLint2.java b/test/langtools/tools/javac/warnings/WerrorLint2.java new file mode 100644 index 00000000000..c2ec7d370d9 --- /dev/null +++ b/test/langtools/tools/javac/warnings/WerrorLint2.java @@ -0,0 +1,19 @@ +/* + * @test /nodynamiccopyright/ + * @bug 8380971 + * + * @compile/fail/ref=WerrorLint2.fail1.out -XDrawDiagnostics -Xlint:all -Werror:all WerrorLint2.java + * @compile/ref=WerrorLint2.warn1.out -XDrawDiagnostics -Xlint:all -Werror:all,-empty WerrorLint2.java + * @compile/ref=WerrorLint2.warn1.out -XDrawDiagnostics -Xlint:all -Werror:-empty WerrorLint2.java + * @compile/fail/ref=WerrorLint2.fail1.out -XDrawDiagnostics -Xlint:all -XDfind=diamond -Werror:all WerrorLint2.java + * @compile/fail/ref=WerrorLint2.fail2.out -XDrawDiagnostics -Xlint:all -XDfind=diamond -Werror:all,-empty WerrorLint2.java + * @compile/ref=WerrorLint2.warn2.out -XDrawDiagnostics -Xlint:all -XDfind=diamond -Werror:-empty WerrorLint2.java + */ + +class WerrorLint2 { + ThreadLocal t; + void m() { + if (hashCode() == 1) ; // warning: [empty] empty statement after if + t = new ThreadLocal(); // warning: Redundant type arguments in new expression (use diamond operator instead). + } +} diff --git a/test/langtools/tools/javac/warnings/WerrorLint2.warn1.out b/test/langtools/tools/javac/warnings/WerrorLint2.warn1.out new file mode 100644 index 00000000000..91aff9bb948 --- /dev/null +++ b/test/langtools/tools/javac/warnings/WerrorLint2.warn1.out @@ -0,0 +1,2 @@ +WerrorLint2.java:16:30: compiler.warn.empty.if +1 warning diff --git a/test/langtools/tools/javac/warnings/WerrorLint2.warn2.out b/test/langtools/tools/javac/warnings/WerrorLint2.warn2.out new file mode 100644 index 00000000000..c271a489813 --- /dev/null +++ b/test/langtools/tools/javac/warnings/WerrorLint2.warn2.out @@ -0,0 +1,3 @@ +WerrorLint2.java:16:30: compiler.warn.empty.if +WerrorLint2.java:17:28: compiler.warn.diamond.redundant.args +2 warnings