-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
WIP New lint: borrowed_option #11463
Draft
tom-anders
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
tom-anders:borrowed_option
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 |
---|---|---|
|
@@ -188,6 +188,37 @@ declare_clippy_lint! { | |
"a borrow of a boxed type" | ||
} | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for usage of [`&Option<T>`](https://doc.rust-lang.org/std/option/index.html) anywhere in the code. | ||
/// | ||
/// ### Why is this bad? | ||
/// An `&Option<T>` parameter prevents calling the function if the caller holds a different type, e.g. `Result<T, E>`. | ||
/// Using `Option<&T>` generalizes the function, e.g. allowing to pass `res.ok().as_ref()` | ||
/// Returning `&Option<T>` needlessly exposes implementation details and has no advantage over `Option<&T>`. | ||
/// | ||
/// ### Example | ||
/// ```rust,compile_fail | ||
/// fn foo(bar: &Option<i32>) -> &Option<i32> { bar } | ||
/// fn call_foo(bar: &Result<i32, ()>) { | ||
/// foo(bar.ok()); // does not work | ||
/// } | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// | ||
/// ```rust | ||
/// fn foo(bar: Option<&i32>) -> Option<&i32> { bar } | ||
/// fn call_foo(bar: &Result<i32, ()>) { | ||
/// foo(bar.ok().as_ref()); // works! | ||
/// } | ||
/// ``` | ||
Comment on lines
+201
to
+215
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. Let's go with a |
||
#[clippy::version = "1.74.0"] | ||
pub BORROWED_OPTION, | ||
complexity, | ||
"`&Option<T>` instead of `Option<&T>`" | ||
} | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for usage of redundant allocations anywhere in the code. | ||
|
@@ -309,7 +340,7 @@ pub struct Types { | |
avoid_breaking_exported_api: bool, | ||
} | ||
|
||
impl_lint_pass!(Types => [BOX_COLLECTION, VEC_BOX, OPTION_OPTION, LINKEDLIST, BORROWED_BOX, REDUNDANT_ALLOCATION, RC_BUFFER, RC_MUTEX, TYPE_COMPLEXITY]); | ||
impl_lint_pass!(Types => [BOX_COLLECTION, VEC_BOX, OPTION_OPTION, LINKEDLIST, BORROWED_BOX, BORROWED_OPTION, REDUNDANT_ALLOCATION, RC_BUFFER, RC_MUTEX, TYPE_COMPLEXITY]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for Types { | ||
fn check_fn( | ||
|
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 |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
|
||
use std::fmt::Display; | ||
|
||
pub fn test1(foo: &mut Box<bool>) { | ||
pub fn test1(foo: &mut Box<bool>, bar: &mut Option<bool>) { | ||
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. The tests for |
||
// Although this function could be changed to "&mut bool", | ||
// avoiding the Box, mutable references to boxes are not | ||
// flagged by this lint. | ||
|
@@ -18,26 +18,33 @@ pub fn test1(foo: &mut Box<bool>) { | |
// the memory location of the pointed-to object could be | ||
// modified. By passing a mutable reference, the contents | ||
// could change, but not the location. | ||
println!("{:?}", foo) | ||
// | ||
// The same reasoning applies to ignoring &mut Option | ||
println!("{:?} {:?}", foo, bar) | ||
} | ||
|
||
pub fn test2() { | ||
let foo: &Box<bool>; | ||
//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T` | ||
let bar: &Option<bool>; | ||
//~^ ERROR: you seem to be trying to use `&Option<T>`. Consider using `Option<&T>` instead | ||
} | ||
|
||
struct Test3<'a> { | ||
foo: &'a Box<bool>, | ||
//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T` | ||
bar: &'a Option<bool>, | ||
//~^ ERROR: you seem to be trying to use `&Option<T>`. Consider using `Option<&T>` instead | ||
} | ||
|
||
trait Test4 { | ||
fn test4(a: &Box<bool>); | ||
fn test4(a: &Box<bool>, b: &Option<bool>); | ||
//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T` | ||
//~| ERROR: you seem to be trying to use `&Option<T>`. Consider using `Option<&T>` instead | ||
} | ||
|
||
impl<'a> Test4 for Test3<'a> { | ||
fn test4(a: &Box<bool>) { | ||
fn test4(a: &Box<bool>, b: &Option<bool>) { | ||
unimplemented!(); | ||
} | ||
} | ||
|
@@ -106,12 +113,15 @@ pub fn test15(_display: &Box<dyn Display + Send>) {} | |
pub fn test16<'a>(_display: &'a Box<dyn Display + 'a>) {} | ||
//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T` | ||
|
||
pub fn test17(_display: &Box<impl Display>) {} | ||
pub fn test17(_display: &Box<impl Display>, _display2: &Option<impl Display>) {} | ||
//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T` | ||
pub fn test18(_display: &Box<impl Display + Send>) {} | ||
//~| ERROR: you seem to be trying to use `&Option<T>`. Consider using `Option<&T>` instead | ||
pub fn test18(_display: &Box<impl Display + Send>, _display2: &Option<impl Display + Send>) {} | ||
//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T` | ||
pub fn test19<'a>(_display: &'a Box<impl Display + 'a>) {} | ||
//~| ERROR: you seem to be trying to use `&Option<T>`. Consider using `Option<&T>` instead | ||
pub fn test19<'a>(_display: &'a Box<impl Display + 'a>, _display2: &'a Option<impl Display + 'a>) {} | ||
//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T` | ||
//~| ERROR: you seem to be trying to use `&Option<T>`. Consider using `Option<&T>` instead | ||
|
||
// This exists only to check what happens when parentheses are already present. | ||
// Even though the current implementation doesn't put extra parentheses, | ||
|
@@ -120,7 +130,7 @@ pub fn test20(_display: &Box<(dyn Display + Send)>) {} | |
//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T` | ||
|
||
fn main() { | ||
test1(&mut Box::new(false)); | ||
test1(&mut Box::new(false), &mut Some(false)); | ||
test2(); | ||
test5(&mut (Box::new(false) as Box<dyn Any>)); | ||
test6(); | ||
|
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
Oops, something went wrong.
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.
You could mention that
&Option<T>
requires dereferencing to determine the variant whereasOption<&T>
does not