Skip to content

Commit 4844325

Browse files
Add await_holding_invalid_type lint
changelog: [`await_holding_invalid_type`] This lint allows users to create a denylist of types which are not allowed to be held across await points. This is essentially a re-implementation of the language-level [`must_not_suspend` lint](rust-lang/rust#83310). That lint has a lot of work still to be done before it will reach Rust stable, and in the meantime there are a lot of types which can trip up developers if they are used improperly.
1 parent aade96f commit 4844325

13 files changed

+215
-40
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3182,6 +3182,7 @@ Released 2018-09-13
31823182
[`assign_op_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#assign_op_pattern
31833183
[`assign_ops`]: https://rust-lang.github.io/rust-clippy/master/index.html#assign_ops
31843184
[`async_yields_async`]: https://rust-lang.github.io/rust-clippy/master/index.html#async_yields_async
3185+
[`await_holding_invalid_type`]: https://rust-lang.github.io/rust-clippy/master/index.html#await_holding_invalid_type
31853186
[`await_holding_lock`]: https://rust-lang.github.io/rust-clippy/master/index.html#await_holding_lock
31863187
[`await_holding_refcell_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#await_holding_refcell_ref
31873188
[`bad_bit_mask`]: https://rust-lang.github.io/rust-clippy/master/index.html#bad_bit_mask

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ futures = "0.3"
5050
parking_lot = "0.11.2"
5151
tokio = { version = "1", features = ["io-util"] }
5252
rustc-semver = "1.1"
53+
tracing = "0.1"
5354

5455
[build-dependencies]
5556
rustc_tools_util = { version = "0.2", path = "rustc_tools_util" }

clippy_lints/src/await_holding_invalid.rs

+127-38
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::{match_def_path, paths};
3+
use rustc_data_structures::fx::FxHashMap;
34
use rustc_hir::def_id::DefId;
4-
use rustc_hir::{AsyncGeneratorKind, Body, BodyId, GeneratorKind};
5+
use rustc_hir::{def::Res, AsyncGeneratorKind, Body, BodyId, GeneratorKind};
56
use rustc_lint::{LateContext, LateLintPass};
67
use rustc_middle::ty::GeneratorInteriorTypeCause;
7-
use rustc_session::{declare_lint_pass, declare_tool_lint};
8+
use rustc_session::{declare_tool_lint, impl_lint_pass};
89
use rustc_span::Span;
910

11+
use crate::utils::conf::DisallowedType;
12+
1013
declare_clippy_lint! {
1114
/// ### What it does
1215
/// Checks for calls to await while holding a non-async-aware MutexGuard.
@@ -127,17 +130,81 @@ declare_clippy_lint! {
127130
"inside an async function, holding a `RefCell` ref while calling `await`"
128131
}
129132

130-
declare_lint_pass!(AwaitHolding => [AWAIT_HOLDING_LOCK, AWAIT_HOLDING_REFCELL_REF]);
133+
declare_clippy_lint! {
134+
/// ### What it does
135+
/// Allows users to configure types which should not be held across `await`
136+
/// suspension points.
137+
///
138+
/// ### Why is this bad?
139+
/// There are some types which are perfectly "safe" to be used concurrently
140+
/// from a memory access perspective but will cause bugs at runtime if they
141+
/// are held in such a way.
142+
///
143+
/// ### Known problems
144+
///
145+
/// ### Example
146+
///
147+
/// The `tracing` library has types which should not be held across `await`
148+
/// points.
149+
///
150+
/// ```toml
151+
/// await-holding-invalid-types = [
152+
/// "tracing::span::Entered",
153+
/// "tracing::span::EnteredSpan",
154+
/// ]
155+
/// ```
156+
///
157+
/// ```rust
158+
/// # async fn baz() {}
159+
/// async fn foo() {
160+
/// let _entered = tracing::info_span!("baz").entered();
161+
/// baz().await;
162+
/// }
163+
/// ```
164+
#[clippy::version = "1.49.0"]
165+
pub AWAIT_HOLDING_INVALID_TYPE,
166+
suspicious,
167+
"inside an async function, holding a type across an await point which is not safe to be held across an await point"
168+
}
169+
170+
impl_lint_pass!(AwaitHolding => [AWAIT_HOLDING_LOCK, AWAIT_HOLDING_REFCELL_REF, AWAIT_HOLDING_INVALID_TYPE]);
171+
172+
#[derive(Debug)]
173+
pub struct AwaitHolding {
174+
conf_invalid_types: Vec<DisallowedType>,
175+
def_ids: FxHashMap<DefId, DisallowedType>,
176+
}
177+
178+
impl AwaitHolding {
179+
pub(crate) fn new(conf_invalid_types: Vec<DisallowedType>) -> Self {
180+
Self {
181+
conf_invalid_types,
182+
def_ids: FxHashMap::default(),
183+
}
184+
}
185+
}
131186

132187
impl LateLintPass<'_> for AwaitHolding {
188+
fn check_crate(&mut self, cx: &LateContext<'_>) {
189+
for conf in &self.conf_invalid_types {
190+
let path = match conf {
191+
DisallowedType::Simple(path) | DisallowedType::WithReason { path, .. } => path,
192+
};
193+
let segs: Vec<_> = path.split("::").collect();
194+
if let Res::Def(_, id) = clippy_utils::def_path_res(cx, &segs) {
195+
self.def_ids.insert(id, conf.clone());
196+
}
197+
}
198+
}
199+
133200
fn check_body(&mut self, cx: &LateContext<'_>, body: &'_ Body<'_>) {
134201
use AsyncGeneratorKind::{Block, Closure, Fn};
135202
if let Some(GeneratorKind::Async(Block | Closure | Fn)) = body.generator_kind {
136203
let body_id = BodyId {
137204
hir_id: body.value.hir_id,
138205
};
139206
let typeck_results = cx.tcx.typeck_body(body_id);
140-
check_interior_types(
207+
self.check_interior_types(
141208
cx,
142209
typeck_results.generator_interior_types.as_ref().skip_binder(),
143210
body.value.span,
@@ -146,46 +213,68 @@ impl LateLintPass<'_> for AwaitHolding {
146213
}
147214
}
148215

149-
fn check_interior_types(cx: &LateContext<'_>, ty_causes: &[GeneratorInteriorTypeCause<'_>], span: Span) {
150-
for ty_cause in ty_causes {
151-
if let rustc_middle::ty::Adt(adt, _) = ty_cause.ty.kind() {
152-
if is_mutex_guard(cx, adt.did()) {
153-
span_lint_and_then(
154-
cx,
155-
AWAIT_HOLDING_LOCK,
156-
ty_cause.span,
157-
"this `MutexGuard` is held across an `await` point",
158-
|diag| {
159-
diag.help(
160-
"consider using an async-aware `Mutex` type or ensuring the \
216+
impl AwaitHolding {
217+
fn check_interior_types(&self, cx: &LateContext<'_>, ty_causes: &[GeneratorInteriorTypeCause<'_>], span: Span) {
218+
for ty_cause in ty_causes {
219+
if let rustc_middle::ty::Adt(adt, _) = ty_cause.ty.kind() {
220+
if is_mutex_guard(cx, adt.did()) {
221+
span_lint_and_then(
222+
cx,
223+
AWAIT_HOLDING_LOCK,
224+
ty_cause.span,
225+
"this `MutexGuard` is held across an `await` point",
226+
|diag| {
227+
diag.help(
228+
"consider using an async-aware `Mutex` type or ensuring the \
161229
`MutexGuard` is dropped before calling await",
162-
);
163-
diag.span_note(
164-
ty_cause.scope_span.unwrap_or(span),
165-
"these are all the `await` points this lock is held through",
166-
);
167-
},
168-
);
169-
}
170-
if is_refcell_ref(cx, adt.did()) {
171-
span_lint_and_then(
172-
cx,
173-
AWAIT_HOLDING_REFCELL_REF,
174-
ty_cause.span,
175-
"this `RefCell` reference is held across an `await` point",
176-
|diag| {
177-
diag.help("ensure the reference is dropped before calling `await`");
178-
diag.span_note(
179-
ty_cause.scope_span.unwrap_or(span),
180-
"these are all the `await` points this reference is held through",
181-
);
182-
},
183-
);
230+
);
231+
diag.span_note(
232+
ty_cause.scope_span.unwrap_or(span),
233+
"these are all the `await` points this lock is held through",
234+
);
235+
},
236+
);
237+
} else if is_refcell_ref(cx, adt.did()) {
238+
span_lint_and_then(
239+
cx,
240+
AWAIT_HOLDING_REFCELL_REF,
241+
ty_cause.span,
242+
"this `RefCell` reference is held across an `await` point",
243+
|diag| {
244+
diag.help("ensure the reference is dropped before calling `await`");
245+
diag.span_note(
246+
ty_cause.scope_span.unwrap_or(span),
247+
"these are all the `await` points this reference is held through",
248+
);
249+
},
250+
);
251+
} else if let Some(disallowed) = self.def_ids.get(&adt.did()) {
252+
emit_invalid_type(cx, ty_cause.span, disallowed);
253+
}
184254
}
185255
}
186256
}
187257
}
188258

259+
fn emit_invalid_type(cx: &LateContext<'_>, span: Span, disallowed: &DisallowedType) {
260+
let (type_name, reason) = match disallowed {
261+
DisallowedType::Simple(path) => (path, &None),
262+
DisallowedType::WithReason { path, reason } => (path, reason),
263+
};
264+
265+
span_lint_and_then(
266+
cx,
267+
AWAIT_HOLDING_INVALID_TYPE,
268+
span,
269+
&format!("`{type_name}` may not be held across an `await` point according to config",),
270+
|diag| {
271+
if let Some(reason) = reason {
272+
diag.note(format!("{reason} (according to clippy.toml)"));
273+
}
274+
},
275+
);
276+
}
277+
189278
fn is_mutex_guard(cx: &LateContext<'_>, def_id: DefId) -> bool {
190279
match_def_path(cx, def_id, &paths::MUTEX_GUARD)
191280
|| match_def_path(cx, def_id, &paths::RWLOCK_READ_GUARD)

clippy_lints/src/lib.register_all.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
1414
LintId::of(attrs::DEPRECATED_SEMVER),
1515
LintId::of(attrs::MISMATCHED_TARGET_OS),
1616
LintId::of(attrs::USELESS_ATTRIBUTE),
17+
LintId::of(await_holding_invalid::AWAIT_HOLDING_INVALID_TYPE),
1718
LintId::of(await_holding_invalid::AWAIT_HOLDING_LOCK),
1819
LintId::of(await_holding_invalid::AWAIT_HOLDING_REFCELL_REF),
1920
LintId::of(bit_mask::BAD_BIT_MASK),

clippy_lints/src/lib.register_lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ store.register_lints(&[
5252
attrs::INLINE_ALWAYS,
5353
attrs::MISMATCHED_TARGET_OS,
5454
attrs::USELESS_ATTRIBUTE,
55+
await_holding_invalid::AWAIT_HOLDING_INVALID_TYPE,
5556
await_holding_invalid::AWAIT_HOLDING_LOCK,
5657
await_holding_invalid::AWAIT_HOLDING_REFCELL_REF,
5758
bit_mask::BAD_BIT_MASK,

clippy_lints/src/lib.register_suspicious.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
store.register_group(true, "clippy::suspicious", Some("clippy_suspicious"), vec![
66
LintId::of(assign_ops::MISREFACTORED_ASSIGN_OP),
77
LintId::of(attrs::BLANKET_CLIPPY_RESTRICTION_LINTS),
8+
LintId::of(await_holding_invalid::AWAIT_HOLDING_INVALID_TYPE),
89
LintId::of(await_holding_invalid::AWAIT_HOLDING_LOCK),
910
LintId::of(await_holding_invalid::AWAIT_HOLDING_REFCELL_REF),
1011
LintId::of(casts::CAST_ABS_TO_UNSIGNED),

clippy_lints/src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,12 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
515515
}
516516

517517
store.register_late_pass(|| Box::new(utils::author::Author));
518-
store.register_late_pass(|| Box::new(await_holding_invalid::AwaitHolding));
518+
let await_holding_invalid_types = conf.await_holding_invalid_types.clone();
519+
store.register_late_pass(move || {
520+
Box::new(await_holding_invalid::AwaitHolding::new(
521+
await_holding_invalid_types.clone(),
522+
))
523+
});
519524
store.register_late_pass(|| Box::new(serde_api::SerdeApi));
520525
let vec_box_size_threshold = conf.vec_box_size_threshold;
521526
let type_complexity_threshold = conf.type_complexity_threshold;

clippy_lints/src/utils/conf.rs

+2
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ define_Conf! {
310310
/// the slice pattern that is suggested. If more elements would be necessary, the lint is suppressed.
311311
/// For example, `[_, _, _, e, ..]` is a slice pattern with 4 elements.
312312
(max_suggested_slice_pattern_length: u64 = 3),
313+
/// Lint: AWAIT_HOLDING_INVALID_TYPE
314+
(await_holding_invalid_types: Vec<crate::utils::conf::DisallowedType> = Vec::new()),
313315
}
314316

315317
/// Search for the configuration file.

tests/compile-test.rs

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ static TEST_DEPENDENCIES: &[&str] = &[
3535
"tokio",
3636
"parking_lot",
3737
"rustc_semver",
38+
"tracing",
3839
];
3940

4041
// Test dependencies may need an `extern crate` here to ensure that they show up
@@ -59,6 +60,9 @@ extern crate rustc_semver;
5960
extern crate syn;
6061
#[allow(unused_extern_crates)]
6162
extern crate tokio;
63+
#[allow(unused_extern_crates, unused_imports)]
64+
#[macro_use]
65+
extern crate tracing;
6266

6367
/// Produces a string with an `--extern` flag for all UI test crate
6468
/// dependencies.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#![warn(clippy::await_holding_invalid_type)]
2+
use std::net::Ipv4Addr;
3+
4+
async fn bad() -> u32 {
5+
let _x = String::from("hello");
6+
baz().await
7+
}
8+
9+
async fn bad_reason() -> u32 {
10+
let _x = Ipv4Addr::new(127, 0, 0, 1);
11+
baz().await
12+
}
13+
14+
async fn good() -> u32 {
15+
{
16+
let _x = String::from("hi!");
17+
let _y = Ipv4Addr::new(127, 0, 0, 1);
18+
}
19+
baz().await;
20+
let _x = String::from("hi!");
21+
47
22+
}
23+
24+
async fn baz() -> u32 {
25+
42
26+
}
27+
28+
#[allow(clippy::manual_async_fn)]
29+
fn block_bad() -> impl std::future::Future<Output = u32> {
30+
async move {
31+
let _x = String::from("hi!");
32+
baz().await
33+
}
34+
}
35+
36+
fn main() {
37+
good();
38+
bad();
39+
bad_reason();
40+
block_bad();
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: `std::string::String` may not be held across an `await` point according to config
2+
--> $DIR/await_holding_invalid_type.rs:5:9
3+
|
4+
LL | let _x = String::from("hello");
5+
| ^^
6+
|
7+
= note: `-D clippy::await-holding-invalid-type` implied by `-D warnings`
8+
= note: strings are bad (according to clippy.toml)
9+
10+
error: `std::net::Ipv4Addr` may not be held across an `await` point according to config
11+
--> $DIR/await_holding_invalid_type.rs:10:9
12+
|
13+
LL | let _x = Ipv4Addr::new(127, 0, 0, 1);
14+
| ^^
15+
16+
error: `std::string::String` may not be held across an `await` point according to config
17+
--> $DIR/await_holding_invalid_type.rs:31:13
18+
|
19+
LL | let _x = String::from("hi!");
20+
| ^^
21+
|
22+
= note: strings are bad (according to clippy.toml)
23+
24+
error: aborting due to 3 previous errors
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
await-holding-invalid-types = [
2+
{ path = "std::string::String", reason = "strings are bad" },
3+
"std::net::Ipv4Addr",
4+
]
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `avoid-breaking-exported-api`, `msrv`, `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `pass-by-value-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-trait-bounds`, `max-struct-bools`, `max-fn-params-bools`, `warn-on-all-wildcard-imports`, `disallowed-methods`, `disallowed-types`, `unreadable-literal-lint-fractions`, `upper-case-acronyms-aggressive`, `cargo-ignore-publish`, `standard-macro-braces`, `enforced-import-renames`, `allowed-scripts`, `enable-raw-pointer-heuristic-for-send`, `max-suggested-slice-pattern-length`, `third-party` at line 5 column 1
1+
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `avoid-breaking-exported-api`, `msrv`, `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `pass-by-value-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-trait-bounds`, `max-struct-bools`, `max-fn-params-bools`, `warn-on-all-wildcard-imports`, `disallowed-methods`, `disallowed-types`, `unreadable-literal-lint-fractions`, `upper-case-acronyms-aggressive`, `cargo-ignore-publish`, `standard-macro-braces`, `enforced-import-renames`, `allowed-scripts`, `enable-raw-pointer-heuristic-for-send`, `max-suggested-slice-pattern-length`, `await-holding-invalid-types`, `third-party` at line 5 column 1
22

33
error: aborting due to previous error
44

0 commit comments

Comments
 (0)