X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_lints%2Fsrc%2Fconsts.rs;h=f56dc3aed6946cbcc9cbac3dd26dc815a1581213;hb=b6c3a6a09f339079aa1213226aa43b65a3fe0fe9;hp=832fb8752867a499577f7b4f31266c4433787f5b;hpb=488cdebd2619dd031256813b4069b46377c016e8;p=rust.git diff --git a/clippy_lints/src/consts.rs b/clippy_lints/src/consts.rs index 832fb875286..f56dc3aed69 100644 --- a/clippy_lints/src/consts.rs +++ b/clippy_lints/src/consts.rs @@ -1,6 +1,7 @@ #![allow(clippy::float_cmp)] -use crate::utils::{clip, sext, unsext}; +use crate::utils::{clip, get_def_path, sext, unsext}; +use if_chain::if_chain; use rustc::hir::def::Def; use rustc::hir::*; use rustc::lint::LateContext; @@ -234,6 +235,31 @@ pub fn expr(&mut self, e: &Expr) -> Option { UnDeref => Some(o), }), ExprKind::Binary(op, ref left, ref right) => self.binop(op, left, right), + ExprKind::Call(ref callee, ref args) => { + // We only handle a few const functions for now + if_chain! { + if args.is_empty(); + if let ExprKind::Path(qpath) = &callee.node; + let def = self.tables.qpath_def(qpath, callee.hir_id); + if let Some(def_id) = def.opt_def_id(); + let def_path = get_def_path(self.tcx, def_id); + if let &["core", "num", impl_ty, "max_value"] = &def_path[..]; + then { + let value = match impl_ty { + "" => i8::max_value() as u128, + "" => i16::max_value() as u128, + "" => i32::max_value() as u128, + "" => i64::max_value() as u128, + "" => i128::max_value() as u128, + _ => return None, + }; + Some(Constant::Int(value)) + } + else { + None + } + } + }, // TODO: add other expressions _ => None, }