-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
ruff
] Unnecessary round()
cast (RUF046
)
- Loading branch information
1 parent
84748be
commit 18ffb33
Showing
8 changed files
with
331 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
### Errors | ||
int(round(0)) | ||
int(round(0, 0)) | ||
int(round(0, None)) | ||
|
||
int(round(0.1)) | ||
int(round(0.1, 0)) | ||
int(round(0.1, None)) | ||
|
||
# Argument type is not checked | ||
foo = type("Foo", (), {"__round__": lambda self: 4.2})() | ||
|
||
int(round(foo)) | ||
int(round(foo, 0)) | ||
int(round(foo, None)) | ||
|
||
|
||
### No errors | ||
int(round(0, 3.14)) | ||
int(round(0, non_literal)) | ||
int(round(0, 0), base) | ||
int(round(0, 0, extra=keyword)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
crates/ruff_linter/src/rules/ruff/rules/unnecessary_round_cast.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; | ||
use ruff_macros::{derive_message_formats, ViolationMetadata}; | ||
use ruff_python_ast::{Expr, ExprCall, ExprNumberLiteral, Number}; | ||
use ruff_python_semantic::SemanticModel; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for `int` conversions of `round()` calls | ||
/// with the second argument being either omitted, `0` or `None`. | ||
/// | ||
/// ## Why is this bad? | ||
/// Such a `round()` call already returns an integer, | ||
/// so calling `int()` is unnecessary. | ||
/// Additionally, the second argument can be omitted if it is `0` or `None`. | ||
/// | ||
/// ## Known problems | ||
/// This rule is prone to false positives due to type inference limitations. | ||
/// | ||
/// ## Example | ||
/// | ||
/// ```python | ||
/// int(round(foo, 0)) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// | ||
/// ```python | ||
/// round(foo) | ||
/// ``` | ||
#[derive(ViolationMetadata)] | ||
pub(crate) struct UnnecessaryRoundCast; | ||
|
||
impl AlwaysFixableViolation for UnnecessaryRoundCast { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
"The result of `round()` is already an integer".to_string() | ||
} | ||
|
||
fn fix_title(&self) -> String { | ||
"Replace with `round(...)`".to_string() | ||
} | ||
} | ||
|
||
/// RUF046 | ||
pub(crate) fn unnecessary_round_cast(checker: &mut Checker, call: &ExprCall) { | ||
let semantic = checker.semantic(); | ||
|
||
let Some(argument) = single_argument_to_int_call(semantic, call) else { | ||
return; | ||
}; | ||
|
||
let Some(argument) = first_argument_to_round_to_int_call(semantic, argument) else { | ||
return; | ||
}; | ||
|
||
let new = format!("round({})", checker.locator().slice(argument)); | ||
let edit = Edit::range_replacement(new, call.range); | ||
let fix = Fix::safe_edit(edit); | ||
|
||
let diagnostic = Diagnostic::new(UnnecessaryRoundCast, call.range); | ||
|
||
checker.diagnostics.push(diagnostic.with_fix(fix)); | ||
} | ||
|
||
fn single_argument_to_int_call<'a>( | ||
semantic: &SemanticModel, | ||
call: &'a ExprCall, | ||
) -> Option<&'a Expr> { | ||
let ExprCall { | ||
func, arguments, .. | ||
} = call; | ||
|
||
if !semantic.match_builtin_expr(func, "int") { | ||
return None; | ||
} | ||
|
||
if !arguments.keywords.is_empty() { | ||
return None; | ||
} | ||
|
||
let [argument] = &*arguments.args else { | ||
return None; | ||
}; | ||
|
||
Some(argument) | ||
} | ||
|
||
fn first_argument_to_round_to_int_call<'a>( | ||
semantic: &SemanticModel, | ||
expr: &'a Expr, | ||
) -> Option<&'a Expr> { | ||
let Expr::Call(ExprCall { | ||
func, arguments, .. | ||
}) = expr | ||
else { | ||
return None; | ||
}; | ||
|
||
if !semantic.match_builtin_expr(func, "round") { | ||
return None; | ||
} | ||
|
||
if arguments.len() != 1 && arguments.len() != 2 { | ||
return None; | ||
} | ||
|
||
let number = arguments.find_argument("number", 0)?; | ||
let Some(ndigits) = arguments.find_argument("ndigits", 1) else { | ||
return Some(number); | ||
}; | ||
|
||
match ndigits { | ||
Expr::NumberLiteral(ExprNumberLiteral { value, .. }) => { | ||
matches!(value, Number::Int(..)).then_some(number) | ||
} | ||
Expr::NoneLiteral(_) => Some(number), | ||
_ => None, | ||
} | ||
} |
181 changes: 181 additions & 0 deletions
181
.../src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF046_RUF046.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/ruff/mod.rs | ||
snapshot_kind: text | ||
--- | ||
RUF046.py:2:1: RUF046 [*] The result of `round()` is already an integer | ||
| | ||
1 | ### Errors | ||
2 | int(round(0)) | ||
| ^^^^^^^^^^^^^ RUF046 | ||
3 | int(round(0, 0)) | ||
4 | int(round(0, None)) | ||
| | ||
= help: Replace with `round(...)` | ||
|
||
ℹ Safe fix | ||
1 1 | ### Errors | ||
2 |-int(round(0)) | ||
2 |+round(0) | ||
3 3 | int(round(0, 0)) | ||
4 4 | int(round(0, None)) | ||
5 5 | | ||
|
||
RUF046.py:3:1: RUF046 [*] The result of `round()` is already an integer | ||
| | ||
1 | ### Errors | ||
2 | int(round(0)) | ||
3 | int(round(0, 0)) | ||
| ^^^^^^^^^^^^^^^^ RUF046 | ||
4 | int(round(0, None)) | ||
| | ||
= help: Replace with `round(...)` | ||
|
||
ℹ Safe fix | ||
1 1 | ### Errors | ||
2 2 | int(round(0)) | ||
3 |-int(round(0, 0)) | ||
3 |+round(0) | ||
4 4 | int(round(0, None)) | ||
5 5 | | ||
6 6 | int(round(0.1)) | ||
|
||
RUF046.py:4:1: RUF046 [*] The result of `round()` is already an integer | ||
| | ||
2 | int(round(0)) | ||
3 | int(round(0, 0)) | ||
4 | int(round(0, None)) | ||
| ^^^^^^^^^^^^^^^^^^^ RUF046 | ||
5 | | ||
6 | int(round(0.1)) | ||
| | ||
= help: Replace with `round(...)` | ||
|
||
ℹ Safe fix | ||
1 1 | ### Errors | ||
2 2 | int(round(0)) | ||
3 3 | int(round(0, 0)) | ||
4 |-int(round(0, None)) | ||
4 |+round(0) | ||
5 5 | | ||
6 6 | int(round(0.1)) | ||
7 7 | int(round(0.1, 0)) | ||
|
||
RUF046.py:6:1: RUF046 [*] The result of `round()` is already an integer | ||
| | ||
4 | int(round(0, None)) | ||
5 | | ||
6 | int(round(0.1)) | ||
| ^^^^^^^^^^^^^^^ RUF046 | ||
7 | int(round(0.1, 0)) | ||
8 | int(round(0.1, None)) | ||
| | ||
= help: Replace with `round(...)` | ||
|
||
ℹ Safe fix | ||
3 3 | int(round(0, 0)) | ||
4 4 | int(round(0, None)) | ||
5 5 | | ||
6 |-int(round(0.1)) | ||
6 |+round(0.1) | ||
7 7 | int(round(0.1, 0)) | ||
8 8 | int(round(0.1, None)) | ||
9 9 | | ||
|
||
RUF046.py:7:1: RUF046 [*] The result of `round()` is already an integer | ||
| | ||
6 | int(round(0.1)) | ||
7 | int(round(0.1, 0)) | ||
| ^^^^^^^^^^^^^^^^^^ RUF046 | ||
8 | int(round(0.1, None)) | ||
| | ||
= help: Replace with `round(...)` | ||
|
||
ℹ Safe fix | ||
4 4 | int(round(0, None)) | ||
5 5 | | ||
6 6 | int(round(0.1)) | ||
7 |-int(round(0.1, 0)) | ||
7 |+round(0.1) | ||
8 8 | int(round(0.1, None)) | ||
9 9 | | ||
10 10 | # Argument type is not checked | ||
|
||
RUF046.py:8:1: RUF046 [*] The result of `round()` is already an integer | ||
| | ||
6 | int(round(0.1)) | ||
7 | int(round(0.1, 0)) | ||
8 | int(round(0.1, None)) | ||
| ^^^^^^^^^^^^^^^^^^^^^ RUF046 | ||
9 | | ||
10 | # Argument type is not checked | ||
| | ||
= help: Replace with `round(...)` | ||
|
||
ℹ Safe fix | ||
5 5 | | ||
6 6 | int(round(0.1)) | ||
7 7 | int(round(0.1, 0)) | ||
8 |-int(round(0.1, None)) | ||
8 |+round(0.1) | ||
9 9 | | ||
10 10 | # Argument type is not checked | ||
11 11 | foo = type("Foo", (), {"__round__": lambda self: 4.2})() | ||
|
||
RUF046.py:13:1: RUF046 [*] The result of `round()` is already an integer | ||
| | ||
11 | foo = type("Foo", (), {"__round__": lambda self: 4.2})() | ||
12 | | ||
13 | int(round(foo)) | ||
| ^^^^^^^^^^^^^^^ RUF046 | ||
14 | int(round(foo, 0)) | ||
15 | int(round(foo, None)) | ||
| | ||
= help: Replace with `round(...)` | ||
|
||
ℹ Safe fix | ||
10 10 | # Argument type is not checked | ||
11 11 | foo = type("Foo", (), {"__round__": lambda self: 4.2})() | ||
12 12 | | ||
13 |-int(round(foo)) | ||
13 |+round(foo) | ||
14 14 | int(round(foo, 0)) | ||
15 15 | int(round(foo, None)) | ||
16 16 | | ||
|
||
RUF046.py:14:1: RUF046 [*] The result of `round()` is already an integer | ||
| | ||
13 | int(round(foo)) | ||
14 | int(round(foo, 0)) | ||
| ^^^^^^^^^^^^^^^^^^ RUF046 | ||
15 | int(round(foo, None)) | ||
| | ||
= help: Replace with `round(...)` | ||
|
||
ℹ Safe fix | ||
11 11 | foo = type("Foo", (), {"__round__": lambda self: 4.2})() | ||
12 12 | | ||
13 13 | int(round(foo)) | ||
14 |-int(round(foo, 0)) | ||
14 |+round(foo) | ||
15 15 | int(round(foo, None)) | ||
16 16 | | ||
17 17 | | ||
|
||
RUF046.py:15:1: RUF046 [*] The result of `round()` is already an integer | ||
| | ||
13 | int(round(foo)) | ||
14 | int(round(foo, 0)) | ||
15 | int(round(foo, None)) | ||
| ^^^^^^^^^^^^^^^^^^^^^ RUF046 | ||
| | ||
= help: Replace with `round(...)` | ||
|
||
ℹ Safe fix | ||
12 12 | | ||
13 13 | int(round(foo)) | ||
14 14 | int(round(foo, 0)) | ||
15 |-int(round(foo, None)) | ||
15 |+round(foo) | ||
16 16 | | ||
17 17 | | ||
18 18 | ### No errors |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.