Skip to content

Commit ff2fb65

Browse files
SONARPY-2163 Migrate S5708 CaughtExceptionsCheck to the V2 type model (#2007)
1 parent 0a146e5 commit ff2fb65

File tree

1 file changed

+8
-25
lines changed

1 file changed

+8
-25
lines changed

python-checks/src/main/java/org/sonar/python/checks/CaughtExceptionsCheck.java

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
*/
2020
package org.sonar.python.checks;
2121

22-
import java.util.Arrays;
23-
import java.util.HashSet;
24-
import java.util.Set;
2522
import java.util.function.Predicate;
2623
import javax.annotation.Nullable;
2724
import org.sonar.check.Rule;
@@ -37,24 +34,20 @@
3734
import org.sonar.plugins.python.api.tree.Expression;
3835
import org.sonar.plugins.python.api.tree.Token;
3936
import org.sonar.plugins.python.api.tree.Tree;
40-
import org.sonar.plugins.python.api.types.InferredType;
4137
import org.sonar.python.quickfix.TextEditUtils;
4238
import org.sonar.python.tree.TreeUtils;
39+
import org.sonar.python.types.v2.PythonType;
40+
import org.sonar.python.types.v2.TriBool;
4341

4442
import static org.sonar.plugins.python.api.symbols.Symbol.Kind.CLASS;
4543
import static org.sonar.plugins.python.api.tree.Tree.Kind.EXCEPT_CLAUSE;
4644
import static org.sonar.plugins.python.api.tree.Tree.Kind.EXCEPT_GROUP_CLAUSE;
4745
import static org.sonar.plugins.python.api.types.BuiltinTypes.BASE_EXCEPTION;
48-
import static org.sonar.plugins.python.api.types.BuiltinTypes.DICT;
49-
import static org.sonar.plugins.python.api.types.BuiltinTypes.LIST;
50-
import static org.sonar.plugins.python.api.types.BuiltinTypes.SET;
51-
import static org.sonar.plugins.python.api.types.BuiltinTypes.TUPLE;
5246

5347
@Rule(key = "S5708")
5448
public class CaughtExceptionsCheck extends PythonSubscriptionCheck {
5549

5650
private static final String MESSAGE = "Change this expression to be a class deriving from BaseException or a tuple of such classes.";
57-
private static final Set<String> NON_COMPLIANT_TYPES = new HashSet<>(Arrays.asList(LIST, SET, DICT));
5851
public static final String QUICK_FIX_MESSAGE_FORMAT = "Make \"%s\" deriving from \"Exception\"";
5952

6053
@Override
@@ -74,7 +67,7 @@ private static void checkExceptClause(SubscriptionContext ctx) {
7467
var notInheritsFromBaseException = expressionSymbolOpt
7568
.filter(Predicate.not(CaughtExceptionsCheck::inheritsFromBaseException))
7669
.isPresent();
77-
if (!canBeOrExtendBaseException(expression.type()) || notInheritsFromBaseException) {
70+
if (!canBeOrExtendBaseException(expression, ctx) || notInheritsFromBaseException) {
7871
var issue = ctx.addIssue(expression, MESSAGE);
7972
expressionSymbolOpt.ifPresent(symbol -> addQuickFix(issue, symbol));
8073
}
@@ -111,21 +104,11 @@ private static void addQuickFix(PreciseIssue issue, Symbol symbol) {
111104
});
112105
}
113106

114-
private static boolean canBeOrExtendBaseException(InferredType type) {
115-
if (NON_COMPLIANT_TYPES.stream().anyMatch(type::canOnlyBe)) {
116-
// due to some limitations in type inference engine,
117-
// type.canBeOrExtend("list" | "set" | "dict") returns true
118-
return false;
119-
}
120-
if (type.canBeOrExtend(TUPLE)) {
121-
// avoid FP on variables holding a tuple: SONARPY-713
122-
return true;
123-
}
124-
if (type.canBeOrExtend("type")) {
125-
// SONARPY-1666: Here we should only exclude type objects that represent Exception types
126-
return true;
127-
}
128-
return type.canBeOrExtend(BASE_EXCEPTION);
107+
private static boolean canBeOrExtendBaseException(Expression expression, SubscriptionContext ctx) {
108+
PythonType pythonType = expression.typeV2();
109+
TriBool isBaseException = ctx.typeChecker().typeCheckBuilder().isInstanceOf("BaseException").check(pythonType);
110+
TriBool isTuple = ctx.typeChecker().typeCheckBuilder().isBuiltinWithName("tuple").check(pythonType);
111+
return isBaseException != TriBool.FALSE || isTuple != TriBool.FALSE;
129112
}
130113

131114
private static boolean inheritsFromBaseException(@Nullable Symbol symbol) {

0 commit comments

Comments
 (0)