Skip to content

Commit d0860ca

Browse files
committed
fensu: XSQ009 also reports conversion fields bound by ColumnSpec patterns
XSQ009 only saw `.field` reads, so shared code could still take a conversion holding field out of a column through a pattern such as `let ColumnSpec { holding, .. } = spec`, a match arm or a parameter. conversion_field_lines now also reports a field bound at the top level of a `ColumnSpec { ... }` pattern, on the line where the binding starts. A brace counts as a pattern when its body ends in `..` or it is followed by `=>`, `=`, `:`, `|` or `if`. Struct literals (including `..base` updates), type headers and fields of nested patterns are not reported. The remediation text now says "read or destructure". No such pattern exists in the tree today, so the finding set is unchanged. With the previous matcher, the three new reporting cases fail.
1 parent b2dcdb8 commit d0860ca

4 files changed

Lines changed: 108 additions & 5 deletions

File tree

‎scripts/fensu_policy/_helpers/dialect_features.py‎

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
ATTRIBUTE_LINE,
1515
CFG_FEATURE,
1616
CFG_WORD,
17+
CLOSING_BRACKETS,
18+
COLUMN_SPEC_BRACE,
19+
CONVERSION_FIELD_BINDING,
1720
CONVERSION_FIELD_READ,
1821
DEFAULT_FEATURE,
1922
DEPENDENCIES_TABLE,
@@ -24,12 +27,17 @@
2427
DIALECT_VARIANT_PATH,
2528
FEATURE_SEPARATOR,
2629
FEATURES_TABLE,
30+
FIELD_SEPARATOR,
2731
MODULE_DECLARATION,
2832
NESTED_PATH_PARTS,
33+
OPENING_BRACKETS,
2934
OPTIONAL_KEY,
35+
PATTERN_FOLLOWER,
3036
PROFILE_NAME_CALLS,
37+
REST_PATTERN_END,
3138
RUST_SUFFIX,
3239
SOURCE_ROOT,
40+
TYPE_DECLARATION_WORDS,
3341
WEAK_FEATURE_MARK,
3442
)
3543
from scripts.fensu_policy.models import CrateFeatures, DialectVocabulary, SourceLine
@@ -203,15 +211,69 @@ def dialect_name_lines(*, source: str, dialects: tuple[str, ...]) -> tuple[Sourc
203211

204212

205213
def conversion_field_lines(*, source: str) -> tuple[SourceLine, ...]:
206-
"""Return each line where code reads a field recording how the conversion holds values."""
207-
return _matched_lines(source=source, patterns=(CONVERSION_FIELD_READ,))
214+
"""Return each line where code reads a conversion holding field, by `.field` or a pattern."""
215+
code: str = strip_comments_and_strings(source=source)
216+
starts: set[int] = {match.start() for match in CONVERSION_FIELD_READ.finditer(code)}
217+
starts.update(_pattern_binding_starts(code=code))
218+
return _lines_at(source=source, code=code, starts=starts)
219+
220+
221+
def _pattern_binding_starts(*, code: str) -> set[int]:
222+
"""Return where a `ColumnSpec { .. }` pattern binds a conversion field at its top level."""
223+
starts: set[int] = set()
224+
for match in COLUMN_SPEC_BRACE.finditer(code):
225+
preceding: list[str] = code[: match.start()].split()
226+
if preceding and preceding[-1] in TYPE_DECLARATION_WORDS:
227+
continue
228+
close: int | None = _closing_brace(code=code, opening=match.end() - 1)
229+
if close is None:
230+
continue
231+
body: str = code[match.end() : close]
232+
if not (REST_PATTERN_END.search(body) or PATTERN_FOLLOWER.match(code, close + 1)):
233+
continue
234+
for offset, segment in _top_level_segments(text=body):
235+
if CONVERSION_FIELD_BINDING.match(segment):
236+
starts.add(match.end() + offset + len(segment) - len(segment.lstrip()))
237+
return starts
238+
239+
240+
def _closing_brace(*, code: str, opening: int) -> int | None:
241+
depth: int = 0
242+
for index in range(opening, len(code)):
243+
if code[index] in OPENING_BRACKETS:
244+
depth += 1
245+
elif code[index] in CLOSING_BRACKETS:
246+
depth -= 1
247+
if depth == 0:
248+
return index
249+
return None
250+
251+
252+
def _top_level_segments(*, text: str) -> list[tuple[int, str]]:
253+
segments: list[tuple[int, str]] = []
254+
depth: int = 0
255+
start: int = 0
256+
for index, character in enumerate(text):
257+
if character in OPENING_BRACKETS:
258+
depth += 1
259+
elif character in CLOSING_BRACKETS:
260+
depth -= 1
261+
elif character == FIELD_SEPARATOR and depth == 0:
262+
segments.append((start, text[start:index]))
263+
start = index + 1
264+
segments.append((start, text[start:]))
265+
return segments
208266

209267

210268
def _matched_lines(*, source: str, patterns: tuple[re.Pattern[str], ...]) -> tuple[SourceLine, ...]:
211-
raw_lines: list[str] = strip_comments(source=source).split("\n")
212269
code: str = strip_comments_and_strings(source=source)
213270
starts: set[int] = set()
214271
for pattern in patterns:
215272
starts.update(match.start() for match in pattern.finditer(code))
273+
return _lines_at(source=source, code=code, starts=starts)
274+
275+
276+
def _lines_at(*, source: str, code: str, starts: set[int]) -> tuple[SourceLine, ...]:
277+
raw_lines: list[str] = strip_comments(source=source).split("\n")
216278
numbers: list[int] = sorted({code.count("\n", 0, start) + 1 for start in starts})
217279
return tuple(SourceLine(line=number, text=raw_lines[number - 1].strip()) for number in numbers)

‎scripts/fensu_policy/constants.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@
6969
CONVERSION_FIELD_READ: re.Pattern[str] = re.compile(
7070
r"\.\s*(?:{fields})\b(?!\s*\()".format(fields="|".join(CONVERSION_FIELDS))
7171
)
72+
COLUMN_SPEC_BRACE: re.Pattern[str] = re.compile(r"\bColumnSpec\s*(?:::\s*<[^{}]*>\s*)?\{")
73+
TYPE_DECLARATION_WORDS: frozenset[str] = frozenset({"impl", "struct", "enum", "trait", "for"})
74+
REST_PATTERN_END: re.Pattern[str] = re.compile(r"(?:^|,)\s*\.\.\s*$")
75+
PATTERN_FOLLOWER: re.Pattern[str] = re.compile(r"\s*(?:=>|=(?!=)|:(?!:)|\||if\b)")
76+
CONVERSION_FIELD_BINDING: re.Pattern[str] = re.compile(
77+
r"\s*(?:ref\s+)?(?:mut\s+)?(?:{fields})\s*(?::(?!:)|$)".format(
78+
fields="|".join(CONVERSION_FIELDS)
79+
)
80+
)
81+
OPENING_BRACKETS: str = "([{"
82+
CLOSING_BRACKETS: str = ")]}"
83+
FIELD_SEPARATOR: str = ","
7284

7385
DEFAULT_DIALECTS: tuple[str, ...] = (
7486
"snowflake",

‎scripts/fensu_policy/rules/conversion_fields.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
slug="conversion-fields-unread",
2323
message="shared code leaves the conversion's holding fields to the conversion",
2424
remediation=(
25-
"Read a column's holding, typed_leaves, decimal_leaves, untainted, sourced, utc_offset, "
26-
"declared_offsets, widened or suffixed only in the source dialect's src/<dialect>/ code. "
25+
"Read or destructure a column's holding, typed_leaves, decimal_leaves, untainted, sourced, "
26+
"utc_offset, declared_offsets, widened or suffixed only in the source dialect's "
27+
"src/<dialect>/ code. "
2728
"Shared code asks the column's type through `DialectType` instead."
2829
),
2930
analyzers=(AnalyzerId.RUST,),

‎tests/unit/scripts/fensu_policy/test_conversion_fields.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,34 @@
6060
"let x = plan.holding();\nlet y = row.sourced_at;\nlet z = Holding::is_json;\n",
6161
(),
6262
),
63+
ConversionFieldLinesTestCase(
64+
"destructuring a field from a column fails",
65+
"let ColumnSpec { holding, .. } = spec;\n",
66+
(1,),
67+
),
68+
ConversionFieldLinesTestCase(
69+
"a match arm binding fields fails on each bound field's line",
70+
"match spec {\n ColumnSpec {\n name,\n utc_offset: true,\n"
71+
" ref mut declared_offsets,\n ..\n } => name,\n}\n",
72+
(4, 5),
73+
),
74+
ConversionFieldLinesTestCase(
75+
"an if-let pattern without a rest, and a parameter pattern, fail",
76+
"if let ColumnSpec { widened: w, name, data_type } = spec {}\n"
77+
"fn f(ColumnSpec::<T> { sourced, .. }: ColumnSpec<T>) {}\n",
78+
(1, 2),
79+
),
80+
ConversionFieldLinesTestCase(
81+
"a pattern binding only other fields, or a nested pattern's field, passes",
82+
"let ColumnSpec { name, data_type: Inner { holding, .. }, .. } = spec;\n",
83+
(),
84+
),
85+
ConversionFieldLinesTestCase(
86+
"a literal building a column from a base passes, as do type headers",
87+
"let c = ColumnSpec {\n holding,\n suffixed: false,\n ..base\n};\n"
88+
"impl ColumnSpec {\n fn f() {}\n}\npub struct ColumnSpec<T> {\n holding: H,\n}\n",
89+
(),
90+
),
6391
ConversionFieldLinesTestCase(
6492
"a field named in a string or comment passes",
6593
'// column.holding\nlet s = "column.widened";\n',

0 commit comments

Comments
 (0)