Skip to content

Commit 3525a40

Browse files
committed
8359596: Behavior change when both -Xlint:options and -Xlint:-options flags are given
Reviewed-by: mcimadamore, uschindler
1 parent 712d866 commit 3525a40

File tree

6 files changed

+89
-35
lines changed

6 files changed

+89
-35
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ private void initializeRootIfNeeded() {
183183

184184
// Look for specific overrides
185185
for (LintCategory lc : LintCategory.values()) {
186-
if (options.isExplicitlyEnabled(Option.XLINT, lc)) {
186+
if (options.isLintExplicitlyEnabled(lc)) {
187187
values.add(lc);
188-
} else if (options.isExplicitlyDisabled(Option.XLINT, lc)) {
188+
} else if (options.isLintExplicitlyDisabled(lc)) {
189189
values.remove(lc);
190190
}
191191
}

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ protected Modules(Context context) {
205205

206206
allowAccessIntoSystem = options.isUnset(Option.RELEASE);
207207

208-
lintOptions = !options.isExplicitlyDisabled(Option.XLINT, LintCategory.OPTIONS);
208+
lintOptions = !options.isLintDisabled(LintCategory.OPTIONS);
209209

210210
multiModuleMode = fileManager.hasLocation(StandardLocation.MODULE_SOURCE_PATH);
211211
ClassWriter classWriter = ClassWriter.instance(context);

src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ public boolean validate() {
503503
}
504504
} else {
505505
// single-module or legacy mode
506-
boolean lintPaths = !options.isExplicitlyDisabled(Option.XLINT, LintCategory.PATH);
506+
boolean lintPaths = !options.isLintDisabled(LintCategory.PATH);
507507
if (lintPaths) {
508508
Path outDirParent = outDir.getParent();
509509
if (outDirParent != null && Files.exists(outDirParent.resolve("module-info.class"))) {
@@ -576,7 +576,7 @@ public boolean validate() {
576576
reportDiag(Errors.SourcepathModulesourcepathConflict);
577577
}
578578

579-
boolean lintOptions = !options.isExplicitlyDisabled(Option.XLINT, LintCategory.OPTIONS);
579+
boolean lintOptions = !options.isLintDisabled(LintCategory.OPTIONS);
580580
if (lintOptions && source.compareTo(Source.DEFAULT) < 0 && !options.isSet(Option.RELEASE)) {
581581
if (fm instanceof BaseFileManager baseFileManager) {
582582
if (source.compareTo(Source.JDK8) <= 0) {

src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -172,55 +172,67 @@ public boolean isUnset(Option option, String value) {
172172
}
173173

174174
/**
175-
* Check whether the given lint category is explicitly enabled or disabled.
175+
* Determine if a specific {@link LintCategory} is enabled via a custom
176+
* option flag of the form {@code -Xlint}, {@code -Xlint:all}, or {@code -Xlint:key}.
176177
*
177178
* <p>
178-
* If the category is neither enabled nor disabled, return the given default value.
179+
* Note: It's possible the category was also disabled; this method does not check that.
179180
*
180-
* @param option the plain (non-custom) option
181181
* @param lc the {@link LintCategory} in question
182-
* @param defaultValue presumed default value
183-
* @return true if {@code lc} would be included
182+
* @return true if {@code lc} has been enabled
184183
*/
185-
public boolean isSet(Option option, LintCategory lc, boolean defaultValue) {
186-
Option customOption = option.getCustom();
187-
if (lc.optionList.stream().anyMatch(alias -> isSet(customOption, alias))) {
188-
return true;
189-
}
190-
if (lc.optionList.stream().anyMatch(alias -> isSet(customOption, "-" + alias))) {
191-
return false;
192-
}
193-
if (isSet(option) || isSet(customOption, Option.LINT_CUSTOM_ALL)) {
194-
return true;
195-
}
196-
if (isSet(customOption, Option.LINT_CUSTOM_NONE)) {
197-
return false;
198-
}
199-
return defaultValue;
184+
public boolean isLintEnabled(LintCategory lc) {
185+
return isLintExplicitlyEnabled(lc) ||
186+
isSet(Option.XLINT_CUSTOM) ||
187+
isSet(Option.XLINT_CUSTOM, Option.LINT_CUSTOM_ALL);
200188
}
201189

202190
/**
203-
* Determine if a specific {@link LintCategory} was explicitly enabled via a custom option flag
204-
* of the form {@code -Flag:all} or {@code -Flag:key}.
191+
* Determine if a specific {@link LintCategory} is disabled via a custom
192+
* option flag of the form {@code -Xlint:none} or {@code -Xlint:-key}.
193+
*
194+
* <p>
195+
* Note: It's possible the category was also enabled; this method does not check that.
196+
*
197+
* @param lc the {@link LintCategory} in question
198+
* @return true if {@code lc} has been disabled
199+
*/
200+
public boolean isLintDisabled(LintCategory lc) {
201+
return isLintExplicitlyDisabled(lc) || isSet(Option.XLINT_CUSTOM, Option.LINT_CUSTOM_NONE);
202+
}
203+
204+
/**
205+
* Determine if a specific {@link LintCategory} is explicitly enabled via a custom
206+
* option flag of the form {@code -Xlint:key}.
207+
*
208+
* <p>
209+
* Note: This does not check for option flags of the form {@code -Xlint} or {@code -Xlint:all}.
210+
*
211+
* <p>
212+
* Note: It's possible the category was also disabled; this method does not check that.
205213
*
206-
* @param option the option
207214
* @param lc the {@link LintCategory} in question
208215
* @return true if {@code lc} has been explicitly enabled
209216
*/
210-
public boolean isExplicitlyEnabled(Option option, LintCategory lc) {
211-
return isSet(option, lc, false);
217+
public boolean isLintExplicitlyEnabled(LintCategory lc) {
218+
return lc.optionList.stream().anyMatch(alias -> isSet(Option.XLINT_CUSTOM, alias));
212219
}
213220

214221
/**
215-
* Determine if a specific {@link LintCategory} was explicitly disabled via a custom option flag
216-
* of the form {@code -Flag:none} or {@code -Flag:-key}.
222+
* Determine if a specific {@link LintCategory} is explicitly disabled via a custom
223+
* option flag of the form {@code -Xlint:-key}.
224+
*
225+
* <p>
226+
* Note: This does not check for an option flag of the form {@code -Xlint:none}.
227+
*
228+
* <p>
229+
* Note: It's possible the category was also enabled; this method does not check that.
217230
*
218-
* @param option the option
219231
* @param lc the {@link LintCategory} in question
220232
* @return true if {@code lc} has been explicitly disabled
221233
*/
222-
public boolean isExplicitlyDisabled(Option option, LintCategory lc) {
223-
return !isSet(option, lc, true);
234+
public boolean isLintExplicitlyDisabled(LintCategory lc) {
235+
return lc.optionList.stream().anyMatch(alias -> isSet(Option.XLINT_CUSTOM, "-" + alias));
224236
}
225237

226238
public void put(String name, String value) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
/*
27+
* @test
28+
* @bug 8359596
29+
* @summary Verify behavior when both "-Xlint:options" and "-Xlint:-options" are given
30+
* @compile/fail/ref=LintOptions.out -Werror -XDrawDiagnostics -source 21 -target 21 LintOptions.java
31+
* @compile/fail/ref=LintOptions.out -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:options LintOptions.java
32+
* @compile -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:-options LintOptions.java
33+
* @compile -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:options -Xlint:-options LintOptions.java
34+
* @compile -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:none LintOptions.java
35+
* @compile -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:options -Xlint:none LintOptions.java
36+
*/
37+
class LintOptions {
38+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- compiler.warn.source.no.system.modules.path: 21, (compiler.misc.source.no.system.modules.path.with.target: 21, 21)
2+
- compiler.err.warnings.and.werror
3+
1 error
4+
1 warning

0 commit comments

Comments
 (0)