Skip to content

Commit fc97c4c

Browse files
committed
Auto merge of #10023 - Jarcho:issue_10017, r=xFrednet
Fix `zero_ptr` suggestion for `no_std` crates fixes #10017 --- changelog: [`zero_ptr`]: Now suggests `core::` paths for `no_std` crates [#10023](#10023)
2 parents cb8df45 + 6ba2cda commit fc97c4c

File tree

5 files changed

+111
-25
lines changed

5 files changed

+111
-25
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
538538
store.register_late_pass(|_| Box::new(needless_bool::NeedlessBool));
539539
store.register_late_pass(|_| Box::new(needless_bool::BoolComparison));
540540
store.register_late_pass(|_| Box::new(needless_for_each::NeedlessForEach));
541-
store.register_late_pass(|_| Box::new(misc::MiscLints));
541+
store.register_late_pass(|_| Box::<misc::LintPass>::default());
542542
store.register_late_pass(|_| Box::new(eta_reduction::EtaReduction));
543543
store.register_late_pass(|_| Box::new(mut_mut::MutMut));
544544
store.register_late_pass(|_| Box::new(mut_reference::UnnecessaryMutPassed));

clippy_lints/src/misc.rs

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ use rustc_hir::{
99
};
1010
use rustc_lint::{LateContext, LateLintPass};
1111
use rustc_middle::lint::in_external_macro;
12-
use rustc_session::{declare_lint_pass, declare_tool_lint};
12+
use rustc_session::{declare_tool_lint, impl_lint_pass};
1313
use rustc_span::hygiene::DesugaringKind;
1414
use rustc_span::source_map::{ExpnKind, Span};
1515

1616
use clippy_utils::sugg::Sugg;
17-
use clippy_utils::{get_parent_expr, in_constant, is_integer_literal, iter_input_pats, last_path_segment, SpanlessEq};
17+
use clippy_utils::{
18+
get_parent_expr, in_constant, is_integer_literal, is_no_std_crate, iter_input_pats, last_path_segment, SpanlessEq,
19+
};
1820

1921
declare_clippy_lint! {
2022
/// ### What it does
@@ -120,14 +122,28 @@ declare_clippy_lint! {
120122
"using `0 as *{const, mut} T`"
121123
}
122124

123-
declare_lint_pass!(MiscLints => [
125+
pub struct LintPass {
126+
std_or_core: &'static str,
127+
}
128+
impl Default for LintPass {
129+
fn default() -> Self {
130+
Self { std_or_core: "std" }
131+
}
132+
}
133+
impl_lint_pass!(LintPass => [
124134
TOPLEVEL_REF_ARG,
125135
USED_UNDERSCORE_BINDING,
126136
SHORT_CIRCUIT_STATEMENT,
127137
ZERO_PTR,
128138
]);
129139

130-
impl<'tcx> LateLintPass<'tcx> for MiscLints {
140+
impl<'tcx> LateLintPass<'tcx> for LintPass {
141+
fn check_crate(&mut self, cx: &LateContext<'_>) {
142+
if is_no_std_crate(cx) {
143+
self.std_or_core = "core";
144+
}
145+
}
146+
131147
fn check_fn(
132148
&mut self,
133149
cx: &LateContext<'tcx>,
@@ -231,7 +247,7 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
231247

232248
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
233249
if let ExprKind::Cast(e, ty) = expr.kind {
234-
check_cast(cx, expr.span, e, ty);
250+
self.check_cast(cx, expr.span, e, ty);
235251
return;
236252
}
237253
if in_attributes_expansion(expr) || expr.span.is_desugaring(DesugaringKind::Await) {
@@ -310,26 +326,28 @@ fn non_macro_local(cx: &LateContext<'_>, res: def::Res) -> bool {
310326
}
311327
}
312328

313-
fn check_cast(cx: &LateContext<'_>, span: Span, e: &Expr<'_>, ty: &hir::Ty<'_>) {
314-
if_chain! {
315-
if let TyKind::Ptr(ref mut_ty) = ty.kind;
316-
if is_integer_literal(e, 0);
317-
if !in_constant(cx, e.hir_id);
318-
then {
319-
let (msg, sugg_fn) = match mut_ty.mutbl {
320-
Mutability::Mut => ("`0 as *mut _` detected", "std::ptr::null_mut"),
321-
Mutability::Not => ("`0 as *const _` detected", "std::ptr::null"),
322-
};
329+
impl LintPass {
330+
fn check_cast(&self, cx: &LateContext<'_>, span: Span, e: &Expr<'_>, ty: &hir::Ty<'_>) {
331+
if_chain! {
332+
if let TyKind::Ptr(ref mut_ty) = ty.kind;
333+
if is_integer_literal(e, 0);
334+
if !in_constant(cx, e.hir_id);
335+
then {
336+
let (msg, sugg_fn) = match mut_ty.mutbl {
337+
Mutability::Mut => ("`0 as *mut _` detected", "ptr::null_mut"),
338+
Mutability::Not => ("`0 as *const _` detected", "ptr::null"),
339+
};
323340

324-
let (sugg, appl) = if let TyKind::Infer = mut_ty.ty.kind {
325-
(format!("{sugg_fn}()"), Applicability::MachineApplicable)
326-
} else if let Some(mut_ty_snip) = snippet_opt(cx, mut_ty.ty.span) {
327-
(format!("{sugg_fn}::<{mut_ty_snip}>()"), Applicability::MachineApplicable)
328-
} else {
329-
// `MaybeIncorrect` as type inference may not work with the suggested code
330-
(format!("{sugg_fn}()"), Applicability::MaybeIncorrect)
331-
};
332-
span_lint_and_sugg(cx, ZERO_PTR, span, msg, "try", sugg, appl);
341+
let (sugg, appl) = if let TyKind::Infer = mut_ty.ty.kind {
342+
(format!("{}::{sugg_fn}()", self.std_or_core), Applicability::MachineApplicable)
343+
} else if let Some(mut_ty_snip) = snippet_opt(cx, mut_ty.ty.span) {
344+
(format!("{}::{sugg_fn}::<{mut_ty_snip}>()", self.std_or_core), Applicability::MachineApplicable)
345+
} else {
346+
// `MaybeIncorrect` as type inference may not work with the suggested code
347+
(format!("{}::{sugg_fn}()", self.std_or_core), Applicability::MaybeIncorrect)
348+
};
349+
span_lint_and_sugg(cx, ZERO_PTR, span, msg, "try", sugg, appl);
350+
}
333351
}
334352
}
335353
}

tests/ui/zero_ptr_no_std.fixed

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// run-rustfix
2+
3+
#![feature(lang_items, start, libc)]
4+
#![no_std]
5+
#![deny(clippy::zero_ptr)]
6+
7+
#[start]
8+
fn main(_argc: isize, _argv: *const *const u8) -> isize {
9+
let _ = core::ptr::null::<usize>();
10+
let _ = core::ptr::null_mut::<f64>();
11+
let _: *const u8 = core::ptr::null();
12+
0
13+
}
14+
15+
#[panic_handler]
16+
fn panic(_info: &core::panic::PanicInfo) -> ! {
17+
loop {}
18+
}
19+
20+
#[lang = "eh_personality"]
21+
extern "C" fn eh_personality() {}

tests/ui/zero_ptr_no_std.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// run-rustfix
2+
3+
#![feature(lang_items, start, libc)]
4+
#![no_std]
5+
#![deny(clippy::zero_ptr)]
6+
7+
#[start]
8+
fn main(_argc: isize, _argv: *const *const u8) -> isize {
9+
let _ = 0 as *const usize;
10+
let _ = 0 as *mut f64;
11+
let _: *const u8 = 0 as *const _;
12+
0
13+
}
14+
15+
#[panic_handler]
16+
fn panic(_info: &core::panic::PanicInfo) -> ! {
17+
loop {}
18+
}
19+
20+
#[lang = "eh_personality"]
21+
extern "C" fn eh_personality() {}

tests/ui/zero_ptr_no_std.stderr

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: `0 as *const _` detected
2+
--> $DIR/zero_ptr_no_std.rs:9:13
3+
|
4+
LL | let _ = 0 as *const usize;
5+
| ^^^^^^^^^^^^^^^^^ help: try: `core::ptr::null::<usize>()`
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/zero_ptr_no_std.rs:5:9
9+
|
10+
LL | #![deny(clippy::zero_ptr)]
11+
| ^^^^^^^^^^^^^^^^
12+
13+
error: `0 as *mut _` detected
14+
--> $DIR/zero_ptr_no_std.rs:10:13
15+
|
16+
LL | let _ = 0 as *mut f64;
17+
| ^^^^^^^^^^^^^ help: try: `core::ptr::null_mut::<f64>()`
18+
19+
error: `0 as *const _` detected
20+
--> $DIR/zero_ptr_no_std.rs:11:24
21+
|
22+
LL | let _: *const u8 = 0 as *const _;
23+
| ^^^^^^^^^^^^^ help: try: `core::ptr::null()`
24+
25+
error: aborting due to 3 previous errors
26+

0 commit comments

Comments
 (0)