This commit is contained in:
logan-dev-oss 2020-04-10 10:40:49 +02:00
parent 82be9dc606
commit 9c89cf00c0
3 changed files with 55 additions and 1 deletions

View file

@ -5,6 +5,23 @@ use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Spanned; use rustc_span::source_map::Spanned;
const ODD_FUNCTIONS_WHITELIST: [&str; 14] = [
"asin",
"asinh",
"atan",
"atanh",
"cbrt",
"fract",
"round",
"signum",
"sin",
"sinh",
"tan",
"tanh",
"to_degrees",
"to_radians",
];
declare_clippy_lint! { declare_clippy_lint! {
/// **What it does:** Checks for operations where precedence may be unclear /// **What it does:** Checks for operations where precedence may be unclear
/// and suggests to add parentheses. Currently it catches the following: /// and suggests to add parentheses. Currently it catches the following:
@ -86,11 +103,16 @@ impl EarlyLintPass for Precedence {
} }
if let ExprKind::Unary(UnOp::Neg, ref rhs) = expr.kind { if let ExprKind::Unary(UnOp::Neg, ref rhs) = expr.kind {
if let ExprKind::MethodCall(_, ref args) = rhs.kind { if let ExprKind::MethodCall(ref path_segment, ref args) = rhs.kind {
if let Some(slf) = args.first() { if let Some(slf) = args.first() {
if let ExprKind::Lit(ref lit) = slf.kind { if let ExprKind::Lit(ref lit) = slf.kind {
match lit.kind { match lit.kind {
LitKind::Int(..) | LitKind::Float(..) => { LitKind::Int(..) | LitKind::Float(..) => {
for &odd_function in &ODD_FUNCTIONS_WHITELIST {
if odd_function == &*path_segment.ident.name.as_str() {
return;
}
}
let mut applicability = Applicability::MachineApplicable; let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg( span_lint_and_sugg(
cx, cx,

View file

@ -32,6 +32,22 @@ fn main() {
let _ = -(1i32.abs()); let _ = -(1i32.abs());
let _ = -(1f32.abs()); let _ = -(1f32.abs());
// Odd functions shoud not trigger an error
let _ = -1f64.asin();
let _ = -1f64.asinh();
let _ = -1f64.atan();
let _ = -1f64.atanh();
let _ = -1f64.cbrt();
let _ = -1f64.fract();
let _ = -1f64.round();
let _ = -1f64.signum();
let _ = -1f64.sin();
let _ = -1f64.sinh();
let _ = -1f64.tan();
let _ = -1f64.tanh();
let _ = -1f64.to_degrees();
let _ = -1f64.to_radians();
let b = 3; let b = 3;
trip!(b * 8); trip!(b * 8);
} }

View file

@ -32,6 +32,22 @@ fn main() {
let _ = -(1i32.abs()); let _ = -(1i32.abs());
let _ = -(1f32.abs()); let _ = -(1f32.abs());
// Odd functions shoud not trigger an error
let _ = -1f64.asin();
let _ = -1f64.asinh();
let _ = -1f64.atan();
let _ = -1f64.atanh();
let _ = -1f64.cbrt();
let _ = -1f64.fract();
let _ = -1f64.round();
let _ = -1f64.signum();
let _ = -1f64.sin();
let _ = -1f64.sinh();
let _ = -1f64.tan();
let _ = -1f64.tanh();
let _ = -1f64.to_degrees();
let _ = -1f64.to_radians();
let b = 3; let b = 3;
trip!(b * 8); trip!(b * 8);
} }