Skip to content

Commit 7fc3764

Browse files
committed
fix: alloc_instead_of_core FP when alloc is an alias
1 parent 1b21661 commit 7fc3764

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

clippy_lints/src/std_instead_of_core.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_config::Conf;
22
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
3-
use clippy_utils::is_from_proc_macro;
43
use clippy_utils::msrvs::Msrv;
4+
use clippy_utils::{is_from_proc_macro, is_res_crate};
55
use rustc_errors::Applicability;
66
use rustc_hir::def::{DefKind, Res};
77
use rustc_hir::def_id::DefId;
@@ -126,6 +126,7 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports {
126126
&& !is_from_proc_macro(cx, &first_segment.ident)
127127
&& !matches!(def_kind, DefKind::Macro(_))
128128
&& let Some(last_segment) = path.segments.last()
129+
&& is_res_crate(first_segment.res)
129130
{
130131
let (lint, used_mod, replace_with) = match first_segment.ident.name {
131132
sym::std => match cx.tcx.crate_name(def_id.krate) {

clippy_utils/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3664,3 +3664,8 @@ pub fn is_expr_async_block(expr: &Expr<'_>) -> bool {
36643664
})
36653665
)
36663666
}
3667+
3668+
/// Checks if a `Res` refers to a crate
3669+
pub fn is_res_crate(res: Res) -> bool {
3670+
matches!(res, Res::Def(DefKind::Mod, def_id) if def_id.is_crate_root() && !def_id.is_local())
3671+
}

tests/ui/std_instead_of_core.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,10 @@ fn msrv_1_76(_: std::net::IpAddr) {}
8989
#[clippy::msrv = "1.77"]
9090
fn msrv_1_77(_: core::net::IpAddr) {}
9191
//~^ std_instead_of_core
92+
93+
#[warn(clippy::alloc_instead_of_core)]
94+
fn issue15579() {
95+
use std::alloc;
96+
97+
let layout = alloc::Layout::new::<u8>();
98+
}

tests/ui/std_instead_of_core.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,10 @@ fn msrv_1_76(_: std::net::IpAddr) {}
8989
#[clippy::msrv = "1.77"]
9090
fn msrv_1_77(_: std::net::IpAddr) {}
9191
//~^ std_instead_of_core
92+
93+
#[warn(clippy::alloc_instead_of_core)]
94+
fn issue15579() {
95+
use std::alloc;
96+
97+
let layout = alloc::Layout::new::<u8>();
98+
}

0 commit comments

Comments
 (0)