-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New lint
useless-nonzero-new_unchecked
(#13993)
changelog: [`useless-nonzero-new_unchecked`]: new lint Close #13991 ### What it does Checks for `NonZero*::new_unchecked(<literal>)` being used in a `const` context. ### Why is this bad? Using `NonZero*::new_unchecked()` is an `unsafe` function and requires an `unsafe` context. When used with an integer literal in a `const` context, `NonZero*::new().unwrap()` will provide the same result with identical runtime performances while not requiring `unsafe`. ### Example ```no_run const PLAYERS: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(3) }; ``` Use instead: ```no_run const PLAYERS: NonZeroUsize = NonZeroUsize::new(3).unwrap(); ```
- Loading branch information
Showing
8 changed files
with
233 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then}; | ||
use clippy_utils::is_inside_always_const_context; | ||
use clippy_utils::msrvs::{self, Msrv}; | ||
use clippy_utils::source::snippet_with_applicability; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Block, BlockCheckMode, Expr, ExprKind, Node, QPath, UnsafeSource}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::sym; | ||
|
||
use super::USELESS_NONZERO_NEW_UNCHECKED; | ||
|
||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, func: &Expr<'tcx>, args: &[Expr<'_>], msrv: &Msrv) { | ||
if msrv.meets(msrvs::CONST_UNWRAP) | ||
&& let ExprKind::Path(QPath::TypeRelative(ty, segment)) = func.kind | ||
&& segment.ident.name == sym::new_unchecked | ||
&& let [init_arg] = args | ||
&& is_inside_always_const_context(cx.tcx, expr.hir_id) | ||
&& is_type_diagnostic_item(cx, cx.typeck_results().node_type(ty.hir_id), sym::NonZero) | ||
{ | ||
let mut app = Applicability::MachineApplicable; | ||
let ty_str = snippet_with_applicability(cx, ty.span, "_", &mut app); | ||
let msg = format!("`{ty_str}::new()` and `Option::unwrap()` can be safely used in a `const` context"); | ||
let sugg = format!( | ||
"{ty_str}::new({}).unwrap()", | ||
snippet_with_applicability(cx, init_arg.span, "_", &mut app) | ||
); | ||
|
||
if let Node::Block(Block { | ||
stmts: [], | ||
span: block_span, | ||
rules: BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided), | ||
.. | ||
}) = cx.tcx.parent_hir_node(expr.hir_id) | ||
{ | ||
if !block_span.from_expansion() { | ||
// The expression is the only component of an `unsafe` block. Propose | ||
// to replace the block altogether. | ||
span_lint_and_sugg( | ||
cx, | ||
USELESS_NONZERO_NEW_UNCHECKED, | ||
*block_span, | ||
msg, | ||
"use instead", | ||
sugg, | ||
app, | ||
); | ||
} | ||
} else { | ||
// The expression is enclosed in a larger `unsafe` context. Indicate that | ||
// this may no longer be needed for the fixed expression. | ||
span_lint_and_then(cx, USELESS_NONZERO_NEW_UNCHECKED, expr.span, msg, |diagnostic| { | ||
diagnostic | ||
.span_suggestion(expr.span, "use instead", sugg, app) | ||
.note("the fixed expression does not require an `unsafe` context"); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#![warn(clippy::useless_nonzero_new_unchecked)] | ||
|
||
use std::num::{NonZero, NonZeroUsize}; | ||
|
||
#[clippy::msrv = "1.83"] | ||
const fn func() -> NonZeroUsize { | ||
const { NonZeroUsize::new(3).unwrap() } | ||
//~^ ERROR: `Option::unwrap()` can be safely used in a `const` context | ||
} | ||
|
||
#[clippy::msrv = "1.82"] | ||
const fn func_older() -> NonZeroUsize { | ||
unsafe { NonZeroUsize::new_unchecked(3) } | ||
} | ||
|
||
const fn func_performance_hit_if_linted() -> NonZeroUsize { | ||
unsafe { NonZeroUsize::new_unchecked(3) } | ||
} | ||
|
||
const fn func_may_panic_at_run_time_if_linted(x: usize) -> NonZeroUsize { | ||
unsafe { NonZeroUsize::new_unchecked(x) } | ||
} | ||
|
||
macro_rules! uns { | ||
($expr:expr) => { | ||
unsafe { $expr } | ||
}; | ||
} | ||
|
||
macro_rules! nzu { | ||
() => { | ||
NonZeroUsize::new_unchecked(1) | ||
}; | ||
} | ||
|
||
fn main() { | ||
const _A: NonZeroUsize = NonZeroUsize::new(3).unwrap(); | ||
//~^ ERROR: `Option::unwrap()` can be safely used in a `const` context | ||
|
||
static _B: NonZero<u8> = NonZero::<u8>::new(42).unwrap(); | ||
//~^ ERROR: `Option::unwrap()` can be safely used in a `const` context | ||
|
||
const _C: usize = unsafe { NonZeroUsize::new(3).unwrap().get() }; | ||
//~^ ERROR: `Option::unwrap()` can be safely used in a `const` context | ||
|
||
const AUX: usize = 3; | ||
const _D: NonZeroUsize = NonZeroUsize::new(AUX).unwrap(); | ||
//~^ ERROR: `Option::unwrap()` can be safely used in a `const` context | ||
|
||
const _X: NonZeroUsize = uns!(NonZeroUsize::new_unchecked(3)); | ||
const _Y: NonZeroUsize = unsafe { nzu!() }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#![warn(clippy::useless_nonzero_new_unchecked)] | ||
|
||
use std::num::{NonZero, NonZeroUsize}; | ||
|
||
#[clippy::msrv = "1.83"] | ||
const fn func() -> NonZeroUsize { | ||
const { unsafe { NonZeroUsize::new_unchecked(3) } } | ||
//~^ ERROR: `Option::unwrap()` can be safely used in a `const` context | ||
} | ||
|
||
#[clippy::msrv = "1.82"] | ||
const fn func_older() -> NonZeroUsize { | ||
unsafe { NonZeroUsize::new_unchecked(3) } | ||
} | ||
|
||
const fn func_performance_hit_if_linted() -> NonZeroUsize { | ||
unsafe { NonZeroUsize::new_unchecked(3) } | ||
} | ||
|
||
const fn func_may_panic_at_run_time_if_linted(x: usize) -> NonZeroUsize { | ||
unsafe { NonZeroUsize::new_unchecked(x) } | ||
} | ||
|
||
macro_rules! uns { | ||
($expr:expr) => { | ||
unsafe { $expr } | ||
}; | ||
} | ||
|
||
macro_rules! nzu { | ||
() => { | ||
NonZeroUsize::new_unchecked(1) | ||
}; | ||
} | ||
|
||
fn main() { | ||
const _A: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(3) }; | ||
//~^ ERROR: `Option::unwrap()` can be safely used in a `const` context | ||
|
||
static _B: NonZero<u8> = unsafe { NonZero::<u8>::new_unchecked(42) }; | ||
//~^ ERROR: `Option::unwrap()` can be safely used in a `const` context | ||
|
||
const _C: usize = unsafe { NonZeroUsize::new_unchecked(3).get() }; | ||
//~^ ERROR: `Option::unwrap()` can be safely used in a `const` context | ||
|
||
const AUX: usize = 3; | ||
const _D: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(AUX) }; | ||
//~^ ERROR: `Option::unwrap()` can be safely used in a `const` context | ||
|
||
const _X: NonZeroUsize = uns!(NonZeroUsize::new_unchecked(3)); | ||
const _Y: NonZeroUsize = unsafe { nzu!() }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
error: `NonZeroUsize::new()` and `Option::unwrap()` can be safely used in a `const` context | ||
--> tests/ui/useless_nonzero_new_unchecked.rs:7:13 | ||
| | ||
LL | const { unsafe { NonZeroUsize::new_unchecked(3) } } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use instead: `NonZeroUsize::new(3).unwrap()` | ||
| | ||
= note: `-D clippy::useless-nonzero-new-unchecked` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::useless_nonzero_new_unchecked)]` | ||
|
||
error: `NonZeroUsize::new()` and `Option::unwrap()` can be safely used in a `const` context | ||
--> tests/ui/useless_nonzero_new_unchecked.rs:37:30 | ||
| | ||
LL | const _A: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(3) }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use instead: `NonZeroUsize::new(3).unwrap()` | ||
|
||
error: `NonZero::<u8>::new()` and `Option::unwrap()` can be safely used in a `const` context | ||
--> tests/ui/useless_nonzero_new_unchecked.rs:40:30 | ||
| | ||
LL | static _B: NonZero<u8> = unsafe { NonZero::<u8>::new_unchecked(42) }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use instead: `NonZero::<u8>::new(42).unwrap()` | ||
|
||
error: `NonZeroUsize::new()` and `Option::unwrap()` can be safely used in a `const` context | ||
--> tests/ui/useless_nonzero_new_unchecked.rs:43:32 | ||
| | ||
LL | const _C: usize = unsafe { NonZeroUsize::new_unchecked(3).get() }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use instead: `NonZeroUsize::new(3).unwrap()` | ||
| | ||
= note: the fixed expression does not require an `unsafe` context | ||
|
||
error: `NonZeroUsize::new()` and `Option::unwrap()` can be safely used in a `const` context | ||
--> tests/ui/useless_nonzero_new_unchecked.rs:47:30 | ||
| | ||
LL | const _D: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(AUX) }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use instead: `NonZeroUsize::new(AUX).unwrap()` | ||
|
||
error: aborting due to 5 previous errors | ||
|