Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit e0014af

Browse files
committed
Add suggestions for std_instead_of_core
Fixes rust-lang#11446
1 parent b9906ac commit e0014af

File tree

4 files changed

+101
-59
lines changed

4 files changed

+101
-59
lines changed

clippy_lints/src/std_instead_of_core.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use clippy_utils::diagnostics::span_lint_and_help;
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use rustc_errors::Applicability;
23
use rustc_hir::def::Res;
34
use rustc_hir::def_id::DefId;
45
use rustc_hir::{HirId, Path, PathSegment};
@@ -99,17 +100,17 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports {
99100
&& let Some(first_segment) = get_first_segment(path)
100101
&& is_stable(cx, def_id)
101102
{
102-
let (lint, msg, help) = match first_segment.ident.name {
103+
let (lint, used_mod, replace_with) = match first_segment.ident.name {
103104
sym::std => match cx.tcx.crate_name(def_id.krate) {
104105
sym::core => (
105106
STD_INSTEAD_OF_CORE,
106-
"used import from `std` instead of `core`",
107-
"consider importing the item from `core`",
107+
"std",
108+
"core",
108109
),
109110
sym::alloc => (
110111
STD_INSTEAD_OF_ALLOC,
111-
"used import from `std` instead of `alloc`",
112-
"consider importing the item from `alloc`",
112+
"std",
113+
"alloc",
113114
),
114115
_ => {
115116
self.prev_span = path.span;
@@ -120,8 +121,8 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports {
120121
if cx.tcx.crate_name(def_id.krate) == sym::core {
121122
(
122123
ALLOC_INSTEAD_OF_CORE,
123-
"used import from `alloc` instead of `core`",
124-
"consider importing the item from `core`",
124+
"alloc",
125+
"core",
125126
)
126127
} else {
127128
self.prev_span = path.span;
@@ -131,7 +132,14 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports {
131132
_ => return,
132133
};
133134
if path.span != self.prev_span {
134-
span_lint_and_help(cx, lint, path.span, msg, None, help);
135+
span_lint_and_sugg(
136+
cx,
137+
lint,
138+
first_segment.ident.span,
139+
&format!("used import from `{used_mod}` instead of `{replace_with}`"),
140+
&format!("consider importing the item from `{replace_with}`"),
141+
replace_with.to_string(),
142+
Applicability::MachineApplicable);
135143
self.prev_span = path.span;
136144
}
137145
}

tests/ui/std_instead_of_core.fixed

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#![warn(clippy::std_instead_of_core)]
2+
#![allow(unused_imports)]
3+
4+
extern crate alloc;
5+
6+
#[warn(clippy::std_instead_of_core)]
7+
fn std_instead_of_core() {
8+
// Regular import
9+
use core::hash::Hasher;
10+
//~^ ERROR: used import from `std` instead of `core`
11+
// Absolute path
12+
use ::core::hash::Hash;
13+
//~^ ERROR: used import from `std` instead of `core`
14+
// Don't lint on `env` macro
15+
use std::env;
16+
17+
// Multiple imports
18+
use core::fmt::{Debug, Result};
19+
//~^ ERROR: used import from `std` instead of `core`
20+
21+
// Function calls
22+
let ptr = core::ptr::null::<u32>();
23+
//~^ ERROR: used import from `std` instead of `core`
24+
let ptr_mut = ::core::ptr::null_mut::<usize>();
25+
//~^ ERROR: used import from `std` instead of `core`
26+
27+
// Types
28+
let cell = core::cell::Cell::new(8u32);
29+
//~^ ERROR: used import from `std` instead of `core`
30+
let cell_absolute = ::core::cell::Cell::new(8u32);
31+
//~^ ERROR: used import from `std` instead of `core`
32+
33+
let _ = std::env!("PATH");
34+
35+
// do not lint until `error_in_core` is stable
36+
use std::error::Error;
37+
38+
// lint items re-exported from private modules, `core::iter::traits::iterator::Iterator`
39+
use core::iter::Iterator;
40+
//~^ ERROR: used import from `std` instead of `core`
41+
}
42+
43+
#[warn(clippy::std_instead_of_alloc)]
44+
fn std_instead_of_alloc() {
45+
// Only lint once.
46+
use alloc::vec;
47+
//~^ ERROR: used import from `std` instead of `alloc`
48+
use alloc::vec::Vec;
49+
//~^ ERROR: used import from `std` instead of `alloc`
50+
}
51+
52+
#[warn(clippy::alloc_instead_of_core)]
53+
fn alloc_instead_of_core() {
54+
use core::slice::from_ref;
55+
//~^ ERROR: used import from `alloc` instead of `core`
56+
}
57+
58+
fn main() {
59+
std_instead_of_core();
60+
std_instead_of_alloc();
61+
alloc_instead_of_core();
62+
}

tests/ui/std_instead_of_core.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ fn std_instead_of_core() {
1717
// Multiple imports
1818
use std::fmt::{Debug, Result};
1919
//~^ ERROR: used import from `std` instead of `core`
20-
//~| ERROR: used import from `std` instead of `core`
2120

2221
// Function calls
2322
let ptr = std::ptr::null::<u32>();

tests/ui/std_instead_of_core.stderr

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,100 +2,73 @@ error: used import from `std` instead of `core`
22
--> $DIR/std_instead_of_core.rs:9:9
33
|
44
LL | use std::hash::Hasher;
5-
| ^^^^^^^^^^^^^^^^^
5+
| ^^^ help: consider importing the item from `core`: `core`
66
|
7-
= help: consider importing the item from `core`
87
= note: `-D clippy::std-instead-of-core` implied by `-D warnings`
98

109
error: used import from `std` instead of `core`
11-
--> $DIR/std_instead_of_core.rs:12:9
10+
--> $DIR/std_instead_of_core.rs:12:11
1211
|
1312
LL | use ::std::hash::Hash;
14-
| ^^^^^^^^^^^^^^^^^
15-
|
16-
= help: consider importing the item from `core`
13+
| ^^^ help: consider importing the item from `core`: `core`
1714

1815
error: used import from `std` instead of `core`
19-
--> $DIR/std_instead_of_core.rs:18:20
16+
--> $DIR/std_instead_of_core.rs:18:9
2017
|
2118
LL | use std::fmt::{Debug, Result};
22-
| ^^^^^
23-
|
24-
= help: consider importing the item from `core`
25-
26-
error: used import from `std` instead of `core`
27-
--> $DIR/std_instead_of_core.rs:18:27
28-
|
29-
LL | use std::fmt::{Debug, Result};
30-
| ^^^^^^
31-
|
32-
= help: consider importing the item from `core`
19+
| ^^^ help: consider importing the item from `core`: `core`
3320

3421
error: used import from `std` instead of `core`
35-
--> $DIR/std_instead_of_core.rs:23:15
22+
--> $DIR/std_instead_of_core.rs:22:15
3623
|
3724
LL | let ptr = std::ptr::null::<u32>();
38-
| ^^^^^^^^^^^^^^^^^^^^^
39-
|
40-
= help: consider importing the item from `core`
25+
| ^^^ help: consider importing the item from `core`: `core`
4126

4227
error: used import from `std` instead of `core`
43-
--> $DIR/std_instead_of_core.rs:25:19
28+
--> $DIR/std_instead_of_core.rs:24:21
4429
|
4530
LL | let ptr_mut = ::std::ptr::null_mut::<usize>();
46-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
47-
|
48-
= help: consider importing the item from `core`
31+
| ^^^ help: consider importing the item from `core`: `core`
4932

5033
error: used import from `std` instead of `core`
51-
--> $DIR/std_instead_of_core.rs:29:16
34+
--> $DIR/std_instead_of_core.rs:28:16
5235
|
5336
LL | let cell = std::cell::Cell::new(8u32);
54-
| ^^^^^^^^^^^^^^^
55-
|
56-
= help: consider importing the item from `core`
37+
| ^^^ help: consider importing the item from `core`: `core`
5738

5839
error: used import from `std` instead of `core`
59-
--> $DIR/std_instead_of_core.rs:31:25
40+
--> $DIR/std_instead_of_core.rs:30:27
6041
|
6142
LL | let cell_absolute = ::std::cell::Cell::new(8u32);
62-
| ^^^^^^^^^^^^^^^^^
63-
|
64-
= help: consider importing the item from `core`
43+
| ^^^ help: consider importing the item from `core`: `core`
6544

6645
error: used import from `std` instead of `core`
67-
--> $DIR/std_instead_of_core.rs:40:9
46+
--> $DIR/std_instead_of_core.rs:39:9
6847
|
6948
LL | use std::iter::Iterator;
70-
| ^^^^^^^^^^^^^^^^^^^
71-
|
72-
= help: consider importing the item from `core`
49+
| ^^^ help: consider importing the item from `core`: `core`
7350

7451
error: used import from `std` instead of `alloc`
75-
--> $DIR/std_instead_of_core.rs:47:9
52+
--> $DIR/std_instead_of_core.rs:46:9
7653
|
7754
LL | use std::vec;
78-
| ^^^^^^^^
55+
| ^^^ help: consider importing the item from `alloc`: `alloc`
7956
|
80-
= help: consider importing the item from `alloc`
8157
= note: `-D clippy::std-instead-of-alloc` implied by `-D warnings`
8258

8359
error: used import from `std` instead of `alloc`
84-
--> $DIR/std_instead_of_core.rs:49:9
60+
--> $DIR/std_instead_of_core.rs:48:9
8561
|
8662
LL | use std::vec::Vec;
87-
| ^^^^^^^^^^^^^
88-
|
89-
= help: consider importing the item from `alloc`
63+
| ^^^ help: consider importing the item from `alloc`: `alloc`
9064

9165
error: used import from `alloc` instead of `core`
92-
--> $DIR/std_instead_of_core.rs:55:9
66+
--> $DIR/std_instead_of_core.rs:54:9
9367
|
9468
LL | use alloc::slice::from_ref;
95-
| ^^^^^^^^^^^^^^^^^^^^^^
69+
| ^^^^^ help: consider importing the item from `core`: `core`
9670
|
97-
= help: consider importing the item from `core`
9871
= note: `-D clippy::alloc-instead-of-core` implied by `-D warnings`
9972

100-
error: aborting due to 12 previous errors
73+
error: aborting due to 11 previous errors
10174

0 commit comments

Comments
 (0)