Skip to content

Commit 1bcaf29

Browse files
Correctly suggest std or core path depending if this is a no_std crate
1 parent 37947ff commit 1bcaf29

File tree

7 files changed

+44
-28
lines changed

7 files changed

+44
-28
lines changed

clippy_lints/src/default_instead_of_iter_empty.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::last_path_segment;
32
use clippy_utils::source::snippet_with_context;
3+
use clippy_utils::{is_no_std_crate, last_path_segment};
44
use rustc_errors::Applicability;
55
use rustc_hir::{def, Expr, ExprKind, GenericArg, QPath, TyKind};
66
use rustc_lint::{LateContext, LateLintPass};
@@ -42,12 +42,17 @@ impl<'tcx> LateLintPass<'tcx> for DefaultIterEmpty {
4242
&& ty.span.ctxt() == ctxt
4343
{
4444
let mut applicability = Applicability::MachineApplicable;
45-
let sugg = make_sugg(cx, ty_path, ctxt, &mut applicability);
45+
let path = if is_no_std_crate(cx) {
46+
"core::iter::empty"
47+
} else {
48+
"std::iter::empty"
49+
};
50+
let sugg = make_sugg(cx, ty_path, ctxt, &mut applicability, path);
4651
span_lint_and_sugg(
4752
cx,
4853
DEFAULT_INSTEAD_OF_ITER_EMPTY,
4954
expr.span,
50-
"`std::iter::empty()` is the more idiomatic way",
55+
&format!("`{path}()` is the more idiomatic way"),
5156
"try",
5257
sugg,
5358
applicability,
@@ -61,6 +66,7 @@ fn make_sugg(
6166
ty_path: &rustc_hir::QPath<'_>,
6267
ctxt: SyntaxContext,
6368
applicability: &mut Applicability,
69+
path: &str,
6470
) -> String {
6571
if let Some(last) = last_path_segment(ty_path).args
6672
&& let Some(iter_ty) = last.args.iter().find_map(|arg| match arg {
@@ -69,10 +75,10 @@ fn make_sugg(
6975
})
7076
{
7177
format!(
72-
"std::iter::empty::<{}>()",
78+
"{path}::<{}>()",
7379
snippet_with_context(cx, iter_ty.span, ctxt, "..", applicability).0
7480
)
7581
} else {
76-
"std::iter::empty()".to_owned()
82+
format!("{path}()")
7783
}
7884
}

clippy_lints/src/mem_replace.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg, span_lin
33
use clippy_utils::source::{snippet, snippet_with_applicability};
44
use clippy_utils::sugg::Sugg;
55
use clippy_utils::ty::is_non_aggregate_primitive_type;
6-
use clippy_utils::{is_default_equivalent, is_res_lang_ctor, path_res, peel_ref_operators};
6+
use clippy_utils::{is_default_equivalent, is_no_std_crate, is_res_lang_ctor, path_res, peel_ref_operators};
77
use rustc_errors::Applicability;
88
use rustc_hir::LangItem::OptionNone;
99
use rustc_hir::{Expr, ExprKind};
@@ -123,6 +123,10 @@ fn check_replace_option_with_none(cx: &LateContext<'_>, dest: &Expr<'_>, expr_sp
123123
);
124124
}
125125

126+
fn get_top_crate(cx: &LateContext<'_>) -> &'static str {
127+
if is_no_std_crate(cx) { "core" } else { "std" }
128+
}
129+
126130
fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'_>, expr_span: Span) {
127131
if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(src.hir_id)
128132
// check if replacement is mem::MaybeUninit::uninit().assume_init()
@@ -136,7 +140,8 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
136140
"replacing with `mem::MaybeUninit::uninit().assume_init()`",
137141
"consider using",
138142
format!(
139-
"std::ptr::read({})",
143+
"{}::ptr::read({})",
144+
get_top_crate(cx),
140145
snippet_with_applicability(cx, dest.span, "", &mut applicability)
141146
),
142147
applicability,
@@ -157,7 +162,8 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
157162
"replacing with `mem::uninitialized()`",
158163
"consider using",
159164
format!(
160-
"std::ptr::read({})",
165+
"{}::ptr::read({})",
166+
get_top_crate(cx),
161167
snippet_with_applicability(cx, dest.span, "", &mut applicability)
162168
),
163169
applicability,
@@ -184,14 +190,17 @@ fn check_replace_with_default(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<
184190
return;
185191
}
186192
if is_default_equivalent(cx, src) && !in_external_macro(cx.tcx.sess, expr_span) {
193+
let top_crate = get_top_crate(cx);
187194
span_lint_and_then(
188195
cx,
189196
MEM_REPLACE_WITH_DEFAULT,
190197
expr_span,
191-
"replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`",
198+
&format!(
199+
"replacing a value of type `T` with `T::default()` is better expressed using `{top_crate}::mem::take`"
200+
),
192201
|diag| {
193202
if !expr_span.from_expansion() {
194-
let suggestion = format!("std::mem::take({})", snippet(cx, dest.span, ""));
203+
let suggestion = format!("{top_crate}::mem::take({})", snippet(cx, dest.span, ""));
195204

196205
diag.span_suggestion(
197206
expr_span,

clippy_lints/src/methods/iter_on_single_or_empty_collections.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, method_name: &str, re
5858
return;
5959
}
6060

61+
let top_crate = if is_no_std_crate(cx) { "core" } else { "std" };
6162
if let Some(i) = item {
6263
let sugg = format!(
63-
"{}::iter::once({}{})",
64-
if is_no_std_crate(cx) { "core" } else { "std" },
64+
"{top_crate}::iter::once({}{})",
6565
iter_type.ref_prefix(),
6666
snippet(cx, i.span, "...")
6767
);
@@ -81,11 +81,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, method_name: &str, re
8181
expr.span,
8282
&format!("`{method_name}` call on an empty collection"),
8383
"try",
84-
if is_no_std_crate(cx) {
85-
"core::iter::empty()".to_string()
86-
} else {
87-
"std::iter::empty()".to_string()
88-
},
84+
format!("{top_crate}::iter::empty()"),
8985
Applicability::MaybeIncorrect,
9086
);
9187
}

clippy_lints/src/methods/unnecessary_sort_by.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ pub(super) fn check<'tcx>(
204204
cx,
205205
UNNECESSARY_SORT_BY,
206206
expr.span,
207-
"use Vec::sort_by_key here instead",
207+
"consider using `sort_by_key`",
208208
"try",
209209
format!(
210210
"{}.sort{}_by_key(|{}| {})",
@@ -227,7 +227,7 @@ pub(super) fn check<'tcx>(
227227
cx,
228228
UNNECESSARY_SORT_BY,
229229
expr.span,
230-
"use Vec::sort here instead",
230+
"consider using `sort`",
231231
"try",
232232
format!(
233233
"{}.sort{}()",

clippy_lints/src/operators/ptr_eq.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::is_no_std_crate;
23
use clippy_utils::source::snippet_opt;
34
use rustc_errors::Applicability;
45
use rustc_hir::{BinOpKind, Expr, ExprKind};
56
use rustc_lint::LateContext;
67

78
use super::PTR_EQ;
89

9-
static LINT_MSG: &str = "use `std::ptr::eq` when comparing raw pointers";
10-
1110
pub(super) fn check<'tcx>(
1211
cx: &LateContext<'tcx>,
1312
expr: &'tcx Expr<'_>,
@@ -26,13 +25,14 @@ pub(super) fn check<'tcx>(
2625
&& let Some(left_snip) = snippet_opt(cx, left_var.span)
2726
&& let Some(right_snip) = snippet_opt(cx, right_var.span)
2827
{
28+
let top_crate = if is_no_std_crate(cx) { "core" } else { "std" };
2929
span_lint_and_sugg(
3030
cx,
3131
PTR_EQ,
3232
expr.span,
33-
LINT_MSG,
33+
&format!("use `{top_crate}::ptr::eq` when comparing raw pointers"),
3434
"try",
35-
format!("std::ptr::eq({left_snip}, {right_snip})"),
35+
format!("{top_crate}::ptr::eq({left_snip}, {right_snip})"),
3636
Applicability::MachineApplicable,
3737
);
3838
}

clippy_lints/src/transmute/transmute_int_to_char.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::TRANSMUTE_INT_TO_CHAR;
22
use clippy_utils::diagnostics::span_lint_and_then;
3-
use clippy_utils::sugg;
3+
use clippy_utils::{is_no_std_crate, sugg};
44
use rustc_ast as ast;
55
use rustc_errors::Applicability;
66
use rustc_hir::Expr;
@@ -34,7 +34,10 @@ pub(super) fn check<'tcx>(
3434
diag.span_suggestion(
3535
e.span,
3636
"consider using",
37-
format!("std::char::from_u32({arg}).unwrap()"),
37+
format!(
38+
"{}::char::from_u32({arg}).unwrap()",
39+
if is_no_std_crate(cx) { "core" } else { "std" }
40+
),
3841
Applicability::Unspecified,
3942
);
4043
},

clippy_lints/src/transmute/transmute_ref_to_ref.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{TRANSMUTE_BYTES_TO_STR, TRANSMUTE_PTR_TO_PTR};
22
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
33
use clippy_utils::source::snippet;
4-
use clippy_utils::sugg;
4+
use clippy_utils::{is_no_std_crate, sugg};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, Mutability};
77
use rustc_lint::LateContext;
@@ -29,16 +29,18 @@ pub(super) fn check<'tcx>(
2929

3030
let snippet = snippet(cx, arg.span, "..");
3131

32+
let top_crate = if is_no_std_crate(cx) { "core" } else { "std" };
33+
3234
span_lint_and_sugg(
3335
cx,
3436
TRANSMUTE_BYTES_TO_STR,
3537
e.span,
3638
&format!("transmute from a `{from_ty}` to a `{to_ty}`"),
3739
"consider using",
3840
if const_context {
39-
format!("std::str::from_utf8_unchecked{postfix}({snippet})")
41+
format!("{top_crate}::str::from_utf8_unchecked{postfix}({snippet})")
4042
} else {
41-
format!("std::str::from_utf8{postfix}({snippet}).unwrap()")
43+
format!("{top_crate}::str::from_utf8{postfix}({snippet}).unwrap()")
4244
},
4345
Applicability::MaybeIncorrect,
4446
);

0 commit comments

Comments
 (0)