-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update and add ui tests for core/std suggestions
- Loading branch information
1 parent
1bcaf29
commit 40a45a4
Showing
22 changed files
with
614 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#![warn(clippy::default_instead_of_iter_empty)] | ||
#![allow(dead_code)] | ||
#![feature(lang_items)] | ||
#![no_std] | ||
|
||
use core::panic::PanicInfo; | ||
|
||
#[lang = "eh_personality"] | ||
extern "C" fn eh_personality() {} | ||
|
||
#[panic_handler] | ||
fn panic(info: &PanicInfo) -> ! { | ||
loop {} | ||
} | ||
|
||
#[derive(Default)] | ||
struct Iter { | ||
iter: core::iter::Empty<usize>, | ||
} | ||
|
||
fn main() { | ||
// Do lint. | ||
let _ = core::iter::empty::<usize>(); | ||
let _foo: core::iter::Empty<usize> = core::iter::empty(); | ||
|
||
// Do not lint. | ||
let _ = Iter::default(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#![warn(clippy::default_instead_of_iter_empty)] | ||
#![allow(dead_code)] | ||
#![feature(lang_items)] | ||
#![no_std] | ||
|
||
use core::panic::PanicInfo; | ||
|
||
#[lang = "eh_personality"] | ||
extern "C" fn eh_personality() {} | ||
|
||
#[panic_handler] | ||
fn panic(info: &PanicInfo) -> ! { | ||
loop {} | ||
} | ||
|
||
#[derive(Default)] | ||
struct Iter { | ||
iter: core::iter::Empty<usize>, | ||
} | ||
|
||
fn main() { | ||
// Do lint. | ||
let _ = core::iter::Empty::<usize>::default(); | ||
let _foo: core::iter::Empty<usize> = core::iter::Empty::default(); | ||
|
||
// Do not lint. | ||
let _ = Iter::default(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
error: `core::iter::empty()` is the more idiomatic way | ||
--> $DIR/default_instead_of_iter_empty_no_std.rs:23:13 | ||
| | ||
LL | let _ = core::iter::Empty::<usize>::default(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `core::iter::empty::<usize>()` | ||
| | ||
= note: `-D clippy::default-instead-of-iter-empty` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::default_instead_of_iter_empty)]` | ||
|
||
error: `core::iter::empty()` is the more idiomatic way | ||
--> $DIR/default_instead_of_iter_empty_no_std.rs:24:42 | ||
| | ||
LL | let _foo: core::iter::Empty<usize> = core::iter::Empty::default(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `core::iter::empty()` | ||
|
||
error: aborting due to 2 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#![allow(unused)] | ||
#![warn( | ||
clippy::all, | ||
clippy::style, | ||
clippy::mem_replace_option_with_none, | ||
clippy::mem_replace_with_default | ||
)] | ||
#![feature(lang_items)] | ||
#![no_std] | ||
|
||
use core::mem; | ||
use core::panic::PanicInfo; | ||
|
||
#[lang = "eh_personality"] | ||
extern "C" fn eh_personality() {} | ||
|
||
#[panic_handler] | ||
fn panic(info: &PanicInfo) -> ! { | ||
loop {} | ||
} | ||
|
||
fn replace_option_with_none() { | ||
let mut an_option = Some(1); | ||
let _ = an_option.take(); | ||
let an_option = &mut Some(1); | ||
let _ = an_option.take(); | ||
} | ||
|
||
fn replace_with_default() { | ||
let mut refstr = "hello"; | ||
let _ = core::mem::take(&mut refstr); | ||
|
||
let mut slice: &[i32] = &[1, 2, 3]; | ||
let _ = core::mem::take(&mut slice); | ||
} | ||
|
||
// lint is disabled for primitives because in this case `take` | ||
// has no clear benefit over `replace` and sometimes is harder to read | ||
fn dont_lint_primitive() { | ||
let mut pbool = true; | ||
let _ = mem::replace(&mut pbool, false); | ||
|
||
let mut pint = 5; | ||
let _ = mem::replace(&mut pint, 0); | ||
} | ||
|
||
fn main() { | ||
replace_option_with_none(); | ||
replace_with_default(); | ||
dont_lint_primitive(); | ||
} | ||
|
||
fn issue9824() { | ||
struct Foo<'a>(Option<&'a str>); | ||
impl<'a> core::ops::Deref for Foo<'a> { | ||
type Target = Option<&'a str>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
impl<'a> core::ops::DerefMut for Foo<'a> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
struct Bar { | ||
opt: Option<u8>, | ||
val: u8, | ||
} | ||
|
||
let mut f = Foo(Some("foo")); | ||
let mut b = Bar { opt: Some(1), val: 12 }; | ||
|
||
// replace option with none | ||
let _ = f.0.take(); | ||
let _ = (*f).take(); | ||
let _ = b.opt.take(); | ||
// replace with default | ||
let _ = mem::replace(&mut b.val, u8::default()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#![allow(unused)] | ||
#![warn( | ||
clippy::all, | ||
clippy::style, | ||
clippy::mem_replace_option_with_none, | ||
clippy::mem_replace_with_default | ||
)] | ||
#![feature(lang_items)] | ||
#![no_std] | ||
|
||
use core::mem; | ||
use core::panic::PanicInfo; | ||
|
||
#[lang = "eh_personality"] | ||
extern "C" fn eh_personality() {} | ||
|
||
#[panic_handler] | ||
fn panic(info: &PanicInfo) -> ! { | ||
loop {} | ||
} | ||
|
||
fn replace_option_with_none() { | ||
let mut an_option = Some(1); | ||
let _ = mem::replace(&mut an_option, None); | ||
let an_option = &mut Some(1); | ||
let _ = mem::replace(an_option, None); | ||
} | ||
|
||
fn replace_with_default() { | ||
let mut refstr = "hello"; | ||
let _ = mem::replace(&mut refstr, ""); | ||
|
||
let mut slice: &[i32] = &[1, 2, 3]; | ||
let _ = mem::replace(&mut slice, &[]); | ||
} | ||
|
||
// lint is disabled for primitives because in this case `take` | ||
// has no clear benefit over `replace` and sometimes is harder to read | ||
fn dont_lint_primitive() { | ||
let mut pbool = true; | ||
let _ = mem::replace(&mut pbool, false); | ||
|
||
let mut pint = 5; | ||
let _ = mem::replace(&mut pint, 0); | ||
} | ||
|
||
fn main() { | ||
replace_option_with_none(); | ||
replace_with_default(); | ||
dont_lint_primitive(); | ||
} | ||
|
||
fn issue9824() { | ||
struct Foo<'a>(Option<&'a str>); | ||
impl<'a> core::ops::Deref for Foo<'a> { | ||
type Target = Option<&'a str>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
impl<'a> core::ops::DerefMut for Foo<'a> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
struct Bar { | ||
opt: Option<u8>, | ||
val: u8, | ||
} | ||
|
||
let mut f = Foo(Some("foo")); | ||
let mut b = Bar { opt: Some(1), val: 12 }; | ||
|
||
// replace option with none | ||
let _ = mem::replace(&mut f.0, None); | ||
let _ = mem::replace(&mut *f, None); | ||
let _ = mem::replace(&mut b.opt, None); | ||
// replace with default | ||
let _ = mem::replace(&mut b.val, u8::default()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
error: replacing an `Option` with `None` | ||
--> $DIR/mem_replace_no_std.rs:24:13 | ||
| | ||
LL | let _ = mem::replace(&mut an_option, None); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()` | ||
| | ||
= note: `-D clippy::mem-replace-option-with-none` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::mem_replace_option_with_none)]` | ||
|
||
error: replacing an `Option` with `None` | ||
--> $DIR/mem_replace_no_std.rs:26:13 | ||
| | ||
LL | let _ = mem::replace(an_option, None); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()` | ||
|
||
error: replacing a value of type `T` with `T::default()` is better expressed using `core::mem::take` | ||
--> $DIR/mem_replace_no_std.rs:31:13 | ||
| | ||
LL | let _ = mem::replace(&mut refstr, ""); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `core::mem::take(&mut refstr)` | ||
| | ||
= note: `-D clippy::mem-replace-with-default` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::mem_replace_with_default)]` | ||
|
||
error: replacing a value of type `T` with `T::default()` is better expressed using `core::mem::take` | ||
--> $DIR/mem_replace_no_std.rs:34:13 | ||
| | ||
LL | let _ = mem::replace(&mut slice, &[]); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `core::mem::take(&mut slice)` | ||
|
||
error: replacing an `Option` with `None` | ||
--> $DIR/mem_replace_no_std.rs:77:13 | ||
| | ||
LL | let _ = mem::replace(&mut f.0, None); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `f.0.take()` | ||
|
||
error: replacing an `Option` with `None` | ||
--> $DIR/mem_replace_no_std.rs:78:13 | ||
| | ||
LL | let _ = mem::replace(&mut *f, None); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `(*f).take()` | ||
|
||
error: replacing an `Option` with `None` | ||
--> $DIR/mem_replace_no_std.rs:79:13 | ||
| | ||
LL | let _ = mem::replace(&mut b.opt, None); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `b.opt.take()` | ||
|
||
error: aborting due to 7 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#![warn(clippy::ptr_eq)] | ||
#![no_std] | ||
#![feature(lang_items)] | ||
|
||
use core::panic::PanicInfo; | ||
|
||
#[lang = "eh_personality"] | ||
extern "C" fn eh_personality() {} | ||
|
||
#[panic_handler] | ||
fn panic(info: &PanicInfo) -> ! { | ||
loop {} | ||
} | ||
|
||
macro_rules! mac { | ||
($a:expr, $b:expr) => { | ||
$a as *const _ as usize == $b as *const _ as usize | ||
}; | ||
} | ||
|
||
macro_rules! another_mac { | ||
($a:expr, $b:expr) => { | ||
$a as *const _ == $b as *const _ | ||
}; | ||
} | ||
|
||
fn main() { | ||
let a = &[1, 2, 3]; | ||
let b = &[1, 2, 3]; | ||
|
||
let _ = core::ptr::eq(a, b); | ||
let _ = core::ptr::eq(a, b); | ||
let _ = a.as_ptr() == b as *const _; | ||
let _ = a.as_ptr() == b.as_ptr(); | ||
|
||
// Do not lint | ||
|
||
let _ = mac!(a, b); | ||
let _ = another_mac!(a, b); | ||
|
||
let a = &mut [1, 2, 3]; | ||
let b = &mut [1, 2, 3]; | ||
|
||
let _ = a.as_mut_ptr() == b as *mut [i32] as *mut _; | ||
let _ = a.as_mut_ptr() == b.as_mut_ptr(); | ||
|
||
let _ = a == b; | ||
let _ = core::ptr::eq(a, b); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#![warn(clippy::ptr_eq)] | ||
#![no_std] | ||
#![feature(lang_items)] | ||
|
||
use core::panic::PanicInfo; | ||
|
||
#[lang = "eh_personality"] | ||
extern "C" fn eh_personality() {} | ||
|
||
#[panic_handler] | ||
fn panic(info: &PanicInfo) -> ! { | ||
loop {} | ||
} | ||
|
||
macro_rules! mac { | ||
($a:expr, $b:expr) => { | ||
$a as *const _ as usize == $b as *const _ as usize | ||
}; | ||
} | ||
|
||
macro_rules! another_mac { | ||
($a:expr, $b:expr) => { | ||
$a as *const _ == $b as *const _ | ||
}; | ||
} | ||
|
||
fn main() { | ||
let a = &[1, 2, 3]; | ||
let b = &[1, 2, 3]; | ||
|
||
let _ = a as *const _ as usize == b as *const _ as usize; | ||
let _ = a as *const _ == b as *const _; | ||
let _ = a.as_ptr() == b as *const _; | ||
let _ = a.as_ptr() == b.as_ptr(); | ||
|
||
// Do not lint | ||
|
||
let _ = mac!(a, b); | ||
let _ = another_mac!(a, b); | ||
|
||
let a = &mut [1, 2, 3]; | ||
let b = &mut [1, 2, 3]; | ||
|
||
let _ = a.as_mut_ptr() == b as *mut [i32] as *mut _; | ||
let _ = a.as_mut_ptr() == b.as_mut_ptr(); | ||
|
||
let _ = a == b; | ||
let _ = core::ptr::eq(a, b); | ||
} |
Oops, something went wrong.