Skip to content

Commit

Permalink
Clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Apr 14, 2023
1 parent af24c5b commit 09cbc45
Show file tree
Hide file tree
Showing 31 changed files with 113 additions and 121 deletions.
4 changes: 2 additions & 2 deletions crates/ruff/src/autofix/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 3 additions & 6 deletions crates/ruff/src/autofix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 2 additions & 3 deletions crates/ruff/src/directives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff/src/docstrings/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff/src/docstrings/sections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
});
}
}

Expand All @@ -172,8 +172,8 @@ impl<'a> SectionContexts<'a> {
}

Self {
docstring,
contexts,
docstring,
}
}

Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff/src/message/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ pub(super) struct Diff<'a> {

impl<'a> Diff<'a> {
pub fn from_message(message: &'a Message) -> Option<Diff> {
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
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/ruff/src/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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(|| {
Expand All @@ -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);
Expand Down Expand Up @@ -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(),
);

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -485,7 +485,7 @@ impl FromIterator<TextRange> for NoqaMapping {
fn from_iter<T: IntoIterator<Item = TextRange>>(iter: T) -> Self {
let mut mappings = NoqaMapping::default();

for range in iter.into_iter() {
for range in iter {
mappings.push_mapping(range);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ impl Violation for HardcodedBindAllInterfaces {
}

/// S104
pub fn hardcoded_bind_all_interfaces(value: &str, range: &TextRange) -> Option<Diagnostic> {
pub fn hardcoded_bind_all_interfaces(value: &str, range: TextRange) -> Option<Diagnostic> {
if value == "0.0.0.0" {
Some(Diagnostic::new(HardcodedBindAllInterfaces, *range))
Some(Diagnostic::new(HardcodedBindAllInterfaces, range))
} else {
None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ pub fn shebang_newline(
first_line: bool,
) -> Option<Diagnostic> {
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
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff/src/rules/flake8_implicit_str_concat/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
));
}
Expand Down
3 changes: 1 addition & 2 deletions crates/ruff/src/rules/flake8_return/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
19 changes: 10 additions & 9 deletions crates/ruff/src/rules/isort/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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(())
}
Expand All @@ -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(())
}
Expand Down Expand Up @@ -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(())
}
Expand All @@ -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(())
}
Expand All @@ -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(())
}
Expand All @@ -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(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Expand Down
Loading

0 comments on commit 09cbc45

Please sign in to comment.