From d2fe7d29533afb3a34cb97d8b2359feebcfa6b4d Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Wed, 6 Oct 2021 15:17:14 +0200 Subject: [PATCH] [java] infer nullity: do not suggest to annotate catch parameters (IDEA-193905) GitOrigin-RevId: 62db2fd552762a0d2d8dc6a2593782eee78f65c0 --- .../inferNullity/NullityInferrer.java | 4 ++-- .../nullityinferrer/afterCatchParams.java | 13 ++++++++++++ .../nullityinferrer/beforeCatchParams.java | 13 ++++++++++++ .../java/codeInsight/NullityInferrerTest.java | 20 +++++-------------- 4 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/nullityinferrer/afterCatchParams.java create mode 100644 java/java-tests/testData/codeInsight/nullityinferrer/beforeCatchParams.java diff --git a/java/java-impl/src/com/intellij/codeInspection/inferNullity/NullityInferrer.java b/java/java-impl/src/com/intellij/codeInspection/inferNullity/NullityInferrer.java index da9ae4930d9f..e432f04bf6c9 100644 --- a/java/java-impl/src/com/intellij/codeInspection/inferNullity/NullityInferrer.java +++ b/java/java-impl/src/com/intellij/codeInspection/inferNullity/NullityInferrer.java @@ -1,4 +1,4 @@ -// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.codeInspection.inferNullity; import com.intellij.codeInsight.Nullability; @@ -74,7 +74,7 @@ public class NullityInferrer { return false; } } - else if (!variable.hasModifierProperty(PsiModifier.FINAL)) { + else if (!variable.hasModifierProperty(PsiModifier.FINAL) || variable instanceof PsiParameter && ((PsiParameter)variable).getDeclarationScope() instanceof PsiCatchSection) { return false; } final Query references = ReferencesSearch.search(variable); diff --git a/java/java-tests/testData/codeInsight/nullityinferrer/afterCatchParams.java b/java/java-tests/testData/codeInsight/nullityinferrer/afterCatchParams.java new file mode 100644 index 000000000000..697426d6d708 --- /dev/null +++ b/java/java-tests/testData/codeInsight/nullityinferrer/afterCatchParams.java @@ -0,0 +1,13 @@ +import org.jetbrains.annotations.*; + +public class Infer { + static public boolean a(@NotNull String s) { + try { + return s.length() > 0; + } catch (final NullPointerException e) { + e.printStackTrace(); + return false; + } + } + +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/nullityinferrer/beforeCatchParams.java b/java/java-tests/testData/codeInsight/nullityinferrer/beforeCatchParams.java new file mode 100644 index 000000000000..8437d507f8dc --- /dev/null +++ b/java/java-tests/testData/codeInsight/nullityinferrer/beforeCatchParams.java @@ -0,0 +1,13 @@ +import org.jetbrains.annotations.*; + +public class Infer { + static public boolean a(String s) { + try { + return s.length() > 0; + } catch (final NullPointerException e) { + e.printStackTrace(); + return false; + } + } + +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/NullityInferrerTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/NullityInferrerTest.java index 0118752e591b..937a90e7d751 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/NullityInferrerTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/NullityInferrerTest.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.java.codeInsight; import com.intellij.JavaTestUtil; @@ -94,6 +80,10 @@ public class NullityInferrerTest extends LightJavaCodeInsightTestCase { public void testTryEnumSwitch() throws Exception { doTest(true); } + + public void testCatchParams() throws Exception { + doTest(true); + } //-----------------------fields--------------------------------------------------- public void testFieldsAssignment() throws Exception {