Skip to content

Commit

Permalink
Use a trait
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jun 15, 2023
1 parent d086b80 commit b19964b
Show file tree
Hide file tree
Showing 31 changed files with 208 additions and 195 deletions.
24 changes: 12 additions & 12 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use rustpython_parser::ast::{
use ruff_diagnostics::{Diagnostic, Fix, IsolationLevel};
use ruff_python_ast::all::{extract_all_names, AllNamesFlags};
use ruff_python_ast::helpers::{extract_handled_exceptions, to_module_path};
use ruff_python_ast::identifier::Identifier;
use ruff_python_ast::source_code::{Generator, Indexer, Locator, Quote, Stylist};
use ruff_python_ast::str::trailing_quote;
use ruff_python_ast::types::Node;
Expand Down Expand Up @@ -367,7 +368,7 @@ where
if self.enabled(Rule::AmbiguousFunctionName) {
if let Some(diagnostic) =
pycodestyle::rules::ambiguous_function_name(name, || {
identifier::statement(stmt, self.locator)
stmt.identifier(self.locator)
})
{
self.diagnostics.push(diagnostic);
Expand Down Expand Up @@ -692,7 +693,7 @@ where
}
if self.enabled(Rule::AmbiguousClassName) {
if let Some(diagnostic) = pycodestyle::rules::ambiguous_class_name(name, || {
identifier::statement(stmt, self.locator)
stmt.identifier(self.locator)
}) {
self.diagnostics.push(diagnostic);
}
Expand Down Expand Up @@ -809,7 +810,7 @@ where
self.add_binding(
name,
// NOTE: Incorrect, needs to be the range of the alias.
identifier::alias(&alias, self.locator),
alias.identifier(self.locator),
BindingKind::FutureImportation,
BindingFlags::empty(),
);
Expand All @@ -830,7 +831,7 @@ where
self.add_binding(
name,
// NOTE: Incorrect, needs to be the range of `name`.
identifier::alias(&alias, self.locator),
alias.identifier(self.locator),
BindingKind::SubmoduleImportation(SubmoduleImportation {
qualified_name,
}),
Expand All @@ -842,7 +843,7 @@ where
self.add_binding(
name,
// NOTE: Incorrect, needs to be the range of `name`.
identifier::alias(&alias, self.locator),
alias.identifier(self.locator),
BindingKind::Importation(Importation { qualified_name }),
if alias
.asname
Expand Down Expand Up @@ -1088,7 +1089,7 @@ where
self.add_binding(
name,
// NOTE: Incorrect, needs to be the range of `name`.
identifier::alias(&alias, self.locator),
alias.identifier(self.locator),
BindingKind::FutureImportation,
BindingFlags::empty(),
);
Expand Down Expand Up @@ -1150,7 +1151,7 @@ where
self.add_binding(
name,
// NOTE: Incorrect, needs to be the range of `name`.
identifier::alias(&alias, self.locator),
alias.identifier(self.locator),
BindingKind::FromImportation(FromImportation { qualified_name }),
if alias
.asname
Expand Down Expand Up @@ -1877,7 +1878,7 @@ where
self.add_binding(
name,
// NOTE: Correct!
identifier::statement(stmt, self.locator),
stmt.identifier(self.locator),
BindingKind::FunctionDefinition,
BindingFlags::empty(),
);
Expand Down Expand Up @@ -2101,7 +2102,7 @@ where
self.add_binding(
name,
// NOTE: Correct!
identifier::statement(stmt, self.locator),
stmt.identifier(self.locator),
BindingKind::ClassDefinition,
BindingFlags::empty(),
);
Expand Down Expand Up @@ -3909,8 +3910,7 @@ where
}
match name {
Some(name) => {
let range = identifier::exception_range(excepthandler, self.locator)
.expect("Failed to find `name` range");
let range = excepthandler.identifier(self.locator);

if self.enabled(Rule::AmbiguousVariableName) {
if let Some(diagnostic) =
Expand Down Expand Up @@ -4028,7 +4028,7 @@ where
// upstream.
self.add_binding(
&arg.arg,
identifier::arg(arg, self.locator),
arg.identifier(self.locator),
BindingKind::Argument,
BindingFlags::empty(),
);
Expand Down
15 changes: 8 additions & 7 deletions crates/ruff/src/rules/flake8_annotations/rules/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use rustpython_parser::ast::{Expr, Ranged, Stmt};
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::ReturnStatementVisitor;
use ruff_python_ast::identifier::Identifier;
use ruff_python_ast::statement_visitor::StatementVisitor;
use ruff_python_ast::{cast, identifier};
use ruff_python_ast::{cast};
use ruff_python_semantic::analyze::visibility;
use ruff_python_semantic::{Definition, Member, MemberKind, SemanticModel};
use ruff_python_stdlib::typing::SIMPLE_MAGIC_RETURN_TYPES;
Expand Down Expand Up @@ -640,7 +641,7 @@ pub(crate) fn definition(
MissingReturnTypeClassMethod {
name: name.to_string(),
},
identifier::statement(stmt, checker.locator),
stmt.identifier(checker.locator),
));
}
} else if is_method
Expand All @@ -651,7 +652,7 @@ pub(crate) fn definition(
MissingReturnTypeStaticMethod {
name: name.to_string(),
},
identifier::statement(stmt, checker.locator),
stmt.identifier(checker.locator),
));
}
} else if is_method && visibility::is_init(name) {
Expand All @@ -663,7 +664,7 @@ pub(crate) fn definition(
MissingReturnTypeSpecialMethod {
name: name.to_string(),
},
identifier::statement(stmt, checker.locator),
stmt.identifier(checker.locator),
);
if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
Expand All @@ -680,7 +681,7 @@ pub(crate) fn definition(
MissingReturnTypeSpecialMethod {
name: name.to_string(),
},
identifier::statement(stmt, checker.locator),
stmt.identifier(checker.locator),
);
let return_type = SIMPLE_MAGIC_RETURN_TYPES.get(name);
if let Some(return_type) = return_type {
Expand All @@ -701,7 +702,7 @@ pub(crate) fn definition(
MissingReturnTypeUndocumentedPublicFunction {
name: name.to_string(),
},
identifier::statement(stmt, checker.locator),
stmt.identifier(checker.locator),
));
}
}
Expand All @@ -711,7 +712,7 @@ pub(crate) fn definition(
MissingReturnTypePrivateFunction {
name: name.to_string(),
},
identifier::statement(stmt, checker.locator),
stmt.identifier(checker.locator),
));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rustpython_parser::ast::{self, Constant, Expr, Keyword, Ranged, Stmt};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::identifier::statement;
use ruff_python_ast::identifier::Identifier;
use ruff_python_semantic::analyze::visibility::{is_abstract, is_overload};
use ruff_python_semantic::SemanticModel;

Expand Down Expand Up @@ -134,7 +134,7 @@ pub(crate) fn abstract_base_class(
AbstractBaseClassWithoutAbstractMethod {
name: name.to_string(),
},
statement(stmt, checker.locator),
stmt.identifier(checker.locator),
));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rustpython_parser::ast::{self, Expr, Stmt};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::identifier;
use ruff_python_ast::identifier::Identifier;

use crate::checkers::ast::Checker;

Expand Down Expand Up @@ -31,6 +31,6 @@ pub(crate) fn f_string_docstring(checker: &mut Checker, body: &[Stmt]) {
};
checker.diagnostics.push(Diagnostic::new(
FStringDocstring,
identifier::statement(stmt, checker.locator),
stmt.identifier(checker.locator),
));
}
4 changes: 2 additions & 2 deletions crates/ruff/src/rules/flake8_builtins/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ruff_text_size::TextRange;
use rustpython_parser::ast::{Excepthandler, Expr, Ranged, Stmt};

use ruff_python_ast::identifier::statement;
use ruff_python_ast::identifier::Identifier;
use ruff_python_ast::source_code::Locator;
use ruff_python_stdlib::builtins::BUILTINS;

Expand All @@ -20,7 +20,7 @@ impl AnyShadowing<'_> {
pub(crate) fn range(self, locator: &Locator) -> TextRange {
match self {
AnyShadowing::Expression(expr) => expr.range(),
AnyShadowing::Statement(stmt) => statement(stmt, locator),
AnyShadowing::Statement(stmt) => stmt.identifier(locator),
AnyShadowing::ExceptHandler(handler) => handler.range(),
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/ruff/src/rules/flake8_pyi/rules/non_self_return_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustpython_parser::ast::{self, Arguments, Decorator, Expr, Stmt};
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::map_subscript;
use ruff_python_ast::identifier::statement;
use ruff_python_ast::identifier::Identifier;
use ruff_python_semantic::analyze::visibility::{is_abstract, is_final, is_overload};
use ruff_python_semantic::{ScopeKind, SemanticModel};

Expand Down Expand Up @@ -148,7 +148,7 @@ pub(crate) fn non_self_return_type(
class_name: class_def.name.to_string(),
method_name: name.to_string(),
},
statement(stmt, checker.locator),
stmt.identifier(checker.locator),
));
}
return;
Expand All @@ -162,7 +162,7 @@ pub(crate) fn non_self_return_type(
class_name: class_def.name.to_string(),
method_name: name.to_string(),
},
statement(stmt, checker.locator),
stmt.identifier(checker.locator),
));
}
return;
Expand All @@ -177,7 +177,7 @@ pub(crate) fn non_self_return_type(
class_name: class_def.name.to_string(),
method_name: name.to_string(),
},
statement(stmt, checker.locator),
stmt.identifier(checker.locator),
));
}
return;
Expand All @@ -193,7 +193,7 @@ pub(crate) fn non_self_return_type(
class_name: class_def.name.to_string(),
method_name: name.to_string(),
},
statement(stmt, checker.locator),
stmt.identifier(checker.locator),
));
}
}
Expand All @@ -206,7 +206,7 @@ pub(crate) fn non_self_return_type(
class_name: class_def.name.to_string(),
method_name: name.to_string(),
},
statement(stmt, checker.locator),
stmt.identifier(checker.locator),
));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustpython_parser::ast::Stmt;

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::identifier::statement;
use ruff_python_ast::identifier::Identifier;
use ruff_python_semantic::analyze::visibility::is_abstract;

use crate::autofix::edits::delete_stmt;
Expand Down Expand Up @@ -90,7 +90,7 @@ pub(crate) fn str_or_repr_defined_in_stub(checker: &mut Checker, stmt: &Stmt) {
StrOrReprDefinedInStub {
name: name.to_string(),
},
statement(stmt, checker.locator),
stmt.identifier(checker.locator),
);
if checker.patch(diagnostic.kind.rule()) {
let stmt = checker.semantic().stmt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustpython_parser::ast::Stmt;
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::is_docstring_stmt;
use ruff_python_ast::identifier;
use ruff_python_ast::identifier::Identifier;

use crate::checkers::ast::Checker;

Expand Down Expand Up @@ -32,6 +32,6 @@ pub(crate) fn stub_body_multiple_statements(checker: &mut Checker, stmt: &Stmt,

checker.diagnostics.push(Diagnostic::new(
StubBodyMultipleStatements,
identifier::statement(stmt, checker.locator),
stmt.identifier(checker.locator),
));
}
7 changes: 4 additions & 3 deletions crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ use ruff_diagnostics::{Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path::collect_call_path;
use ruff_python_ast::helpers::collect_arg_names;
use ruff_python_ast::identifier::Identifier;
use ruff_python_ast::prelude::Decorator;
use ruff_python_ast::source_code::Locator;
use ruff_python_ast::visitor::Visitor;
use ruff_python_ast::{identifier, visitor};
use ruff_python_ast::{visitor};
use ruff_python_semantic::analyze::visibility::is_abstract;
use ruff_python_semantic::SemanticModel;

Expand Down Expand Up @@ -378,7 +379,7 @@ fn check_fixture_returns(checker: &mut Checker, stmt: &Stmt, name: &str, body: &
PytestIncorrectFixtureNameUnderscore {
function: name.to_string(),
},
identifier::statement(stmt, checker.locator),
stmt.identifier(checker.locator),
));
} else if checker.enabled(Rule::PytestMissingFixtureNameUnderscore)
&& !visitor.has_return_with_value
Expand All @@ -389,7 +390,7 @@ fn check_fixture_returns(checker: &mut Checker, stmt: &Stmt, name: &str, body: &
PytestMissingFixtureNameUnderscore {
function: name.to_string(),
},
identifier::statement(stmt, checker.locator),
stmt.identifier(checker.locator),
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustpython_parser::ast::{Expr, StmtClassDef};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::identifier::statement;
use ruff_python_ast::identifier::Identifier;
use ruff_python_ast::prelude::Stmt;

use crate::checkers::ast::Checker;
Expand Down Expand Up @@ -77,7 +77,7 @@ pub(crate) fn no_slots_in_namedtuple_subclass(
if !has_slots(&class.body) {
checker.diagnostics.push(Diagnostic::new(
NoSlotsInNamedtupleSubclass,
statement(stmt, checker.locator),
stmt.identifier(checker.locator),
));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rustpython_parser::ast::{Stmt, StmtClassDef};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::identifier::statement;
use ruff_python_ast::identifier::Identifier;

use crate::checkers::ast::Checker;
use crate::rules::flake8_slots::rules::helpers::has_slots;
Expand Down Expand Up @@ -61,7 +61,7 @@ pub(crate) fn no_slots_in_str_subclass(checker: &mut Checker, stmt: &Stmt, class
if !has_slots(&class.body) {
checker.diagnostics.push(Diagnostic::new(
NoSlotsInStrSubclass,
statement(stmt, checker.locator),
stmt.identifier(checker.locator),
));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustpython_parser::ast::{Stmt, StmtClassDef};
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::map_subscript;
use ruff_python_ast::identifier::statement;
use ruff_python_ast::identifier::Identifier;

use crate::checkers::ast::Checker;
use crate::rules::flake8_slots::rules::helpers::has_slots;
Expand Down Expand Up @@ -65,7 +65,7 @@ pub(crate) fn no_slots_in_tuple_subclass(checker: &mut Checker, stmt: &Stmt, cla
if !has_slots(&class.body) {
checker.diagnostics.push(Diagnostic::new(
NoSlotsInTupleSubclass,
statement(stmt, checker.locator),
stmt.identifier(checker.locator),
));
}
}
Expand Down
Loading

0 comments on commit b19964b

Please sign in to comment.