Skip to content

Commit b8e6853

Browse files
SONARJAVA-4142 Revert support of constructors in S1450
1 parent cf15173 commit b8e6853

File tree

5 files changed

+20
-17
lines changed

5 files changed

+20
-17
lines changed
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
{
2-
'org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/HttpOutput.java':[
3-
1825,
4-
],
52
'org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/PushBuilderImpl.java':[
63
59,
74
],
85
'org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/resource/ByteBufferRangeWriter.java':[
96
33,
107
],
11-
'org.eclipse.jetty:jetty-project:jetty-util/src/main/java/org/eclipse/jetty/util/DateCache.java':[
12-
49,
13-
51,
14-
],
158
}

its/ruling/src/test/resources/sonar-server/java-S1450.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

java-checks/src/main/java/org/sonar/java/checks/PrivateFieldUsedLocallyCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private static MethodTree usedInOneMethodOnly(Symbol privateFieldSymbol, TypeSym
101101
MethodTree method = null;
102102

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

106106
if (enclosingMethod == null
107107
|| !enclosingMethod.symbol().owner().equals(classSymbol)

java-checks/src/test/files/checks/PrivateFieldUsedLocallyCheck.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,19 @@ void method2() {
227227

228228
}
229229

230-
// Constructor are also supported
230+
// Constructors
231231
class Class17 {
232-
private int privateField; // Noncompliant
232+
private int privateField; // Compliant: this field should be reported by S1068 (unused field), and not by this rule
233233
Class17() {
234+
this.privateField = 5;
235+
}
236+
}
237+
238+
class Class18 {
239+
private int privateField; // Not reported by S1068, we could decide to report it in this rule (see SONARJAVA-4142).
240+
Class18() {
234241
privateField = 5;
242+
System.out.println(privateField);
235243
}
236244
}
237245
}

java-frontend/src/main/java/org/sonar/java/model/ExpressionUtils.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,18 @@ public static IdentifierTree methodName(MethodInvocationTree mit) {
166166
return id;
167167
}
168168

169+
/**
170+
* Return the first enclosing method or constructor containing the given expression.
171+
*/
169172
@CheckForNull
170173
public static MethodTree getEnclosingMethod(ExpressionTree expr) {
174+
return getEnclosingElement(expr, Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR);
175+
}
176+
177+
@CheckForNull
178+
public static MethodTree getEnclosingElement(ExpressionTree expr, Tree.Kind... kinds) {
171179
Tree result = expr.parent();
172-
while (result != null && !result.is(Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR)) {
180+
while (result != null && !result.is(kinds)) {
173181
result = result.parent();
174182
}
175183
return (MethodTree) result;

0 commit comments

Comments
 (0)