MartinThoma / flake8-simplify

❄ A flake8 plugin that helps you to simplify code
MIT License
187 stars 19 forks source link

[New Rule] Unnecessary use of backslash escaping #162

Open rpdelaney opened 2 years ago

rpdelaney commented 2 years ago

Explanation

raw strings are more readable and maintainable for humans in many cases.

Prior art:

Example

# Bad
path = "C:\\Users\\root"

# Good
path = r"C:\Users\root"

# Bad
regex = "\\^\\(\\?P<major>0\\|\\[1\\-9\\]\\\\d\\*\\)\\\\\\.\\(\\?P<minor>0\\|\\[1\\-9\\]\\\\d\\*\\)\\\\\\.\\(\\?P<patch>0\\|\\[1\\-9\\]\\\\d\\*\\)\\(\\?:\\-\\(\\?P<prerelease>\\(\\?:0\\|\\[1\\-9\\]\\\\d\\*\\|\\\\d\\*\\[a\\-zA\\-Z\\-\\]\\[0\\-9a\\-zA\\-Z\\-\\]\\*\\)\\(\\?:\\\\\\.\\(\\?:0\\|\\[1\\-9\\]\\\\d\\*\\|\\\\d\\*\\[a\\-zA\\-Z\\-\\]\\[0\\-9a\\-zA\\-Z\\-\\]\\*\\)\\)\\*\\)\\)\\?\\(\\?:\\\\\\+\\(\\?P<buildmetadata>\\[0\\-9a\\-zA\\-Z\\-\\]\\+\\(\\?:\\\\\\.\\[0\\-9a\\-zA\\-Z\\-\\]\\+\\)\\*\\)\\)\\?\\$"

# Good
regex = r"^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"