diff --git a/java-frontend/src/main/java/org/sonar/java/se/checks/LocksNotUnlockedCheck.java b/java-frontend/src/main/java/org/sonar/java/se/checks/LocksNotUnlockedCheck.java index dd3a01b80d2..e75a20f300f 100644 --- a/java-frontend/src/main/java/org/sonar/java/se/checks/LocksNotUnlockedCheck.java +++ b/java-frontend/src/main/java/org/sonar/java/se/checks/LocksNotUnlockedCheck.java @@ -191,10 +191,12 @@ public void checkEndOfExecutionPath(CheckerContext context, ConstraintManager co } private void reportIssue(JavaFileScannerContext.Location location) { - MethodInvocationTree syntaxNode = (MethodInvocationTree) location.syntaxNode; - String flowMsg = "Lock '" + SyntaxTreeNameFinder.getName(syntaxNode.methodSelect()) + "' is never unlocked."; - Tree tree = issueTree(syntaxNode); - reportIssue(tree, "Unlock this lock along all executions paths of this method.", FlowComputation.singleton(flowMsg, tree)); + if (location.syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) { + MethodInvocationTree syntaxNode = (MethodInvocationTree) location.syntaxNode; + String flowMsg = "Lock '" + SyntaxTreeNameFinder.getName(syntaxNode.methodSelect()) + "' is never unlocked."; + Tree tree = issueTree(syntaxNode); + reportIssue(tree, "Unlock this lock along all executions paths of this method.", FlowComputation.singleton(flowMsg, tree)); + } } private static Tree issueTree(MethodInvocationTree syntaxNode) { diff --git a/java-frontend/src/test/files/se/LocksNotUnlockedCheck.java b/java-frontend/src/test/files/se/LocksNotUnlockedCheck.java index 69a7f005ede..9749b251b5e 100644 --- a/java-frontend/src/test/files/se/LocksNotUnlockedCheck.java +++ b/java-frontend/src/test/files/se/LocksNotUnlockedCheck.java @@ -1,6 +1,5 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; - public class MyClass { Lock l1 = new ReentrantLock(); @@ -260,7 +259,7 @@ public void unlockingInTryCatchButMissed() { } } } - + private volatile ScheduledExecutorService executorService; private final Runnable task = new Task(); @@ -289,7 +288,7 @@ public void unlockingInTryCatchButMissed() { } }); } - + private final AbstractService delegate = new AbstractService() { private volatile Future runningTask; private volatile ScheduledExecutorService executorService; @@ -324,4 +323,19 @@ public void lock() { } } } + + public class SonarFail { + java.util.List list; + Object test(Lock local) { + Lock l1 = list.get(0); + local.lock(); + try { + if (l1 == local) { + return null; + } + } finally { + local.unlock(); + } + } + } }