Skip to content

Commit 435f115

Browse files
committed
std_instead_of_core and friends
1 parent 0f5a38f commit 435f115

File tree

6 files changed

+87
-0
lines changed

6 files changed

+87
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3901,6 +3901,7 @@ Released 2018-09-13
39013901
[`skip_while_next`]: https://rust-lang.github.io/rust-clippy/master/index.html#skip_while_next
39023902
[`slow_vector_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#slow_vector_initialization
39033903
[`stable_sort_primitive`]: https://rust-lang.github.io/rust-clippy/master/index.html#stable_sort_primitive
3904+
[`std_instead_of_core`]: https://rust-lang.github.io/rust-clippy/master/index.html#std_instead_of_core
39043905
[`str_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#str_to_string
39053906
[`string_add`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_add
39063907
[`string_add_assign`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_add_assign

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ store.register_lints(&[
497497
size_of_in_element_count::SIZE_OF_IN_ELEMENT_COUNT,
498498
slow_vector_initialization::SLOW_VECTOR_INITIALIZATION,
499499
stable_sort_primitive::STABLE_SORT_PRIMITIVE,
500+
std_instead_of_core::STD_INSTEAD_OF_CORE,
500501
strings::STRING_ADD,
501502
strings::STRING_ADD_ASSIGN,
502503
strings::STRING_FROM_UTF8_AS_BYTES,

clippy_lints/src/lib.register_nursery.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
2525
LintId::of(path_buf_push_overwrite::PATH_BUF_PUSH_OVERWRITE),
2626
LintId::of(redundant_pub_crate::REDUNDANT_PUB_CRATE),
2727
LintId::of(regex::TRIVIAL_REGEX),
28+
LintId::of(std_instead_of_core::STD_INSTEAD_OF_CORE),
2829
LintId::of(strings::STRING_LIT_AS_BYTES),
2930
LintId::of(suspicious_operation_groupings::SUSPICIOUS_OPERATION_GROUPINGS),
3031
LintId::of(trailing_empty_array::TRAILING_EMPTY_ARRAY),

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ mod single_component_path_imports;
365365
mod size_of_in_element_count;
366366
mod slow_vector_initialization;
367367
mod stable_sort_primitive;
368+
mod std_instead_of_core;
368369
mod strings;
369370
mod strlen_on_c_strings;
370371
mod suspicious_operation_groupings;
@@ -915,6 +916,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
915916
let verbose_bit_mask_threshold = conf.verbose_bit_mask_threshold;
916917
store.register_late_pass(move || Box::new(operators::Operators::new(verbose_bit_mask_threshold)));
917918
store.register_late_pass(|| Box::new(invalid_utf8_in_unchecked::InvalidUtf8InUnchecked));
919+
store.register_late_pass(|| Box::new(std_instead_of_core::StdReexports));
918920
// add lints here, do not remove this comment, it's used in `new_lint`
919921
}
920922

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use clippy_utils::diagnostics::span_lint_and_help;
2+
use rustc_hir::{def::Res, Item, ItemKind};
3+
use rustc_lint::{LateContext, LateLintPass, Lint};
4+
use rustc_session::{declare_lint_pass, declare_tool_lint};
5+
use rustc_span::{sym, Symbol};
6+
7+
declare_clippy_lint! {
8+
/// ### What it does
9+
///
10+
/// ### Why is this bad?
11+
///
12+
/// ### Example
13+
/// ```rust
14+
/// // example code where clippy issues a warning
15+
/// ```
16+
/// Use instead:
17+
/// ```rust
18+
/// // example code which does not raise clippy warning
19+
/// ```
20+
#[clippy::version = "1.64.0"]
21+
pub STD_INSTEAD_OF_CORE,
22+
nursery,
23+
"default lint description"
24+
}
25+
// TODO: Make multi pass: see DropForgetRef
26+
declare_lint_pass!(StdReexports => [STD_INSTEAD_OF_CORE]);
27+
28+
impl<'tcx> LateLintPass<'tcx> for StdReexports {
29+
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &Item<'tcx>) {
30+
// std_instead_of_core
31+
run_lint(cx, item, sym::core, sym::std, STD_INSTEAD_OF_CORE);
32+
// std_instead_of_alloc
33+
run_lint(cx, item, sym::alloc, sym::std, STD_INSTEAD_OF_CORE);
34+
// alloc_instead_of_core
35+
run_lint(cx, item, sym::core, sym::alloc, STD_INSTEAD_OF_CORE);
36+
}
37+
}
38+
39+
fn run_lint(
40+
cx: &LateContext<'_>,
41+
item: &Item<'_>,
42+
item_crate_name: Symbol,
43+
use_crate_segment_name: Symbol,
44+
lint: &'static Lint,
45+
) {
46+
if_chain! {
47+
if let ItemKind::Use(path, _) = item.kind;
48+
if let Res::Def(_, def_id) = path.res;
49+
// check if the resolved path is in the crate
50+
if item_crate_name == cx.tcx.crate_name(def_id.krate);
51+
52+
// check if the first segment of the path is from std or
53+
if let Some(path_root_segment) = path.segments.first();
54+
55+
// and check that the first segment of the import refers crate we lint for.
56+
if use_crate_segment_name == path_root_segment.ident.name;
57+
58+
then {
59+
span_lint_and_help(
60+
cx,
61+
lint,
62+
path.span,
63+
&format!("used `{}` import instead of `{}`", use_crate_segment_name, item_crate_name),
64+
None,
65+
&format!("consider importing from `{}`", item_crate_name),
66+
);
67+
}
68+
}
69+
}

tests/ui/std_instead_of_core.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![warn(clippy::std_instead_of_core)]
2+
#![allow(unused_imports)]
3+
4+
extern crate alloc;
5+
6+
use alloc::slice::from_ref;
7+
use std::hash::Hasher;
8+
use std::vec::Vec;
9+
10+
fn main() {
11+
let test1 = alloc::vec::Vec::<u32>::new();
12+
let test = ::std::vec::Vec::<u32>::new();
13+
}

0 commit comments

Comments
 (0)