Skip to content

Commit 52dacbc

Browse files
committed
Auto merge of rust-lang#5445 - logan-dev-oss:master, r=flip1995
Fixes issue rust-lang#4892. First contribution here 😊 ! Do not hesitate to correct me. This PR is related to issue rust-lang#4892 . # Summary ```rust -literal.method_call(args) ``` The main idea is to not trigger `clippy::precedence` when the method call is an odd function. # Example ```rust // should trigger lint let _ = -1.0_f64.abs() //precedence of method call abs() and neg ('-') is ambiguous // should not trigger lint let _ = -1.0_f64.sin() // sin is an odd function => -sin(x) = sin(-x) ``` # Theory Rust allows following literals: - char - string - integers - floats - byte - bool Only integers/floats implements the relevant `std::ops::Neg`. Following odd functions are implemented on i[8-128] and/or f[32-64]: - `asin` - `asinh` - `atan` - `atanh` - `cbrt` - `fract` - `round` - `signum` - `sin` - `sinh` - `tan` - `tanh ` - `to_degrees` - `to_radians` # Implementation As suggested by `flip1995` in [comment](rust-lang/rust-clippy#4892 (comment)), this PR add a whitelist of odd functions and compare method call to the the whitelist before triggering lint. changelog: Don't trigger [`clippy::precedence`] on odd functions.
2 parents 3ea8e5e + 66b855c commit 52dacbc

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

clippy_lints/src/precedence.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@ use rustc_lint::{EarlyContext, EarlyLintPass};
55
use rustc_session::{declare_lint_pass, declare_tool_lint};
66
use rustc_span::source_map::Spanned;
77

8+
const ODD_FUNCTIONS_WHITELIST: [&str; 14] = [
9+
"asin",
10+
"asinh",
11+
"atan",
12+
"atanh",
13+
"cbrt",
14+
"fract",
15+
"round",
16+
"signum",
17+
"sin",
18+
"sinh",
19+
"tan",
20+
"tanh",
21+
"to_degrees",
22+
"to_radians",
23+
];
24+
825
declare_clippy_lint! {
926
/// **What it does:** Checks for operations where precedence may be unclear
1027
/// and suggests to add parentheses. Currently it catches the following:
@@ -86,11 +103,18 @@ impl EarlyLintPass for Precedence {
86103
}
87104

88105
if let ExprKind::Unary(UnOp::Neg, ref rhs) = expr.kind {
89-
if let ExprKind::MethodCall(_, ref args) = rhs.kind {
106+
if let ExprKind::MethodCall(ref path_segment, ref args) = rhs.kind {
107+
let path_segment_str = path_segment.ident.name.as_str();
90108
if let Some(slf) = args.first() {
91109
if let ExprKind::Lit(ref lit) = slf.kind {
92110
match lit.kind {
93111
LitKind::Int(..) | LitKind::Float(..) => {
112+
if ODD_FUNCTIONS_WHITELIST
113+
.iter()
114+
.any(|odd_function| **odd_function == *path_segment_str)
115+
{
116+
return;
117+
}
94118
let mut applicability = Applicability::MachineApplicable;
95119
span_lint_and_sugg(
96120
cx,

tests/ui/precedence.fixed

+16
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ fn main() {
3232
let _ = -(1i32.abs());
3333
let _ = -(1f32.abs());
3434

35+
// Odd functions shoud not trigger an error
36+
let _ = -1f64.asin();
37+
let _ = -1f64.asinh();
38+
let _ = -1f64.atan();
39+
let _ = -1f64.atanh();
40+
let _ = -1f64.cbrt();
41+
let _ = -1f64.fract();
42+
let _ = -1f64.round();
43+
let _ = -1f64.signum();
44+
let _ = -1f64.sin();
45+
let _ = -1f64.sinh();
46+
let _ = -1f64.tan();
47+
let _ = -1f64.tanh();
48+
let _ = -1f64.to_degrees();
49+
let _ = -1f64.to_radians();
50+
3551
let b = 3;
3652
trip!(b * 8);
3753
}

tests/ui/precedence.rs

+16
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ fn main() {
3232
let _ = -(1i32.abs());
3333
let _ = -(1f32.abs());
3434

35+
// Odd functions shoud not trigger an error
36+
let _ = -1f64.asin();
37+
let _ = -1f64.asinh();
38+
let _ = -1f64.atan();
39+
let _ = -1f64.atanh();
40+
let _ = -1f64.cbrt();
41+
let _ = -1f64.fract();
42+
let _ = -1f64.round();
43+
let _ = -1f64.signum();
44+
let _ = -1f64.sin();
45+
let _ = -1f64.sinh();
46+
let _ = -1f64.tan();
47+
let _ = -1f64.tanh();
48+
let _ = -1f64.to_degrees();
49+
let _ = -1f64.to_radians();
50+
3551
let b = 3;
3652
trip!(b * 8);
3753
}

0 commit comments

Comments
 (0)