Skip to content

Commit 894cd13

Browse files
[refurb] Ignore methods in reimplemented-operator (FURB118) (#11270)
## Summary This rule does more harm than good when applied to methods. Closes #10898. Closes #11045.
1 parent f3284fd commit 894cd13

File tree

10 files changed

+21
-17
lines changed

10 files changed

+21
-17
lines changed

crates/ruff_linter/resources/test/fixtures/refurb/FURB118.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,10 @@ def op_add4(x, y=1):
7373
def op_add5(x, y):
7474
print("op_add5")
7575
return x + y
76+
77+
78+
# OK
79+
class Class:
80+
@staticmethod
81+
def add(x, y):
82+
return x + y

crates/ruff_linter/src/rules/flake8_unused_arguments/rules/unused_arguments.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ pub(crate) fn unused_arguments(
322322
return;
323323
}
324324

325-
let Some(parent) = &checker.semantic().first_non_type_parent_scope(scope) else {
325+
let Some(parent) = checker.semantic().first_non_type_parent_scope(scope) else {
326326
return;
327327
};
328328

crates/ruff_linter/src/rules/pep8_naming/rules/invalid_first_argument_name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ pub(crate) fn invalid_first_argument_name(
190190
panic!("Expected ScopeKind::Function")
191191
};
192192

193-
let Some(parent) = &checker.semantic().first_non_type_parent_scope(scope) else {
193+
let Some(parent) = checker.semantic().first_non_type_parent_scope(scope) else {
194194
return;
195195
};
196196

crates/ruff_linter/src/rules/pylint/rules/bad_staticmethod_argument.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub(crate) fn bad_staticmethod_argument(
6464
..
6565
} = func;
6666

67-
let Some(parent) = &checker.semantic().first_non_type_parent_scope(scope) else {
67+
let Some(parent) = checker.semantic().first_non_type_parent_scope(scope) else {
6868
return;
6969
};
7070

crates/ruff_linter/src/rules/pylint/rules/no_self_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub(crate) fn no_self_use(
4949
scope: &Scope,
5050
diagnostics: &mut Vec<Diagnostic>,
5151
) {
52-
let Some(parent) = &checker.semantic().first_non_type_parent_scope(scope) else {
52+
let Some(parent) = checker.semantic().first_non_type_parent_scope(scope) else {
5353
return;
5454
};
5555

crates/ruff_linter/src/rules/pylint/rules/singledispatch_method.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub(crate) fn singledispatch_method(
7474
..
7575
} = func;
7676

77-
let Some(parent) = &checker.semantic().first_non_type_parent_scope(scope) else {
77+
let Some(parent) = checker.semantic().first_non_type_parent_scope(scope) else {
7878
return;
7979
};
8080

crates/ruff_linter/src/rules/pylint/rules/singledispatchmethod_function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub(crate) fn singledispatchmethod_function(
7272
..
7373
} = func;
7474

75-
let Some(parent) = &checker.semantic().first_non_type_parent_scope(scope) else {
75+
let Some(parent) = checker.semantic().first_non_type_parent_scope(scope) else {
7676
return;
7777
};
7878

crates/ruff_linter/src/rules/pylint/rules/super_without_brackets.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub(crate) fn super_without_brackets(checker: &mut Checker, func: &Expr) {
8383
return;
8484
};
8585

86-
let Some(parent) = &checker.semantic().first_non_type_parent_scope(scope) else {
86+
let Some(parent) = checker.semantic().first_non_type_parent_scope(scope) else {
8787
return;
8888
};
8989

crates/ruff_linter/src/rules/refurb/rules/reimplemented_operator.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ impl Violation for ReimplementedOperator {
6969

7070
/// FURB118
7171
pub(crate) fn reimplemented_operator(checker: &mut Checker, target: &FunctionLike) {
72+
// Ignore methods.
73+
if target.kind() == FunctionLikeKind::Function {
74+
if checker.semantic().current_scope().kind.is_class() {
75+
return;
76+
}
77+
}
78+
7279
let Some(params) = target.parameters() else {
7380
return;
7481
};

crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB118_FURB118.py.snap

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -793,13 +793,3 @@ FURB118.py:40:1: FURB118 Use `operator.add` instead of defining a function
793793
| |________________^ FURB118
794794
|
795795
= help: Replace with `operator.add`
796-
797-
FURB118.py:45:5: FURB118 Use `operator.add` instead of defining a function
798-
|
799-
44 | class Adder:
800-
45 | def add(x, y):
801-
| _____^
802-
46 | | return x + y
803-
| |____________________^ FURB118
804-
|
805-
= help: Replace with `operator.add`

0 commit comments

Comments
 (0)