Skip to content

Commit

Permalink
Run nightly Clippy over the Ruff repo (#5670)
Browse files Browse the repository at this point in the history
## Summary

This is the result of running `cargo +nightly clippy --workspace
--all-targets --all-features -- -D warnings` and fixing all violations.
Just wanted to see if there were any interesting new checks on nightly
:eyes:
  • Loading branch information
charliermarsh authored Jul 11, 2023
1 parent e7e2f44 commit 4dee49d
Show file tree
Hide file tree
Showing 40 changed files with 220 additions and 470 deletions.
8 changes: 4 additions & 4 deletions crates/ruff/src/autofix/edits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,10 @@ mod tests {
Some(TextSize::from(6))
);

let contents = r#"
let contents = r"
x = 1 \
; y = 1
"#
"
.trim();
let program = Suite::parse(contents, "<filename>")?;
let stmt = program.first().unwrap();
Expand Down Expand Up @@ -349,10 +349,10 @@ x = 1 \
TextSize::from(6)
);

let contents = r#"
let contents = r"
x = 1 \
; y = 1
"#
"
.trim();
let locator = Locator::new(contents);
assert_eq!(
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ where
pycodestyle::rules::module_import_not_at_top_of_file(self, stmt, self.locator);
}
if self.enabled(Rule::GlobalStatement) {
for name in names.iter() {
for name in names {
if let Some(asname) = name.asname.as_ref() {
pylint::rules::global_statement(self, asname);
} else {
Expand Down Expand Up @@ -972,7 +972,7 @@ where
pycodestyle::rules::module_import_not_at_top_of_file(self, stmt, self.locator);
}
if self.enabled(Rule::GlobalStatement) {
for name in names.iter() {
for name in names {
if let Some(asname) = name.asname.as_ref() {
pylint::rules::global_statement(self, asname);
} else {
Expand Down Expand Up @@ -1617,7 +1617,7 @@ where
flake8_bandit::rules::assign_hardcoded_password_string(self, value, targets);
}
if self.enabled(Rule::GlobalStatement) {
for target in targets.iter() {
for target in targets {
if let Expr::Name(ast::ExprName { id, .. }) = target {
pylint::rules::global_statement(self, id);
}
Expand Down Expand Up @@ -1749,7 +1749,7 @@ where
}
Stmt::Delete(ast::StmtDelete { targets, range: _ }) => {
if self.enabled(Rule::GlobalStatement) {
for target in targets.iter() {
for target in targets {
if let Expr::Name(ast::ExprName { id, .. }) = target {
pylint::rules::global_statement(self, id);
}
Expand Down
12 changes: 6 additions & 6 deletions crates/ruff/src/directives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,30 +427,30 @@ ghi
NoqaMapping::from_iter([TextRange::new(TextSize::from(6), TextSize::from(28))])
);

let contents = r#"x = \
1"#;
let contents = r"x = \
1";
assert_eq!(
noqa_mappings(contents),
NoqaMapping::from_iter([TextRange::new(TextSize::from(0), TextSize::from(6))])
);

let contents = r#"from foo import \
let contents = r"from foo import \
bar as baz, \
qux as quux"#;
qux as quux";
assert_eq!(
noqa_mappings(contents),
NoqaMapping::from_iter([TextRange::new(TextSize::from(0), TextSize::from(36))])
);

let contents = r#"
let contents = r"
# Foo
from foo import \
bar as baz, \
qux as quux # Baz
x = \
1
y = \
2"#;
2";
assert_eq!(
noqa_mappings(contents),
NoqaMapping::from_iter([
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/message/azure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ mod tests {

#[test]
fn output() {
let mut emitter = AzureEmitter::default();
let mut emitter = AzureEmitter;
let content = capture_emitter_output(&mut emitter, &create_messages());

assert_snapshot!(content);
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/message/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ mod tests {

#[test]
fn output() {
let mut emitter = GithubEmitter::default();
let mut emitter = GithubEmitter;
let content = capture_emitter_output(&mut emitter, &create_messages());

assert_snapshot!(content);
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/message/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ mod tests {

#[test]
fn output() {
let mut emitter = JsonEmitter::default();
let mut emitter = JsonEmitter;
let content = capture_emitter_output(&mut emitter, &create_messages());

assert_snapshot!(content);
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/message/json_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ impl Emitter for JsonLinesEmitter {

#[cfg(test)]
mod tests {
use crate::message::json_lines::JsonLinesEmitter;
use insta::assert_snapshot;

use crate::message::json_lines::JsonLinesEmitter;
use crate::message::tests::{capture_emitter_output, create_messages};

#[test]
fn output() {
let mut emitter = JsonLinesEmitter::default();
let mut emitter = JsonLinesEmitter;
let content = capture_emitter_output(&mut emitter, &create_messages());

assert_snapshot!(content);
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/message/junit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ mod tests {

#[test]
fn output() {
let mut emitter = JunitEmitter::default();
let mut emitter = JunitEmitter;
let content = capture_emitter_output(&mut emitter, &create_messages());

assert_snapshot!(content);
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/message/pylint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mod tests {

#[test]
fn output() {
let mut emitter = PylintEmitter::default();
let mut emitter = PylintEmitter;
let content = capture_emitter_output(&mut emitter, &create_messages());

assert_snapshot!(content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ fn is_wildcard_command(expr: &Expr) -> bool {
if let Expr::List(ast::ExprList { elts, .. }) = expr {
let mut has_star = false;
let mut has_command = false;
for elt in elts.iter() {
for elt in elts {
if let Some(text) = string_literal(elt) {
has_star |= text.contains('*');
has_command |= text.contains("chown")
Expand Down
7 changes: 1 addition & 6 deletions crates/ruff/src/rules/flake8_bandit/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ impl From<Options> for Settings {
.hardcoded_tmp_directory
.unwrap_or_else(default_tmp_dirs)
.into_iter()
.chain(
options
.hardcoded_tmp_directory_extend
.unwrap_or_default()
.into_iter(),
)
.chain(options.hardcoded_tmp_directory_extend.unwrap_or_default())
.collect(),
check_typed_exception: options.check_typed_exception.unwrap_or(false),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ pub(crate) fn reuse_of_groupby_generator(
return;
}
let mut finder = GroupNameFinder::new(group_name);
for stmt in body.iter() {
for stmt in body {
finder.visit_stmt(stmt);
}
for expr in finder.exprs {
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/flake8_comprehensions/fixes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ fn pad_expression(content: String, range: TextRange, checker: &Checker) -> Strin
// If the expression is immediately preceded by an opening brace, then
// we need to add a space before the expression.
let prefix = checker.locator.up_to(range.start());
let left_pad = matches!(prefix.chars().rev().next(), Some('{'));
let left_pad = matches!(prefix.chars().next_back(), Some('{'));

// If the expression is immediately preceded by an opening brace, then
// we need to add a space before the expression.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@ pub(crate) fn all_with_model_form(
{
return None;
}
for element in body.iter() {
for element in body {
let Stmt::ClassDef(ast::StmtClassDef { name, body, .. }) = element else {
continue;
};
if name != "Meta" {
continue;
}
for element in body.iter() {
for element in body {
let Stmt::Assign(ast::StmtAssign { targets, value, .. }) = element else {
continue;
};
for target in targets.iter() {
for target in targets {
let Expr::Name(ast::ExprName { id, .. }) = target else {
continue;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@ pub(crate) fn exclude_with_model_form(
{
return None;
}
for element in body.iter() {
for element in body {
let Stmt::ClassDef(ast::StmtClassDef { name, body, .. }) = element else {
continue;
};
if name != "Meta" {
continue;
}
for element in body.iter() {
for element in body {
let Stmt::Assign(ast::StmtAssign { targets, .. }) = element else {
continue;
};
for target in targets.iter() {
for target in targets {
let Expr::Name(ast::ExprName { id, .. }) = target else {
continue;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn has_dunder_method(body: &[Stmt]) -> bool {
}

fn is_non_abstract_model(bases: &[Expr], body: &[Stmt], semantic: &SemanticModel) -> bool {
for base in bases.iter() {
for base in bases {
if is_model_abstract(body) {
continue;
}
Expand All @@ -94,18 +94,18 @@ fn is_non_abstract_model(bases: &[Expr], body: &[Stmt], semantic: &SemanticModel

/// Check if class is abstract, in terms of Django model inheritance.
fn is_model_abstract(body: &[Stmt]) -> bool {
for element in body.iter() {
for element in body {
let Stmt::ClassDef(ast::StmtClassDef { name, body, .. }) = element else {
continue;
};
if name != "Meta" {
continue;
}
for element in body.iter() {
for element in body {
let Stmt::Assign(ast::StmtAssign { targets, value, .. }) = element else {
continue;
};
for target in targets.iter() {
for target in targets {
let Expr::Name(ast::ExprName { id, .. }) = target else {
continue;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Violation for DjangoNullableModelStringField {

/// DJ001
pub(crate) fn nullable_model_string_field(checker: &mut Checker, body: &[Stmt]) {
for statement in body.iter() {
for statement in body {
let Stmt::Assign(ast::StmtAssign { value, .. }) = statement else {
continue;
};
Expand Down Expand Up @@ -87,7 +87,7 @@ fn is_nullable_field<'a>(checker: &'a Checker, value: &'a Expr) -> Option<&'a st
let mut null_key = false;
let mut blank_key = false;
let mut unique_key = false;
for keyword in keywords.iter() {
for keyword in keywords {
let Some(argument) = &keyword.arg else {
continue;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub(crate) fn unordered_body_content_in_model(
// Track all the element types we've seen so far.
let mut element_types = Vec::new();
let mut prev_element_type = None;
for element in body.iter() {
for element in body {
let Some(element_type) = get_element_type(element, checker.semantic()) else {
continue;
};
Expand Down
7 changes: 1 addition & 6 deletions crates/ruff/src/rules/flake8_gettext/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@ impl From<Options> for Settings {
.function_names
.unwrap_or_else(default_func_names)
.into_iter()
.chain(
options
.extend_function_names
.unwrap_or_default()
.into_iter(),
)
.chain(options.extend_function_names.unwrap_or_default())
.collect(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/flake8_quotes/rules/from_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ fn strings(locator: &Locator, sequence: &[TextRange], settings: &Settings) -> Ve
string_contents.contains(good_single(quotes_settings.inline_quotes))
});

for (range, trivia) in sequence.iter().zip(trivia.into_iter()) {
for (range, trivia) in sequence.iter().zip(trivia) {
if trivia.is_multiline {
// If our string is or contains a known good string, ignore it.
if trivia
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff/src/rules/flake8_todos/rules/todos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ impl Violation for MissingSpaceAfterTodoColon {

static ISSUE_LINK_REGEX_SET: Lazy<RegexSet> = Lazy::new(|| {
RegexSet::new([
r#"^#\s*(http|https)://.*"#, // issue link
r#"^#\s*\d+$"#, // issue code - like "003"
r#"^#\s*[A-Z]{1,6}\-?\d+$"#, // issue code - like "TD003"
r"^#\s*(http|https)://.*", // issue link
r"^#\s*\d+$", // issue code - like "003"
r"^#\s*[A-Z]{1,6}\-?\d+$", // issue code - like "TD003"
])
.unwrap()
});
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/rules/flake8_type_checking/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub(crate) fn runtime_evaluated(

fn runtime_evaluated_base_class(base_classes: &[String], semantic: &SemanticModel) -> bool {
if let ScopeKind::Class(ast::StmtClassDef { bases, .. }) = &semantic.scope().kind {
for base in bases.iter() {
for base in bases {
if let Some(call_path) = semantic.resolve_call_path(base) {
if base_classes
.iter()
Expand All @@ -54,7 +54,7 @@ fn runtime_evaluated_base_class(base_classes: &[String], semantic: &SemanticMode

fn runtime_evaluated_decorators(decorators: &[String], semantic: &SemanticModel) -> bool {
if let ScopeKind::Class(ast::StmtClassDef { decorator_list, .. }) = &semantic.scope().kind {
for decorator in decorator_list.iter() {
for decorator in decorator_list {
if let Some(call_path) = semantic.resolve_call_path(map_callable(&decorator.expression))
{
if decorators
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/isort/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub(crate) fn order_imports<'a>(
)
.chain(
// Include all star imports.
block.import_from_star.into_iter(),
block.import_from_star,
)
.map(
|(
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/pycodestyle/rules/not_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub(crate) fn not_tests(
if !matches!(&ops[..], [CmpOp::In | CmpOp::Is]) {
return;
}
for op in ops.iter() {
for op in ops {
match op {
CmpOp::In => {
if check_not_in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl AlwaysAutofixableViolation for LRUCacheWithMaxsizeNone {

/// UP033
pub(crate) fn lru_cache_with_maxsize_none(checker: &mut Checker, decorator_list: &[Decorator]) {
for decorator in decorator_list.iter() {
for decorator in decorator_list {
let Expr::Call(ast::ExprCall {
func,
args,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl AlwaysAutofixableViolation for LRUCacheWithoutParameters {

/// UP011
pub(crate) fn lru_cache_without_parameters(checker: &mut Checker, decorator_list: &[Decorator]) {
for decorator in decorator_list.iter() {
for decorator in decorator_list {
let Expr::Call(ast::ExprCall {
func,
args,
Expand Down
Loading

0 comments on commit 4dee49d

Please sign in to comment.