diff --git a/doc/whatsnew/2/2.14/full.rst b/doc/whatsnew/2/2.14/full.rst index b8023ddc6d..5cca166b0e 100644 --- a/doc/whatsnew/2/2.14/full.rst +++ b/doc/whatsnew/2/2.14/full.rst @@ -5,6 +5,10 @@ What's New in Pylint 2.14.2? ---------------------------- Release date: TBA +* Fixed a false positive in ``consider-using-f-string`` if the left side of a ``%`` is not a string. + + Closes #6689 + * Fixed a false positive in ``unnecessary-list-index-lookup`` and ``unnecessary-dict-index-lookup`` when the subscript is updated in the body of a nested loop. diff --git a/pylint/checkers/refactoring/recommendation_checker.py b/pylint/checkers/refactoring/recommendation_checker.py index 72c8228390..7873dc25ed 100644 --- a/pylint/checkers/refactoring/recommendation_checker.py +++ b/pylint/checkers/refactoring/recommendation_checker.py @@ -394,6 +394,12 @@ def _detect_replacable_format_call(self, node: nodes.Const) -> None: if "\\" in node.parent.right.as_string(): return + # If % applied to another type than str, it's modulo and can't be replaced by formatting + if not hasattr(node.parent.left, "value") or not isinstance( + node.parent.left.value, str + ): + return + inferred_right = utils.safe_infer(node.parent.right) # If dicts or lists of length > 1 are used diff --git a/tests/functional/c/consider/consider_using_f_string.py b/tests/functional/c/consider/consider_using_f_string.py index f6801989e8..e961c44f70 100644 --- a/tests/functional/c/consider/consider_using_f_string.py +++ b/tests/functional/c/consider/consider_using_f_string.py @@ -72,6 +72,9 @@ def statement_good(): "{}".format("\n".join(i for i in "string")) "%s" % "\n" "%s" % "\n".join(i for i in "string") + 1 % "str" + (1, 2) % 'garbage' + def statement_bad(): "String %f" % PARAM_1 # [consider-using-f-string] diff --git a/tests/functional/c/consider/consider_using_f_string.txt b/tests/functional/c/consider/consider_using_f_string.txt index f3ac4adf0e..1d68e947be 100644 --- a/tests/functional/c/consider/consider_using_f_string.txt +++ b/tests/functional/c/consider/consider_using_f_string.txt @@ -8,23 +8,23 @@ consider-using-f-string:51:10:51:21:print_bad:Formatting a regular string which consider-using-f-string:52:10:52:21:print_bad:Formatting a regular string which could be a f-string:UNDEFINED consider-using-f-string:53:10:53:24:print_bad:Formatting a regular string which could be a f-string:UNDEFINED consider-using-f-string:54:10:54:21:print_bad:Formatting a regular string which could be a f-string:UNDEFINED -consider-using-f-string:77:4:77:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED -consider-using-f-string:78:4:78:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED -consider-using-f-string:79:4:79:22:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED -consider-using-f-string:80:4:80:11:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED -consider-using-f-string:81:4:81:20:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED -consider-using-f-string:82:4:82:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED -consider-using-f-string:83:4:83:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED -consider-using-f-string:84:4:84:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED -consider-using-f-string:85:4:85:18:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED +consider-using-f-string:80:4:80:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED +consider-using-f-string:81:4:81:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED +consider-using-f-string:82:4:82:22:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED +consider-using-f-string:83:4:83:11:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED +consider-using-f-string:84:4:84:20:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED +consider-using-f-string:85:4:85:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED consider-using-f-string:86:4:86:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED -consider-using-f-string:109:8:109:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED -consider-using-f-string:110:8:110:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED -consider-using-f-string:111:8:111:26:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED -consider-using-f-string:112:8:112:15:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED -consider-using-f-string:113:8:113:24:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED -consider-using-f-string:114:8:114:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED -consider-using-f-string:115:8:115:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED -consider-using-f-string:116:8:116:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED -consider-using-f-string:117:8:117:22:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED +consider-using-f-string:87:4:87:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED +consider-using-f-string:88:4:88:18:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED +consider-using-f-string:89:4:89:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED +consider-using-f-string:112:8:112:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED +consider-using-f-string:113:8:113:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED +consider-using-f-string:114:8:114:26:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED +consider-using-f-string:115:8:115:15:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED +consider-using-f-string:116:8:116:24:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED +consider-using-f-string:117:8:117:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED consider-using-f-string:118:8:118:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED +consider-using-f-string:119:8:119:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED +consider-using-f-string:120:8:120:22:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED +consider-using-f-string:121:8:121:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED