Skip to content

Commit fa8935d

Browse files
authored
SONARRUBY-98 clean sonar issues (#89)
1 parent a0df14f commit fa8935d

File tree

17 files changed

+367
-354
lines changed

17 files changed

+367
-354
lines changed

its/plugin/src/test/java/org/sonarsource/slang/ProfileRegistrarTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void testPluginRegistersRuleInDefaultRubyProfile() {
117117
// This rule is registered by the TestProfileRegistrar in the test-plugin
118118
var testRules = rulesResponse.getRulesList().stream()
119119
.filter(rule -> "ruby-test:TEST001".equals(rule.getKey()))
120-
.collect(Collectors.toList());
120+
.toList();
121121

122122
assertThat(testRules)
123123
.as("Rule ruby-test:TEST001 should be registered in the default Ruby profile by TestProfileRegistrar")

sonar-ruby-plugin/src/main/java/org/sonarsource/ruby/checks/UnusedLocalVariableRubyCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void initialize(InitContext init) {
4242

4343
List<IdentifierTree> unusedVariables = variableIdentifiers.stream()
4444
.filter(variable -> identifierTrees.stream().noneMatch(identifier -> SyntacticEquivalence.areEquivalent(variable, identifier)))
45-
.collect(Collectors.toList());
45+
.toList();
4646

4747
if (unusedVariables.isEmpty()) {
4848
return;

sonar-ruby-plugin/src/main/java/org/sonarsource/ruby/converter/RubyConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ Tree parseContent(String content) {
126126
List<Comment> comments = rubyComments.stream()
127127
.map(rubyComment -> new CommentAdapter(runtime, rubyComment))
128128
.map(CommentAdapter::toSlangComment)
129-
.collect(Collectors.toList());
129+
.toList();
130130
List<Token> tokens = rubyTokens.stream()
131131
.map(rubyToken -> new TokenAdapter(runtime, (RubyArrayTwoObject) rubyToken))
132132
.filter(tokenAdapter -> !COMMENT_TOKEN_TYPE.equals(tokenAdapter.getTokenType().asJavaString()))
133133
.map(TokenAdapter::toSlangToken)
134134
.filter(Objects::nonNull)
135-
.collect(Collectors.toList());
135+
.toList();
136136
TreeMetaDataProvider metaDataProvider = new TreeMetaDataProvider(comments, tokens);
137137

138138
if (tokens.isEmpty() && comments.isEmpty()) {

sonar-ruby-plugin/src/main/java/org/sonarsource/ruby/converter/RubyVisitor.java

Lines changed: 34 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,7 @@ public Tree visitNode(AstNode node, List<?> children) {
157157
switch (node.type()) {
158158
case "and":
159159
return createLogicalOperation(node, children, Operator.CONDITIONAL_AND);
160-
case "arg":
161-
case "optarg":
162-
case "restarg":
163-
case "kwarg":
164-
case "kwoptarg":
165-
case "kwrestarg":
166-
case "blockarg":
167-
case "procarg0":
168-
case "shadowarg":
160+
case "arg", "optarg", "restarg", "kwarg", "kwoptarg", "kwrestarg", "blockarg", "procarg0", "shadowarg":
169161
// note obj-c arguments are not supported https://github.com/whitequark/parser/blob/master/doc/AST_FORMAT.md#objective-c-arguments
170162
return createParameterTree(node, children);
171163
case "begin":
@@ -180,23 +172,17 @@ public Tree visitNode(AstNode node, List<?> children) {
180172
return createFromConst(node, children);
181173
case "class":
182174
return createClassDeclarationTree(node, children);
183-
case "def":
184-
case "defs":
175+
case "def", "defs":
185176
return createFunctionDeclarationTree(node, children);
186-
case "cvasgn":
187-
case "gvasgn":
188-
case "ivasgn":
189-
case "lvasgn":
177+
case "cvasgn", "gvasgn", "ivasgn", "lvasgn":
190178
return createFromAssign(node, children);
191179
case "if":
192180
return createIfTree(node, children);
193181
case "indexasgn":
194182
return createFromIndexasgn(node, children);
195183
case "int":
196184
return createIntegerLiteralTree(node);
197-
case "cvar":
198-
case "lvar":
199-
case "ivar":
185+
case "cvar", "lvar", "ivar":
200186
return createFromVar(node, children);
201187
case "masgn":
202188
return createFromMasgn(node, children);
@@ -214,8 +200,7 @@ public Tree visitNode(AstNode node, List<?> children) {
214200
return createLogicalOperation(node, children, Operator.CONDITIONAL_OR);
215201
case "send":
216202
return createFromSendNode(node, children);
217-
case "true":
218-
case "false":
203+
case "true", "false":
219204
return new LiteralTreeImpl(metaData(node), node.type());
220205
case "when":
221206
return createCaseTree(node, children);
@@ -227,11 +212,9 @@ public Tree visitNode(AstNode node, List<?> children) {
227212
return createCatchTree(node, children);
228213
case "ensure":
229214
return updateExceptionHandlingWithFinally(node, children);
230-
case "while_post":
231-
case "until_post":
215+
case "while_post", "until_post":
232216
return createLoopTree(node, children, LoopTree.LoopKind.DOWHILE);
233-
case "while":
234-
case "until":
217+
case "while", "until":
235218
return createLoopTree(node, children, LoopTree.LoopKind.WHILE);
236219
case "for":
237220
return createForLoopTree(node, children);
@@ -276,9 +259,8 @@ private Tree createForLoopTree(AstNode node, List<?> children) {
276259
}
277260

278261
private Tree createFromKwBeginNode(AstNode node, List<?> children) {
279-
if (children.size() == 1 && children.get(0) instanceof RubyPartialExceptionHandlingTree) {
262+
if (children.size() == 1 && children.get(0) instanceof RubyPartialExceptionHandlingTree partialExceptionTree) {
280263
// this begin is used as a "begin...rescue...end" or "begin...ensure...end" block
281-
RubyPartialExceptionHandlingTree partialExceptionTree = (RubyPartialExceptionHandlingTree) children.get(0);
282264
TreeMetaData treeMetaData = metaData(node);
283265
List<CatchTree> catchTrees = partialExceptionTree.catchBlocks();
284266
Tree tryBlock = partialExceptionTree.tryBlock();
@@ -292,7 +274,7 @@ private Tree createFromKwBeginNode(AstNode node, List<?> children) {
292274
if (!treeMetaData.commentsInside().isEmpty()) {
293275
// Update range for empty "rescue" and "ensure" clauses that have potential comments inside them
294276
TextRange endRange = getTokenByAttribute(node, "end").textRange();
295-
if (finallyBlock instanceof BlockTree && ((BlockTree) finallyBlock).statementOrExpressions().isEmpty()) {
277+
if (finallyBlock instanceof BlockTree blockTree && blockTree.statementOrExpressions().isEmpty()) {
296278
TextPointer from = finallyBlock.metaData().textRange().start();
297279
finallyBlock = createEmptyBlockTree(from, endRange.start());
298280
endRange = finallyBlock.textRange();
@@ -301,7 +283,7 @@ private Tree createFromKwBeginNode(AstNode node, List<?> children) {
301283
catchTrees = updateEmptyBlockRanges(
302284
catchTrees,
303285
endRange,
304-
catchTree -> catchTree.catchBlock() instanceof BlockTree && ((BlockTree) catchTree.catchBlock()).statementOrExpressions().isEmpty(),
286+
catchTree -> catchTree.catchBlock() instanceof BlockTree blockTree && blockTree.statementOrExpressions().isEmpty(),
305287
(catchTree, newBlockTree) -> new CatchTreeImpl(newBlockTree.metaData(), catchTree.catchParameter(), newBlockTree, catchTree.keyword()));
306288
}
307289

@@ -329,8 +311,8 @@ private Tree updateExceptionHandlingWithFinally(AstNode node, List<?> children)
329311

330312
Tree body = (Tree) children.get(0);
331313
RubyPartialExceptionHandlingTree exceptionHandlingTree;
332-
if (body instanceof RubyPartialExceptionHandlingTree) {
333-
exceptionHandlingTree = (RubyPartialExceptionHandlingTree) body;
314+
if (body instanceof RubyPartialExceptionHandlingTree rubyPartialExceptionHandlingTree) {
315+
exceptionHandlingTree = rubyPartialExceptionHandlingTree;
334316
} else {
335317
exceptionHandlingTree = new RubyPartialExceptionHandlingTree(body, emptyList());
336318
}
@@ -380,13 +362,13 @@ private Tree createCatchTree(AstNode node, List<?> children) {
380362
.limit(2)
381363
.filter(Objects::nonNull)
382364
.map(Tree.class::cast)
383-
.collect(Collectors.toList());
365+
.toList();
384366

385367
Tree catchParameter = null;
386368
if (catchParameterChildren.size() == 1) {
387369
catchParameter = catchParameterChildren.get(0);
388370
} else if (!catchParameterChildren.isEmpty()) {
389-
List<TextRange> textRanges = catchParameterChildren.stream().map(Tree::textRange).collect(Collectors.toList());
371+
List<TextRange> textRanges = catchParameterChildren.stream().map(Tree::textRange).toList();
390372
TextRange catchParameterRange = TextRanges.merge(textRanges);
391373
catchParameter = new NativeTreeImpl(metaDataProvider.metaData(catchParameterRange), new RubyNativeKind(node.type()), catchParameterChildren);
392374
}
@@ -478,7 +460,7 @@ private Tree createFromMasgn(AstNode node, List<?> children) {
478460
.stream()
479461
.filter(IdentifierTree.class::isInstance)
480462
.map(IdentifierTree.class::cast)
481-
.collect(Collectors.toList());
463+
.toList();
482464

483465
List<Tree> rhsChild = getChildIfArray((Tree) children.get(1));
484466

@@ -499,12 +481,10 @@ private Tree createFromMasgn(AstNode node, List<?> children) {
499481
}
500482

501483
private static List<Tree> getChildIfArray(Tree node) {
502-
if(node instanceof NativeTree) {
503-
NativeTree nativeNode = (NativeTree) node;
504-
if(nativeNode.nativeKind().equals(new RubyNativeKind("array"))) {
484+
if(node instanceof NativeTree nativeNode && nativeNode.nativeKind().equals(new RubyNativeKind("array"))) {
505485
return nativeNode.children();
506-
}
507486
}
487+
508488
return Collections.emptyList();
509489
}
510490

@@ -526,7 +506,7 @@ private Tree createFromIndexasgn(AstNode node, List<?> children) {
526506
List<Tree> lhsChildren = children.subList(0, children.size() - 1).stream()
527507
.filter(Tree.class::isInstance)
528508
.map(Tree.class::cast)
529-
.collect(Collectors.toList());
509+
.toList();
530510

531511
// such ruby native kind is required to have tree equivalence
532512
Tree lhs = new NativeTreeImpl(lhsMeta, new RubyNativeKind("index"), lhsChildren);
@@ -596,7 +576,7 @@ private Tree createMatchTree(AstNode node, List<?> children) {
596576
whens = updateEmptyBlockRanges(
597577
whens,
598578
endRange,
599-
caseTree -> caseTree.body() instanceof BlockTree && ((BlockTree) caseTree.body()).statementOrExpressions().isEmpty(),
579+
caseTree -> caseTree.body() instanceof BlockTree blockTree && blockTree.statementOrExpressions().isEmpty(),
600580
(caseTree, newBlockTree) -> new MatchCaseTreeImpl(newBlockTree.metaData(), caseTree.expression(), newBlockTree));
601581
}
602582

@@ -683,8 +663,8 @@ private Tree createFromBeginNode(AstNode node, List<?> children) {
683663
private static void setModifiers(List<Tree> children) {
684664
ModifierTree currentModifierTree = null;
685665
for (Tree child: children) {
686-
if (child instanceof ModifierTree) {
687-
currentModifierTree = (ModifierTree)child;
666+
if (child instanceof ModifierTree modifierTree) {
667+
currentModifierTree = modifierTree;
688668
} else if (currentModifierTree != null && child instanceof FunctionDeclarationTree) {
689669
((FunctionDeclarationTreeImpl) child).setModifiers(Arrays.asList(currentModifierTree));
690670
}
@@ -693,8 +673,8 @@ private static void setModifiers(List<Tree> children) {
693673

694674
private Tree createFromSendNode(AstNode node, List<?> children) {
695675
Object callee = children.get(1);
696-
if (callee instanceof RubySymbol) {
697-
String calleeSymbol = ((RubySymbol) callee).asJavaString();
676+
if (callee instanceof RubySymbol rubySymbol) {
677+
String calleeSymbol = rubySymbol.asJavaString();
698678
if (UNARY_OPERATOR_MAP.containsKey(calleeSymbol)) {
699679
Tree argument = (Tree) children.get(0);
700680
return new UnaryExpressionTreeImpl(metaData(node), UNARY_OPERATOR_MAP.get(calleeSymbol), argument);
@@ -764,8 +744,8 @@ private FunctionDeclarationTree createFunctionDeclarationTree(AstNode node, List
764744

765745
BlockTree body;
766746
Tree rubyBodyBlock = (Tree) children.get(2 + childrenIndexShift);
767-
if (rubyBodyBlock instanceof BlockTree) {
768-
body = (BlockTree) rubyBodyBlock;
747+
if (rubyBodyBlock instanceof BlockTree blockTree) {
748+
body = blockTree;
769749
} else if (rubyBodyBlock != null) {
770750
List<Tree> statements = singletonList(rubyBodyBlock);
771751
body = new BlockTreeImpl(rubyBodyBlock.metaData(), statements);
@@ -790,8 +770,8 @@ private ClassDeclarationTree createClassDeclarationTree(AstNode node, List<?> ch
790770

791771
Object nameChild = children.get(0);
792772
IdentifierTree classNameIdentifier;
793-
if (nameChild instanceof IdentifierTree) {
794-
classNameIdentifier = (IdentifierTree) nameChild;
773+
if (nameChild instanceof IdentifierTree identifierTree) {
774+
classNameIdentifier = identifierTree;
795775
} else {
796776
List<Tree> nameChildren = ((Tree) nameChild).children();
797777
classNameIdentifier = (IdentifierTree) nameChildren.get(nameChildren.size() - 1);
@@ -818,7 +798,7 @@ private Tree createFromConst(AstNode node, List<?> children) {
818798

819799
private Tree createIfTree(AstNode node, List<?> children) {
820800
Optional<Token> mainKeyword = lookForTokenByAttribute(node, KEYWORD_ATTRIBUTE);
821-
if (!mainKeyword.isPresent()) {
801+
if (mainKeyword.isEmpty()) {
822802
// "ternary"
823803
Tree condition = (Tree) children.get(0);
824804
Tree thenBranch = (Tree) children.get(1);
@@ -907,7 +887,7 @@ private Tree createReturnTree(AstNode node, List<?> children) {
907887
expression = (Tree) children.get(0);
908888
} else if (!children.isEmpty()) {
909889
List<Tree> childTrees = convertChildren(node, children);
910-
TextRange childRange = TextRanges.merge(childTrees.stream().map(Tree::textRange).collect(Collectors.toList()));
890+
TextRange childRange = TextRanges.merge(childTrees.stream().map(Tree::textRange).toList());
911891
expression = new NativeTreeImpl(metaDataProvider.metaData(childRange), new RubyNativeKind("returnExpression"), childTrees);
912892
}
913893
return new ReturnTreeImpl(metaData(node), keyword, expression);
@@ -920,7 +900,7 @@ private NativeTree createNativeTree(AstNode node, List<?> children, String type)
920900
}
921901

922902
private List<Tree> convertChildren(AstNode node, List<?> children) {
923-
return children.stream().flatMap(child -> treeForChild(node, child)).collect(Collectors.toList());
903+
return children.stream().flatMap(child -> treeForChild(node, child)).toList();
924904
}
925905

926906
@CheckForNull
@@ -933,10 +913,10 @@ private NativeTree createNativeTree(AstNode node, List<?> children) {
933913
}
934914

935915
private Stream<Tree> treeForChild(AstNode node, @Nullable Object child) {
936-
if (child instanceof Tree) {
937-
return Stream.of((Tree) child);
938-
} else if (child instanceof RubySymbol) {
939-
return Stream.of(identifierFromSymbol(node, (RubySymbol) child));
916+
if (child instanceof Tree tree) {
917+
return Stream.of(tree);
918+
} else if (child instanceof RubySymbol rubySymbol) {
919+
return Stream.of(identifierFromSymbol(node, rubySymbol));
940920
} else if (child != null) {
941921
return Stream.of(createNativeTree(node, emptyList(), String.valueOf(child)));
942922
} else {

sonar-ruby-plugin/src/main/java/org/sonarsource/ruby/externalreport/rubocop/RuboCopJsonReportReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ private void onOffense(@Nullable String filePath, JSONObject offense) {
9494
}
9595

9696
private static Integer toInteger(Object value) {
97-
if (value instanceof Number) {
98-
return ((Number) value).intValue();
97+
if (value instanceof Number number) {
98+
return number.intValue();
9999
}
100100
return null;
101101
}

sonar-ruby-plugin/src/main/java/org/sonarsource/ruby/plugin/SimpleCovSensor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ private static void mergeHitPerLines(Map<String, Map<Integer, Integer>> coverage
143143
int line = i + 1;
144144
Integer currentHits = fileCoverage.getOrDefault(line, 0);
145145
// Hits can be a Long (coverage data available), null or "ignored".
146-
if (hits instanceof Long) {
147-
fileCoverage.put(line, mergeHitsForLine(((Long) hits).intValue(), currentHits));
146+
if (hits instanceof Long longHits) {
147+
fileCoverage.put(line, mergeHitsForLine((longHits).intValue(), currentHits));
148148
} else if (hits == null) {
149149
fileCoverage.put(line, mergeHitsForLine(null, currentHits));
150150
}

0 commit comments

Comments
 (0)