Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
392 changes: 294 additions & 98 deletions clippy_lints/src/std_instead_of_core.rs

Large diffs are not rendered by default.

30 changes: 23 additions & 7 deletions tests/ui/std_instead_of_core.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,6 @@ fn msrv_1_76(_: std::net::IpAddr) {}
fn msrv_1_77(_: core::net::IpAddr) {}
//~^ std_instead_of_core

#[warn(clippy::alloc_instead_of_core)]
fn issue15579() {
use std::alloc;

let layout = alloc::Layout::new::<u8>();
}

#[warn(clippy::std_instead_of_core)]
fn issue13158_core_io() {
// items moved from std::io into core::io are stable in an unstable module.
Expand All @@ -115,3 +108,26 @@ fn issue13158_msrv_1_80(_: &dyn std::error::Error) {}
#[clippy::msrv = "1.81"]
fn issue13158_msrv_1_81(_: &dyn core::error::Error) {}
//~^ std_instead_of_core

#[warn(clippy::std_instead_of_core)]
fn issue17260() {
use core::concat;
//~^ std_instead_of_core
}

#[warn(clippy::std_instead_of_alloc)]
fn non_use_paths() {
type Foo = alloc::sync::Arc<std::sync::Mutex<bool>>;
//~^ std_instead_of_alloc

let _x = core::iter::repeat(u8::default());
//~^ std_instead_of_core

fn impl_trait(_: impl core::fmt::Display) {}
//~^ std_instead_of_core

struct Bar {
layout: core::alloc::Layout,
//~^ std_instead_of_core
}
}
30 changes: 23 additions & 7 deletions tests/ui/std_instead_of_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,6 @@ fn msrv_1_76(_: std::net::IpAddr) {}
fn msrv_1_77(_: std::net::IpAddr) {}
//~^ std_instead_of_core

#[warn(clippy::alloc_instead_of_core)]
fn issue15579() {
use std::alloc;

let layout = alloc::Layout::new::<u8>();
}

#[warn(clippy::std_instead_of_core)]
fn issue13158_core_io() {
// items moved from std::io into core::io are stable in an unstable module.
Expand All @@ -115,3 +108,26 @@ fn issue13158_msrv_1_80(_: &dyn std::error::Error) {}
#[clippy::msrv = "1.81"]
fn issue13158_msrv_1_81(_: &dyn std::error::Error) {}
//~^ std_instead_of_core

#[warn(clippy::std_instead_of_core)]
fn issue17260() {
use std::concat;
//~^ std_instead_of_core
}

#[warn(clippy::std_instead_of_alloc)]
fn non_use_paths() {
type Foo = std::sync::Arc<std::sync::Mutex<bool>>;
//~^ std_instead_of_alloc

let _x = std::iter::repeat(u8::default());
//~^ std_instead_of_core

fn impl_trait(_: impl std::fmt::Display) {}
//~^ std_instead_of_core

struct Bar {
layout: std::alloc::Layout,
//~^ std_instead_of_core
}
}
36 changes: 33 additions & 3 deletions tests/ui/std_instead_of_core.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,46 @@ LL | fn msrv_1_77(_: std::net::IpAddr) {}
| ^^^ help: consider importing the item from `core`: `core`

error: used import from `std` instead of `core`
--> tests/ui/std_instead_of_core.rs:109:33
--> tests/ui/std_instead_of_core.rs:102:33
|
LL | fn issue13158_msrv_1_41(_: &dyn std::panic::UnwindSafe) {}
| ^^^ help: consider importing the item from `core`: `core`

error: used import from `std` instead of `core`
--> tests/ui/std_instead_of_core.rs:116:33
--> tests/ui/std_instead_of_core.rs:109:33
|
LL | fn issue13158_msrv_1_81(_: &dyn std::error::Error) {}
| ^^^ help: consider importing the item from `core`: `core`

error: aborting due to 17 previous errors
error: used import from `std` instead of `core`
--> tests/ui/std_instead_of_core.rs:114:9
|
LL | use std::concat;
| ^^^ help: consider importing the item from `core`: `core`

error: used import from `std` instead of `alloc`
--> tests/ui/std_instead_of_core.rs:120:16
|
LL | type Foo = std::sync::Arc<std::sync::Mutex<bool>>;
| ^^^ help: consider importing the item from `alloc`: `alloc`

error: used import from `std` instead of `core`
--> tests/ui/std_instead_of_core.rs:123:14
|
LL | let _x = std::iter::repeat(u8::default());
| ^^^ help: consider importing the item from `core`: `core`

error: used import from `std` instead of `core`
--> tests/ui/std_instead_of_core.rs:126:27
|
LL | fn impl_trait(_: impl std::fmt::Display) {}
| ^^^ help: consider importing the item from `core`: `core`

error: used import from `std` instead of `core`
--> tests/ui/std_instead_of_core.rs:130:17
|
LL | layout: std::alloc::Layout,
| ^^^ help: consider importing the item from `core`: `core`

error: aborting due to 22 previous errors

37 changes: 37 additions & 0 deletions tests/ui/std_instead_of_core_local_alloc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#![warn(clippy::std_instead_of_alloc)]
#![allow(unused_imports)]

#[rustfmt::skip]
fn issue16695() {
extern crate alloc;

use alloc::collections::VecDeque;
//~^ std_instead_of_alloc

{
use alloc::vec::Vec;
//~^ std_instead_of_alloc
}
}

mod a {
extern crate alloc as alloc_a;

fn b() {
mod c {
extern crate alloc as alloc_b;

fn d() {
fn e() {
// This should suggest alloc_b
use alloc_b::vec::Vec;
//~^ std_instead_of_alloc
}
}
}
}

// This should suggest alloc_a
use alloc_a::vec::Vec;
//~^ std_instead_of_alloc
}
37 changes: 37 additions & 0 deletions tests/ui/std_instead_of_core_local_alloc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#![warn(clippy::std_instead_of_alloc)]
#![allow(unused_imports)]

#[rustfmt::skip]
fn issue16695() {
extern crate alloc;

use std::collections::VecDeque;
//~^ std_instead_of_alloc

{
use std::vec::Vec;
//~^ std_instead_of_alloc
}
}

mod a {
extern crate alloc as alloc_a;

fn b() {
mod c {
extern crate alloc as alloc_b;

fn d() {
fn e() {
// This should suggest alloc_b
use std::vec::Vec;
//~^ std_instead_of_alloc
}
}
}
}

// This should suggest alloc_a
use std::vec::Vec;
//~^ std_instead_of_alloc
}
29 changes: 29 additions & 0 deletions tests/ui/std_instead_of_core_local_alloc.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
error: used import from `std` instead of `alloc`
--> tests/ui/std_instead_of_core_local_alloc.rs:8:9
|
LL | use std::collections::VecDeque;
| ^^^ help: consider importing the item from `alloc`: `alloc`
|
= note: `-D clippy::std-instead-of-alloc` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::std_instead_of_alloc)]`

error: used import from `std` instead of `alloc`
--> tests/ui/std_instead_of_core_local_alloc.rs:12:13
|
LL | use std::vec::Vec;
| ^^^ help: consider importing the item from `alloc`: `alloc`

error: used import from `std` instead of `alloc`
--> tests/ui/std_instead_of_core_local_alloc.rs:27:25
|
LL | use std::vec::Vec;
| ^^^ help: consider importing the item from `alloc`: `alloc_b`

error: used import from `std` instead of `alloc`
--> tests/ui/std_instead_of_core_local_alloc.rs:35:9
|
LL | use std::vec::Vec;
| ^^^ help: consider importing the item from `alloc`: `alloc_a`

error: aborting due to 4 previous errors

97 changes: 97 additions & 0 deletions tests/ui/std_instead_of_core_unfixable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,100 @@ fn pr16964() {
ffi::OsString,
};
}

#[warn(clippy::alloc_instead_of_core)]
fn issue15579() {
use std::alloc;

let layout = alloc::Layout::new::<u8>();
//~^ std_instead_of_core
}

#[rustfmt::skip]
fn issue12468() {
use std::{
fmt::Result,
//~^ std_instead_of_core
io::Write,
};

use std::sync::{
Arc,
//~^ std_instead_of_alloc
Mutex,
};
}

#[allow(clippy::legacy_numeric_constants)]
#[warn(clippy::alloc_instead_of_core)]
#[rustfmt::skip]
fn pr17252_large() {
extern crate alloc;

use {
std::sync::Mutex,
::{
std::{
fmt::{*, Formatter},
//~^ std_instead_of_alloc
//~| std_instead_of_core
fs,
//~v std_instead_of_core
sync::atomic,
//~v std_instead_of_core
sync::atomic::{
AtomicUsize,
AtomicU8,
Ordering,
},
},
core::u32,
std::collections::HashMap,
},
alloc::{
fmt::Result as FmtResult,
//~^ alloc_instead_of_core
format,
}
};
}

#[warn(clippy::alloc_instead_of_core)]
#[rustfmt::skip]
fn issue11159() {
use std::fmt;
//~^ std_instead_of_alloc

struct S;

impl fmt::Display for S {
//~^ alloc_instead_of_core
fn fmt(
&self,
_: &mut fmt::Formatter<'_>,
//~^ alloc_instead_of_core
) -> fmt::Result {
//~^ alloc_instead_of_core
todo!()
}
}
}

#[warn(clippy::alloc_instead_of_core)]
#[rustfmt::skip]
fn mixed_name_spaces() {
extern crate alloc;

use std::{
io,
iter::repeat_with,
//~^ std_instead_of_core
};

use alloc::alloc::{
alloc,
dealloc,
Layout,
//~^ alloc_instead_of_core
};
}
Loading