diff --git a/crates/ruff/src/autofix/actions.rs b/crates/ruff/src/autofix/actions.rs index 854d0a9d74e711..1e815af59021ae 100644 --- a/crates/ruff/src/autofix/actions.rs +++ b/crates/ruff/src/autofix/actions.rs @@ -383,8 +383,8 @@ pub fn remove_argument( } } else if args .iter() - .map(|node| node.start()) - .chain(keywords.iter().map(|node| node.start())) + .map(Expr::start) + .chain(keywords.iter().map(Keyword::start)) .any(|location| location > expr_at) { // Case 2: argument or keyword is _not_ the last node. diff --git a/crates/ruff/src/autofix/mod.rs b/crates/ruff/src/autofix/mod.rs index d586e56b491fdf..50283071e7af96 100644 --- a/crates/ruff/src/autofix/mod.rs +++ b/crates/ruff/src/autofix/mod.rs @@ -140,8 +140,8 @@ class A(object): ); let diagnostics = create_diagnostics([Edit::replacement( "Bar".to_string(), - TextSize::from_u32(8), - TextSize::from_u32(14), + TextSize::new(8), + TextSize::new(14), )]); let (contents, fixed) = apply_fixes(diagnostics.iter(), &locator); assert_eq!( @@ -164,10 +164,7 @@ class A(object): "# .trim(), ); - let diagnostics = create_diagnostics([Edit::deletion( - TextSize::from_u32(7), - TextSize::from_u32(15), - )]); + let diagnostics = create_diagnostics([Edit::deletion(TextSize::new(7), TextSize::new(15))]); let (contents, fixed) = apply_fixes(diagnostics.iter(), &locator); assert_eq!( contents, diff --git a/crates/ruff/src/checkers/ast/mod.rs b/crates/ruff/src/checkers/ast/mod.rs index 4910868d5acfd6..100f3c947cfb8c 100644 --- a/crates/ruff/src/checkers/ast/mod.rs +++ b/crates/ruff/src/checkers/ast/mod.rs @@ -3438,7 +3438,7 @@ where .enabled(Rule::HardcodedBindAllInterfaces) { if let Some(diagnostic) = - flake8_bandit::rules::hardcoded_bind_all_interfaces(value, &expr.range()) + flake8_bandit::rules::hardcoded_bind_all_interfaces(value, expr.range()) { self.diagnostics.push(diagnostic); } @@ -4934,7 +4934,7 @@ impl<'a> Checker<'a> { // F822 if self.settings.rules.enabled(Rule::UndefinedExport) { if !self.path.ends_with("__init__.py") { - if let Some((names, range)) = &all_names { + if let Some((names, range)) = all_names { diagnostics .extend(pyflakes::rules::undefined_export(names, range, scope)); } @@ -5379,11 +5379,11 @@ impl<'a> Checker<'a> { #[allow(deprecated)] let location = self.locator.compute_source_location(expr.start()); warn_user!( - "Docstring at {}:{}:{} contains implicit string concatenation; ignoring...", - relativize_path(self.path), - location.row, - location.column - ); + "Docstring at {}:{}:{} contains implicit string concatenation; ignoring...", + relativize_path(self.path), + location.row, + location.column + ); continue; } diff --git a/crates/ruff/src/directives.rs b/crates/ruff/src/directives.rs index f94c18ef13a9d2..373a7ee8a62f7d 100644 --- a/crates/ruff/src/directives.rs +++ b/crates/ruff/src/directives.rs @@ -125,10 +125,9 @@ pub fn extract_noqa_line_for( if last_range.end() == *continuation_line { last = Some(TextRange::new(last_range.start(), line_end)); continue; - } else { - // new continuation - continuation_mappings.push(last_range); } + // new continuation + continuation_mappings.push(last_range); } last = Some(TextRange::new(*continuation_line, line_end)); diff --git a/crates/ruff/src/docstrings/definition.rs b/crates/ruff/src/docstrings/definition.rs index 840b313e332980..0c9ee20d08898e 100644 --- a/crates/ruff/src/docstrings/definition.rs +++ b/crates/ruff/src/docstrings/definition.rs @@ -37,7 +37,7 @@ pub struct Docstring<'a> { impl<'a> Docstring<'a> { pub fn body(&self) -> DocstringBody { - DocstringBody { docstring: &self } + DocstringBody { docstring: self } } pub const fn start(&self) -> TextSize { @@ -64,16 +64,16 @@ pub struct DocstringBody<'a> { impl<'a> DocstringBody<'a> { #[inline] - pub fn start(&self) -> TextSize { + pub fn start(self) -> TextSize { self.docstring.body_range.start() + self.docstring.start() } #[inline] - pub fn end(&self) -> TextSize { + pub fn end(self) -> TextSize { self.docstring.body_range.end() + self.docstring.start() } - pub fn as_str(&self) -> &'a str { + pub fn as_str(self) -> &'a str { &self.docstring.contents[self.docstring.body_range] } } diff --git a/crates/ruff/src/docstrings/sections.rs b/crates/ruff/src/docstrings/sections.rs index c3785273e20913..36e7de84e8b61e 100644 --- a/crates/ruff/src/docstrings/sections.rs +++ b/crates/ruff/src/docstrings/sections.rs @@ -159,7 +159,7 @@ impl<'a> SectionContexts<'a> { name_range: section_range + line.start(), range: TextRange::empty(line.start()), summary_full_end: line.full_end(), - }) + }); } } @@ -172,8 +172,8 @@ impl<'a> SectionContexts<'a> { } Self { - docstring, contexts, + docstring, } } @@ -263,7 +263,7 @@ impl<'a> SectionContext<'a> { self.range().end() == self.docstring_body.end() } - /// The "kind" of the section, e.g. "SectionKind::Args" or "SectionKind::Returns". + /// The `kind` of the section, e.g. [`SectionKind::Args`] or [`SectionKind::Returns`]. pub const fn kind(&self) -> SectionKind { self.data.kind } diff --git a/crates/ruff/src/logging.rs b/crates/ruff/src/logging.rs index 9a797e3c2f720b..49ee5a4ed59fe7 100644 --- a/crates/ruff/src/logging.rs +++ b/crates/ruff/src/logging.rs @@ -42,13 +42,13 @@ macro_rules! warn_user_once { #[macro_export] macro_rules! warn_user { - ($($arg:tt)*) => { + ($($arg:tt)*) => {{ use colored::Colorize; use log::warn; let message = format!("{}", format_args!($($arg)*)); warn!("{}", message.bold()); - }; + }}; } #[macro_export] diff --git a/crates/ruff/src/message/diff.rs b/crates/ruff/src/message/diff.rs index 99e3cf57e6d0d7..96f2bf638dea6c 100644 --- a/crates/ruff/src/message/diff.rs +++ b/crates/ruff/src/message/diff.rs @@ -22,13 +22,13 @@ pub(super) struct Diff<'a> { impl<'a> Diff<'a> { pub fn from_message(message: &'a Message) -> Option { - if !message.fix.is_empty() { + if message.fix.is_empty() { + None + } else { Some(Diff { source_code: message.file.source_code(), fix: &message.fix, }) - } else { - None } } } diff --git a/crates/ruff/src/noqa.rs b/crates/ruff/src/noqa.rs index 7e667ff03a37b9..9f8b6e933e38e8 100644 --- a/crates/ruff/src/noqa.rs +++ b/crates/ruff/src/noqa.rs @@ -244,7 +244,7 @@ fn add_noqa_inner( continue; } Directive::Codes(.., codes, _) => { - if includes(diagnostic.kind.rule(), &codes) { + if includes(diagnostic.kind.rule(), codes) { continue; } } @@ -262,7 +262,7 @@ fn add_noqa_inner( continue; } Directive::Codes(.., codes, _) => { - if !includes(diagnostic.kind.rule(), &codes) { + if !includes(diagnostic.kind.rule(), codes) { matches_by_line .entry(directive_line.range.start()) .or_insert_with(|| { @@ -288,7 +288,7 @@ fn add_noqa_inner( let mut output = String::with_capacity(locator.len()); let mut prev_end = TextSize::default(); - for (offset, (rules, directive)) in matches_by_line.into_iter() { + for (offset, (rules, directive)) in matches_by_line { output.push_str(&locator.contents()[TextRange::up_to(prev_end)]); let line = locator.full_line(offset); @@ -326,7 +326,7 @@ fn add_noqa_inner( rules .iter() .map(|r| r.noqa_code().to_string()) - .chain(existing.into_iter().map(ToString::to_string)) + .chain(existing.iter().map(ToString::to_string)) .sorted_unstable(), ); @@ -381,7 +381,7 @@ impl<'a> NoqaDirectives<'a> { Directive::None => { continue; } - directive @ Directive::All(..) | directive @ Directive::Codes(..) => directive, + directive @ (Directive::All(..) | Directive::Codes(..)) => directive, }; // noqa comments are guaranteed to be single line. @@ -485,7 +485,7 @@ impl FromIterator for NoqaMapping { fn from_iter>(iter: T) -> Self { let mut mappings = NoqaMapping::default(); - for range in iter.into_iter() { + for range in iter { mappings.push_mapping(range); } diff --git a/crates/ruff/src/rules/flake8_bandit/rules/hardcoded_bind_all_interfaces.rs b/crates/ruff/src/rules/flake8_bandit/rules/hardcoded_bind_all_interfaces.rs index d3afe3c2f2edfc..a74288a8416c8b 100644 --- a/crates/ruff/src/rules/flake8_bandit/rules/hardcoded_bind_all_interfaces.rs +++ b/crates/ruff/src/rules/flake8_bandit/rules/hardcoded_bind_all_interfaces.rs @@ -13,9 +13,9 @@ impl Violation for HardcodedBindAllInterfaces { } /// S104 -pub fn hardcoded_bind_all_interfaces(value: &str, range: &TextRange) -> Option { +pub fn hardcoded_bind_all_interfaces(value: &str, range: TextRange) -> Option { if value == "0.0.0.0" { - Some(Diagnostic::new(HardcodedBindAllInterfaces, *range)) + Some(Diagnostic::new(HardcodedBindAllInterfaces, range)) } else { None } diff --git a/crates/ruff/src/rules/flake8_executable/rules/shebang_newline.rs b/crates/ruff/src/rules/flake8_executable/rules/shebang_newline.rs index 55338d7afd1681..cd3cc3aae2615d 100644 --- a/crates/ruff/src/rules/flake8_executable/rules/shebang_newline.rs +++ b/crates/ruff/src/rules/flake8_executable/rules/shebang_newline.rs @@ -22,14 +22,14 @@ pub fn shebang_newline( first_line: bool, ) -> Option { if let ShebangDirective::Match(_, start, end, _) = shebang { - if !first_line { + if first_line { + None + } else { let diagnostic = Diagnostic::new( ShebangNotFirstLine, TextRange::new(range.start() + start, range.start() + end), ); Some(diagnostic) - } else { - None } } else { None diff --git a/crates/ruff/src/rules/flake8_implicit_str_concat/rules.rs b/crates/ruff/src/rules/flake8_implicit_str_concat/rules.rs index 79319188030e72..90ab53a28146be 100644 --- a/crates/ruff/src/rules/flake8_implicit_str_concat/rules.rs +++ b/crates/ruff/src/rules/flake8_implicit_str_concat/rules.rs @@ -131,14 +131,14 @@ pub fn implicit(tokens: &[LexResult], settings: &Settings, locator: &Locator) -> .tuple_windows() { if matches!(a_tok, Tok::String { .. }) && matches!(b_tok, Tok::String { .. }) { - if !locator.contains_line_break(TextRange::new(*a_end, *b_start)) { + if locator.contains_line_break(TextRange::new(*a_end, *b_start)) { diagnostics.push(Diagnostic::new( - SingleLineImplicitStringConcatenation, + MultiLineImplicitStringConcatenation, TextRange::new(*a_start, *b_end), )); } else { diagnostics.push(Diagnostic::new( - MultiLineImplicitStringConcatenation, + SingleLineImplicitStringConcatenation, TextRange::new(*a_start, *b_end), )); } diff --git a/crates/ruff/src/rules/flake8_return/rules.rs b/crates/ruff/src/rules/flake8_return/rules.rs index 0865713926245d..f8f78769d0a3cd 100644 --- a/crates/ruff/src/rules/flake8_return/rules.rs +++ b/crates/ruff/src/rules/flake8_return/rules.rs @@ -327,9 +327,8 @@ fn has_refs_before_next_assign( if *location > return_location { after_assign = Some(location); break; - } else { - before_assign = location; } + before_assign = location; } } diff --git a/crates/ruff/src/rules/isort/mod.rs b/crates/ruff/src/rules/isort/mod.rs index 07eae3ed269861..a52f13fa8d6daf 100644 --- a/crates/ruff/src/rules/isort/mod.rs +++ b/crates/ruff/src/rules/isort/mod.rs @@ -333,6 +333,7 @@ mod tests { use crate::assert_messages; use anyhow::Result; + use crate::message::Message; use test_case::test_case; use crate::registry::Rule; @@ -602,7 +603,7 @@ mod tests { ..Settings::for_rule(Rule::UnsortedImports) }, )?; - diagnostics.sort_by_key(|diagnostic| diagnostic.start()); + diagnostics.sort_by_key(Message::start); assert_messages!(snapshot, diagnostics); Ok(()) } @@ -630,7 +631,7 @@ mod tests { ..Settings::for_rule(Rule::UnsortedImports) }, )?; - diagnostics.sort_by_key(|diagnostic| diagnostic.start()); + diagnostics.sort_by_key(Message::start); assert_messages!(snapshot, diagnostics); Ok(()) } @@ -660,7 +661,7 @@ mod tests { ..Settings::for_rule(Rule::UnsortedImports) }, )?; - diagnostics.sort_by_key(|diagnostic| diagnostic.start()); + diagnostics.sort_by_key(Message::start); assert_messages!(snapshot, diagnostics); Ok(()) } @@ -688,7 +689,7 @@ mod tests { ..Settings::for_rule(Rule::UnsortedImports) }, )?; - diagnostics.sort_by_key(|diagnostic| diagnostic.start()); + diagnostics.sort_by_key(Message::start); assert_messages!(snapshot, diagnostics); Ok(()) } @@ -708,7 +709,7 @@ mod tests { ..Settings::for_rule(Rule::UnsortedImports) }, )?; - diagnostics.sort_by_key(|diagnostic| diagnostic.start()); + diagnostics.sort_by_key(Message::start); assert_messages!(snapshot, diagnostics); Ok(()) } @@ -843,7 +844,7 @@ mod tests { ..Settings::for_rule(Rule::UnsortedImports) }, )?; - diagnostics.sort_by_key(|diagnostic| diagnostic.start()); + diagnostics.sort_by_key(Message::start); assert_messages!(snapshot, diagnostics); Ok(()) } @@ -868,7 +869,7 @@ mod tests { ..Settings::for_rule(Rule::UnsortedImports) }, )?; - diagnostics.sort_by_key(|diagnostic| diagnostic.start()); + diagnostics.sort_by_key(Message::start); assert_messages!(snapshot, diagnostics); Ok(()) } @@ -889,7 +890,7 @@ mod tests { ..Settings::for_rule(Rule::UnsortedImports) }, )?; - diagnostics.sort_by_key(|diagnostic| diagnostic.start()); + diagnostics.sort_by_key(Message::start); assert_messages!(snapshot, diagnostics); Ok(()) } @@ -908,7 +909,7 @@ mod tests { ..Settings::for_rule(Rule::UnsortedImports) }, )?; - diagnostics.sort_by_key(|diagnostic| diagnostic.start()); + diagnostics.sort_by_key(Message::start); assert_messages!(snapshot, diagnostics); Ok(()) } diff --git a/crates/ruff/src/rules/pycodestyle/rules/invalid_escape_sequence.rs b/crates/ruff/src/rules/pycodestyle/rules/invalid_escape_sequence.rs index 605da2eabc8c17..9df754fae1e6c1 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/invalid_escape_sequence.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/invalid_escape_sequence.rs @@ -95,7 +95,7 @@ pub fn invalid_escape_sequence( }; // If the next character is a valid escape sequence, skip. - if VALID_ESCAPE_SEQUENCES.contains(&next_char) { + if VALID_ESCAPE_SEQUENCES.contains(next_char) { continue; } diff --git a/crates/ruff/src/rules/pycodestyle/rules/logical_lines/extraneous_whitespace.rs b/crates/ruff/src/rules/pycodestyle/rules/logical_lines/extraneous_whitespace.rs index 5c073a84e5dbc4..5d668d672d8a5c 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/logical_lines/extraneous_whitespace.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/logical_lines/extraneous_whitespace.rs @@ -132,8 +132,7 @@ pub(crate) fn extraneous_whitespace(line: &LogicalLine) -> Vec<(TextSize, Diagno { if !matches!(last_token, Some(TokenKind::Comma)) { let start = token.start(); - diagnostics - .push((start - TextSize::try_from(offset).unwrap(), diagnostic_kind)); + diagnostics.push((start - offset, diagnostic_kind)); } } } diff --git a/crates/ruff/src/rules/pycodestyle/rules/trailing_whitespace.rs b/crates/ruff/src/rules/pycodestyle/rules/trailing_whitespace.rs index 9e816821d0bef3..da6074c65d8398 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/trailing_whitespace.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/trailing_whitespace.rs @@ -84,7 +84,7 @@ pub(crate) fn trailing_whitespace( .chars() .rev() .take_while(|c| c.is_whitespace()) - .map(|c| c.text_len()) + .map(TextLen::text_len) .sum(); if whitespace_len > TextSize::from(0) { let range = TextRange::new(line.end() - whitespace_len, line.end()); diff --git a/crates/ruff/src/rules/pydocstyle/rules/indent.rs b/crates/ruff/src/rules/pydocstyle/rules/indent.rs index 14915d4da7ba36..70fdc1aff21a58 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/indent.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/indent.rs @@ -75,7 +75,7 @@ pub fn indent(checker: &mut Checker, docstring: &Docstring) { continue; } - let line_indent = whitespace::leading_space(&line); + let line_indent = whitespace::leading_space(line); // We only report tab indentation once, so only check if we haven't seen a tab // yet. @@ -141,7 +141,7 @@ pub fn indent(checker: &mut Checker, docstring: &Docstring) { // If the last line is over-indented... if let Some(last) = lines.last() { - let line_indent = whitespace::leading_space(&last); + let line_indent = whitespace::leading_space(last); if line_indent.len() > docstring.indentation.len() { let mut diagnostic = Diagnostic::new(OverIndentation, TextRange::empty(last.start())); diff --git a/crates/ruff/src/rules/pydocstyle/rules/newline_after_last_paragraph.rs b/crates/ruff/src/rules/pydocstyle/rules/newline_after_last_paragraph.rs index d6979cdaa65bec..edc9a905454846 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/newline_after_last_paragraph.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/newline_after_last_paragraph.rs @@ -49,7 +49,7 @@ pub fn newline_after_last_paragraph(checker: &mut Checker, docstring: &Docstring .rev() .skip(usize::from(num_trailing_quotes)) .take_while(|c| c.is_whitespace()) - .map(|c| c.text_len()) + .map(TextLen::text_len) .sum(); let content = format!( "{}{}", diff --git a/crates/ruff/src/rules/pydocstyle/rules/sections.rs b/crates/ruff/src/rules/pydocstyle/rules/sections.rs index 9db88a40d9d183..1a6434398f5165 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/sections.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/sections.rs @@ -572,7 +572,6 @@ fn blanks_and_section_underline( docstring.range(), )); } - return; } } @@ -670,7 +669,7 @@ fn common_section(checker: &mut Checker, docstring: &Docstring, context: &Sectio .rules .enabled(Rule::NoBlankLineBeforeSection) { - if !context.previous_line().map_or(false, |l| l.is_empty()) { + if !context.previous_line().map_or(false, str::is_empty) { let mut diagnostic = Diagnostic::new( NoBlankLineBeforeSection { name: context.section_name().to_string(), diff --git a/crates/ruff/src/rules/pyflakes/mod.rs b/crates/ruff/src/rules/pyflakes/mod.rs index b2f70273f959e4..6df5ea86f3902d 100644 --- a/crates/ruff/src/rules/pyflakes/mod.rs +++ b/crates/ruff/src/rules/pyflakes/mod.rs @@ -11,6 +11,7 @@ mod tests { use anyhow::Result; use regex::Regex; + use ruff_diagnostics::Diagnostic; use rustpython_parser::lexer::LexResult; use test_case::test_case; use textwrap::dedent; @@ -276,7 +277,7 @@ mod tests { flags::Noqa::Enabled, flags::Autofix::Enabled, ); - diagnostics.sort_by_key(|diagnostic| diagnostic.start()); + diagnostics.sort_by_key(Diagnostic::start); let actual = diagnostics .iter() .map(|diagnostic| diagnostic.kind.rule()) diff --git a/crates/ruff/src/rules/pyflakes/rules/f_string_missing_placeholders.rs b/crates/ruff/src/rules/pyflakes/rules/f_string_missing_placeholders.rs index e4443a7641afc9..5e0697b8771ee2 100644 --- a/crates/ruff/src/rules/pyflakes/rules/f_string_missing_placeholders.rs +++ b/crates/ruff/src/rules/pyflakes/rules/f_string_missing_placeholders.rs @@ -85,8 +85,8 @@ fn unescape_f_string(content: &str) -> String { } fn fix_f_string_missing_placeholders( - prefix_range: &TextRange, - tok_range: &TextRange, + prefix_range: TextRange, + tok_range: TextRange, checker: &mut Checker, ) -> Edit { let content = &checker.locator.contents()[TextRange::new(prefix_range.end(), tok_range.end())]; @@ -107,8 +107,8 @@ pub fn f_string_missing_placeholders(expr: &Expr, values: &[Expr], checker: &mut let mut diagnostic = Diagnostic::new(FStringMissingPlaceholders, tok_range); if checker.patch(diagnostic.kind.rule()) { diagnostic.set_fix(fix_f_string_missing_placeholders( - &prefix_range, - &tok_range, + prefix_range, + tok_range, checker, )); } diff --git a/crates/ruff/src/rules/pyflakes/rules/undefined_export.rs b/crates/ruff/src/rules/pyflakes/rules/undefined_export.rs index b55db5c739c187..d5a4417d176ec1 100644 --- a/crates/ruff/src/rules/pyflakes/rules/undefined_export.rs +++ b/crates/ruff/src/rules/pyflakes/rules/undefined_export.rs @@ -17,7 +17,7 @@ impl Violation for UndefinedExport { } /// F822 -pub fn undefined_export(names: &[&str], range: &TextRange, scope: &Scope) -> Vec { +pub fn undefined_export(names: &[&str], range: TextRange, scope: &Scope) -> Vec { let mut diagnostics = Vec::new(); if !scope.uses_star_imports() { for name in names { @@ -26,7 +26,7 @@ pub fn undefined_export(names: &[&str], range: &TextRange, scope: &Scope) -> Vec UndefinedExport { name: (*name).to_string(), }, - *range, + range, )); } } diff --git a/crates/ruff/src/rules/pyflakes/rules/unused_variable.rs b/crates/ruff/src/rules/pyflakes/rules/unused_variable.rs index 4111cdd66a56a1..0ee624f534fa5c 100644 --- a/crates/ruff/src/rules/pyflakes/rules/unused_variable.rs +++ b/crates/ruff/src/rules/pyflakes/rules/unused_variable.rs @@ -193,12 +193,12 @@ enum DeletionKind { /// enclosing [`Stmt`] and the [`Range`] of the variable binding. fn remove_unused_variable( stmt: &Stmt, - range: &TextRange, + range: TextRange, checker: &Checker, ) -> Option<(DeletionKind, Edit)> { // First case: simple assignment (`x = 1`) if let StmtKind::Assign { targets, value, .. } = &stmt.node { - if let Some(target) = targets.iter().find(|target| *range == target.range()) { + if let Some(target) = targets.iter().find(|target| range == target.range()) { if matches!(target.node, ExprKind::Name { .. }) { return if targets.len() > 1 || contains_effect(value, |id| checker.ctx.is_builtin(id)) @@ -290,7 +290,7 @@ fn remove_unused_variable( // TODO(charlie): Store the `Withitem` in the `Binding`. for item in items { if let Some(optional_vars) = &item.optional_vars { - if optional_vars.range() == *range { + if optional_vars.range() == range { return Some(( DeletionKind::Partial, Edit::deletion( @@ -337,7 +337,7 @@ pub fn unused_variable(checker: &mut Checker, scope: ScopeId) { ); if checker.patch(diagnostic.kind.rule()) { if let Some(stmt) = binding.source.as_ref().map(Into::into) { - if let Some((kind, fix)) = remove_unused_variable(stmt, &binding.range, checker) + if let Some((kind, fix)) = remove_unused_variable(stmt, binding.range, checker) { if matches!(kind, DeletionKind::Whole) { checker.deletions.insert(RefEquality(stmt)); diff --git a/crates/ruff_python_ast/src/newlines.rs b/crates/ruff_python_ast/src/newlines.rs index f66db3323cc8aa..54edacaaa7905d 100644 --- a/crates/ruff_python_ast/src/newlines.rs +++ b/crates/ruff_python_ast/src/newlines.rs @@ -116,25 +116,22 @@ impl DoubleEndedIterator for UniversalNewlineIterator<'_> { // Find the end of the previous line. The previous line is the text up to, but not including // the newline character. - let line = match haystack.rfind(['\n', '\r']) { + let line = if let Some(line_end) = haystack.rfind(['\n', '\r']) { // '\n' or '\r' or '\r\n' - Some(line_end) => { - let (remainder, line) = self.text.split_at(line_end + 1); - self.text = remainder; - self.offset_back -= line.text_len(); + let (remainder, line) = self.text.split_at(line_end + 1); + self.text = remainder; + self.offset_back -= line.text_len(); - Line { - text: line, - offset: self.offset_back, - } + Line { + text: line, + offset: self.offset_back, } + } else { // Last line - None => { - let offset = self.offset_back - self.text.text_len(); - Line { - text: std::mem::take(&mut self.text), - offset, - } + let offset = self.offset_back - self.text.text_len(); + Line { + text: std::mem::take(&mut self.text), + offset, } }; @@ -242,7 +239,7 @@ impl<'a> Line<'a> { /// Returns the line's text, including the terminating new line character. #[inline] pub fn as_full_str(&self) -> &'a str { - &self.text + self.text } #[inline] @@ -377,6 +374,6 @@ mod tests { lines.next_back(), Some(Line::new("\r\n", TextSize::from(8))) ); - assert_eq!(lines.next(), None) + assert_eq!(lines.next(), None); } } diff --git a/crates/ruff_python_ast/src/source_code/indexer.rs b/crates/ruff_python_ast/src/source_code/indexer.rs index cc271e6645a789..db6cfb7c0eb251 100644 --- a/crates/ruff_python_ast/src/source_code/indexer.rs +++ b/crates/ruff_python_ast/src/source_code/indexer.rs @@ -134,7 +134,7 @@ if True: "# .trim(); let lxr: Vec = lexer::lex(contents, Mode::Module).collect(); - let indexer = Indexer::from_tokens(&lxr.as_slice(), &Locator::new(contents)); + let indexer = Indexer::from_tokens(lxr.as_slice(), &Locator::new(contents)); assert_eq!( indexer.continuation_line_starts(), [ @@ -166,7 +166,7 @@ import os "# .trim(); let lxr: Vec = lexer::lex(contents, Mode::Module).collect(); - let indexer = Indexer::from_tokens(&lxr.as_slice(), &Locator::new(contents)); + let indexer = Indexer::from_tokens(lxr.as_slice(), &Locator::new(contents)); assert_eq!( indexer.continuation_line_starts(), [ @@ -182,7 +182,7 @@ import os fn string_ranges() { let contents = r#""this is a single-quoted string""#; let lxr: Vec = lexer::lex(contents, Mode::Module).collect(); - let indexer = Indexer::from_tokens(&lxr.as_slice(), &Locator::new(contents)); + let indexer = Indexer::from_tokens(lxr.as_slice(), &Locator::new(contents)); assert_eq!(indexer.string_ranges(), []); let contents = r#" @@ -191,7 +191,7 @@ import os """ "#; let lxr: Vec = lexer::lex(contents, Mode::Module).collect(); - let indexer = Indexer::from_tokens(&lxr.as_slice(), &Locator::new(contents)); + let indexer = Indexer::from_tokens(lxr.as_slice(), &Locator::new(contents)); assert_eq!( indexer.string_ranges(), [TextRange::new(TextSize::from(13), TextSize::from(71))] @@ -203,7 +203,7 @@ import os """ "#; let lxr: Vec = lexer::lex(contents, Mode::Module).collect(); - let indexer = Indexer::from_tokens(&lxr.as_slice(), &Locator::new(contents)); + let indexer = Indexer::from_tokens(lxr.as_slice(), &Locator::new(contents)); assert_eq!( indexer.string_ranges(), [TextRange::new(TextSize::from(13), TextSize::from(107))] @@ -220,7 +220,7 @@ import os """ "#; let lxr: Vec = lexer::lex(contents, Mode::Module).collect(); - let indexer = Indexer::from_tokens(&lxr.as_slice(), &Locator::new(contents)); + let indexer = Indexer::from_tokens(lxr.as_slice(), &Locator::new(contents)); assert_eq!( indexer.string_ranges(), &[ diff --git a/crates/ruff_python_ast/src/source_code/line_index.rs b/crates/ruff_python_ast/src/source_code/line_index.rs index 26864113ee5fd8..9198fd673090ce 100644 --- a/crates/ruff_python_ast/src/source_code/line_index.rs +++ b/crates/ruff_python_ast/src/source_code/line_index.rs @@ -105,7 +105,7 @@ impl LineIndex { } else { // Don't count the BOM character as a column. if line_start == TextSize::from(0) && content.starts_with('\u{feff}') { - line_start = line_start + '\u{feff}'.text_len() + line_start += '\u{feff}'.text_len(); } content[TextRange::new(line_start, offset)].chars().count() diff --git a/crates/ruff_python_ast/src/source_code/mod.rs b/crates/ruff_python_ast/src/source_code/mod.rs index 9e4a000af580df..ab04d5911e8a5f 100644 --- a/crates/ruff_python_ast/src/source_code/mod.rs +++ b/crates/ruff_python_ast/src/source_code/mod.rs @@ -148,7 +148,7 @@ impl SourceFileBuilder { } pub fn set_source_text(&mut self, text: &str) { - self.set_source_code(&SourceCode::new(text, &LineIndex::from_source_text(text))) + self.set_source_code(&SourceCode::new(text, &LineIndex::from_source_text(text))); } /// Consumes `self` and returns a builder for a file with the source text `text`. Builds the [`LineIndex`] from `text`. diff --git a/crates/ruff_python_formatter/src/cst/mod.rs b/crates/ruff_python_formatter/src/cst/mod.rs index 4b29e562f7e91b..cd212285b3c5b0 100644 --- a/crates/ruff_python_formatter/src/cst/mod.rs +++ b/crates/ruff_python_formatter/src/cst/mod.rs @@ -6,7 +6,7 @@ use std::ops::Deref; use itertools::Itertools; use ruff_text_size::{TextRange, TextSize}; use rustpython_parser::ast::Constant; -use rustpython_parser::Mode; +use rustpython_parser::{ast, Mode}; use ruff_python_ast::source_code::Locator; @@ -53,7 +53,7 @@ impl Located { } pub fn id(&self) -> usize { - &self.node as *const _ as usize + std::ptr::addr_of!(self.node) as usize } } @@ -946,7 +946,7 @@ impl From<(rustpython_parser::ast::Stmt, &Locator<'_>)> for Stmt { range: TextRange::new( decorator_list .first() - .map_or(stmt.range.start(), |d| d.start()), + .map_or(stmt.range.start(), ast::Located::start), body.end(), ), node: StmtKind::FunctionDef { @@ -1383,7 +1383,7 @@ impl From<(rustpython_parser::ast::Stmt, &Locator<'_>)> for Stmt { // Find the start and end of the `orelse`. let orelse = (!orelse.is_empty()).then(|| { let orelse_range = expand_indented_block( - handlers.last().map_or(body.end(), |handler| handler.end()), + handlers.last().map_or(body.end(), Located::end), orelse.last().unwrap().end(), locator, ); @@ -1402,8 +1402,8 @@ impl From<(rustpython_parser::ast::Stmt, &Locator<'_>)> for Stmt { let finalbody = (!finalbody.is_empty()).then(|| { let finalbody_range = expand_indented_block( orelse.as_ref().map_or( - handlers.last().map_or(body.end(), |handler| handler.end()), - |orelse| orelse.end(), + handlers.last().map_or(body.end(), Located::end), + Located::end, ), finalbody.last().unwrap().end(), locator, @@ -1421,10 +1421,10 @@ impl From<(rustpython_parser::ast::Stmt, &Locator<'_>)> for Stmt { let end_location = finalbody.as_ref().map_or( orelse.as_ref().map_or( - handlers.last().map_or(body.end(), |handler| handler.end()), - |orelse| orelse.end(), + handlers.last().map_or(body.end(), Located::end), + Located::end, ), - |finalbody| finalbody.end(), + Located::end, ); Stmt { @@ -1471,7 +1471,7 @@ impl From<(rustpython_parser::ast::Stmt, &Locator<'_>)> for Stmt { // Find the start and end of the `orelse`. let orelse = (!orelse.is_empty()).then(|| { let orelse_range = expand_indented_block( - handlers.last().map_or(body.end(), |handler| handler.end()), + handlers.last().map_or(body.end(), Located::end), orelse.last().unwrap().end(), locator, ); @@ -1490,8 +1490,8 @@ impl From<(rustpython_parser::ast::Stmt, &Locator<'_>)> for Stmt { let finalbody = (!finalbody.is_empty()).then(|| { let finalbody_range = expand_indented_block( orelse.as_ref().map_or( - handlers.last().map_or(body.end(), |handler| handler.end()), - |orelse| orelse.end(), + handlers.last().map_or(body.end(), Located::end), + Located::end, ), finalbody.last().unwrap().end(), locator, @@ -1509,10 +1509,10 @@ impl From<(rustpython_parser::ast::Stmt, &Locator<'_>)> for Stmt { let end_location = finalbody.as_ref().map_or( orelse.as_ref().map_or( - handlers.last().map_or(body.end(), |handler| handler.end()), - |orelse| orelse.end(), + handlers.last().map_or(body.end(), Located::end), + Located::end, ), - |finalbody| finalbody.end(), + Located::end, ); Stmt { diff --git a/crates/ruff_python_formatter/src/trivia.rs b/crates/ruff_python_formatter/src/trivia.rs index 4ae40347b51453..99eb081a5a7542 100644 --- a/crates/ruff_python_formatter/src/trivia.rs +++ b/crates/ruff_python_formatter/src/trivia.rs @@ -760,7 +760,7 @@ fn sorted_child_nodes_inner<'a>(node: Node<'a>, result: &mut Vec>) { } } -pub fn sorted_child_nodes<'a>(node: Node<'a>) -> Vec> { +pub fn sorted_child_nodes(node: Node) -> Vec { let mut result = Vec::new(); sorted_child_nodes_inner(node, &mut result); @@ -802,11 +802,11 @@ pub fn decorate_token<'a>( let existing_start = existing.start(); let existing_end = existing.end(); if start == existing_start && end == existing_end { - enclosed_node = Some(child.clone()); + enclosed_node = Some(child); } } else { if token.start() <= start && token.end() >= end { - enclosed_node = Some(child.clone()); + enclosed_node = Some(child); } } @@ -820,7 +820,7 @@ pub fn decorate_token<'a>( // Because we will never consider this node or any nodes // before it again, this node must be the closest preceding // node we have encountered so far. - preceding_node = Some(child.clone()); + preceding_node = Some(child); left = middle + 1; continue; } @@ -830,7 +830,7 @@ pub fn decorate_token<'a>( // Because we will never consider this node or any nodes after // it again, this node must be the closest following node we // have encountered so far. - following_node = Some(child.clone()); + following_node = Some(child); right = middle; continue; } diff --git a/crates/ruff_wasm/src/lib.rs b/crates/ruff_wasm/src/lib.rs index 9de731564fc4f1..9677084cfaa799 100644 --- a/crates/ruff_wasm/src/lib.rs +++ b/crates/ruff_wasm/src/lib.rs @@ -214,7 +214,7 @@ pub fn check(contents: &str, options: JsValue) -> Result { code: message.kind.rule().noqa_code().to_string(), message: message.kind.body, location: start_location, - end_location: end_location, + end_location, fix: if message.fix.is_empty() { None } else {