Skip to content

Commit f0046ab

Browse files
authored
Move has_comments to CommentRanges (#11495)
## Summary This PR moves the `has_comments` function from `Indexer` to `CommentRanges`. The main motivation is that the `CommentRanges` will now be built by the parser which is shared between the linter and the formatter. Thus, the `CommentRanges` will be removed from the `Indexer`. ## Test Plan `cargo test`
1 parent 5bb9720 commit f0046ab

File tree

10 files changed

+65
-29
lines changed

10 files changed

+65
-29
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,11 @@ pub(crate) fn unittest_raises_assertion(
384384
},
385385
call.func.range(),
386386
);
387-
if !checker.indexer().has_comments(call, checker.locator()) {
387+
if !checker
388+
.indexer()
389+
.comment_ranges()
390+
.has_comments(call, checker.locator())
391+
{
388392
if let Some(args) = to_pytest_raises_args(checker, attr.as_str(), &call.arguments) {
389393
diagnostic.try_set_fix(|| {
390394
let (import_edit, binding) = checker.importer().get_or_import_symbol(

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,11 @@ pub(crate) fn compare_with_tuple(checker: &mut Checker, expr: &Expr) {
526526
}
527527

528528
// Avoid removing comments.
529-
if checker.indexer().has_comments(expr, checker.locator()) {
529+
if checker
530+
.indexer()
531+
.comment_ranges()
532+
.has_comments(expr, checker.locator())
533+
{
530534
continue;
531535
}
532536

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,11 @@ pub(crate) fn if_else_block_instead_of_dict_get(checker: &mut Checker, stmt_if:
209209
},
210210
stmt_if.range(),
211211
);
212-
if !checker.indexer().has_comments(stmt_if, checker.locator()) {
212+
if !checker
213+
.indexer()
214+
.comment_ranges()
215+
.has_comments(stmt_if, checker.locator())
216+
{
213217
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
214218
contents,
215219
stmt_if.range(),
@@ -295,7 +299,11 @@ pub(crate) fn if_exp_instead_of_dict_get(
295299
},
296300
expr.range(),
297301
);
298-
if !checker.indexer().has_comments(expr, checker.locator()) {
302+
if !checker
303+
.indexer()
304+
.comment_ranges()
305+
.has_comments(expr, checker.locator())
306+
{
299307
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
300308
contents,
301309
expr.range(),

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ pub(crate) fn if_else_block_instead_of_if_exp(checker: &mut Checker, stmt_if: &a
142142
},
143143
stmt_if.range(),
144144
);
145-
if !checker.indexer().has_comments(stmt_if, checker.locator()) {
145+
if !checker
146+
.indexer()
147+
.comment_ranges()
148+
.has_comments(stmt_if, checker.locator())
149+
{
146150
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
147151
contents,
148152
stmt_if.range(),

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,11 @@ pub(crate) fn needless_bool(checker: &mut Checker, stmt: &Stmt) {
193193
}
194194

195195
// Generate the replacement condition.
196-
let condition = if checker.indexer().has_comments(&range, checker.locator()) {
196+
let condition = if checker
197+
.indexer()
198+
.comment_ranges()
199+
.has_comments(&range, checker.locator())
200+
{
197201
None
198202
} else {
199203
// If the return values are inverted, wrap the condition in a `not`.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,11 @@ pub(crate) fn suppressible_exception(
125125
},
126126
stmt.range(),
127127
);
128-
if !checker.indexer().has_comments(stmt, checker.locator()) {
128+
if !checker
129+
.indexer()
130+
.comment_ranges()
131+
.has_comments(stmt, checker.locator())
132+
{
129133
diagnostic.try_set_fix(|| {
130134
// let range = statement_range(stmt, checker.locator(), checker.indexer());
131135

crates/ruff_linter/src/rules/pylint/rules/nested_min_max.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ pub(crate) fn nested_min_max(
155155
MinMax::try_from_call(func.as_ref(), keywords.as_ref(), checker.semantic()) == Some(min_max)
156156
}) {
157157
let mut diagnostic = Diagnostic::new(NestedMinMax { func: min_max }, expr.range());
158-
if !checker.indexer().has_comments(expr, checker.locator()) {
158+
if !checker
159+
.indexer()
160+
.comment_ranges()
161+
.has_comments(expr, checker.locator())
162+
{
159163
let flattened_expr = Expr::Call(ast::ExprCall {
160164
func: Box::new(func.clone()),
161165
arguments: Arguments {

crates/ruff_linter/src/rules/ruff/rules/collection_literal_concatenation.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,11 @@ pub(crate) fn collection_literal_concatenation(checker: &mut Checker, expr: &Exp
198198
},
199199
expr.range(),
200200
);
201-
if !checker.indexer().has_comments(expr, checker.locator()) {
201+
if !checker
202+
.indexer()
203+
.comment_ranges()
204+
.has_comments(expr, checker.locator())
205+
{
202206
// This suggestion could be unsafe if the non-literal expression in the
203207
// expression has overridden the `__add__` (or `__radd__`) magic methods.
204208
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(

crates/ruff_python_index/src/indexer.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,25 +112,6 @@ impl Indexer {
112112
self.continuation_lines.binary_search(&line_start).is_ok()
113113
}
114114

115-
/// Returns `true` if a statement or expression includes at least one comment.
116-
pub fn has_comments<T>(&self, node: &T, locator: &Locator) -> bool
117-
where
118-
T: Ranged,
119-
{
120-
let start = if has_leading_content(node.start(), locator) {
121-
node.start()
122-
} else {
123-
locator.line_start(node.start())
124-
};
125-
let end = if has_trailing_content(node.end(), locator) {
126-
node.end()
127-
} else {
128-
locator.line_end(node.end())
129-
};
130-
131-
self.comment_ranges().intersects(TextRange::new(start, end))
132-
}
133-
134115
/// Given an offset at the end of a line (including newlines), return the offset of the
135116
/// continuation at the end of that line.
136117
fn find_continuation(&self, offset: TextSize, locator: &Locator) -> Option<TextSize> {

crates/ruff_python_trivia/src/comment_ranges.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ruff_source_file::Locator;
66

77
use ruff_text_size::{Ranged, TextRange, TextSize};
88

9-
use crate::is_python_whitespace;
9+
use crate::{has_leading_content, has_trailing_content, is_python_whitespace};
1010

1111
/// Stores the ranges of comments sorted by [`TextRange::start`] in increasing order. No two ranges are overlapping.
1212
#[derive(Clone, Default)]
@@ -49,6 +49,25 @@ impl CommentRanges {
4949
}
5050
}
5151

52+
/// Returns `true` if a statement or expression includes at least one comment.
53+
pub fn has_comments<T>(&self, node: &T, locator: &Locator) -> bool
54+
where
55+
T: Ranged,
56+
{
57+
let start = if has_leading_content(node.start(), locator) {
58+
node.start()
59+
} else {
60+
locator.line_start(node.start())
61+
};
62+
let end = if has_trailing_content(node.end(), locator) {
63+
node.end()
64+
} else {
65+
locator.line_end(node.end())
66+
};
67+
68+
self.intersects(TextRange::new(start, end))
69+
}
70+
5271
/// Given a [`CommentRanges`], determine which comments are grouped together
5372
/// in "comment blocks". A "comment block" is a sequence of consecutive
5473
/// own-line comments in which the comment hash (`#`) appears in the same

0 commit comments

Comments
 (0)