Skip to content

Lint zero_ptr in const contexts #10212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rustc_span::source_map::{ExpnKind, Span};

use clippy_utils::sugg::Sugg;
use clippy_utils::{
get_parent_expr, in_constant, is_integer_literal, is_no_std_crate, iter_input_pats, last_path_segment, SpanlessEq,
get_parent_expr, is_integer_literal, is_no_std_crate, iter_input_pats, last_path_segment, SpanlessEq,
};

declare_clippy_lint! {
Expand Down Expand Up @@ -328,26 +328,21 @@ fn non_macro_local(cx: &LateContext<'_>, res: def::Res) -> bool {

impl LintPass {
fn check_cast(&self, cx: &LateContext<'_>, span: Span, e: &Expr<'_>, ty: &hir::Ty<'_>) {
if_chain! {
if let TyKind::Ptr(ref mut_ty) = ty.kind;
if is_integer_literal(e, 0);
if !in_constant(cx, e.hir_id);
then {
let (msg, sugg_fn) = match mut_ty.mutbl {
Mutability::Mut => ("`0 as *mut _` detected", "ptr::null_mut"),
Mutability::Not => ("`0 as *const _` detected", "ptr::null"),
};
if let TyKind::Ptr(ref mut_ty) = ty.kind && is_integer_literal(e, 0) {
let (msg, sugg_fn) = match mut_ty.mutbl {
Mutability::Mut => ("`0 as *mut _` detected", "ptr::null_mut"),
Mutability::Not => ("`0 as *const _` detected", "ptr::null"),
};

let (sugg, appl) = if let TyKind::Infer = mut_ty.ty.kind {
(format!("{}::{sugg_fn}()", self.std_or_core), Applicability::MachineApplicable)
} else if let Some(mut_ty_snip) = snippet_opt(cx, mut_ty.ty.span) {
(format!("{}::{sugg_fn}::<{mut_ty_snip}>()", self.std_or_core), Applicability::MachineApplicable)
} else {
// `MaybeIncorrect` as type inference may not work with the suggested code
(format!("{}::{sugg_fn}()", self.std_or_core), Applicability::MaybeIncorrect)
};
span_lint_and_sugg(cx, ZERO_PTR, span, msg, "try", sugg, appl);
}
let (sugg, appl) = if let TyKind::Infer = mut_ty.ty.kind {
(format!("{}::{sugg_fn}()", self.std_or_core), Applicability::MachineApplicable)
} else if let Some(mut_ty_snip) = snippet_opt(cx, mut_ty.ty.span) {
(format!("{}::{sugg_fn}::<{mut_ty_snip}>()", self.std_or_core), Applicability::MachineApplicable)
} else {
// `MaybeIncorrect` as type inference may not work with the suggested code
(format!("{}::{sugg_fn}()", self.std_or_core), Applicability::MaybeIncorrect)
};
span_lint_and_sugg(cx, ZERO_PTR, span, msg, "try", sugg, appl);
}
}
}
10 changes: 8 additions & 2 deletions tests/ui/cmp_nan.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#![warn(clippy::cmp_nan)]
#![allow(clippy::float_cmp, clippy::no_effect, clippy::unnecessary_operation)]

const NAN_F32: f32 = f32::NAN;
const NAN_F64: f64 = f64::NAN;

#[warn(clippy::cmp_nan)]
#[allow(clippy::float_cmp, clippy::no_effect, clippy::unnecessary_operation)]
// don't lint these, since `{f32,f64}::is_nan` is not yet const stable
// FIXME: make this lint when `{f32,f64}::is_nan` becomes const stable
const IS_F32_NAN: bool = NAN_F32 == f32::NAN;
const IS_F64_NAN: bool = NAN_F64 == f64::NAN;

fn main() {
let x = 5f32;
x == f32::NAN;
Expand Down
48 changes: 24 additions & 24 deletions tests/ui/cmp_nan.stderr
Original file line number Diff line number Diff line change
@@ -1,145 +1,145 @@
error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:8:5
--> $DIR/cmp_nan.rs:14:5
|
LL | x == f32::NAN;
| ^^^^^^^^^^^^^
|
= note: `-D clippy::cmp-nan` implied by `-D warnings`

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:9:5
--> $DIR/cmp_nan.rs:15:5
|
LL | x != f32::NAN;
| ^^^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:10:5
--> $DIR/cmp_nan.rs:16:5
|
LL | x < f32::NAN;
| ^^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:11:5
--> $DIR/cmp_nan.rs:17:5
|
LL | x > f32::NAN;
| ^^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:12:5
--> $DIR/cmp_nan.rs:18:5
|
LL | x <= f32::NAN;
| ^^^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:13:5
--> $DIR/cmp_nan.rs:19:5
|
LL | x >= f32::NAN;
| ^^^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:14:5
--> $DIR/cmp_nan.rs:20:5
|
LL | x == NAN_F32;
| ^^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:15:5
--> $DIR/cmp_nan.rs:21:5
|
LL | x != NAN_F32;
| ^^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:16:5
--> $DIR/cmp_nan.rs:22:5
|
LL | x < NAN_F32;
| ^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:17:5
--> $DIR/cmp_nan.rs:23:5
|
LL | x > NAN_F32;
| ^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:18:5
--> $DIR/cmp_nan.rs:24:5
|
LL | x <= NAN_F32;
| ^^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:19:5
--> $DIR/cmp_nan.rs:25:5
|
LL | x >= NAN_F32;
| ^^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:22:5
--> $DIR/cmp_nan.rs:28:5
|
LL | y == f64::NAN;
| ^^^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:23:5
--> $DIR/cmp_nan.rs:29:5
|
LL | y != f64::NAN;
| ^^^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:24:5
--> $DIR/cmp_nan.rs:30:5
|
LL | y < f64::NAN;
| ^^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:25:5
--> $DIR/cmp_nan.rs:31:5
|
LL | y > f64::NAN;
| ^^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:26:5
--> $DIR/cmp_nan.rs:32:5
|
LL | y <= f64::NAN;
| ^^^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:27:5
--> $DIR/cmp_nan.rs:33:5
|
LL | y >= f64::NAN;
| ^^^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:28:5
--> $DIR/cmp_nan.rs:34:5
|
LL | y == NAN_F64;
| ^^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:29:5
--> $DIR/cmp_nan.rs:35:5
|
LL | y != NAN_F64;
| ^^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:30:5
--> $DIR/cmp_nan.rs:36:5
|
LL | y < NAN_F64;
| ^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:31:5
--> $DIR/cmp_nan.rs:37:5
|
LL | y > NAN_F64;
| ^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:32:5
--> $DIR/cmp_nan.rs:38:5
|
LL | y <= NAN_F64;
| ^^^^^^^^^^^^

error: doomed comparison with `NAN`, use `{f32,f64}::is_nan()` instead
--> $DIR/cmp_nan.rs:33:5
--> $DIR/cmp_nan.rs:39:5
|
LL | y >= NAN_F64;
| ^^^^^^^^^^^^
Expand Down
34 changes: 0 additions & 34 deletions tests/ui/crashes/mut_mut_macro.rs

This file was deleted.