-
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
Types borrow box #1501
Types borrow box #1501
Conversation
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.
Looks good!
clippy_lints/src/types.rs
Outdated
BOX_VEC, | ||
ast_ty.span, | ||
"you seem to be trying to use `&Box<T>`. Consider using just `&T`", | ||
"replace `&Box<T>` with simply `&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.
Could you use span_suggestion
here?
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.
I just need to read through some other examples and I think I can use span_suggestion
. Something like mcarton@44daa8b?
tests/compile-fail/borrow_box.rs
Outdated
#![allow(boxed_local)] | ||
#![allow(blacklisted_name)] | ||
|
||
pub fn test(foo: &Box<bool>) { //~ ERROR you seem to be trying to use `&Box<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.
For completeness, could you had a test with a &Box<T>
in a struct, in an expression (eg. let foo: &Box<T>
), etc.?
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.
I will add some more exhaustive tests
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.
In the new tests, the type of a binding (e.g. let foo: &Box<bool>
) is not detected by the lint. It seems like the TypePass
needs an implementation for check_expr
in order to catch this; is my understanding correct?
This will also have the effect of adding this check to the other two lints in the same pass (BOX_VEC and LINKEDLIST); that seems fine, but I just want to make sure that is desired.
I am also going to add a test for associated constants/types on a trait.
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.
After thinking about it, let foo: &Box<T>
should probably not be linted about:
let foo: &Box<T> = some_extern_crate::bar();
It's not your fault if the crate returns that. You can't change it.
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.
uhm... let foo: &T = some_extern_crate::bar()
will work just fine, even if that function returns a &Box<T>
: https://is.gd/kdISz8
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.
Good point
So after looking into testing a few more cases, I am wondering what to check. For example, the current For trait items, For impl items, #605 prevents checking methods, and I want to be sure the same is not true for associated types/constants. As of right now I have tried to add the following tests: pub fn test2() {
let foo: &Box<bool>; //~ ERROR you seem to be trying to use `&Box<T>`
}
trait Test4 {
type T;
}
impl<'a> Test4 for Test3<'a> {
type T = &'a Box<bool>; //~ ERROR you seem to be trying to use `&Box<T>`
} But neither is actually detected. Should I implement |
Yes. If you define a trait with one of those it's bad. If you implement a trait with those, you cannot change that, it's not your fault (unless you've also defined the trait, but then you'll have the warning there anyway). |
I think for the first it is |
Are you planning to continue this PR or do you want someone to take it over? |
I would like to finish it, but I don't think I have the time right now. I'm sorry for squating on it. I will watch this PR to see how the rest is done and pick up another issue after my finals. |
No worries! Just wanted to check up on it. |
Just a reminder that you have a PR open here. No need to hurry or anything. |
ab4b5f2
to
4d076ac
Compare
I am going to dedicate more time to this. I rebased onto master, and I notice some changes in how testing works. Are the "annotated source" style tests gone? Should I just add an example for this lint to the |
I think I understand the new system after rereading the contributing file. I will move the test over and get back up to speed with what this lint should cover. |
@mcarton I took a crack at adding a I will work on the remaining |
I think you need to rerun the tests and commit the changes, since you changed the output |
Thank you for sticking with me, I hope after I get through one lint I can make a lot more progress on other lints alone. I had been running only my example, which doesn't update the stderr file. |
I added I am also not perfectly happy with the extra bool flag needed for |
Adds a boolean flag to indicate whether the current type in `check_ty` is in a local declaration, as only the borrowed box lint should consider these types.
f0fc4c0
to
74ebe6e
Compare
Add negative tests for types in local declarations in the `LINKEDLIST` and `BOX_VEC` lints. They share a pass with `BORROWED_BOX` which does check local delclarations.
I added tests to the other lints in the same pass, to make sure they are not checking local declarations. |
clippy_lints/src/types.rs
Outdated
if let Some(def_id) = opt_def_id(def) { | ||
if Some(def_id) == cx.tcx.lang_items.owned_box() { | ||
if_let_chain! {[ | ||
let QPath::Resolved(None, ref path) = *qpath, |
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 can merge the above two ifs into this if let chain.
println!("{:?}", foo) | ||
} | ||
|
||
pub fn test2() { |
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.
Can you add a trait method taking a box reference and an impl to show that only the trait is linted and not the impl?
Two nits, then we are good to go. |
The bool flag is fine with me. You could use an enum if you wanted, but it's not necessary. Maybe just add a comment explaining the argument? |
I added the test and merged the |
Great work. Thanks. |
Lint for #1480 to check for borrowed boxes