Skip to content

Commit

Permalink
SONARJAVA-4142 Revert support of constructors in S1450
Browse files Browse the repository at this point in the history
  • Loading branch information
1 parent cf15173 commit b8e6853
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 17 deletions.
7 changes: 0 additions & 7 deletions its/ruling/src/test/resources/eclipse-jetty/java-S1450.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
{
'org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/HttpOutput.java':[
1825,
],
'org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/PushBuilderImpl.java':[
59,
],
'org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/resource/ByteBufferRangeWriter.java':[
33,
],
'org.eclipse.jetty:jetty-project:jetty-util/src/main/java/org/eclipse/jetty/util/DateCache.java':[
49,
51,
],
}
6 changes: 0 additions & 6 deletions its/ruling/src/test/resources/sonar-server/java-S1450.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private static MethodTree usedInOneMethodOnly(Symbol privateFieldSymbol, TypeSym
MethodTree method = null;

for (IdentifierTree usageIdentifier : privateFieldSymbol.usages()) {
MethodTree enclosingMethod = ExpressionUtils.getEnclosingMethod(usageIdentifier);
MethodTree enclosingMethod = ExpressionUtils.getEnclosingElement(usageIdentifier, Kind.METHOD);

if (enclosingMethod == null
|| !enclosingMethod.symbol().owner().equals(classSymbol)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,19 @@ void method2() {

}

// Constructor are also supported
// Constructors
class Class17 {
private int privateField; // Noncompliant
private int privateField; // Compliant: this field should be reported by S1068 (unused field), and not by this rule
Class17() {
this.privateField = 5;
}
}

class Class18 {
private int privateField; // Not reported by S1068, we could decide to report it in this rule (see SONARJAVA-4142).
Class18() {
privateField = 5;
System.out.println(privateField);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,18 @@ public static IdentifierTree methodName(MethodInvocationTree mit) {
return id;
}

/**
* Return the first enclosing method or constructor containing the given expression.
*/
@CheckForNull
public static MethodTree getEnclosingMethod(ExpressionTree expr) {
return getEnclosingElement(expr, Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR);
}

@CheckForNull
public static MethodTree getEnclosingElement(ExpressionTree expr, Tree.Kind... kinds) {
Tree result = expr.parent();
while (result != null && !result.is(Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR)) {
while (result != null && !result.is(kinds)) {
result = result.parent();
}
return (MethodTree) result;
Expand Down

0 comments on commit b8e6853

Please sign in to comment.