Skip to content

Commit 7211615

Browse files
authored
SONARPY-628 Fix FP on S1226 and S1481 with raw f-strings (#656)
1 parent 1ad9936 commit 7211615

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

python-checks/src/test/resources/checks/unusedLocalVariable.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def string_interpolation():
2828
foo(F'{value5} foo')
2929
value6 = ''
3030
print(f"{'}' + value6}")
31+
value7 = ''
32+
printf(rf'{value7}')
3133
return f'{value1}, {2*value2}, value3bis, value4'
3234

3335
def function_with_lambdas():

python-frontend/src/main/java/org/sonar/python/tree/StringElementImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public String trimmedQuotesValue() {
8787

8888
@Override
8989
public String prefix() {
90-
return value.substring(0, value.length() - removePrefix(value).length());
90+
return value.substring(0, prefixLength(value));
9191
}
9292

9393
@Override
@@ -97,7 +97,8 @@ public boolean isTripleQuoted() {
9797

9898
@Override
9999
public boolean isInterpolated() {
100-
return prefix().equalsIgnoreCase("F");
100+
String prefix = prefix();
101+
return prefix.indexOf('f') >= 0 || prefix.indexOf('F') >= 0;
101102
}
102103

103104
@Override
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* SonarQube Python Plugin
3+
* Copyright (C) 2011-2020 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.python.tree;
21+
22+
import org.junit.Test;
23+
import org.sonar.plugins.python.api.tree.StringElement;
24+
import org.sonar.plugins.python.api.tree.StringLiteral;
25+
import org.sonar.python.PythonTestUtils;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
public class StringElementImplTest {
30+
31+
@Test
32+
public void isInterpolated() {
33+
assertThat(stringElement("'abc'").isInterpolated()).isFalse();
34+
assertThat(stringElement("f'abc'").isInterpolated()).isTrue();
35+
assertThat(stringElement("F'abc'").isInterpolated()).isTrue();
36+
assertThat(stringElement("r'abc'").isInterpolated()).isFalse();
37+
assertThat(stringElement("rf'abc'").isInterpolated()).isTrue();
38+
}
39+
40+
private StringElement stringElement(String code) {
41+
return ((StringLiteral) PythonTestUtils.lastExpression(code)).stringElements().get(0);
42+
}
43+
}

0 commit comments

Comments
 (0)