Skip to content

Commit

Permalink
b015 more resilient check if expression is at the end of function scope
Browse files Browse the repository at this point in the history
  • Loading branch information
dsal3389 committed Aug 17, 2024
1 parent 1493b38 commit 3d52f2d
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::Expr;
use ruff_python_ast::{Expr, Stmt};
use ruff_python_semantic::ScopeKind;
use ruff_text_size::Ranged;

Expand Down Expand Up @@ -70,7 +70,12 @@ pub(crate) fn useless_comparison(checker: &mut Checker, expr: &Expr) {
}

if let ScopeKind::Function(func_def) = semantic.current_scope().kind {
if func_def.range.end() == expr.range().end() {
if func_def
.body
.last()
.and_then(Stmt::as_expr_stmt)
.is_some_and(|last_stmt| &*last_stmt.value == expr)
{
checker.diagnostics.push(Diagnostic::new(
UselessComparison {
at: ComparisonLocationAt::EndOfFunction,
Expand Down

0 comments on commit 3d52f2d

Please sign in to comment.