Skip to content

Commit 9c89cf0

Browse files
author
logan-dev-oss
committed
Fix issue rust-lang#4892.
1 parent 82be9dc commit 9c89cf0

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

clippy_lints/src/precedence.rs

+23-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,16 @@ 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 {
90107
if let Some(slf) = args.first() {
91108
if let ExprKind::Lit(ref lit) = slf.kind {
92109
match lit.kind {
93110
LitKind::Int(..) | LitKind::Float(..) => {
111+
for &odd_function in &ODD_FUNCTIONS_WHITELIST {
112+
if odd_function == &*path_segment.ident.name.as_str() {
113+
return;
114+
}
115+
}
94116
let mut applicability = Applicability::MachineApplicable;
95117
span_lint_and_sugg(
96118
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)