Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
]
}
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
]
}
5 changes: 5 additions & 0 deletions its/ruling/src/test/resources/expected/guava/java-S9434.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"guava:src/com/google/common/collect/TreeRangeMap.java": [
547
]
}
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]] {{}}
}
}
}

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")
Comment thread
rombirli marked this conversation as resolved.
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;
}
}
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();
}
}
Loading
Loading