Skip to content

Commit 3cd4953

Browse files
committed
Ruff rules SIM for simplification
1 parent 26a8d68 commit 3cd4953

File tree

3 files changed

+59
-80
lines changed

3 files changed

+59
-80
lines changed

cpplint.py

Lines changed: 51 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,11 +1326,10 @@ def IsInAlphabeticalOrder(self, clean_lines, linenum, header_path):
13261326
#
13271327
# If previous line was a blank line, assume that the headers are
13281328
# intentionally sorted the way they are.
1329-
if self._last_header > header_path and re.match(
1330-
r"^\s*#\s*include\b", clean_lines.elided[linenum - 1]
1331-
):
1332-
return False
1333-
return True
1329+
return not (
1330+
self._last_header > header_path
1331+
and re.match(r"^\s*#\s*include\b", clean_lines.elided[linenum - 1])
1332+
)
13341333

13351334
def CheckNextIncludeOrder(self, header_type):
13361335
"""Returns a non-empty error message if the next header is out of order.
@@ -1844,10 +1843,7 @@ def _ShouldPrintError(category, confidence, filename, linenum):
18441843
# should have been checked for in SetFilter.
18451844
msg = f"Invalid filter: {one_filter}"
18461845
raise ValueError(msg)
1847-
if is_filtered:
1848-
return False
1849-
1850-
return True
1846+
return not is_filtered
18511847

18521848

18531849
def Error(filename, linenum, category, confidence, message):
@@ -2262,11 +2258,10 @@ def FindEndOfExpressionInLine(line, startpos, stack):
22622258

22632259
# Pop the stack if there is a matching '<'. Otherwise, ignore
22642260
# this '>' since it must be an operator.
2265-
if stack:
2266-
if stack[-1] == "<":
2267-
stack.pop()
2268-
if not stack:
2269-
return (i + 1, None)
2261+
if stack and stack[-1] == "<":
2262+
stack.pop()
2263+
if not stack:
2264+
return (i + 1, None)
22702265
elif char == ";":
22712266
# Found something that look like end of statements. If we are currently
22722267
# expecting a '>', the matching '<' must have been an operator, since
@@ -2963,10 +2958,7 @@ def IsMacroDefinition(clean_lines, linenum):
29632958
if re.search(r"^#define", clean_lines[linenum]):
29642959
return True
29652960

2966-
if linenum > 0 and re.search(r"\\$", clean_lines[linenum - 1]):
2967-
return True
2968-
2969-
return False
2961+
return bool(linenum > 0 and re.search(r"\\$", clean_lines[linenum - 1]))
29702962

29712963

29722964
def IsForwardClassDeclaration(clean_lines, linenum):
@@ -4352,20 +4344,19 @@ def CheckParenthesisSpacing(filename, clean_lines, linenum, error):
43524344
line,
43534345
)
43544346
if match:
4355-
if len(match.group(2)) != len(match.group(4)):
4356-
if not (
4357-
match.group(3) == ";"
4358-
and len(match.group(2)) == 1 + len(match.group(4))
4359-
or not match.group(2)
4360-
and re.search(r"\bfor\s*\(.*; \)", line)
4361-
):
4362-
error(
4363-
filename,
4364-
linenum,
4365-
"whitespace/parens",
4366-
5,
4367-
f"Mismatching spaces inside () in {match.group(1)}",
4368-
)
4347+
if len(match.group(2)) != len(match.group(4)) and not (
4348+
match.group(3) == ";"
4349+
and len(match.group(2)) == 1 + len(match.group(4))
4350+
or not match.group(2)
4351+
and re.search(r"\bfor\s*\(.*; \)", line)
4352+
):
4353+
error(
4354+
filename,
4355+
linenum,
4356+
"whitespace/parens",
4357+
5,
4358+
f"Mismatching spaces inside () in {match.group(1)}",
4359+
)
43694360
if len(match.group(2)) not in [0, 1]:
43704361
error(
43714362
filename,
@@ -4586,9 +4577,7 @@ def IsDecltype(clean_lines, linenum, column):
45864577
(text, _, start_col) = ReverseCloseExpression(clean_lines, linenum, column)
45874578
if start_col < 0:
45884579
return False
4589-
if re.search(r"\bdecltype\s*$", text[0:start_col]):
4590-
return True
4591-
return False
4580+
return bool(re.search(r"\bdecltype\s*$", text[0:start_col]))
45924581

45934582

45944583
def CheckSectionSpacing(filename, clean_lines, class_info, linenum, error):
@@ -4761,23 +4750,18 @@ def CheckBraces(filename, clean_lines, linenum, error):
47614750
# No control clauses with braces should have its contents on the same line
47624751
# Exclude } which will be covered by empty-block detect
47634752
# Exclude ; which may be used by while in a do-while
4764-
if keyword := re.search(
4765-
r"\b(else if|if|while|for|switch)" # These have parens
4766-
r"\s*\(.*\)\s*(?:\[\[(?:un)?likely\]\]\s*)?{\s*[^\s\\};]",
4767-
line,
4768-
):
4769-
error(
4770-
filename,
4771-
linenum,
4772-
"whitespace/newline",
4773-
5,
4774-
f"Controlled statements inside brackets of {keyword.group(1)} clause"
4775-
" should be on a separate line",
4753+
if (
4754+
keyword := re.search(
4755+
r"\b(else if|if|while|for|switch)" # These have parens
4756+
r"\s*\(.*\)\s*(?:\[\[(?:un)?likely\]\]\s*)?{\s*[^\s\\};]",
4757+
line,
4758+
)
4759+
) or (
4760+
keyword := re.search(
4761+
r"\b(else|do|try)" # These don't have parens
4762+
r"\s*(?:\[\[(?:un)?likely\]\]\s*)?{\s*[^\s\\}]",
4763+
line,
47764764
)
4777-
elif keyword := re.search(
4778-
r"\b(else|do|try)" # These don't have parens
4779-
r"\s*(?:\[\[(?:un)?likely\]\]\s*)?{\s*[^\s\\}]",
4780-
line,
47814765
):
47824766
error(
47834767
filename,
@@ -5430,10 +5414,9 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state, er
54305414
)
54315415

54325416
# Check if the line is a header guard.
5433-
is_header_guard = False
5434-
if IsHeaderExtension(file_extension):
5435-
if line.startswith((f"#ifndef {cppvar}", f"#define {cppvar}", f"#endif // {cppvar}")):
5436-
is_header_guard = True
5417+
is_header_guard = IsHeaderExtension(file_extension) and line.startswith(
5418+
(f"#ifndef {cppvar}", f"#define {cppvar}", f"#endif // {cppvar}")
5419+
)
54375420
# #include lines and header guards can be long, since there's no clean way to
54385421
# split them.
54395422
#
@@ -5648,17 +5631,18 @@ def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
56485631
# We also make an exception for Lua headers, which follow google
56495632
# naming convention but not the include convention.
56505633
match = re.match(r'#include\s*"([^/]+\.(.*))"', line)
5651-
if match:
5652-
if IsHeaderExtension(match.group(2)) and not _THIRD_PARTY_HEADERS_PATTERN.match(
5653-
match.group(1)
5654-
):
5655-
error(
5656-
filename,
5657-
linenum,
5658-
"build/include_subdir",
5659-
4,
5660-
"Include the directory when naming header files",
5661-
)
5634+
if (
5635+
match
5636+
and IsHeaderExtension(match.group(2))
5637+
and not _THIRD_PARTY_HEADERS_PATTERN.match(match.group(1))
5638+
):
5639+
error(
5640+
filename,
5641+
linenum,
5642+
"build/include_subdir",
5643+
4,
5644+
"Include the directory when naming header files",
5645+
)
56625646

56635647
# we shouldn't include a file more than once. actually, there are a
56645648
# handful of instances where doing so is okay, but in general it's
@@ -6326,8 +6310,7 @@ def CheckForNonConstReference(filename, clean_lines, linenum, nesting_state, err
63266310
# function body, including one that was just introduced by a trailing '{'.
63276311
# TODO(unknown): Doesn't account for 'catch(Exception& e)' [rare].
63286312
if nesting_state.previous_stack_top and not (
6329-
isinstance(nesting_state.previous_stack_top, _ClassInfo)
6330-
or isinstance(nesting_state.previous_stack_top, _NamespaceInfo)
6313+
isinstance(nesting_state.previous_stack_top, (_ClassInfo, _NamespaceInfo))
63316314
):
63326315
# Not at toplevel, not within a class, and not within a namespace
63336316
return

cpplint_clitest.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
"""Command Line interface integration test for cpplint.py."""
3232

33+
import contextlib
3334
import glob
3435
import os
3536
import shutil
@@ -54,7 +55,7 @@ def run_shell_command(cmd: str, args: str, cwd="."):
5455
cwd: from which folder to run.
5556
"""
5657
cmd, args = cmd.split(), args.split()
57-
proc = subprocess.run(cmd + args, cwd=cwd, capture_output=True)
58+
proc = subprocess.run(cmd + args, cwd=cwd, capture_output=True, check=False)
5859
out, err = proc.stdout, proc.stderr
5960

6061
# Make output system-agnostic, aka support Windows
@@ -99,10 +100,8 @@ def setUpClass(cls):
99100
shutil.copytree("samples", os.path.join(cls._root, "samples"))
100101
cls.prepare_directory(cls._root)
101102
except Exception:
102-
try:
103+
with contextlib.suppress(Exception):
103104
cls.tearDownClass()
104-
except Exception: # noqa: BLE001
105-
pass
106105
raise
107106

108107
@classmethod

pyproject.toml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,12 @@ lint.select = [
9393
"PERF", # Perflint
9494
"PGH", # pygrep-hooks
9595
"PIE", # flake8-pie
96-
"PLC", # Pylint conventions
97-
"PLE", # Pylint errors
96+
"PL", # Pylint
9897
"PYI", # flake8-pyi
98+
"Q", # flake8-quotes
9999
"RET", # flake8-return
100100
"RSE", # flake8-raise
101+
"SIM", # flake8-simplify
101102
"SLOT", # flake8-slots
102103
"T10", # flake8-debugger
103104
"TC", # flake8-type-checking
@@ -114,14 +115,10 @@ lint.select = [
114115
# "DOC", # pydoclint
115116
# "ERA", # eradicate
116117
# "N", # pep8-naming
117-
# "PLR", # Pylint refactor
118-
# "PLW", # Pylint warnings
119118
# "PT", # flake8-pytest-style
120119
# "PTH", # flake8-use-pathlib
121-
# "Q", # flake8-quotes
122120
# "RUF", # Ruff-specific rules
123121
# "S", # flake8-bandit
124-
# "SIM", # flake8-simplify
125122
# "SLF", # flake8-self
126123
# "T20", # flake8-print
127124
# "TD", # flake8-todos
@@ -132,8 +129,8 @@ lint.ignore = [
132129
"ISC003", # flake8-implicit-str-concat
133130
"PIE790", # Unnecessary `pass` statement
134131
]
135-
lint.per-file-ignores."cpplint.py" = [ "ICN001", "PERF401", "PLR5501", "PLW0603", "PLW2901" ]
136-
lint.per-file-ignores."cpplint_unittest.py" = [ "FLY002", "PLW0604", "UP031" ]
132+
lint.per-file-ignores."cpplint.py" = [ "ICN001", "PERF401", "PLR5501", "PLW0603", "PLW2901", "SIM102", "SIM108" ]
133+
lint.per-file-ignores."cpplint_unittest.py" = [ "FLY002", "PLW0604", "SIM115", "UP031" ]
137134
lint.mccabe.max-complexity = 29
138135
lint.pylint.allow-magic-value-types = [ "int", "str" ]
139136
lint.pylint.max-args = 10 # Default is 5

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy