Skip to content

Commit 2e0a975

Browse files
authored
Disallow access to Parsed output, use the API instead (#11741)
## Summary This PR is a follow-up to #11740 to restrict access to the `Parsed` output by replacing the `parsed` API function with a more specific one. Currently, that is `comment_ranges` but the linked PR exposes a `tokens` method. The main motivation is so that there's no way to get an incorrect information from the checker. And, it also encapsulates the source of the comment ranges and the tokens itself. This way it would become easier to just update the checker if the source for these information changes in the future. ## Test Plan `cargo insta test`
1 parent b021b5b commit 2e0a975

36 files changed

+44
-70
lines changed

crates/ruff_linter/src/checkers/ast/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use ruff_python_semantic::{
5858
StarImport, SubmoduleImport,
5959
};
6060
use ruff_python_stdlib::builtins::{IPYTHON_BUILTINS, MAGIC_GLOBALS, PYTHON_BUILTINS};
61+
use ruff_python_trivia::CommentRanges;
6162
use ruff_source_file::{Locator, OneIndexed, SourceRow};
6263
use ruff_text_size::{Ranged, TextRange, TextSize};
6364

@@ -326,9 +327,9 @@ impl<'a> Checker<'a> {
326327
}
327328
}
328329

329-
/// The [`Parsed`] output for the current file, which contains the tokens, AST, and more.
330-
pub(crate) const fn parsed(&self) -> &'a Parsed<ModModule> {
331-
self.parsed
330+
/// Returns the [`CommentRanges`] for the parsed source code.
331+
pub(crate) fn comment_ranges(&self) -> &'a CommentRanges {
332+
self.parsed.comment_ranges()
332333
}
333334

334335
/// Returns the [`Tokens`] for the parsed type annotation if the checker is in a typing context

crates/ruff_linter/src/rules/flake8_bugbear/rules/zip_without_explicit_strict.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub(crate) fn zip_without_explicit_strict(checker: &mut Checker, call: &ast::Exp
6868
add_argument(
6969
"strict=False",
7070
&call.arguments,
71-
checker.parsed().comment_ranges(),
71+
checker.comment_ranges(),
7272
checker.locator().contents(),
7373
),
7474
// If the function call contains `**kwargs`, mark the fix as unsafe.

crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ pub(crate) fn unnecessary_generator_list(checker: &mut Checker, call: &ast::Expr
139139
let range = parenthesized_range(
140140
argument.into(),
141141
(&call.arguments).into(),
142-
checker.parsed().comment_ranges(),
142+
checker.comment_ranges(),
143143
checker.locator().contents(),
144144
)
145145
.unwrap_or(argument.range());

crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_dict_kwargs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ pub(crate) fn unnecessary_dict_kwargs(checker: &mut Checker, call: &ast::ExprCal
129129
parenthesized_range(
130130
value.into(),
131131
dict.into(),
132-
checker.parsed().comment_ranges(),
132+
checker.comment_ranges(),
133133
checker.locator().contents(),
134134
)
135135
.unwrap_or(value.range())

crates/ruff_linter/src/rules/flake8_pyi/rules/generic_not_last_base_class.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn generate_fix(
114114
let insertion = add_argument(
115115
locator.slice(generic_base),
116116
arguments,
117-
checker.parsed().comment_ranges(),
117+
checker.comment_ranges(),
118118
source,
119119
);
120120

crates/ruff_linter/src/rules/flake8_pytest_style/rules/assertion.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,15 +284,15 @@ pub(crate) fn unittest_assertion(
284284
// the assertion is part of a larger expression.
285285
if checker.semantic().current_statement().is_expr_stmt()
286286
&& checker.semantic().current_expression_parent().is_none()
287-
&& !checker.parsed().comment_ranges().intersects(expr.range())
287+
&& !checker.comment_ranges().intersects(expr.range())
288288
{
289289
if let Ok(stmt) = unittest_assert.generate_assert(args, keywords) {
290290
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
291291
checker.generator().stmt(&stmt),
292292
parenthesized_range(
293293
expr.into(),
294294
checker.semantic().current_statement().into(),
295-
checker.parsed().comment_ranges(),
295+
checker.comment_ranges(),
296296
checker.locator().contents(),
297297
)
298298
.unwrap_or(expr.range()),
@@ -385,7 +385,6 @@ pub(crate) fn unittest_raises_assertion(
385385
call.func.range(),
386386
);
387387
if !checker
388-
.parsed()
389388
.comment_ranges()
390389
.has_comments(call, checker.locator())
391390
{
@@ -745,7 +744,7 @@ pub(crate) fn composite_condition(
745744
let mut diagnostic = Diagnostic::new(PytestCompositeAssertion, stmt.range());
746745
if matches!(composite, CompositionKind::Simple)
747746
&& msg.is_none()
748-
&& !checker.parsed().comment_ranges().intersects(stmt.range())
747+
&& !checker.comment_ranges().intersects(stmt.range())
749748
&& !checker
750749
.indexer()
751750
.in_multi_statement_line(stmt, checker.locator())

crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) {
353353
let name_range = get_parametrize_name_range(
354354
decorator,
355355
expr,
356-
checker.parsed().comment_ranges(),
356+
checker.comment_ranges(),
357357
checker.locator().contents(),
358358
)
359359
.unwrap_or(expr.range());
@@ -388,7 +388,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) {
388388
let name_range = get_parametrize_name_range(
389389
decorator,
390390
expr,
391-
checker.parsed().comment_ranges(),
391+
checker.comment_ranges(),
392392
checker.locator().contents(),
393393
)
394394
.unwrap_or(expr.range());
@@ -681,7 +681,7 @@ fn check_duplicates(checker: &mut Checker, values: &Expr) {
681681
let element_end =
682682
trailing_comma(element, checker.locator().contents(), values_end);
683683
let deletion_range = TextRange::new(previous_end, element_end);
684-
if !checker.parsed().comment_ranges().intersects(deletion_range) {
684+
if !checker.comment_ranges().intersects(deletion_range) {
685685
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_deletion(deletion_range)));
686686
}
687687
}

crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,6 @@ pub(crate) fn compare_with_tuple(checker: &mut Checker, expr: &Expr) {
527527

528528
// Avoid removing comments.
529529
if checker
530-
.parsed()
531530
.comment_ranges()
532531
.has_comments(expr, checker.locator())
533532
{
@@ -779,7 +778,7 @@ fn is_short_circuit(
779778
parenthesized_range(
780779
furthest.into(),
781780
expr.into(),
782-
checker.parsed().comment_ranges(),
781+
checker.comment_ranges(),
783782
checker.locator().contents(),
784783
)
785784
.unwrap_or(furthest.range())
@@ -807,7 +806,7 @@ fn is_short_circuit(
807806
parenthesized_range(
808807
furthest.into(),
809808
expr.into(),
810-
checker.parsed().comment_ranges(),
809+
checker.comment_ranges(),
811810
checker.locator().contents(),
812811
)
813812
.unwrap_or(furthest.range())

crates/ruff_linter/src/rules/flake8_simplify/rules/ast_ifexp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ pub(crate) fn if_expr_with_true_false(
164164
parenthesized_range(
165165
test.into(),
166166
expr.into(),
167-
checker.parsed().comment_ranges(),
167+
checker.comment_ranges(),
168168
checker.locator().contents(),
169169
)
170170
.unwrap_or(test.range()),

crates/ruff_linter/src/rules/flake8_simplify/rules/ast_with.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ pub(crate) fn multiple_with_statements(
168168
TextRange::new(with_stmt.start(), colon.end()),
169169
);
170170
if !checker
171-
.parsed()
172171
.comment_ranges()
173172
.intersects(TextRange::new(with_stmt.start(), with_stmt.body[0].start()))
174173
{

0 commit comments

Comments
 (0)