Skip to content

Commit 6dbbe3f

Browse files
SONARJAVA-4645 Fix unchecked NPE on initializer on S6806 (4497)
1 parent 92765dc commit 6dbbe3f

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

java-checks-test-sources/src/main/java/checks/spring/ModelAttributeNamingConventionForSpELCheck.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.File;
44
import java.util.HashMap;
55
import java.util.Map;
6+
import org.springframework.ui.Model;
67

78
class ModelAttributeNamingConventionForSpELCheck {
89

@@ -42,6 +43,13 @@ public void foo(org.springframework.ui.Model model) {
4243
model.addAttribute("$d", 8); // Compliant
4344

4445
model.addAllAttributes(new HashMap<>()); // Compliant - test coverage
46+
model.addAllAttributes(Map.of());
47+
48+
49+
}
50+
51+
public void modelAttribute(Model model, String name, Object value) { // Compliant
52+
model.addAttribute(name, value);
4553
}
4654

4755
private Map getMap() {

java-checks/src/main/java/org/sonar/java/checks/spring/ModelAttributeNamingConventionForSpELCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private void checkStringLiteralAndReport(ExpressionTree tree, ExpressionTree rep
9191

9292
private void checkIdentifier(IdentifierTree identifierTree) {
9393
VariableTreeImpl declaration = (VariableTreeImpl) identifierTree.symbol().declaration();
94-
if (declaration != null) {
94+
if (declaration != null && declaration.initializer()!=null) {
9595
checkExpression(declaration.initializer(), identifierTree);
9696
}
9797
}

0 commit comments

Comments
 (0)