4 use syntax::ast_util::{is_comparison_binop, binop_to_string};
5 use syntax::codemap::{Span, Spanned};
6 use syntax::visit::FnKind;
7 use rustc::ast_map::Node::*;
10 use utils::{match_path, snippet, span_lint, walk_ptrs_ty};
13 declare_lint!(pub TOPLEVEL_REF_ARG, Warn,
14 "a function argument is declared `ref` (i.e. `fn foo(ref x: u8)`, but not \
15 `fn foo((ref x, ref y): (u8, u8))`)");
17 #[allow(missing_copy_implementations)]
18 pub struct TopLevelRefPass;
20 impl LintPass for TopLevelRefPass {
21 fn get_lints(&self) -> LintArray {
22 lint_array!(TOPLEVEL_REF_ARG)
25 fn check_fn(&mut self, cx: &Context, k: FnKind, decl: &FnDecl, _: &Block, _: Span, _: NodeId) {
26 if let FnKind::Closure = k {
27 // Does not apply to closures
30 for ref arg in &decl.inputs {
31 if let PatIdent(BindByRef(_), _, _) = arg.pat.node {
35 "`ref` directly on a function argument is ignored. Consider using a reference type instead."
42 declare_lint!(pub CMP_NAN, Deny,
43 "comparisons to NAN (which will always return false, which is probably not intended)");
48 impl LintPass for CmpNan {
49 fn get_lints(&self) -> LintArray {
53 fn check_expr(&mut self, cx: &Context, expr: &Expr) {
54 if let ExprBinary(ref cmp, ref left, ref right) = expr.node {
55 if is_comparison_binop(cmp.node) {
56 if let &ExprPath(_, ref path) = &left.node {
57 check_nan(cx, path, expr.span);
59 if let &ExprPath(_, ref path) = &right.node {
60 check_nan(cx, path, expr.span);
67 fn check_nan(cx: &Context, path: &Path, span: Span) {
68 path.segments.last().map(|seg| if seg.identifier.name == "NAN" {
69 span_lint(cx, CMP_NAN, span,
70 "doomed comparison with NAN, use `std::{f32,f64}::is_nan()` instead");
74 declare_lint!(pub FLOAT_CMP, Warn,
75 "using `==` or `!=` on float values (as floating-point operations \
76 usually involve rounding errors, it is always better to check for approximate \
77 equality within small bounds)");
82 impl LintPass for FloatCmp {
83 fn get_lints(&self) -> LintArray {
84 lint_array!(FLOAT_CMP)
87 fn check_expr(&mut self, cx: &Context, expr: &Expr) {
88 if let ExprBinary(ref cmp, ref left, ref right) = expr.node {
90 if (op == BiEq || op == BiNe) && (is_float(cx, left) || is_float(cx, right)) {
91 if constant(cx, left).or_else(|| constant(cx, right)).map_or(
92 false, |c| c.0.as_float().map_or(false, |f| f == 0.0)) {
95 let parent_id = cx.tcx.map.get_parent(expr.id);
96 match cx.tcx.map.find(parent_id) {
97 Some(NodeItem(&Item{ ref ident, .. })) |
98 Some(NodeTraitItem(&TraitItem{ id: _, ref ident, .. })) |
99 Some(NodeImplItem(&ImplItem{ id: _, ref ident, .. })) => {
100 let name = ident.name.as_str();
101 if &*name == "eq" || name.starts_with("eq_") ||
102 name.ends_with("_eq") { return; }
106 span_lint(cx, FLOAT_CMP, expr.span, &format!(
107 "{}-comparison of f32 or f64 detected. Consider changing this to \
108 `abs({} - {}) < epsilon` for some suitable value of epsilon",
109 binop_to_string(op), snippet(cx, left.span, ".."),
110 snippet(cx, right.span, "..")));
116 fn is_float(cx: &Context, expr: &Expr) -> bool {
117 if let ty::TyFloat(_) = walk_ptrs_ty(cx.tcx.expr_ty(expr)).sty {
124 declare_lint!(pub CMP_OWNED, Warn,
125 "creating owned instances for comparing with others, e.g. `x == \"foo\".to_string()`");
127 #[derive(Copy,Clone)]
130 impl LintPass for CmpOwned {
131 fn get_lints(&self) -> LintArray {
132 lint_array!(CMP_OWNED)
135 fn check_expr(&mut self, cx: &Context, expr: &Expr) {
136 if let ExprBinary(ref cmp, ref left, ref right) = expr.node {
137 if is_comparison_binop(cmp.node) {
138 check_to_owned(cx, left, right.span);
139 check_to_owned(cx, right, left.span)
145 fn check_to_owned(cx: &Context, expr: &Expr, other_span: Span) {
147 ExprMethodCall(Spanned{node: ref ident, ..}, _, ref args) => {
148 let name = ident.name;
149 if name == "to_string" ||
150 name == "to_owned" && is_str_arg(cx, args) {
151 span_lint(cx, CMP_OWNED, expr.span, &format!(
152 "this creates an owned instance just for comparison. \
153 Consider using `{}.as_slice()` to compare without allocation",
154 snippet(cx, other_span, "..")))
157 ExprCall(ref path, _) => {
158 if let &ExprPath(None, ref path) = &path.node {
159 if match_path(path, &["String", "from_str"]) ||
160 match_path(path, &["String", "from"]) {
161 span_lint(cx, CMP_OWNED, expr.span, &format!(
162 "this creates an owned instance just for comparison. \
163 Consider using `{}.as_slice()` to compare without allocation",
164 snippet(cx, other_span, "..")))
172 fn is_str_arg(cx: &Context, args: &[P<Expr>]) -> bool {
173 args.len() == 1 && if let ty::TyStr =
174 walk_ptrs_ty(cx.tcx.expr_ty(&args[0])).sty { true } else { false }
177 declare_lint!(pub MODULO_ONE, Warn, "taking a number modulo 1, which always returns 0");
179 #[derive(Copy,Clone)]
180 pub struct ModuloOne;
182 impl LintPass for ModuloOne {
183 fn get_lints(&self) -> LintArray {
184 lint_array!(MODULO_ONE)
187 fn check_expr(&mut self, cx: &Context, expr: &Expr) {
188 if let ExprBinary(ref cmp, _, ref right) = expr.node {
189 if let &Spanned {node: BinOp_::BiRem, ..} = cmp {
190 if is_lit_one(right) {
191 cx.span_lint(MODULO_ONE, expr.span, "any number modulo 1 will be 0");
198 fn is_lit_one(expr: &Expr) -> bool {
199 if let ExprLit(ref spanned) = expr.node {
200 if let LitInt(1, _) = spanned.node {
207 declare_lint!(pub REDUNDANT_PATTERN, Warn, "using `name @ _` in a pattern");
209 #[derive(Copy,Clone)]
210 pub struct PatternPass;
212 impl LintPass for PatternPass {
213 fn get_lints(&self) -> LintArray {
214 lint_array!(REDUNDANT_PATTERN)
217 fn check_pat(&mut self, cx: &Context, pat: &Pat) {
218 if let PatIdent(_, ref ident, Some(ref right)) = pat.node {
219 if right.node == PatWild(PatWildSingle) {
220 cx.span_lint(REDUNDANT_PATTERN, pat.span, &format!(
221 "the `{} @ _` pattern can be written as just `{}`",
222 ident.node.name, ident.node.name));