From 4248c480c9355681dc5ecb3d5d62a8ee4b437b23 Mon Sep 17 00:00:00 2001 From: erwan-serandour-sonarsource Date: Wed, 13 Dec 2023 17:47:50 +0100 Subject: [PATCH] SONARJAVA-4726 (external contribution) fix crash in SelfAssignementCheck Author: gtoison --- .../default/src/main/java/checks/SelfAssignementCheck.java | 6 ++++++ .../java/org/sonar/java/checks/SelfAssignementCheck.java | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/java-checks-test-sources/default/src/main/java/checks/SelfAssignementCheck.java b/java-checks-test-sources/default/src/main/java/checks/SelfAssignementCheck.java index a57d410dc18..6b1faefcd25 100644 --- a/java-checks-test-sources/default/src/main/java/checks/SelfAssignementCheck.java +++ b/java-checks-test-sources/default/src/main/java/checks/SelfAssignementCheck.java @@ -1,6 +1,12 @@ package checks; class SelfAssignementCheck { + static int staticField = 0; + + static { + staticField = staticField; // Noncompliant + } + int a, c = 0; int[] b = {0}; diff --git a/java-checks/src/main/java/org/sonar/java/checks/SelfAssignementCheck.java b/java-checks/src/main/java/org/sonar/java/checks/SelfAssignementCheck.java index fb13811f72b..712065145cf 100644 --- a/java-checks/src/main/java/org/sonar/java/checks/SelfAssignementCheck.java +++ b/java-checks/src/main/java/org/sonar/java/checks/SelfAssignementCheck.java @@ -90,7 +90,7 @@ private static JavaQuickFix getQuickFix(AssignmentExpressionTree tree) { MethodTree methodParent = (MethodTree) ExpressionUtils.getParentOfType(tree, Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR); String name = getName(tree.variable()); - boolean isMethodParameter = methodParent.parameters().stream() + boolean isMethodParameter = methodParent != null && methodParent.parameters().stream() .map(p -> p.simpleName().name()) .anyMatch(p -> p.equals(name));