Repository navigation
SONARJAVA-7161 Implement rule S9434 - Qualified "this" should not be used when the unqualified form is enough #6360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
67ed2e9
Add rule metadata
rombirli 4564ef3
Add failing reproducer
rombirli 09e8c41
Implement rule
rombirli cdc501f
Update ruling expectations
rombirli 69fbe38
Refresh rule description
rombirli f149e92
Add quick fix
rombirli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
its/ruling/src/test/resources/expected/eclipse-jetty-similar-to-main/java-S9434.json
This file contains hidden or 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,9 @@ | ||
| { | ||
| "eclipse-jetty:jetty-io/src/main/java/org/eclipse/jetty/io/ssl/SslConnection.java": [ | ||
| 371, | ||
| 380 | ||
| ], | ||
| "eclipse-jetty:jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java": [ | ||
| 370 | ||
| ] | ||
| } |
20 changes: 20 additions & 0 deletions
20
its/ruling/src/test/resources/expected/eclipse-jetty/java-S9434.json
This file contains hidden or 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,20 @@ | ||
| { | ||
| "eclipse-jetty:jetty-io/src/main/java/org/eclipse/jetty/io/ssl/SslConnection.java": [ | ||
| 371, | ||
| 380 | ||
| ], | ||
| "eclipse-jetty:jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java": [ | ||
| 370 | ||
| ], | ||
| "eclipse-jetty:jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java": [ | ||
| 410, | ||
| 411, | ||
| 412 | ||
| ], | ||
| "eclipse-jetty:jetty-util/src/main/java/org/eclipse/jetty/util/ssl/SslSelectionDump.java": [ | ||
| 46 | ||
| ], | ||
| "eclipse-jetty:jetty-util/src/main/java/org/eclipse/jetty/util/thread/ScheduledExecutorScheduler.java": [ | ||
| 99 | ||
| ] | ||
| } |
This file contains hidden or 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,5 @@ | ||
| { | ||
| "guava:src/com/google/common/collect/TreeRangeMap.java": [ | ||
| 547 | ||
| ] | ||
| } |
174 changes: 174 additions & 0 deletions
174
java-checks-test-sources/default/src/main/java/checks/RedundantQualifiedThisCheckSample.java
This file contains hidden or 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,174 @@ | ||
| package checks; | ||
|
|
||
| import java.util.function.Supplier; | ||
|
|
||
| public class RedundantQualifiedThisCheckSample { | ||
|
|
||
| private final String id = "id"; | ||
| private int counter; | ||
|
|
||
| String redundantInMethod() { | ||
| return RedundantQualifiedThisCheckSample.this.id; // Noncompliant {{Remove this redundant qualifier.}} [[quickfixes=qf1]] | ||
| // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| // fix@qf1 {{Remove the redundant qualifier}} | ||
| // edit@qf1 [[sc=12;ec=46]] {{}} | ||
| } | ||
|
|
||
| void redundantOnMethodCall() { | ||
| RedundantQualifiedThisCheckSample.this.increment(); // Noncompliant [[quickfixes=qf2]] | ||
| // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| // fix@qf2 {{Remove the redundant qualifier}} | ||
| // edit@qf2 [[sc=5;ec=39]] {{}} | ||
| } | ||
|
|
||
| RedundantQualifiedThisCheckSample redundantWithoutMemberAccess() { | ||
| return RedundantQualifiedThisCheckSample.this; // Noncompliant [[quickfixes=qf3]] | ||
| // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| // fix@qf3 {{Remove the redundant qualifier}} | ||
| // edit@qf3 [[sc=12;ec=46]] {{}} | ||
| } | ||
|
|
||
| Supplier<String> redundantInLambda() { | ||
| return () -> RedundantQualifiedThisCheckSample.this.id; // Noncompliant [[quickfixes=qf4]] | ||
| // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| // fix@qf4 {{Remove the redundant qualifier}} | ||
| // edit@qf4 [[sc=18;ec=52]] {{}} | ||
| } | ||
|
|
||
| Supplier<Supplier<String>> redundantInNestedLambda() { | ||
| return () -> () -> RedundantQualifiedThisCheckSample.this.id; // Noncompliant [[quickfixes=qf5]] | ||
| // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| // fix@qf5 {{Remove the redundant qualifier}} | ||
| // edit@qf5 [[sc=24;ec=58]] {{}} | ||
| } | ||
|
|
||
| void increment() { | ||
| counter++; | ||
| } | ||
|
|
||
| String compliantExplicitThis() { | ||
| return this.id; | ||
| } | ||
|
|
||
| String compliantImplicitThis() { | ||
| return id; | ||
| } | ||
|
|
||
| class Inner { | ||
| private final String id = "inner"; | ||
|
|
||
| String outerQualifierIsRequired() { | ||
| return RedundantQualifiedThisCheckSample.this.id; | ||
| } | ||
|
|
||
| String ownQualifierIsRedundant() { | ||
| return Inner.this.id; // Noncompliant [[quickfixes=qf6]] | ||
| // ^^^^^^^^^^ | ||
| // fix@qf6 {{Remove the redundant qualifier}} | ||
| // edit@qf6 [[sc=14;ec=20]] {{}} | ||
| } | ||
|
|
||
| Supplier<String> outerQualifierIsRequiredInLambda() { | ||
| return () -> RedundantQualifiedThisCheckSample.this.id; | ||
| } | ||
|
|
||
| Supplier<String> ownQualifierIsRedundantInLambda() { | ||
| return () -> Inner.this.id; // Noncompliant [[quickfixes=qf7]] | ||
| // ^^^^^^^^^^ | ||
| // fix@qf7 {{Remove the redundant qualifier}} | ||
| // edit@qf7 [[sc=20;ec=26]] {{}} | ||
| } | ||
|
|
||
| class Innermost { | ||
| String enclosingQualifiersAreRequired() { | ||
| return RedundantQualifiedThisCheckSample.this.id + Inner.this.id; | ||
| } | ||
|
|
||
| String ownQualifierIsRedundant() { | ||
| return Innermost.this.toString(); // Noncompliant [[quickfixes=qf8]] | ||
| // ^^^^^^^^^^^^^^ | ||
| // fix@qf8 {{Remove the redundant qualifier}} | ||
| // edit@qf8 [[sc=16;ec=26]] {{}} | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Supplier<String> anonymousClassNeedsQualifier() { | ||
| return new Supplier<String>() { | ||
| @Override | ||
| public String get() { | ||
| return RedundantQualifiedThisCheckSample.this.id; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| Supplier<String> lambdaInAnonymousClassNeedsQualifier() { | ||
| return new Supplier<String>() { | ||
| @Override | ||
| public String get() { | ||
| Supplier<String> nested = () -> RedundantQualifiedThisCheckSample.this.id; | ||
| return nested.get(); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| String localClassNeedsQualifier() { | ||
| class Local { | ||
| String get() { | ||
| return RedundantQualifiedThisCheckSample.this.id; | ||
| } | ||
| } | ||
| return new Local().get(); | ||
| } | ||
|
|
||
| String localClassOwnQualifierIsRedundant() { | ||
| class Local { | ||
| String get() { | ||
| return Local.this.toString(); // Noncompliant [[quickfixes=qf9]] | ||
| // ^^^^^^^^^^ | ||
| // fix@qf9 {{Remove the redundant qualifier}} | ||
| // edit@qf9 [[sc=16;ec=22]] {{}} | ||
| } | ||
| } | ||
| return new Local().get(); | ||
| } | ||
|
|
||
| static class StaticNested { | ||
| private final String id = "nested"; | ||
|
|
||
| String ownQualifierIsRedundant() { | ||
| return StaticNested.this.id; // Noncompliant [[quickfixes=qf10]] | ||
| // ^^^^^^^^^^^^^^^^^ | ||
| // fix@qf10 {{Remove the redundant qualifier}} | ||
| // edit@qf10 [[sc=14;ec=27]] {{}} | ||
| } | ||
|
|
||
| Supplier<String> ownQualifierIsRedundantInLambda() { | ||
| return () -> StaticNested.this.id; // Noncompliant [[quickfixes=qf11]] | ||
| // ^^^^^^^^^^^^^^^^^ | ||
| // fix@qf11 {{Remove the redundant qualifier}} | ||
| // edit@qf11 [[sc=20;ec=33]] {{}} | ||
| } | ||
| } | ||
|
|
||
| enum Status { | ||
| OPEN; | ||
|
|
||
| String ownQualifierIsRedundant() { | ||
| return Status.this.name(); // Noncompliant [[quickfixes=qf12]] | ||
| // ^^^^^^^^^^^ | ||
| // fix@qf12 {{Remove the redundant qualifier}} | ||
| // edit@qf12 [[sc=14;ec=21]] {{}} | ||
| } | ||
| } | ||
|
|
||
| record Point(int x, int y) { | ||
| int ownQualifierIsRedundant() { | ||
| return Point.this.x; // Noncompliant [[quickfixes=qf13]] | ||
| // ^^^^^^^^^^ | ||
| // fix@qf13 {{Remove the redundant qualifier}} | ||
| // edit@qf13 [[sc=14;ec=20]] {{}} | ||
| } | ||
| } | ||
| } | ||
|
|
81 changes: 81 additions & 0 deletions
81
java-checks/src/main/java/org/sonar/java/checks/RedundantQualifiedThisCheck.java
This file contains hidden or 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,81 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * You can redistribute and/or modify this program under the terms of | ||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.java.checks; | ||
|
|
||
| import java.util.List; | ||
| import javax.annotation.CheckForNull; | ||
| import org.sonar.check.Rule; | ||
| import org.sonar.java.checks.helpers.QuickFixHelper; | ||
| import org.sonar.java.model.ExpressionUtils; | ||
| import org.sonar.java.reporting.JavaQuickFix; | ||
| import org.sonar.java.reporting.JavaTextEdit; | ||
| import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; | ||
| import org.sonar.plugins.java.api.semantic.Symbol; | ||
| import org.sonar.plugins.java.api.tree.ClassTree; | ||
| import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree; | ||
| import org.sonar.plugins.java.api.tree.Tree; | ||
|
|
||
| @Rule(key = "S9434") | ||
| public class RedundantQualifiedThisCheck extends IssuableSubscriptionVisitor { | ||
|
|
||
| private static final String MESSAGE = "Remove this redundant qualifier."; | ||
|
|
||
| @Override | ||
| public List<Tree.Kind> nodesToVisit() { | ||
| return List.of(Tree.Kind.MEMBER_SELECT); | ||
| } | ||
|
|
||
| @Override | ||
| public void visitNode(Tree tree) { | ||
| MemberSelectExpressionTree memberSelect = (MemberSelectExpressionTree) tree; | ||
| if (!ExpressionUtils.isThis(memberSelect.identifier())) { | ||
| return; | ||
| } | ||
| Symbol qualifier = memberSelect.expression().symbolType().symbol(); | ||
| if (!qualifier.isUnknown() && qualifier.equals(innermostEnclosingClass(memberSelect))) { | ||
| QuickFixHelper.newIssue(context) | ||
| .forRule(this) | ||
| .onTree(memberSelect) | ||
| .withMessage(MESSAGE) | ||
| .withQuickFix(() -> removeQualifier(memberSelect)) | ||
| .report(); | ||
| } | ||
| } | ||
|
|
||
| private static JavaQuickFix removeQualifier(MemberSelectExpressionTree memberSelect) { | ||
| return JavaQuickFix.newQuickFix("Remove the redundant qualifier") | ||
| .addTextEdit(JavaTextEdit.removeBetweenTree(memberSelect.expression(), memberSelect.operatorToken())) | ||
| .build(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the symbol of the class whose instance an unqualified {@code this} would denote at the given position. | ||
| * Lambda bodies are transparent because a lambda does not introduce its own {@code this}, while anonymous and | ||
| * local classes do and are therefore returned like any other class. | ||
| */ | ||
| @CheckForNull | ||
| private static Symbol innermostEnclosingClass(Tree tree) { | ||
| Tree parent = tree.parent(); | ||
| while (parent != null) { | ||
| if (parent instanceof ClassTree classTree) { | ||
| return classTree.symbol(); | ||
| } | ||
| parent = parent.parent(); | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
45 changes: 45 additions & 0 deletions
45
java-checks/src/test/java/org/sonar/java/checks/RedundantQualifiedThisCheckTest.java
This file contains hidden or 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,45 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * You can redistribute and/or modify this program under the terms of | ||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.java.checks; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.sonar.java.checks.verifier.CheckVerifier; | ||
|
|
||
| import static org.sonar.java.checks.verifier.TestUtils.mainCodeSourcesPath; | ||
|
|
||
| class RedundantQualifiedThisCheckTest { | ||
|
|
||
| private static final String SAMPLE = "checks/RedundantQualifiedThisCheckSample.java"; | ||
| private static final RedundantQualifiedThisCheck CHECK = new RedundantQualifiedThisCheck(); | ||
|
|
||
| @Test | ||
| void test() { | ||
| CheckVerifier.newVerifier() | ||
| .onFile(mainCodeSourcesPath(SAMPLE)) | ||
| .withCheck(CHECK) | ||
| .verifyIssues(); | ||
| } | ||
|
|
||
| @Test | ||
| void test_without_semantic() { | ||
| CheckVerifier.newVerifier() | ||
| .onFile(mainCodeSourcesPath(SAMPLE)) | ||
| .withCheck(CHECK) | ||
| .withoutSemantic() | ||
| .verifyIssues(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.