Skip to content

Commit b6c3a6a

Browse files
author
Michael Wright
committed
Move max_value handling to consts module
1 parent 488cdeb commit b6c3a6a

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

clippy_lints/src/consts.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(clippy::float_cmp)]
22

3-
use crate::utils::{clip, sext, unsext};
3+
use crate::utils::{clip, get_def_path, sext, unsext};
4+
use if_chain::if_chain;
45
use rustc::hir::def::Def;
56
use rustc::hir::*;
67
use rustc::lint::LateContext;
@@ -234,6 +235,31 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
234235
UnDeref => Some(o),
235236
}),
236237
ExprKind::Binary(op, ref left, ref right) => self.binop(op, left, right),
238+
ExprKind::Call(ref callee, ref args) => {
239+
// We only handle a few const functions for now
240+
if_chain! {
241+
if args.is_empty();
242+
if let ExprKind::Path(qpath) = &callee.node;
243+
let def = self.tables.qpath_def(qpath, callee.hir_id);
244+
if let Some(def_id) = def.opt_def_id();
245+
let def_path = get_def_path(self.tcx, def_id);
246+
if let &["core", "num", impl_ty, "max_value"] = &def_path[..];
247+
then {
248+
let value = match impl_ty {
249+
"<impl i8>" => i8::max_value() as u128,
250+
"<impl i16>" => i16::max_value() as u128,
251+
"<impl i32>" => i32::max_value() as u128,
252+
"<impl i64>" => i64::max_value() as u128,
253+
"<impl i128>" => i128::max_value() as u128,
254+
_ => return None,
255+
};
256+
Some(Constant::Int(value))
257+
}
258+
else {
259+
None
260+
}
261+
}
262+
},
237263
// TODO: add other expressions
238264
_ => None,
239265
}

clippy_lints/src/types.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::consts::{constant, Constant};
44
use crate::reexport::*;
55
use crate::utils::paths;
66
use crate::utils::{
7-
clip, comparisons, differing_macro_contexts, get_def_path, higher, in_constant, in_macro, int_bits,
8-
last_path_segment, match_def_path, match_path, multispan_sugg, opt_def_id, same_tys, sext, snippet, snippet_opt,
7+
clip, comparisons, differing_macro_contexts, higher, in_constant, in_macro, int_bits, last_path_segment,
8+
match_def_path, match_path, multispan_sugg, opt_def_id, same_tys, sext, snippet, snippet_opt,
99
snippet_with_applicability, span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then, unsext,
1010
AbsolutePathBuffer,
1111
};
@@ -1018,23 +1018,6 @@ fn check_loss_of_sign(cx: &LateContext<'_, '_>, expr: &Expr, op: &Expr, cast_fro
10181018
}
10191019
}
10201020

1021-
// don't lint for max_value const fns
1022-
if_chain! {
1023-
if let ExprKind::Call(callee, args) = &op.node;
1024-
if args.is_empty();
1025-
if let ExprKind::Path(qpath) = &callee.node;
1026-
let def = cx.tables.qpath_def(qpath, callee.hir_id);
1027-
if let Some(def_id) = def.opt_def_id();
1028-
let def_path = get_def_path(cx.tcx, def_id);
1029-
if let &["core", "num", impl_ty, "max_value"] = &def_path[..];
1030-
then {
1031-
if let "<impl i8>" | "<impl i16>" | "<impl i32>" |
1032-
"<impl i64>" | "<impl i128>" = impl_ty {
1033-
return;
1034-
}
1035-
}
1036-
}
1037-
10381021
span_lint(
10391022
cx,
10401023
CAST_SIGN_LOSS,

0 commit comments

Comments
 (0)