]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/misc.rs
New upstream version 1.58.1+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / misc.rs
1 use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then, span_lint_hir_and_then};
2 use clippy_utils::source::{snippet, snippet_opt};
3 use clippy_utils::ty::implements_trait;
4 use if_chain::if_chain;
5 use rustc_ast::ast::LitKind;
6 use rustc_errors::Applicability;
7 use rustc_hir::intravisit::FnKind;
8 use rustc_hir::{
9 self as hir, def, BinOpKind, BindingAnnotation, Body, Expr, ExprKind, FnDecl, HirId, Mutability, PatKind, Stmt,
10 StmtKind, TyKind, UnOp,
11 };
12 use rustc_lint::{LateContext, LateLintPass};
13 use rustc_middle::lint::in_external_macro;
14 use rustc_middle::ty::{self, Ty};
15 use rustc_session::{declare_lint_pass, declare_tool_lint};
16 use rustc_span::hygiene::DesugaringKind;
17 use rustc_span::source_map::{ExpnKind, Span};
18 use rustc_span::symbol::sym;
19
20 use clippy_utils::consts::{constant, Constant};
21 use clippy_utils::sugg::Sugg;
22 use clippy_utils::{
23 expr_path_res, get_item_name, get_parent_expr, in_constant, is_diag_trait_item, is_integer_const, iter_input_pats,
24 last_path_segment, match_any_def_paths, paths, unsext, SpanlessEq,
25 };
26
27 declare_clippy_lint! {
28 /// ### What it does
29 /// Checks for function arguments and let bindings denoted as
30 /// `ref`.
31 ///
32 /// ### Why is this bad?
33 /// The `ref` declaration makes the function take an owned
34 /// value, but turns the argument into a reference (which means that the value
35 /// is destroyed when exiting the function). This adds not much value: either
36 /// take a reference type, or take an owned value and create references in the
37 /// body.
38 ///
39 /// For let bindings, `let x = &foo;` is preferred over `let ref x = foo`. The
40 /// type of `x` is more obvious with the former.
41 ///
42 /// ### Known problems
43 /// If the argument is dereferenced within the function,
44 /// removing the `ref` will lead to errors. This can be fixed by removing the
45 /// dereferences, e.g., changing `*x` to `x` within the function.
46 ///
47 /// ### Example
48 /// ```rust,ignore
49 /// // Bad
50 /// fn foo(ref x: u8) -> bool {
51 /// true
52 /// }
53 ///
54 /// // Good
55 /// fn foo(x: &u8) -> bool {
56 /// true
57 /// }
58 /// ```
59 pub TOPLEVEL_REF_ARG,
60 style,
61 "an entire binding declared as `ref`, in a function argument or a `let` statement"
62 }
63
64 declare_clippy_lint! {
65 /// ### What it does
66 /// Checks for comparisons to NaN.
67 ///
68 /// ### Why is this bad?
69 /// NaN does not compare meaningfully to anything – not
70 /// even itself – so those comparisons are simply wrong.
71 ///
72 /// ### Example
73 /// ```rust
74 /// # let x = 1.0;
75 ///
76 /// // Bad
77 /// if x == f32::NAN { }
78 ///
79 /// // Good
80 /// if x.is_nan() { }
81 /// ```
82 pub CMP_NAN,
83 correctness,
84 "comparisons to `NAN`, which will always return false, probably not intended"
85 }
86
87 declare_clippy_lint! {
88 /// ### What it does
89 /// Checks for (in-)equality comparisons on floating-point
90 /// values (apart from zero), except in functions called `*eq*` (which probably
91 /// implement equality for a type involving floats).
92 ///
93 /// ### Why is this bad?
94 /// Floating point calculations are usually imprecise, so
95 /// asking if two values are *exactly* equal is asking for trouble. For a good
96 /// guide on what to do, see [the floating point
97 /// guide](http://www.floating-point-gui.de/errors/comparison).
98 ///
99 /// ### Example
100 /// ```rust
101 /// let x = 1.2331f64;
102 /// let y = 1.2332f64;
103 ///
104 /// // Bad
105 /// if y == 1.23f64 { }
106 /// if y != x {} // where both are floats
107 ///
108 /// // Good
109 /// let error_margin = f64::EPSILON; // Use an epsilon for comparison
110 /// // Or, if Rust <= 1.42, use `std::f64::EPSILON` constant instead.
111 /// // let error_margin = std::f64::EPSILON;
112 /// if (y - 1.23f64).abs() < error_margin { }
113 /// if (y - x).abs() > error_margin { }
114 /// ```
115 pub FLOAT_CMP,
116 pedantic,
117 "using `==` or `!=` on float values instead of comparing difference with an epsilon"
118 }
119
120 declare_clippy_lint! {
121 /// ### What it does
122 /// Checks for conversions to owned values just for the sake
123 /// of a comparison.
124 ///
125 /// ### Why is this bad?
126 /// The comparison can operate on a reference, so creating
127 /// an owned value effectively throws it away directly afterwards, which is
128 /// needlessly consuming code and heap space.
129 ///
130 /// ### Example
131 /// ```rust
132 /// # let x = "foo";
133 /// # let y = String::from("foo");
134 /// if x.to_owned() == y {}
135 /// ```
136 /// Could be written as
137 /// ```rust
138 /// # let x = "foo";
139 /// # let y = String::from("foo");
140 /// if x == y {}
141 /// ```
142 pub CMP_OWNED,
143 perf,
144 "creating owned instances for comparing with others, e.g., `x == \"foo\".to_string()`"
145 }
146
147 declare_clippy_lint! {
148 /// ### What it does
149 /// Checks for getting the remainder of a division by one or minus
150 /// one.
151 ///
152 /// ### Why is this bad?
153 /// The result for a divisor of one can only ever be zero; for
154 /// minus one it can cause panic/overflow (if the left operand is the minimal value of
155 /// the respective integer type) or results in zero. No one will write such code
156 /// deliberately, unless trying to win an Underhanded Rust Contest. Even for that
157 /// contest, it's probably a bad idea. Use something more underhanded.
158 ///
159 /// ### Example
160 /// ```rust
161 /// # let x = 1;
162 /// let a = x % 1;
163 /// let a = x % -1;
164 /// ```
165 pub MODULO_ONE,
166 correctness,
167 "taking a number modulo +/-1, which can either panic/overflow or always returns 0"
168 }
169
170 declare_clippy_lint! {
171 /// ### What it does
172 /// Checks for the use of bindings with a single leading
173 /// underscore.
174 ///
175 /// ### Why is this bad?
176 /// A single leading underscore is usually used to indicate
177 /// that a binding will not be used. Using such a binding breaks this
178 /// expectation.
179 ///
180 /// ### Known problems
181 /// The lint does not work properly with desugaring and
182 /// macro, it has been allowed in the mean time.
183 ///
184 /// ### Example
185 /// ```rust
186 /// let _x = 0;
187 /// let y = _x + 1; // Here we are using `_x`, even though it has a leading
188 /// // underscore. We should rename `_x` to `x`
189 /// ```
190 pub USED_UNDERSCORE_BINDING,
191 pedantic,
192 "using a binding which is prefixed with an underscore"
193 }
194
195 declare_clippy_lint! {
196 /// ### What it does
197 /// Checks for the use of short circuit boolean conditions as
198 /// a
199 /// statement.
200 ///
201 /// ### Why is this bad?
202 /// Using a short circuit boolean condition as a statement
203 /// may hide the fact that the second part is executed or not depending on the
204 /// outcome of the first part.
205 ///
206 /// ### Example
207 /// ```rust,ignore
208 /// f() && g(); // We should write `if f() { g(); }`.
209 /// ```
210 pub SHORT_CIRCUIT_STATEMENT,
211 complexity,
212 "using a short circuit boolean condition as a statement"
213 }
214
215 declare_clippy_lint! {
216 /// ### What it does
217 /// Catch casts from `0` to some pointer type
218 ///
219 /// ### Why is this bad?
220 /// This generally means `null` and is better expressed as
221 /// {`std`, `core`}`::ptr::`{`null`, `null_mut`}.
222 ///
223 /// ### Example
224 /// ```rust
225 /// // Bad
226 /// let a = 0 as *const u32;
227 ///
228 /// // Good
229 /// let a = std::ptr::null::<u32>();
230 /// ```
231 pub ZERO_PTR,
232 style,
233 "using `0 as *{const, mut} T`"
234 }
235
236 declare_clippy_lint! {
237 /// ### What it does
238 /// Checks for (in-)equality comparisons on floating-point
239 /// value and constant, except in functions called `*eq*` (which probably
240 /// implement equality for a type involving floats).
241 ///
242 /// ### Why is this bad?
243 /// Floating point calculations are usually imprecise, so
244 /// asking if two values are *exactly* equal is asking for trouble. For a good
245 /// guide on what to do, see [the floating point
246 /// guide](http://www.floating-point-gui.de/errors/comparison).
247 ///
248 /// ### Example
249 /// ```rust
250 /// let x: f64 = 1.0;
251 /// const ONE: f64 = 1.00;
252 ///
253 /// // Bad
254 /// if x == ONE { } // where both are floats
255 ///
256 /// // Good
257 /// let error_margin = f64::EPSILON; // Use an epsilon for comparison
258 /// // Or, if Rust <= 1.42, use `std::f64::EPSILON` constant instead.
259 /// // let error_margin = std::f64::EPSILON;
260 /// if (x - ONE).abs() < error_margin { }
261 /// ```
262 pub FLOAT_CMP_CONST,
263 restriction,
264 "using `==` or `!=` on float constants instead of comparing difference with an epsilon"
265 }
266
267 declare_lint_pass!(MiscLints => [
268 TOPLEVEL_REF_ARG,
269 CMP_NAN,
270 FLOAT_CMP,
271 CMP_OWNED,
272 MODULO_ONE,
273 USED_UNDERSCORE_BINDING,
274 SHORT_CIRCUIT_STATEMENT,
275 ZERO_PTR,
276 FLOAT_CMP_CONST
277 ]);
278
279 impl<'tcx> LateLintPass<'tcx> for MiscLints {
280 fn check_fn(
281 &mut self,
282 cx: &LateContext<'tcx>,
283 k: FnKind<'tcx>,
284 decl: &'tcx FnDecl<'_>,
285 body: &'tcx Body<'_>,
286 span: Span,
287 _: HirId,
288 ) {
289 if let FnKind::Closure = k {
290 // Does not apply to closures
291 return;
292 }
293 if in_external_macro(cx.tcx.sess, span) {
294 return;
295 }
296 for arg in iter_input_pats(decl, body) {
297 if let PatKind::Binding(BindingAnnotation::Ref | BindingAnnotation::RefMut, ..) = arg.pat.kind {
298 span_lint(
299 cx,
300 TOPLEVEL_REF_ARG,
301 arg.pat.span,
302 "`ref` directly on a function argument is ignored. \
303 Consider using a reference type instead",
304 );
305 }
306 }
307 }
308
309 fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
310 if_chain! {
311 if !in_external_macro(cx.tcx.sess, stmt.span);
312 if let StmtKind::Local(local) = stmt.kind;
313 if let PatKind::Binding(an, .., name, None) = local.pat.kind;
314 if let Some(init) = local.init;
315 if an == BindingAnnotation::Ref || an == BindingAnnotation::RefMut;
316 then {
317 // use the macro callsite when the init span (but not the whole local span)
318 // comes from an expansion like `vec![1, 2, 3]` in `let ref _ = vec![1, 2, 3];`
319 let sugg_init = if init.span.from_expansion() && !local.span.from_expansion() {
320 Sugg::hir_with_macro_callsite(cx, init, "..")
321 } else {
322 Sugg::hir(cx, init, "..")
323 };
324 let (mutopt, initref) = if an == BindingAnnotation::RefMut {
325 ("mut ", sugg_init.mut_addr())
326 } else {
327 ("", sugg_init.addr())
328 };
329 let tyopt = if let Some(ty) = local.ty {
330 format!(": &{mutopt}{ty}", mutopt=mutopt, ty=snippet(cx, ty.span, ".."))
331 } else {
332 String::new()
333 };
334 span_lint_hir_and_then(
335 cx,
336 TOPLEVEL_REF_ARG,
337 init.hir_id,
338 local.pat.span,
339 "`ref` on an entire `let` pattern is discouraged, take a reference with `&` instead",
340 |diag| {
341 diag.span_suggestion(
342 stmt.span,
343 "try",
344 format!(
345 "let {name}{tyopt} = {initref};",
346 name=snippet(cx, name.span, ".."),
347 tyopt=tyopt,
348 initref=initref,
349 ),
350 Applicability::MachineApplicable,
351 );
352 }
353 );
354 }
355 };
356 if_chain! {
357 if let StmtKind::Semi(expr) = stmt.kind;
358 if let ExprKind::Binary(ref binop, a, b) = expr.kind;
359 if binop.node == BinOpKind::And || binop.node == BinOpKind::Or;
360 if let Some(sugg) = Sugg::hir_opt(cx, a);
361 then {
362 span_lint_hir_and_then(
363 cx,
364 SHORT_CIRCUIT_STATEMENT,
365 expr.hir_id,
366 stmt.span,
367 "boolean short circuit operator in statement may be clearer using an explicit test",
368 |diag| {
369 let sugg = if binop.node == BinOpKind::Or { !sugg } else { sugg };
370 diag.span_suggestion(
371 stmt.span,
372 "replace it with",
373 format!(
374 "if {} {{ {}; }}",
375 sugg,
376 &snippet(cx, b.span, ".."),
377 ),
378 Applicability::MachineApplicable, // snippet
379 );
380 });
381 }
382 };
383 }
384
385 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
386 match expr.kind {
387 ExprKind::Cast(e, ty) => {
388 check_cast(cx, expr.span, e, ty);
389 return;
390 },
391 ExprKind::Binary(ref cmp, left, right) => {
392 check_binary(cx, expr, cmp, left, right);
393 return;
394 },
395 _ => {},
396 }
397 if in_attributes_expansion(expr) || expr.span.is_desugaring(DesugaringKind::Await) {
398 // Don't lint things expanded by #[derive(...)], etc or `await` desugaring
399 return;
400 }
401 let binding = match expr.kind {
402 ExprKind::Path(ref qpath) if !matches!(qpath, hir::QPath::LangItem(..)) => {
403 let binding = last_path_segment(qpath).ident.as_str();
404 if binding.starts_with('_') &&
405 !binding.starts_with("__") &&
406 binding != "_result" && // FIXME: #944
407 is_used(cx, expr) &&
408 // don't lint if the declaration is in a macro
409 non_macro_local(cx, cx.qpath_res(qpath, expr.hir_id))
410 {
411 Some(binding)
412 } else {
413 None
414 }
415 },
416 ExprKind::Field(_, ident) => {
417 let name = ident.as_str();
418 if name.starts_with('_') && !name.starts_with("__") {
419 Some(name)
420 } else {
421 None
422 }
423 },
424 _ => None,
425 };
426 if let Some(binding) = binding {
427 span_lint(
428 cx,
429 USED_UNDERSCORE_BINDING,
430 expr.span,
431 &format!(
432 "used binding `{}` which is prefixed with an underscore. A leading \
433 underscore signals that a binding will not be used",
434 binding
435 ),
436 );
437 }
438 }
439 }
440
441 fn get_lint_and_message(
442 is_comparing_constants: bool,
443 is_comparing_arrays: bool,
444 ) -> (&'static rustc_lint::Lint, &'static str) {
445 if is_comparing_constants {
446 (
447 FLOAT_CMP_CONST,
448 if is_comparing_arrays {
449 "strict comparison of `f32` or `f64` constant arrays"
450 } else {
451 "strict comparison of `f32` or `f64` constant"
452 },
453 )
454 } else {
455 (
456 FLOAT_CMP,
457 if is_comparing_arrays {
458 "strict comparison of `f32` or `f64` arrays"
459 } else {
460 "strict comparison of `f32` or `f64`"
461 },
462 )
463 }
464 }
465
466 fn check_nan(cx: &LateContext<'_>, expr: &Expr<'_>, cmp_expr: &Expr<'_>) {
467 if_chain! {
468 if !in_constant(cx, cmp_expr.hir_id);
469 if let Some((value, _)) = constant(cx, cx.typeck_results(), expr);
470 if match value {
471 Constant::F32(num) => num.is_nan(),
472 Constant::F64(num) => num.is_nan(),
473 _ => false,
474 };
475 then {
476 span_lint(
477 cx,
478 CMP_NAN,
479 cmp_expr.span,
480 "doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead",
481 );
482 }
483 }
484 }
485
486 fn is_named_constant<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> bool {
487 if let Some((_, res)) = constant(cx, cx.typeck_results(), expr) {
488 res
489 } else {
490 false
491 }
492 }
493
494 fn is_allowed<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> bool {
495 match constant(cx, cx.typeck_results(), expr) {
496 Some((Constant::F32(f), _)) => f == 0.0 || f.is_infinite(),
497 Some((Constant::F64(f), _)) => f == 0.0 || f.is_infinite(),
498 Some((Constant::Vec(vec), _)) => vec.iter().all(|f| match f {
499 Constant::F32(f) => *f == 0.0 || (*f).is_infinite(),
500 Constant::F64(f) => *f == 0.0 || (*f).is_infinite(),
501 _ => false,
502 }),
503 _ => false,
504 }
505 }
506
507 // Return true if `expr` is the result of `signum()` invoked on a float value.
508 fn is_signum(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
509 // The negation of a signum is still a signum
510 if let ExprKind::Unary(UnOp::Neg, child_expr) = expr.kind {
511 return is_signum(cx, child_expr);
512 }
513
514 if_chain! {
515 if let ExprKind::MethodCall(method_name, _, [ref self_arg, ..], _) = expr.kind;
516 if sym!(signum) == method_name.ident.name;
517 // Check that the receiver of the signum() is a float (expressions[0] is the receiver of
518 // the method call)
519 then {
520 return is_float(cx, self_arg);
521 }
522 }
523 false
524 }
525
526 fn is_float(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
527 let value = &cx.typeck_results().expr_ty(expr).peel_refs().kind();
528
529 if let ty::Array(arr_ty, _) = value {
530 return matches!(arr_ty.kind(), ty::Float(_));
531 };
532
533 matches!(value, ty::Float(_))
534 }
535
536 fn is_array(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
537 matches!(&cx.typeck_results().expr_ty(expr).peel_refs().kind(), ty::Array(_, _))
538 }
539
540 fn check_to_owned(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left: bool) {
541 #[derive(Default)]
542 struct EqImpl {
543 ty_eq_other: bool,
544 other_eq_ty: bool,
545 }
546
547 impl EqImpl {
548 fn is_implemented(&self) -> bool {
549 self.ty_eq_other || self.other_eq_ty
550 }
551 }
552
553 fn symmetric_partial_eq<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, other: Ty<'tcx>) -> Option<EqImpl> {
554 cx.tcx.lang_items().eq_trait().map(|def_id| EqImpl {
555 ty_eq_other: implements_trait(cx, ty, def_id, &[other.into()]),
556 other_eq_ty: implements_trait(cx, other, def_id, &[ty.into()]),
557 })
558 }
559
560 let (arg_ty, snip) = match expr.kind {
561 ExprKind::MethodCall(.., args, _) if args.len() == 1 => {
562 if_chain!(
563 if let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
564 if is_diag_trait_item(cx, expr_def_id, sym::ToString)
565 || is_diag_trait_item(cx, expr_def_id, sym::ToOwned);
566 then {
567 (cx.typeck_results().expr_ty(&args[0]), snippet(cx, args[0].span, ".."))
568 } else {
569 return;
570 }
571 )
572 },
573 ExprKind::Call(path, [arg]) => {
574 if expr_path_res(cx, path)
575 .opt_def_id()
576 .and_then(|id| match_any_def_paths(cx, id, &[&paths::FROM_STR_METHOD, &paths::FROM_FROM]))
577 .is_some()
578 {
579 (cx.typeck_results().expr_ty(arg), snippet(cx, arg.span, ".."))
580 } else {
581 return;
582 }
583 },
584 _ => return,
585 };
586
587 let other_ty = cx.typeck_results().expr_ty(other);
588
589 let without_deref = symmetric_partial_eq(cx, arg_ty, other_ty).unwrap_or_default();
590 let with_deref = arg_ty
591 .builtin_deref(true)
592 .and_then(|tam| symmetric_partial_eq(cx, tam.ty, other_ty))
593 .unwrap_or_default();
594
595 if !with_deref.is_implemented() && !without_deref.is_implemented() {
596 return;
597 }
598
599 let other_gets_derefed = matches!(other.kind, ExprKind::Unary(UnOp::Deref, _));
600
601 let lint_span = if other_gets_derefed {
602 expr.span.to(other.span)
603 } else {
604 expr.span
605 };
606
607 span_lint_and_then(
608 cx,
609 CMP_OWNED,
610 lint_span,
611 "this creates an owned instance just for comparison",
612 |diag| {
613 // This also catches `PartialEq` implementations that call `to_owned`.
614 if other_gets_derefed {
615 diag.span_label(lint_span, "try implementing the comparison without allocating");
616 return;
617 }
618
619 let expr_snip;
620 let eq_impl;
621 if with_deref.is_implemented() {
622 expr_snip = format!("*{}", snip);
623 eq_impl = with_deref;
624 } else {
625 expr_snip = snip.to_string();
626 eq_impl = without_deref;
627 };
628
629 let span;
630 let hint;
631 if (eq_impl.ty_eq_other && left) || (eq_impl.other_eq_ty && !left) {
632 span = expr.span;
633 hint = expr_snip;
634 } else {
635 span = expr.span.to(other.span);
636 if eq_impl.ty_eq_other {
637 hint = format!("{} == {}", expr_snip, snippet(cx, other.span, ".."));
638 } else {
639 hint = format!("{} == {}", snippet(cx, other.span, ".."), expr_snip);
640 }
641 }
642
643 diag.span_suggestion(
644 span,
645 "try",
646 hint,
647 Applicability::MachineApplicable, // snippet
648 );
649 },
650 );
651 }
652
653 /// Heuristic to see if an expression is used. Should be compatible with
654 /// `unused_variables`'s idea
655 /// of what it means for an expression to be "used".
656 fn is_used(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
657 get_parent_expr(cx, expr).map_or(true, |parent| match parent.kind {
658 ExprKind::Assign(_, rhs, _) | ExprKind::AssignOp(_, _, rhs) => SpanlessEq::new(cx).eq_expr(rhs, expr),
659 _ => is_used(cx, parent),
660 })
661 }
662
663 /// Tests whether an expression is in a macro expansion (e.g., something
664 /// generated by `#[derive(...)]` or the like).
665 fn in_attributes_expansion(expr: &Expr<'_>) -> bool {
666 use rustc_span::hygiene::MacroKind;
667 if expr.span.from_expansion() {
668 let data = expr.span.ctxt().outer_expn_data();
669 matches!(data.kind, ExpnKind::Macro(MacroKind::Attr, _))
670 } else {
671 false
672 }
673 }
674
675 /// Tests whether `res` is a variable defined outside a macro.
676 fn non_macro_local(cx: &LateContext<'_>, res: def::Res) -> bool {
677 if let def::Res::Local(id) = res {
678 !cx.tcx.hir().span(id).from_expansion()
679 } else {
680 false
681 }
682 }
683
684 fn check_cast(cx: &LateContext<'_>, span: Span, e: &Expr<'_>, ty: &hir::Ty<'_>) {
685 if_chain! {
686 if let TyKind::Ptr(ref mut_ty) = ty.kind;
687 if let ExprKind::Lit(ref lit) = e.kind;
688 if let LitKind::Int(0, _) = lit.node;
689 if !in_constant(cx, e.hir_id);
690 then {
691 let (msg, sugg_fn) = match mut_ty.mutbl {
692 Mutability::Mut => ("`0 as *mut _` detected", "std::ptr::null_mut"),
693 Mutability::Not => ("`0 as *const _` detected", "std::ptr::null"),
694 };
695
696 let (sugg, appl) = if let TyKind::Infer = mut_ty.ty.kind {
697 (format!("{}()", sugg_fn), Applicability::MachineApplicable)
698 } else if let Some(mut_ty_snip) = snippet_opt(cx, mut_ty.ty.span) {
699 (format!("{}::<{}>()", sugg_fn, mut_ty_snip), Applicability::MachineApplicable)
700 } else {
701 // `MaybeIncorrect` as type inference may not work with the suggested code
702 (format!("{}()", sugg_fn), Applicability::MaybeIncorrect)
703 };
704 span_lint_and_sugg(cx, ZERO_PTR, span, msg, "try", sugg, appl);
705 }
706 }
707 }
708
709 fn check_binary(
710 cx: &LateContext<'a>,
711 expr: &Expr<'_>,
712 cmp: &rustc_span::source_map::Spanned<rustc_hir::BinOpKind>,
713 left: &'a Expr<'_>,
714 right: &'a Expr<'_>,
715 ) {
716 let op = cmp.node;
717 if op.is_comparison() {
718 check_nan(cx, left, expr);
719 check_nan(cx, right, expr);
720 check_to_owned(cx, left, right, true);
721 check_to_owned(cx, right, left, false);
722 }
723 if (op == BinOpKind::Eq || op == BinOpKind::Ne) && (is_float(cx, left) || is_float(cx, right)) {
724 if is_allowed(cx, left) || is_allowed(cx, right) {
725 return;
726 }
727
728 // Allow comparing the results of signum()
729 if is_signum(cx, left) && is_signum(cx, right) {
730 return;
731 }
732
733 if let Some(name) = get_item_name(cx, expr) {
734 let name = name.as_str();
735 if name == "eq" || name == "ne" || name == "is_nan" || name.starts_with("eq_") || name.ends_with("_eq") {
736 return;
737 }
738 }
739 let is_comparing_arrays = is_array(cx, left) || is_array(cx, right);
740 let (lint, msg) = get_lint_and_message(
741 is_named_constant(cx, left) || is_named_constant(cx, right),
742 is_comparing_arrays,
743 );
744 span_lint_and_then(cx, lint, expr.span, msg, |diag| {
745 let lhs = Sugg::hir(cx, left, "..");
746 let rhs = Sugg::hir(cx, right, "..");
747
748 if !is_comparing_arrays {
749 diag.span_suggestion(
750 expr.span,
751 "consider comparing them within some margin of error",
752 format!(
753 "({}).abs() {} error_margin",
754 lhs - rhs,
755 if op == BinOpKind::Eq { '<' } else { '>' }
756 ),
757 Applicability::HasPlaceholders, // snippet
758 );
759 }
760 diag.note("`f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`");
761 });
762 } else if op == BinOpKind::Rem {
763 if is_integer_const(cx, right, 1) {
764 span_lint(cx, MODULO_ONE, expr.span, "any number modulo 1 will be 0");
765 }
766
767 if let ty::Int(ity) = cx.typeck_results().expr_ty(right).kind() {
768 if is_integer_const(cx, right, unsext(cx.tcx, -1, *ity)) {
769 span_lint(
770 cx,
771 MODULO_ONE,
772 expr.span,
773 "any number modulo -1 will panic/overflow or result in 0",
774 );
775 }
776 };
777 }
778 }