Skip to content

Commit

Permalink
std_instead_of_core and friends
Browse files Browse the repository at this point in the history
  • Loading branch information
i509VCB committed Jul 14, 2022
1 parent 0f5a38f commit dedf8b8
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3901,6 +3901,7 @@ Released 2018-09-13
[`skip_while_next`]: https://rust-lang.github.io/rust-clippy/master/index.html#skip_while_next
[`slow_vector_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#slow_vector_initialization
[`stable_sort_primitive`]: https://rust-lang.github.io/rust-clippy/master/index.html#stable_sort_primitive
[`std_instead_of_core`]: https://rust-lang.github.io/rust-clippy/master/index.html#std_instead_of_core
[`str_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#str_to_string
[`string_add`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_add
[`string_add_assign`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_add_assign
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/lib.register_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ store.register_lints(&[
size_of_in_element_count::SIZE_OF_IN_ELEMENT_COUNT,
slow_vector_initialization::SLOW_VECTOR_INITIALIZATION,
stable_sort_primitive::STABLE_SORT_PRIMITIVE,
std_instead_of_core::STD_INSTEAD_OF_CORE,
strings::STRING_ADD,
strings::STRING_ADD_ASSIGN,
strings::STRING_FROM_UTF8_AS_BYTES,
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/lib.register_nursery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
LintId::of(path_buf_push_overwrite::PATH_BUF_PUSH_OVERWRITE),
LintId::of(redundant_pub_crate::REDUNDANT_PUB_CRATE),
LintId::of(regex::TRIVIAL_REGEX),
LintId::of(std_instead_of_core::STD_INSTEAD_OF_CORE),
LintId::of(strings::STRING_LIT_AS_BYTES),
LintId::of(suspicious_operation_groupings::SUSPICIOUS_OPERATION_GROUPINGS),
LintId::of(trailing_empty_array::TRAILING_EMPTY_ARRAY),
Expand Down
2 changes: 2 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ mod single_component_path_imports;
mod size_of_in_element_count;
mod slow_vector_initialization;
mod stable_sort_primitive;
mod std_instead_of_core;
mod strings;
mod strlen_on_c_strings;
mod suspicious_operation_groupings;
Expand Down Expand Up @@ -915,6 +916,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
let verbose_bit_mask_threshold = conf.verbose_bit_mask_threshold;
store.register_late_pass(move || Box::new(operators::Operators::new(verbose_bit_mask_threshold)));
store.register_late_pass(|| Box::new(invalid_utf8_in_unchecked::InvalidUtf8InUnchecked));
store.register_late_pass(|| Box::new(std_instead_of_core::StdReexports));
// add lints here, do not remove this comment, it's used in `new_lint`
}

Expand Down
72 changes: 72 additions & 0 deletions clippy_lints/src/std_instead_of_core.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use clippy_utils::diagnostics::span_lint_and_help;
use rustc_hir::{def::Res, Item, ItemKind};
use rustc_lint::{LateContext, LateLintPass, Lint};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::{sym, Symbol};

declare_clippy_lint! {
/// ### What it does
///
/// ### Why is this bad?
///
/// ### Example
/// ```rust
/// // example code where clippy issues a warning
/// ```
/// Use instead:
/// ```rust
/// // example code which does not raise clippy warning
/// ```
#[clippy::version = "1.64.0"]
pub STD_INSTEAD_OF_CORE,
nursery,
"default lint description"
}
// TODO: Make multi pass: see DropForgetRef
declare_lint_pass!(StdReexports => [STD_INSTEAD_OF_CORE]);

impl<'tcx> LateLintPass<'tcx> for StdReexports {
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &Item<'tcx>) {
// std_instead_of_core
run_lint(cx, item, sym::core, sym::std, STD_INSTEAD_OF_CORE);
// std_instead_of_alloc
run_lint(cx, item, sym::alloc, sym::std, STD_INSTEAD_OF_CORE);
// alloc_instead_of_core
run_lint(cx, item, sym::core, sym::alloc, STD_INSTEAD_OF_CORE);
}

// TODO: Fully qualified items
// - `std::vec::Vec::<u32>::new()`
}

fn run_lint(
cx: &LateContext<'_>,
item: &Item<'_>,
item_crate_name: Symbol,
use_crate_segment_name: Symbol,
lint: &'static Lint,
) {
if_chain! {
if let ItemKind::Use(path, _) = item.kind;
if let Res::Def(_, def_id) = path.res;
// check if the resolved path is in the crate
if item_crate_name == cx.tcx.crate_name(def_id.krate);

// check if the first segment of the path is from std or
if let Some(path_root_segment) = path.segments.first();

// and check that the first segment of the import refers crate we lint for.
if use_crate_segment_name == path_root_segment.ident.name;

then {
span_lint_and_help(
cx,
lint,
path.span,
&format!("used `{}` import instead of `{}`", use_crate_segment_name, item_crate_name),
None,
&format!("consider importing from `{}`", item_crate_name),
);
}
}
}
13 changes: 13 additions & 0 deletions tests/ui/std_instead_of_core.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![warn(clippy::std_instead_of_core)]
#![allow(unused_imports)]

extern crate alloc;

use alloc::slice::from_ref;
use std::hash::Hasher;
use std::vec::Vec;

fn main() {
let test1 = alloc::vec::Vec::<u32>::new();
let test = ::std::vec::Vec::<u32>::new();
}

0 comments on commit dedf8b8

Please sign in to comment.