-
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
base: master
Are you sure you want to change the base?
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @blyxyas (or someone else) soon. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
Regarding The lint documentation should provide examples of why it does this (this i.e. exposes implementation details). The example given is fine, but some who aren't too familiar with the borrow checker may question why this exposes implementation details. |
We should probably make a new lint type for borrows and references. cc #11464 |
☔ The latest upstream changes (presumably #11460) made this pull request unmergeable. Please resolve the merge conflicts. |
…e::get_placeholders_with_values` <rust-lang/rust-clippy#11463>
* Take `&str` instead of `&String` in `ParamValue::get_placeholders_with_values` <https://rust-lang.github.io/rust-clippy/master/index.html#/ptr_arg> * Take `Option<&DataType>` instead of `&Option<DataType>` in `ParamValue::get_placeholders_with_values` <rust-lang/rust-clippy#11463> * Take `&[_]` instead of `&Vec<_>` in `ParamValues::verify` <https://rust-lang.github.io/rust-clippy/master/index.html#/ptr_arg> --------- Co-authored-by: Andrew Lamb <[email protected]>
Hey @tom-anders, this is a ping from triage, since there hasn't been any activity in some time. Could you rebase the branch on master? If you have any questions, you're always welcome to ask them in this PR or on Zulip. @rustbot author |
7f5486e
to
09929e1
Compare
09929e1
to
6919b2c
Compare
(fixed formatting) |
Perfect, thank you for the swift update :D @rustbot ready |
Hey, this is triage: It looks like @blyxyas is currently busy, let's pick a new reviewer. r? clippy |
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
The tests for borrowed_option
should go in a new test file
/// 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>`. |
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 whereas Option<&T>
does not
/// ```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! | ||
/// } | ||
/// ``` |
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.
Let's go with a String
or anything !Copy
rather than i32
to demonstrate the issue
☔ The latest upstream changes (presumably #13442) made this pull request unmergeable. Please resolve the merge conflicts. |
This is inspired by this excellent video: https://www.youtube.com/watch?v=6c7pZYP_iIE
Some things I'd appreciate feedback on:
borrowed_box.rs
check, since these two lints would share a lot of common code. We should probably rename that file, but to what?borrowed_box_or_option.rs
? (same goes for the tests) (Edit: looks like the corresponding issue has been closed for now, so let's keep it like it is for now?&Box<Any>
(seeborrowed_box
lint andAny
#1884), I'm not sure if this is needed for&Option<Any>
as well.What it does
andWhy is this bad
could probably use some improvements, suggestions are welcome!changelog: new lint: [
borrowed_option
]#11463