Sonarlint python:S5361

Please provide

  • Operating system: Windows 10
  • IDE name and flavor/env: VS Core 1.65.2
  • SonarLint plugin version:3.3.3
  • Is connected mode used:No
    • Connected to SonarCloud or SonarQube (and which version):

And a thorough description of the problem / question:
The subject rule, str.replace should be preferred to re.sub, being triggered says re.sub can be replaced with str.replace because the pattern string has no regular expression features. While it is true that the pattern string in my instance has no regular expression features, the re.sub call is using re functionality, namely flags is set to re.IGNORECASE. I cannot obtain this with with str.replace. I do not want to call .lower on my source string before substitution because I need to preserve case in the original text (other than the bit I am replacing). Any advice?

Hey there.

I’ve moved your post to the category for reporting false-positives. Please see this thread and update your topic with the relevant information including a code sample.

Here is a simple example:
re.sub(“something”, “some other thing”, string, flags=re.IGNORECASE)
I can’t use string.replace() because I want to replace regardless of the case of “something”.
I can’t use string.lower().replace() because I don’t want to change the case of string.

Hi @Nmhor_Kmhor
and welcome to the community,

thank you for reporting this false positive. I’ve created the ticket SONARPY-994
and we will solve this problem with the next release.


Thank you Nils and Colin!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.