-
Notifications
You must be signed in to change notification settings - Fork 745
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
StatementSwitchToExpressionSwitch: only trigger on compatible target …
…versions This change introduces a `SourceVersion` utility class, analogous to the existing `RuntimeVersion` class. The new class tells whether selected language features are available for the source version used by the compiler. The new utility class is used to make sure that `StatementSwitchToExpressionSwitch` flags statement switches only if the source version supports expression switches, irrespective of whether the current runtime supports such switch types. Concretely this makes the check compatible with projects that target JDK 11, yet support building with JDK 17. Fixes #3646 FUTURE_COPYBARA_INTEGRATE_REVIEW=#3646 from PicnicSupermarket:bugfix/dont-suggest-switch-expressions-pre-jdk14 641c9dc PiperOrigin-RevId: 498755371
- Loading branch information
Showing
4 changed files
with
121 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
check_api/src/main/java/com/google/errorprone/util/SourceVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2023 The Error Prone Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.errorprone.util; | ||
|
||
import com.sun.tools.javac.code.Source; | ||
import com.sun.tools.javac.util.Context; | ||
|
||
/** | ||
* JDK source version utilities. | ||
* | ||
* @see RuntimeVersion | ||
*/ | ||
public final class SourceVersion { | ||
/** Returns true if the compiler source version level supports switch expressions. */ | ||
public static boolean supportsSwitchExpressions(Context context) { | ||
return sourceIsAtLeast(context, "14"); | ||
} | ||
|
||
/** Returns true if the compiler source version level supports text blocks. */ | ||
public static boolean supportsTextBlocks(Context context) { | ||
return sourceIsAtLeast(context, "15"); | ||
} | ||
|
||
private static boolean sourceIsAtLeast(Context context, String versionString) { | ||
Source lowerBound = Source.lookup(versionString); | ||
return lowerBound != null && Source.instance(context).compareTo(lowerBound) >= 0; | ||
} | ||
|
||
private SourceVersion() {} | ||
} |
66 changes: 66 additions & 0 deletions
66
check_api/src/test/java/com/google/errorprone/util/SourceVersionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2023 The Error Prone Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.errorprone.util; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.sun.tools.javac.main.Option; | ||
import com.sun.tools.javac.util.Context; | ||
import com.sun.tools.javac.util.Options; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class SourceVersionTest { | ||
@Test | ||
public void supportsSwitchExpressions_notSupported() { | ||
Context context = contextWithSourceVersion("13"); | ||
|
||
assertThat(SourceVersion.supportsSwitchExpressions(context)).isFalse(); | ||
} | ||
|
||
@Test | ||
public void supportsSwitchExpressions_conditionallySupported() { | ||
Context context = contextWithSourceVersion("14"); | ||
|
||
assertThat(SourceVersion.supportsSwitchExpressions(context)) | ||
.isEqualTo(RuntimeVersion.isAtLeast14()); | ||
} | ||
|
||
@Test | ||
public void supportsTextBlocks_notSupported() { | ||
Context context = contextWithSourceVersion("14"); | ||
|
||
assertThat(SourceVersion.supportsTextBlocks(context)).isFalse(); | ||
} | ||
|
||
@Test | ||
public void supportsTextBlocks_conditionallySupported() { | ||
Context context = contextWithSourceVersion("15"); | ||
|
||
assertThat(SourceVersion.supportsTextBlocks(context)).isEqualTo(RuntimeVersion.isAtLeast15()); | ||
} | ||
|
||
private static Context contextWithSourceVersion(String versionString) { | ||
Context context = new Context(); | ||
|
||
Options options = Options.instance(context); | ||
options.put(Option.SOURCE, versionString); | ||
|
||
return context; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters