refactor: Return copied TextRange in CommentRanges iterator (#13281)

This commit is contained in:
Micha Reiser 2024-09-08 13:17:37 +02:00 committed by GitHub
parent e4aa479515
commit 35d45c1e4b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 20 additions and 20 deletions

View file

@ -63,7 +63,7 @@ pub(crate) fn check_tokens(
ruff::rules::ambiguous_unicode_character_comment( ruff::rules::ambiguous_unicode_character_comment(
&mut diagnostics, &mut diagnostics,
locator, locator,
*range, range,
settings, settings,
); );
} }

View file

@ -361,7 +361,7 @@ impl<'a> FileNoqaDirectives<'a> {
let mut lines = vec![]; let mut lines = vec![];
for range in comment_ranges { for range in comment_ranges {
match ParsedFileExemption::try_extract(&contents[*range]) { match ParsedFileExemption::try_extract(&contents[range]) {
Err(err) => { Err(err) => {
#[allow(deprecated)] #[allow(deprecated)]
let line = locator.compute_line_index(range.start()); let line = locator.compute_line_index(range.start());
@ -403,7 +403,7 @@ impl<'a> FileNoqaDirectives<'a> {
}; };
lines.push(FileNoqaDirectiveLine { lines.push(FileNoqaDirectiveLine {
range: *range, range,
parsed_file_exemption: exemption, parsed_file_exemption: exemption,
matches, matches,
}); });
@ -922,7 +922,7 @@ impl<'a> NoqaDirectives<'a> {
let mut directives = Vec::new(); let mut directives = Vec::new();
for range in comment_ranges { for range in comment_ranges {
match Directive::try_extract(locator.slice(*range), range.start()) { match Directive::try_extract(locator.slice(range), range.start()) {
Err(err) => { Err(err) => {
#[allow(deprecated)] #[allow(deprecated)]
let line = locator.compute_line_index(range.start()); let line = locator.compute_line_index(range.start());

View file

@ -55,7 +55,7 @@ pub(crate) fn commented_out_code(
// Iterate over all comments in the document. // Iterate over all comments in the document.
for range in comment_ranges { for range in comment_ranges {
let line = locator.lines(*range); let line = locator.lines(range);
// Detect `/// script` tags. // Detect `/// script` tags.
if in_script_tag { if in_script_tag {
@ -75,9 +75,9 @@ pub(crate) fn commented_out_code(
// Verify that the comment is on its own line, and that it contains code. // Verify that the comment is on its own line, and that it contains code.
if is_own_line_comment(line) && comment_contains_code(line, &settings.task_tags[..]) { if is_own_line_comment(line) && comment_contains_code(line, &settings.task_tags[..]) {
let mut diagnostic = Diagnostic::new(CommentedOutCode, *range); let mut diagnostic = Diagnostic::new(CommentedOutCode, range);
diagnostic.set_fix(Fix::display_only_edit(Edit::range_deletion( diagnostic.set_fix(Fix::display_only_edit(Edit::range_deletion(
locator.full_lines_range(*range), locator.full_lines_range(range),
))); )));
diagnostics.push(diagnostic); diagnostics.push(diagnostic);
} }

View file

@ -25,23 +25,23 @@ pub(crate) fn from_tokens(
) { ) {
let mut has_any_shebang = false; let mut has_any_shebang = false;
for range in comment_ranges { for range in comment_ranges {
let comment = locator.slice(*range); let comment = locator.slice(range);
if let Some(shebang) = ShebangDirective::try_extract(comment) { if let Some(shebang) = ShebangDirective::try_extract(comment) {
has_any_shebang = true; has_any_shebang = true;
if let Some(diagnostic) = shebang_missing_python(*range, &shebang) { if let Some(diagnostic) = shebang_missing_python(range, &shebang) {
diagnostics.push(diagnostic); diagnostics.push(diagnostic);
} }
if let Some(diagnostic) = shebang_not_executable(path, *range) { if let Some(diagnostic) = shebang_not_executable(path, range) {
diagnostics.push(diagnostic); diagnostics.push(diagnostic);
} }
if let Some(diagnostic) = shebang_leading_whitespace(*range, locator) { if let Some(diagnostic) = shebang_leading_whitespace(range, locator) {
diagnostics.push(diagnostic); diagnostics.push(diagnostic);
} }
if let Some(diagnostic) = shebang_not_first_line(*range, locator) { if let Some(diagnostic) = shebang_not_first_line(range, locator) {
diagnostics.push(diagnostic); diagnostics.push(diagnostic);
} }
} }

View file

@ -41,10 +41,10 @@ pub(crate) fn type_comment_in_stub(
comment_ranges: &CommentRanges, comment_ranges: &CommentRanges,
) { ) {
for range in comment_ranges { for range in comment_ranges {
let comment = locator.slice(*range); let comment = locator.slice(range);
if TYPE_COMMENT_REGEX.is_match(comment) && !TYPE_IGNORE_REGEX.is_match(comment) { if TYPE_COMMENT_REGEX.is_match(comment) && !TYPE_IGNORE_REGEX.is_match(comment) {
diagnostics.push(Diagnostic::new(TypeCommentInStub, *range)); diagnostics.push(Diagnostic::new(TypeCommentInStub, range));
} }
} }
} }

View file

@ -55,7 +55,7 @@ pub(crate) fn blanket_type_ignore(
locator: &Locator, locator: &Locator,
) { ) {
for range in comment_ranges { for range in comment_ranges {
let line = locator.slice(*range); let line = locator.slice(range);
// Match, e.g., `# type: ignore` or `# type: ignore[attr-defined]`. // Match, e.g., `# type: ignore` or `# type: ignore[attr-defined]`.
// See: https://github.com/python/mypy/blob/b43e0d34247a6d1b3b9d9094d184bbfcb9808bb9/mypy/fastparse.py#L248 // See: https://github.com/python/mypy/blob/b43e0d34247a6d1b3b9d9094d184bbfcb9808bb9/mypy/fastparse.py#L248

View file

@ -56,7 +56,7 @@ pub(crate) fn empty_comments(
} }
// If the line contains an empty comment, add a diagnostic. // If the line contains an empty comment, add a diagnostic.
if let Some(diagnostic) = empty_comment(*range, locator) { if let Some(diagnostic) = empty_comment(range, locator) {
diagnostics.push(diagnostic); diagnostics.push(diagnostic);
} }
} }

View file

@ -75,7 +75,7 @@ pub(crate) fn ignored_formatter_suppression_comment(checker: &mut Checker, suite
.into_iter() .into_iter()
.filter_map(|range| { .filter_map(|range| {
Some(SuppressionComment { Some(SuppressionComment {
range: *range, range,
kind: SuppressionKind::from_comment(locator.slice(range))?, kind: SuppressionKind::from_comment(locator.slice(range))?,
}) })
}) })

View file

@ -215,10 +215,10 @@ impl Debug for CommentRanges {
} }
impl<'a> IntoIterator for &'a CommentRanges { impl<'a> IntoIterator for &'a CommentRanges {
type Item = &'a TextRange; type Item = TextRange;
type IntoIter = std::slice::Iter<'a, TextRange>; type IntoIter = std::iter::Copied<std::slice::Iter<'a, TextRange>>;
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {
self.raw.iter() self.raw.iter().copied()
} }
} }