Skip to content

Commit 377b04f

Browse files
SONARPY-1745 Update code syntax to match Java 17 (#1753)
1 parent 820c44a commit 377b04f

File tree

64 files changed

+199
-271
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+199
-271
lines changed

python-checks-testkit/src/main/java/org/sonar/python/checks/utils/PythonCheckVerifier.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.nio.file.Path;
2525
import java.util.Collections;
2626
import java.util.List;
27-
import java.util.stream.Collectors;
2827
import javax.annotation.Nullable;
2928
import org.sonar.plugins.python.api.IssueLocation;
3029
import org.sonar.plugins.python.api.PythonCheck;
@@ -49,8 +48,8 @@ private PythonCheckVerifier() {
4948

5049
private static List<PreciseIssue> scanFileForIssues(PythonCheck check, PythonVisitorContext context) {
5150
check.scanFile(context);
52-
if (check instanceof PythonSubscriptionCheck) {
53-
SubscriptionVisitor.analyze(Collections.singletonList((PythonSubscriptionCheck) check), context);
51+
if (check instanceof PythonSubscriptionCheck subscriptionCheck) {
52+
SubscriptionVisitor.analyze(Collections.singletonList(subscriptionCheck), context);
5453
}
5554
return context.getIssues();
5655
}
@@ -65,14 +64,14 @@ public static void verifyNoIssue(String path, PythonCheck check) {
6564
}
6665

6766
public static void verify(List<String> paths, PythonCheck check) {
68-
List<File> files = paths.stream().map(File::new).collect(Collectors.toList());
67+
List<File> files = paths.stream().map(File::new).toList();
6968
File baseDirFile = new File(files.get(0).getParent());
7069
ProjectLevelSymbolTable projectLevelSymbolTable = TestPythonVisitorRunner.globalSymbols(files, baseDirFile);
7170
createVerifier(files, check, projectLevelSymbolTable, baseDirFile).assertOneOrMoreIssues();
7271
}
7372

7473
public static void verifyNoIssue(List<String> paths, PythonCheck check) {
75-
List<File> files = paths.stream().map(File::new).collect(Collectors.toList());
74+
List<File> files = paths.stream().map(File::new).toList();
7675
File baseDirFile = new File(files.get(0).getParent());
7776
ProjectLevelSymbolTable projectLevelSymbolTable = TestPythonVisitorRunner.globalSymbols(files, baseDirFile);
7877
createVerifier(files, check, projectLevelSymbolTable, baseDirFile).assertNoIssues();

python-checks/src/main/java/org/sonar/python/checks/AllBranchesAreIdenticalCheck.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,11 @@ private static boolean containsPossibleSideEffect(Expression expression) {
178178
if (expression.is(Tree.Kind.CALL_EXPR)) {
179179
return true;
180180
}
181-
if (expression instanceof BinaryExpression) {
182-
BinaryExpression binaryExpression = (BinaryExpression) expression;
181+
if (expression instanceof BinaryExpression binaryExpression) {
183182
return containsPossibleSideEffect(binaryExpression.leftOperand()) || containsPossibleSideEffect(binaryExpression.rightOperand());
184183
}
185-
if (expression instanceof ParenthesizedExpression) {
186-
return containsPossibleSideEffect(((ParenthesizedExpression) expression).expression());
184+
if (expression instanceof ParenthesizedExpression parenthesizedExpression) {
185+
return containsPossibleSideEffect(parenthesizedExpression.expression());
187186
}
188187
return false;
189188
}

python-checks/src/main/java/org/sonar/python/checks/ArgumentNumberCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private static void checkPositionalParameters(SubscriptionContext ctx, CallExpre
8989

9090
List<RegularArgument> arguments = callExpression.arguments().stream()
9191
.map(RegularArgument.class::cast)
92-
.collect(Collectors.toList());
92+
.toList();
9393
long nbPositionalArgs = arguments.stream().filter(a -> a.keywordArgument() == null).count();
9494
long nbNonKeywordOnlyPassedWithKeyword = arguments.stream()
9595
.map(RegularArgument::keywordArgument)

python-checks/src/main/java/org/sonar/python/checks/BackslashInStringCheck.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,10 @@ public void initialize(Context context) {
5757
private static boolean isBackslashedSpaceAfterInlineMarkup(boolean isThreeQuotes, String string, int position, char current) {
5858
if (isThreeQuotes && current == ' ' && position > 6) {
5959
char twoCharactersBefore = string.charAt(position - 2);
60-
switch (twoCharactersBefore) {
61-
case '`':
62-
case '*':
63-
case '_':
64-
case '|':
65-
return true;
66-
default:
67-
return false;
68-
}
60+
return switch (twoCharactersBefore) {
61+
case '`', '*', '_', '|' -> true;
62+
default -> false;
63+
};
6964
}
7065
return false;
7166
}

python-checks/src/main/java/org/sonar/python/checks/BuiltinGenericsOverTypingModuleCheck.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ private static void checkForTypingModule(SubscriptionContext subscriptionContext
7272
}
7373

7474
private static void checkForGenericsFromTypingModule(SubscriptionContext subscriptionContext, Expression expression) {
75-
if (expression instanceof SubscriptionExpression) {
76-
SubscriptionExpression subscriptionExpression = (SubscriptionExpression) expression;
75+
if (expression instanceof SubscriptionExpression subscriptionExpression) {
7776
getGenericsCounterPartFromTypingModule(subscriptionContext, subscriptionExpression)
7877
.ifPresent(preferredGenerics -> raiseIssueForGenerics(subscriptionContext, subscriptionExpression, preferredGenerics));
7978
}

python-checks/src/main/java/org/sonar/python/checks/BuiltinShadowingAssignmentCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private static PythonQuickFix createQuickFix(Symbol symbol) {
173173
.map(Usage::tree)
174174
.map(Tree::firstToken)
175175
.map(token -> TextEditUtils.insertBefore(token, RENAME_PREFIX))
176-
.collect(Collectors.toList());
176+
.toList();
177177

178178
return PythonQuickFix.newQuickFix(String.format(QUICK_FIX_MESSAGE_FORMAT, symbol.name()))
179179
.addTextEdit(edits)

python-checks/src/main/java/org/sonar/python/checks/ChangeMethodContractCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private static void reportOnMissingParameters(SubscriptionContext ctx, FunctionS
105105
var missingParameters = overriddenParams.subList(indexFirstMissingParam, overriddenParams.size())
106106
.stream()
107107
.map(FunctionSymbol.Parameter::name)
108-
.collect(Collectors.toList());
108+
.toList();
109109
if (!missingParameters.isEmpty()) {
110110
// If at least one parameter missing name - set message as "Add missing N parameters" where N is amount of missing parameters
111111
reportIssue(ctx, getMissingParametersMessage(missingParameters), method.definitionLocation(), overriddenMethod);

python-checks/src/main/java/org/sonar/python/checks/ChildAndParentExceptionCaughtCheck.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ private static PythonQuickFix createQuickFix(Expression currentException) {
148148
}
149149

150150
private static void addExceptionExpression(Expression exceptionExpression, Map<ClassSymbol, List<Expression>> caughtExceptionsByFQN) {
151-
if (exceptionExpression instanceof HasSymbol) {
152-
Symbol symbol = ((HasSymbol) exceptionExpression).symbol();
151+
if (exceptionExpression instanceof HasSymbol hasSymbol) {
152+
Symbol symbol = hasSymbol.symbol();
153153
if (symbol != null && symbol.kind().equals(Symbol.Kind.CLASS)) {
154154
ClassSymbol classSymbol = (ClassSymbol) symbol;
155155
caughtExceptionsByFQN.computeIfAbsent(classSymbol, k -> new ArrayList<>()).add(exceptionExpression);

python-checks/src/main/java/org/sonar/python/checks/ConfusingTypeCheckingCheck.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ private static boolean isUsedInsideInExpression(Expression subscriptionObject) {
125125

126126
private static boolean isRightOperandInExpression(Tree tree) {
127127
Tree parent = tree.parent();
128-
if (parent instanceof InExpression) {
129-
return ((InExpression) parent).rightOperand() == tree;
128+
if (parent instanceof InExpression inExpression) {
129+
return inExpression.rightOperand() == tree;
130130
}
131131
return false;
132132
}

python-checks/src/main/java/org/sonar/python/checks/DuplicatedMethodImplementationCheck.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ private static void addQuickFix(PreciseIssue issue, FunctionDef originalMethod,
135135

136136
private static boolean isClassOrStaticMethod(FunctionDef originalMethod) {
137137
return originalMethod.decorators().stream()
138-
.anyMatch(d -> d.expression() instanceof NameImpl
139-
&& CLASS_AND_STATIC_DECORATORS.contains(((NameImpl) d.expression()).name()));
138+
.anyMatch(d -> d.expression() instanceof NameImpl nameImpl
139+
&& CLASS_AND_STATIC_DECORATORS.contains(nameImpl.name()));
140140
}
141141

142142
private static class MethodVisitor extends BaseTreeVisitor {

0 commit comments

Comments
 (0)