-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Correctly suggest std or core path depending if this is a no_std
crate
#12149
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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
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 |
---|---|---|
|
@@ -204,7 +204,7 @@ pub(super) fn check<'tcx>( | |
cx, | ||
UNNECESSARY_SORT_BY, | ||
expr.span, | ||
"use Vec::sort_by_key here instead", | ||
"consider using `sort_by_key`", | ||
"try", | ||
format!( | ||
"{}.sort{}_by_key(|{}| {})", | ||
|
@@ -227,7 +227,7 @@ pub(super) fn check<'tcx>( | |
cx, | ||
UNNECESSARY_SORT_BY, | ||
expr.span, | ||
"use Vec::sort here instead", | ||
"consider using `sort`", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
"try", | ||
format!( | ||
"{}.sort{}()", | ||
|
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
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
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
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()); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method comes from slices so doesn't make much sense to only write
Vec::
.