From c5f8bf51d5faf90b84e843a84d6c6fa357afb283 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Tue, 25 Oct 2016 20:11:30 +0300 Subject: [PATCH] PY-21161 Check that index fragment of .format() string doesn't contain backslash --- .../codeInsight/intentions/PyConvertToFStringIntention.java | 3 +++ .../formatMethodIndexContainsBackslash.py | 1 + .../formatMethodIndexContainsBackslash_after.py | 1 + .../python/intentions/PyConvertToFStringIntentionTest.java | 4 ++++ 4 files changed, 9 insertions(+) create mode 100644 python/testData/intentions/PyConvertToFStringIntentionTest/formatMethodIndexContainsBackslash.py create mode 100644 python/testData/intentions/PyConvertToFStringIntentionTest/formatMethodIndexContainsBackslash_after.py diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertToFStringIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertToFStringIntention.java index b7a74852f6c8..4a6ac8522e7d 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertToFStringIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertToFStringIntention.java @@ -360,6 +360,9 @@ public class PyConvertToFStringIntention extends PyBaseIntentionAction { escaped.add(part); } else if (part.startsWith("[")) { + if (part.contains("\\")) { + return null; + } final String indexText = part.substring(1, part.length() - 1); if (indexText.matches("\\d+")) { escaped.add(part); diff --git a/python/testData/intentions/PyConvertToFStringIntentionTest/formatMethodIndexContainsBackslash.py b/python/testData/intentions/PyConvertToFStringIntentionTest/formatMethodIndexContainsBackslash.py new file mode 100644 index 000000000000..bc5fc6d168ce --- /dev/null +++ b/python/testData/intentions/PyConvertToFStringIntentionTest/formatMethodIndexContainsBackslash.py @@ -0,0 +1 @@ +'{0[\\]}'.format(undefined) \ No newline at end of file diff --git a/python/testData/intentions/PyConvertToFStringIntentionTest/formatMethodIndexContainsBackslash_after.py b/python/testData/intentions/PyConvertToFStringIntentionTest/formatMethodIndexContainsBackslash_after.py new file mode 100644 index 000000000000..bc5fc6d168ce --- /dev/null +++ b/python/testData/intentions/PyConvertToFStringIntentionTest/formatMethodIndexContainsBackslash_after.py @@ -0,0 +1 @@ +'{0[\\]}'.format(undefined) \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/intentions/PyConvertToFStringIntentionTest.java b/python/testSrc/com/jetbrains/python/intentions/PyConvertToFStringIntentionTest.java index de1f4f3cb2d7..2f028999b558 100644 --- a/python/testSrc/com/jetbrains/python/intentions/PyConvertToFStringIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/intentions/PyConvertToFStringIntentionTest.java @@ -99,6 +99,10 @@ public class PyConvertToFStringIntentionTest extends PyIntentionTestCase { public void testFormatMethodIndexContainsHostAlternativeQuote() { doTest(); } + + public void testFormatMethodIndexContainsBackslash() { + doTest(); + } public void testPercentOperatorWidthAndPrecision() { doTest();