Fix highlighting for empty raises (PY-26510)

This commit is contained in:
Semyon Proshev
2017-10-23 19:42:13 +03:00
parent e9ebf97e2b
commit 55949b3eb4
13 changed files with 74 additions and 96 deletions
@@ -1,18 +1,4 @@
/*
* Copyright 2000-2016 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-2017 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.
package com.jetbrains.python.validation;
import com.google.common.collect.Maps;
@@ -276,8 +262,8 @@ public abstract class CompatibilityVisitor extends PyAnnotator {
public void visitPyRaiseStatement(PyRaiseStatement node) {
super.visitPyRaiseStatement(node);
// empty raise
registerForAllMatchingVersions(level -> UnsupportedFeaturesUtil.raiseHasNoArgs(node, level),
// empty raise under finally
registerForAllMatchingVersions(level -> UnsupportedFeaturesUtil.raiseHasNoArgsUnderFinally(node, level),
" not support this syntax. Raise with no arguments can only be used in an except block",
node,
null,
@@ -1,22 +1,11 @@
/*
* Copyright 2000-2014 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-2017 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.
package com.jetbrains.python.validation;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.psi.PyExceptPart;
import com.jetbrains.python.psi.PyFinallyPart;
import com.jetbrains.python.psi.PyRaiseStatement;
import com.jetbrains.python.psi.PyTryExceptStatement;
/**
@@ -27,7 +16,7 @@ import com.jetbrains.python.psi.PyTryExceptStatement;
public class TryExceptAnnotator extends PyAnnotator {
@Override
public void visitPyTryExceptStatement(final PyTryExceptStatement node) {
PyExceptPart[] exceptParts = node.getExceptParts();
final PyExceptPart[] exceptParts = node.getExceptParts();
boolean haveDefaultExcept = false;
for (PyExceptPart part : exceptParts) {
if (haveDefaultExcept) {
@@ -38,4 +27,11 @@ public class TryExceptAnnotator extends PyAnnotator {
}
}
}
@Override
public void visitPyRaiseStatement(PyRaiseStatement node) {
if (node.getExpressions().length == 0 && PsiTreeUtil.getParentOfType(node, PyExceptPart.class, PyFinallyPart.class) == null) {
markError(node, "No exception to reraise");
}
}
}
@@ -1,18 +1,4 @@
/*
* Copyright 2000-2014 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-2017 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.
package com.jetbrains.python.validation;
import com.intellij.openapi.diagnostic.Logger;
@@ -22,6 +8,7 @@ import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.PythonHelpersLocator;
import com.jetbrains.python.psi.*;
import org.jetbrains.annotations.NotNull;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -88,15 +75,10 @@ public class UnsupportedFeaturesUtil {
}
}
public static boolean raiseHasNoArgs(PyRaiseStatement node, LanguageLevel versionToProcess) {
final PyExpression[] expressions = node.getExpressions();
if (expressions.length == 0 && versionToProcess.isPy3K()) {
final PyExceptPart exceptPart = PsiTreeUtil.getParentOfType(node, PyExceptPart.class);
if (exceptPart == null) {
return true;
}
}
return false;
public static boolean raiseHasNoArgsUnderFinally(@NotNull PyRaiseStatement node, @NotNull LanguageLevel versionToProcess) {
return node.getExpressions().length == 0 &&
versionToProcess.isPython2() &&
PsiTreeUtil.getParentOfType(node, PyFinallyPart.class) != null;
}
public static boolean raiseHasMoreThenOneArg(PyRaiseStatement node, LanguageLevel versionToProcess) {
@@ -0,0 +1,12 @@
<error descr="No exception to reraise">raise</error>
try:
raise ValueError
except:
raise
try:
raise ValueError
finally:
raise
@@ -18,7 +18,7 @@ try:
<error descr="Python version 3.0 does not have module __builtin__">import __builtin__</error>
<warning descr="Python version 3.0 does not support this syntax. Raise with no arguments can only be used in an except block">raise</warning>
<error descr="No exception to reraise">raise</error>
try:
pass
@@ -0,0 +1,4 @@
try:
a
except :
<warning descr="Python version 3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6 do not support this syntax.">raise ImportError, ImportWarning</warning>
@@ -1,6 +0,0 @@
try:
a
except :
<warning descr="Python version 3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6 do not support this syntax.">raise ImportError, ImportWarning</warning>
<warning descr="Python version 3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6 do not support this syntax. Raise with no arguments can only be used in an except block">raise</warning>
@@ -0,0 +1,4 @@
try:
raise ValueError
except:
raise
@@ -0,0 +1,4 @@
try:
raise ValueError
finally:
<warning descr="Python version 2.4, 2.5, 2.6, 2.7 do not support this syntax. Raise with no arguments can only be used in an except block"><warning descr="Python version 2.7 does not support this syntax. Raise with no arguments can only be used in an except block">raise</warning></warning>
@@ -0,0 +1,4 @@
try:
raise ValueError
finally:
<warning descr="Python version 2.4, 2.5, 2.6, 2.7 do not support this syntax. Raise with no arguments can only be used in an except block">raise</warning>
@@ -88,7 +88,7 @@ def f():
# PY-4208
def f(g):
try:
raise
raise ValueError
finally:
g()
<warning descr="This code is unreachable">g()</warning>
@@ -1,18 +1,4 @@
/*
* Copyright 2000-2016 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-2017 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.
package com.jetbrains.python;
import com.intellij.openapi.editor.colors.EditorColorsManager;
@@ -379,6 +365,11 @@ public class PythonHighlightingTest extends PyTestCase {
runWithLanguageLevel(LanguageLevel.PYTHON30, this::doTest);
}
// PY-26510
public void testEmptyRaise() {
doTest(false, false);
}
@NotNull
private static EditorColorsScheme createTemporaryColorScheme() {
EditorColorsManager manager = EditorColorsManager.getInstance();
@@ -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-2017 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.
package com.jetbrains.python.inspections;
import com.jetbrains.python.fixtures.PyInspectionTestCase;
@@ -76,7 +62,7 @@ public class PyCompatibilityInspectionTest extends PyInspectionTestCase {
doTest();
}
public void testRaiseStatement() {
public void testRaiseMultipleArgs() {
doTest();
}
@@ -233,6 +219,21 @@ public class PyCompatibilityInspectionTest extends PyInspectionTestCase {
doTest();
}
// PY-26510
public void testTryExceptEmptyRaise() {
doTest();
}
// PY-26510
public void testTryFinallyEmptyRaisePy2() {
doTest();
}
// PY-26510
public void testTryFinallyEmptyRaisePy3() {
doTest(LanguageLevel.PYTHON30);
}
private void doTest(@NotNull LanguageLevel level) {
runWithLanguageLevel(level, this::doTest);
}