-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
57bf80f
Add lint for holding RefCell Ref across an await
8727169
fmt
070a751
update_lints
0f4abbf
Better naming post copy/paste
b3a427d
Add another test case
3ed69cd
Move existing lint into shared file
ee20eba
Move refcell lint into shared module
d8c6bce
Convert the await holding lints to correctness
86f2b29
Merge lints into one pass
4d33225
Separate tests for each lint
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
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,86 @@ | ||
// edition:2018 | ||
#![warn(clippy::await_holding_refcell_ref)] | ||
|
||
use std::cell::RefCell; | ||
|
||
async fn bad(x: &RefCell<u32>) -> u32 { | ||
let b = x.borrow(); | ||
baz().await | ||
} | ||
|
||
async fn bad_mut(x: &RefCell<u32>) -> u32 { | ||
let b = x.borrow_mut(); | ||
baz().await | ||
} | ||
|
||
async fn good(x: &RefCell<u32>) -> u32 { | ||
{ | ||
let b = x.borrow_mut(); | ||
let y = *b + 1; | ||
} | ||
baz().await; | ||
let b = x.borrow_mut(); | ||
47 | ||
} | ||
|
||
async fn baz() -> u32 { | ||
42 | ||
} | ||
|
||
async fn also_bad(x: &RefCell<u32>) -> u32 { | ||
let first = baz().await; | ||
|
||
let b = x.borrow_mut(); | ||
|
||
let second = baz().await; | ||
|
||
let third = baz().await; | ||
|
||
first + second + third | ||
} | ||
flip1995 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
async fn less_bad(x: &RefCell<u32>) -> u32 { | ||
let first = baz().await; | ||
|
||
let b = x.borrow_mut(); | ||
|
||
let second = baz().await; | ||
|
||
drop(b); | ||
|
||
let third = baz().await; | ||
|
||
first + second + third | ||
} | ||
|
||
async fn not_good(x: &RefCell<u32>) -> u32 { | ||
let first = baz().await; | ||
|
||
let second = { | ||
let b = x.borrow_mut(); | ||
baz().await | ||
}; | ||
|
||
let third = baz().await; | ||
|
||
first + second + third | ||
} | ||
|
||
#[allow(clippy::manual_async_fn)] | ||
fn block_bad(x: &RefCell<u32>) -> impl std::future::Future<Output = u32> + '_ { | ||
async move { | ||
let b = x.borrow_mut(); | ||
baz().await | ||
} | ||
} | ||
|
||
fn main() { | ||
let rc = RefCell::new(100); | ||
good(&rc); | ||
bad(&rc); | ||
bad_mut(&rc); | ||
also_bad(&rc); | ||
less_bad(&rc); | ||
not_good(&rc); | ||
block_bad(&rc); | ||
} |
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,95 @@ | ||
error: this RefCell Ref is held across an 'await' point. Consider ensuring the Ref is dropped before calling await. | ||
--> $DIR/await_holding_refcell_ref.rs:7:9 | ||
| | ||
LL | let b = x.borrow(); | ||
| ^ | ||
| | ||
= note: `-D clippy::await-holding-refcell-ref` implied by `-D warnings` | ||
note: these are all the await points this ref is held through | ||
--> $DIR/await_holding_refcell_ref.rs:7:5 | ||
| | ||
LL | / let b = x.borrow(); | ||
LL | | baz().await | ||
LL | | } | ||
| |_^ | ||
|
||
error: this RefCell Ref is held across an 'await' point. Consider ensuring the Ref is dropped before calling await. | ||
--> $DIR/await_holding_refcell_ref.rs:12:9 | ||
| | ||
LL | let b = x.borrow_mut(); | ||
| ^ | ||
| | ||
note: these are all the await points this ref is held through | ||
--> $DIR/await_holding_refcell_ref.rs:12:5 | ||
| | ||
LL | / let b = x.borrow_mut(); | ||
LL | | baz().await | ||
LL | | } | ||
| |_^ | ||
|
||
error: this RefCell Ref is held across an 'await' point. Consider ensuring the Ref is dropped before calling await. | ||
--> $DIR/await_holding_refcell_ref.rs:33:9 | ||
| | ||
LL | let b = x.borrow_mut(); | ||
| ^ | ||
| | ||
note: these are all the await points this ref is held through | ||
--> $DIR/await_holding_refcell_ref.rs:33:5 | ||
| | ||
LL | / let b = x.borrow_mut(); | ||
LL | | | ||
LL | | let second = baz().await; | ||
LL | | | ||
... | | ||
LL | | first + second + third | ||
LL | | } | ||
| |_^ | ||
|
||
error: this RefCell Ref is held across an 'await' point. Consider ensuring the Ref is dropped before calling await. | ||
--> $DIR/await_holding_refcell_ref.rs:45:9 | ||
| | ||
LL | let b = x.borrow_mut(); | ||
| ^ | ||
| | ||
note: these are all the await points this ref is held through | ||
--> $DIR/await_holding_refcell_ref.rs:45:5 | ||
| | ||
LL | / let b = x.borrow_mut(); | ||
LL | | | ||
LL | | let second = baz().await; | ||
LL | | | ||
... | | ||
LL | | first + second + third | ||
LL | | } | ||
| |_^ | ||
|
||
error: this RefCell Ref is held across an 'await' point. Consider ensuring the Ref is dropped before calling await. | ||
--> $DIR/await_holding_refcell_ref.rs:60:13 | ||
| | ||
LL | let b = x.borrow_mut(); | ||
| ^ | ||
| | ||
note: these are all the await points this ref is held through | ||
--> $DIR/await_holding_refcell_ref.rs:60:9 | ||
| | ||
LL | / let b = x.borrow_mut(); | ||
LL | | baz().await | ||
LL | | }; | ||
| |_____^ | ||
|
||
error: this RefCell Ref is held across an 'await' point. Consider ensuring the Ref is dropped before calling await. | ||
--> $DIR/await_holding_refcell_ref.rs:72:13 | ||
| | ||
LL | let b = x.borrow_mut(); | ||
| ^ | ||
| | ||
note: these are all the await points this ref is held through | ||
--> $DIR/await_holding_refcell_ref.rs:72:9 | ||
| | ||
LL | / let b = x.borrow_mut(); | ||
LL | | baz().await | ||
LL | | } | ||
| |_____^ | ||
|
||
error: aborting due to 6 previous errors | ||
|
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.
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