Skip to content

Commit 65f1b6a

Browse files
SONARPY-994 S5361 should not create false positives when case-insensitive flag is set (#1111)
1 parent 2b591e3 commit 65f1b6a

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

python-checks/src/main/java/org/sonar/python/checks/regex/StringReplaceCheck.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.util.HashMap;
2323
import java.util.Map;
24+
import java.util.regex.Pattern;
2425
import org.sonar.check.Rule;
2526
import org.sonar.plugins.python.api.tree.CallExpression;
2627
import org.sonar.python.regex.PythonRegexIssueLocation;
@@ -44,7 +45,11 @@ protected Map<String, Integer> lookedUpFunctions() {
4445
@Override
4546
public void checkRegex(RegexParseResult regexParseResult, CallExpression callExpression) {
4647
RegexTree regex = regexParseResult.getResult();
47-
if (!regexParseResult.hasSyntaxErrors() && isPlainString(regex)) {
48+
if (regexParseResult.hasSyntaxErrors() || regex.activeFlags().contains(Pattern.CASE_INSENSITIVE)) {
49+
return;
50+
}
51+
52+
if (isPlainString(regex)) {
4853
regexContext.addIssue(callExpression.callee(), MESSAGE)
4954
.secondary(PythonRegexIssueLocation.preciseLocation(regex, SECONDARY_MESSAGE));
5055
}

python-checks/src/test/resources/checks/regex/stringReplaceCheck.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def compliant():
3636
changed = re.sub(r"x+", "It's", input)
3737
changed = re.sub(r"[\\]", "It's", input)
3838
changed = re.sub(r"", "It's", input)
39-
changed = re.match(r"Bob is", input)
39+
changed = re.sub(r"Bob is", "It's", input, flags=re.IGNORECASE)
40+
changed = re.sub(r"Bob is", "It's", input, 1, re.IGNORECASE)
4041

4142
def coverage(input):
4243
re.match(r"Bob is", input)

0 commit comments

Comments
 (0)