Skip to content

Commit

Permalink
Set diagnostic range to be the entire function
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanPlasse committed Aug 13, 2024
1 parent 651fdc2 commit dda1efa
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 108 deletions.
1 change: 1 addition & 0 deletions crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
]) {
flake8_return::rules::function(
checker,
function_def,
body,
decorator_list,
returns.as_ref().map(AsRef::as_ref),
Expand Down
17 changes: 9 additions & 8 deletions crates/ruff_linter/src/rules/flake8_return/rules/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@ fn is_noreturn_func(func: &Expr, semantic: &SemanticModel) -> bool {
semantic.match_typing_qualified_name(&qualified_name, "NoReturn")
}

fn add_return_none(checker: &mut Checker, stmt: &Stmt) {
let mut diagnostic = Diagnostic::new(ImplicitReturn, stmt.range());
fn add_return_none(checker: &mut Checker, stmt: &Stmt, range: TextRange) {
let mut diagnostic = Diagnostic::new(ImplicitReturn, range);
if let Some(indent) = indentation(checker.locator(), stmt) {
let mut content = String::new();
content.push_str(checker.stylist().line_ending().as_str());
Expand Down Expand Up @@ -499,7 +499,7 @@ fn has_implicit_return(checker: &mut Checker, stmt: &Stmt) -> bool {
None | Some(ast::ElifElseClause { test: Some(_), .. })
) {
if checker.settings.preview.is_disabled() {
add_return_none(checker, stmt);
add_return_none(checker, stmt, stmt.range());
}
return true;
}
Expand All @@ -512,7 +512,7 @@ fn has_implicit_return(checker: &mut Checker, stmt: &Stmt) -> bool {
has_implicit_return(checker, last_stmt)
} else {
if checker.settings.preview.is_disabled() {
add_return_none(checker, stmt);
add_return_none(checker, stmt, stmt.range());
}
true
}
Expand Down Expand Up @@ -551,17 +551,17 @@ fn has_implicit_return(checker: &mut Checker, stmt: &Stmt) -> bool {
}
_ => {
if checker.settings.preview.is_disabled() {
add_return_none(checker, stmt);
add_return_none(checker, stmt, stmt.range());
}
true
}
}
}

/// RET503
fn implicit_return(checker: &mut Checker, stmt: &Stmt) {
fn implicit_return(checker: &mut Checker, function_def: &ast::StmtFunctionDef, stmt: &Stmt) {
if has_implicit_return(checker, stmt) && checker.settings.preview.is_enabled() {
add_return_none(checker, stmt);
add_return_none(checker, stmt, function_def.range());
}
}

Expand Down Expand Up @@ -766,6 +766,7 @@ fn superfluous_elif_else(checker: &mut Checker, stack: &Stack) {
/// Run all checks from the `flake8-return` plugin.
pub(crate) fn function(
checker: &mut Checker,
function_def: &ast::StmtFunctionDef,
body: &[Stmt],
decorator_list: &[Decorator],
returns: Option<&Expr>,
Expand Down Expand Up @@ -815,7 +816,7 @@ pub(crate) fn function(
implicit_return_value(checker, &stack);
}
if checker.enabled(Rule::ImplicitReturn) {
implicit_return(checker, last_stmt);
implicit_return(checker, function_def, last_stmt);
}

if checker.enabled(Rule::UnnecessaryAssign) {
Expand Down
Loading

0 comments on commit dda1efa

Please sign in to comment.