forked from rust-lang/rust-clippy
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Changes: ```` rustup to rust-lang/rust#70043 map_clone: avoid suggesting `copied()` for &mut fix redundant_pattern_matching lint Add tests for rust-lang#1654 Don't trigger while_let_on_iterator when the iterator is recreated every iteration Update issue_2356.stderr reference file Update while_let_on_iterator tests Fix while_let_on_iterator suggestion and make it MachineApplicable Add lifetime test case for `new_ret_no_self` rustup rust-lang/rust#71215 Downgrade match_bool to pedantic Run fetch before testing if master contains beta The beta branch update should not require a force push Add a note to the beta sections of release.md Remove apt-get upgrade again Always use the deploy script and templates of the master branch README: fix lit count line clippy_dev: make it fatal when the regex for updating lint count does not match `predecessors_for` will be removed soon Rustup "Remove `BodyAndCache`" Only run (late) internal lints, when they are warn/deny/forbid Only run cargo lints, when they are warn/deny/forbid span_lint_and_note now takes an Option<Span> for the note_span instead of just a span Make lint also capture blocks and closures, adjust language to mention other mutex types don't test the code in the lint docs Switch to matching against full paths instead of just the last element of the path Lint for holding locks across await points Also mention `--fix` for nightly users fix crash on issue-69020-assoc-const-arith-overflow.rs Address review comments remark fixes Update CHANGELOG.md for Rust 1.43 and 1.44 update stderr file util/fetch_prs_between.sh: Add Markdown formatted Link factor ifs into function, add differing mutex test Update the changelog update documentation Apply suggestions from PR review update span_lint_and_help call to six args test for mutex eq, add another test case use if chain cargo dev fmt fix map import to rustc_middle dev update_lints fix internal clippy warnings change visitor name to OppVisitor use Visitor api to find Mutex::lock calls add note about update-all-refs script, revert redundant pat to master move closures to seperate fns, remove known problems use span_lint_and_help, cargo dev fmt creating suggestion progress work on suggestion for auto fix Implement unsafe_derive_deserialize lint Update empty_enum.stderr Formatting and naming Formatting and naming Cleanup: `node_id` -> `hir_id` Fix issue rust-lang#2907. Don't trigger toplevel_ref_arg for `for` loops Cleanup: future_not_send: use `return_ty` method Remove badge FIXME from Cargo.toml Change note_span argument for span_lint_and_note. Add an Option<Span> argument to span_lint_and_help. Fixes internal lint warning in code base. Implement collapsible_span_lint_calls lint. ```` Fixes #71453
- Loading branch information
1 parent
6b96dd1
commit 6567459
Showing
113 changed files
with
2,216 additions
and
432 deletions.
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
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,97 @@ | ||
use crate::utils::{match_def_path, paths, span_lint_and_note}; | ||
use rustc_hir::def_id::DefId; | ||
use rustc_hir::{AsyncGeneratorKind, Body, BodyId, GeneratorKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_middle::ty::GeneratorInteriorTypeCause; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::Span; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks for calls to await while holding a | ||
/// non-async-aware MutexGuard. | ||
/// | ||
/// **Why is this bad?** The Mutex types found in syd::sync and parking_lot | ||
/// are not designed to operator in an async context across await points. | ||
/// | ||
/// There are two potential solutions. One is to use an asynx-aware Mutex | ||
/// type. Many asynchronous foundation crates provide such a Mutex type. The | ||
/// other solution is to ensure the mutex is unlocked before calling await, | ||
/// either by introducing a scope or an explicit call to Drop::drop. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust,ignore | ||
/// use std::sync::Mutex; | ||
/// | ||
/// async fn foo(x: &Mutex<u32>) { | ||
/// let guard = x.lock().unwrap(); | ||
/// *guard += 1; | ||
/// bar.await; | ||
/// } | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```rust,ignore | ||
/// use std::sync::Mutex; | ||
/// | ||
/// async fn foo(x: &Mutex<u32>) { | ||
/// { | ||
/// let guard = x.lock().unwrap(); | ||
/// *guard += 1; | ||
/// } | ||
/// bar.await; | ||
/// } | ||
/// ``` | ||
pub AWAIT_HOLDING_LOCK, | ||
pedantic, | ||
"Inside an async function, holding a MutexGuard while calling await" | ||
} | ||
|
||
declare_lint_pass!(AwaitHoldingLock => [AWAIT_HOLDING_LOCK]); | ||
|
||
impl LateLintPass<'_, '_> for AwaitHoldingLock { | ||
fn check_body(&mut self, cx: &LateContext<'_, '_>, body: &'_ Body<'_>) { | ||
use AsyncGeneratorKind::{Block, Closure, Fn}; | ||
match body.generator_kind { | ||
Some(GeneratorKind::Async(Block)) | ||
| Some(GeneratorKind::Async(Closure)) | ||
| Some(GeneratorKind::Async(Fn)) => { | ||
let body_id = BodyId { | ||
hir_id: body.value.hir_id, | ||
}; | ||
let def_id = cx.tcx.hir().body_owner_def_id(body_id); | ||
let tables = cx.tcx.typeck_tables_of(def_id); | ||
check_interior_types(cx, &tables.generator_interior_types, body.value.span); | ||
}, | ||
_ => {}, | ||
} | ||
} | ||
} | ||
|
||
fn check_interior_types(cx: &LateContext<'_, '_>, ty_causes: &[GeneratorInteriorTypeCause<'_>], span: Span) { | ||
for ty_cause in ty_causes { | ||
if let rustc_middle::ty::Adt(adt, _) = ty_cause.ty.kind { | ||
if is_mutex_guard(cx, adt.did) { | ||
span_lint_and_note( | ||
cx, | ||
AWAIT_HOLDING_LOCK, | ||
ty_cause.span, | ||
"this MutexGuard is held across an 'await' point. Consider using an async-aware Mutex type or ensuring the MutexGuard is dropped before calling await.", | ||
ty_cause.scope_span.or(Some(span)), | ||
"these are all the await points this lock is held through", | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn is_mutex_guard(cx: &LateContext<'_, '_>, def_id: DefId) -> bool { | ||
match_def_path(cx, def_id, &paths::MUTEX_GUARD) | ||
|| match_def_path(cx, def_id, &paths::RWLOCK_READ_GUARD) | ||
|| match_def_path(cx, def_id, &paths::RWLOCK_WRITE_GUARD) | ||
|| match_def_path(cx, def_id, &paths::PARKING_LOT_MUTEX_GUARD) | ||
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_READ_GUARD) | ||
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_WRITE_GUARD) | ||
} |
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.