Python: 8 rules to reduce the complexity of your regular expressions + 9 common rules

Hello Python developers,

Regular Expression Rules

We added 8 new rules to help you reduce the complexity of your regular expressions. They come on top of the rules we released in Nov 2021 to help your write efficient, error-free, and safe regex.

This new set of rules is there to help you rewrite your regex in a simple way to make them more readable and easy to maintain by identifing unnecessary elements.

  • S6396: Superfluous curly brace quantifiers should be avoided
  • S6397: Character classes in regular expressions should not contain only one character
  • S6323: Alternation in regular expressions should not contain empty alternatives
  • S6326: Regular expressions should not contain multiple spaces
  • S6353: Regular expression quantifiers and character classes should be used concisely
  • S6328: Replacement strings should reference existing regular expression groups
  • S6331: Regular expressions should not contain empty groups
  • S6395: Non-capturing groups without quantifier should not be used

Common Rules

We also included in this update 9 common rules that every developer would expect to find in a static analysis product. Surprisingly, the famous rules detecting “TODO” or “NOSONAR” in your code were not there for years so we added them and some others:

  • S3699: The output of functions that don’t return anything should not be used
  • S1172: Unused function parameters should be removed
  • S3801: Functions should use “return” consistently
  • S1135: Track uses of “TODO” tags
  • S1291: Track uses of “NOSONAR” comments
  • S138: Functions should not have too many lines of code
  • S1451: Track lack of copyright and license headers
  • S1940: Boolean checks should not be inverted
  • S2761: Doubled prefix operators “!!” and “~~” should not be used

:snake: We will be at the PyCon22 conference end of April so if you are around, we would love to speak to you at our booth.

Alex

3 Likes