Skip to content

Commit

Permalink
[refurb] Ignore methods in reimplemented-operator (FURB118) (#1…
Browse files Browse the repository at this point in the history
…1270)

## Summary

This rule does more harm than good when applied to methods.

Closes #10898.

Closes #11045.
  • Loading branch information
charliermarsh authored May 3, 2024
1 parent f3284fd commit 894cd13
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 17 deletions.
7 changes: 7 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/refurb/FURB118.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,10 @@ def op_add4(x, y=1):
def op_add5(x, y):
print("op_add5")
return x + y


# OK
class Class:
@staticmethod
def add(x, y):
return x + y
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ pub(crate) fn unused_arguments(
return;
}

let Some(parent) = &checker.semantic().first_non_type_parent_scope(scope) else {
let Some(parent) = checker.semantic().first_non_type_parent_scope(scope) else {
return;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub(crate) fn invalid_first_argument_name(
panic!("Expected ScopeKind::Function")
};

let Some(parent) = &checker.semantic().first_non_type_parent_scope(scope) else {
let Some(parent) = checker.semantic().first_non_type_parent_scope(scope) else {
return;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub(crate) fn bad_staticmethod_argument(
..
} = func;

let Some(parent) = &checker.semantic().first_non_type_parent_scope(scope) else {
let Some(parent) = checker.semantic().first_non_type_parent_scope(scope) else {
return;
};

Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/rules/pylint/rules/no_self_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub(crate) fn no_self_use(
scope: &Scope,
diagnostics: &mut Vec<Diagnostic>,
) {
let Some(parent) = &checker.semantic().first_non_type_parent_scope(scope) else {
let Some(parent) = checker.semantic().first_non_type_parent_scope(scope) else {
return;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub(crate) fn singledispatch_method(
..
} = func;

let Some(parent) = &checker.semantic().first_non_type_parent_scope(scope) else {
let Some(parent) = checker.semantic().first_non_type_parent_scope(scope) else {
return;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub(crate) fn singledispatchmethod_function(
..
} = func;

let Some(parent) = &checker.semantic().first_non_type_parent_scope(scope) else {
let Some(parent) = checker.semantic().first_non_type_parent_scope(scope) else {
return;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub(crate) fn super_without_brackets(checker: &mut Checker, func: &Expr) {
return;
};

let Some(parent) = &checker.semantic().first_non_type_parent_scope(scope) else {
let Some(parent) = checker.semantic().first_non_type_parent_scope(scope) else {
return;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ impl Violation for ReimplementedOperator {

/// FURB118
pub(crate) fn reimplemented_operator(checker: &mut Checker, target: &FunctionLike) {
// Ignore methods.
if target.kind() == FunctionLikeKind::Function {
if checker.semantic().current_scope().kind.is_class() {
return;
}
}

let Some(params) = target.parameters() else {
return;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -793,13 +793,3 @@ FURB118.py:40:1: FURB118 Use `operator.add` instead of defining a function
| |________________^ FURB118
|
= help: Replace with `operator.add`

FURB118.py:45:5: FURB118 Use `operator.add` instead of defining a function
|
44 | class Adder:
45 | def add(x, y):
| _____^
46 | | return x + y
| |____________________^ FURB118
|
= help: Replace with `operator.add`

0 comments on commit 894cd13

Please sign in to comment.