-
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
Add lint for holding RefCell Ref across an await #6029
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @yaahc (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
☔ The latest upstream changes (presumably #6036) made this pull request unmergeable. Please resolve the merge conflicts. Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:
|
@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author |
Error: Label S-waiting-on-review can only be set by Rust team members Please let |
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 great, just one test I think we should add before we can merge this. Also, I went ahead and created a zulip topic to discuss the lint group this should go in.
/// } | ||
/// ``` | ||
pub AWAIT_HOLDING_REFCELL_REF, | ||
pedantic, |
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 think this might be worth putting in correctness
, I notice that the mutex PR you linked is also pedantic. We should definitely be consistent, but that might mean creating a diff PR to move both lints from pedantic to correctness after this PR merges.
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.
My vote would be to make a separate PR changing both at the same time as that would give us time to do a crater run and would be easier to roll back in case of false positives.
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 LGTM. Regarding moving this to correctness: It doesn't really matter if we do it in a separate PR or just in a separate commit. I would even prefer to just add a commit including everything that is necessary for moving it to correctness
in this PR.
fn is_refcell_ref(cx: &LateContext<'_>, def_id: DefId) -> bool { | ||
match_def_path(cx, def_id, &paths::REFCELL_REF) || match_def_path(cx, def_id, &paths::REFCELL_REFMUT) | ||
} |
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.
Since this and the error messages are the only things that differ from the await_holding_lock
lint, I think we should implement those two lints in the same module. You can choose the name of the module ;).
ping from triage @Daniel-B-Smith. Only few changes are left to implement here. Is there anything I can help you with to get this over the finish line? |
Thanks for the ping. I got distracted with some other work. I've merged the lints into one module and marked them as correctness in a separate commit. |
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.
use AsyncGeneratorKind::{Block, Closure, Fn}; | ||
if let Some(GeneratorKind::Async(Block | Closure | Fn)) = body.generator_kind { | ||
let body_id = BodyId { | ||
hir_id: body.value.hir_id, | ||
}; | ||
let def_id = cx.tcx.hir().body_owner_def_id(body_id); | ||
let typeck_results = cx.tcx.typeck(def_id); |
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 is exactly the same code as in line 55. You can just combine the two lint passes.
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.
Done
@@ -90,3 +90,79 @@ fn is_mutex_guard(cx: &LateContext<'_>, def_id: DefId) -> bool { | |||
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_READ_GUARD) | |||
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_WRITE_GUARD) | |||
} | |||
|
|||
declare_clippy_lint! { |
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.
NIT: usually lint declarations are on top of the file
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.
Done
| | ||
LL | / let guard = x.lock().unwrap(); | ||
LL | | baz().await | ||
LL | | } | ||
| |_____^ | ||
|
||
error: aborting due to 4 previous errors | ||
error: this RefCell Ref is held across an 'await' point. Consider ensuring the Ref is dropped before calling await. |
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.
It would be nice to keep the tests separated.
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.
Done. Unfortunately, I don't think I was able to preserve the git history when they were split. I could attempt to squash the commit where I restore the split into the commit where I merged the two files.
4a3084e
to
6b682db
Compare
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.
Squashed the two correctness commits into one
@@ -90,3 +90,79 @@ fn is_mutex_guard(cx: &LateContext<'_>, def_id: DefId) -> bool { | |||
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_READ_GUARD) | |||
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_WRITE_GUARD) | |||
} | |||
|
|||
declare_clippy_lint! { |
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.
Done
use AsyncGeneratorKind::{Block, Closure, Fn}; | ||
if let Some(GeneratorKind::Async(Block | Closure | Fn)) = body.generator_kind { | ||
let body_id = BodyId { | ||
hir_id: body.value.hir_id, | ||
}; | ||
let def_id = cx.tcx.hir().body_owner_def_id(body_id); | ||
let typeck_results = cx.tcx.typeck(def_id); |
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.
Done
| | ||
LL | / let guard = x.lock().unwrap(); | ||
LL | | baz().await | ||
LL | | } | ||
| |_____^ | ||
|
||
error: aborting due to 4 previous errors | ||
error: this RefCell Ref is held across an 'await' point. Consider ensuring the Ref is dropped before calling await. |
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.
Done. Unfortunately, I don't think I was able to preserve the git history when they were split. I could attempt to squash the commit where I restore the split into the commit where I merged the two files.
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.
Thanks! Can you rebase on top of master, to get rid of the merge commit? You will have to force push, but this is ok.
If you want you can squash some commits, but that is not mandatory (make sure to keep 6b682db in a separate commit though).
b46fff5
to
4d33225
Compare
Rebased |
@bors r+ Thanks! |
📌 Commit 4d33225 has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Fixes #6008
This introduces the lint await_holding_refcell_ref. For async functions, we iterate
over all types in generator_interior_types and look for
core::cell::Ref
orcore::cell::RefMut
. If we find one then we emit a lint.Heavily cribs from: #5439
changelog: introduce the await_holding_refcell_ref lint