Improve code based on feedback.
This patch improves the readability of some of the code by using if-let-chains. Also, make use of the `add_feature_diagnostics` function.
This commit is contained in:
parent
f702e89f9d
commit
9650a4168f
6 changed files with 38 additions and 62 deletions
|
@ -662,29 +662,27 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||||
span,
|
span,
|
||||||
self.allow_gen_future.clone(),
|
self.allow_gen_future.clone(),
|
||||||
);
|
);
|
||||||
if self.tcx.features().closure_track_caller {
|
|
||||||
let track_caller = self
|
if self.tcx.features().closure_track_caller
|
||||||
.attrs
|
&& let Some(attrs) = self.attrs.get(&outer_hir_id.local_id)
|
||||||
.get(&outer_hir_id.local_id)
|
&& attrs.into_iter().any(|attr| attr.has_name(sym::track_caller))
|
||||||
.map_or(false, |attrs| attrs.into_iter().any(|attr| attr.has_name(sym::track_caller)));
|
{
|
||||||
if track_caller {
|
self.lower_attrs(
|
||||||
self.lower_attrs(
|
hir_id,
|
||||||
hir_id,
|
&[Attribute {
|
||||||
&[Attribute {
|
kind: AttrKind::Normal(ptr::P(NormalAttr {
|
||||||
kind: AttrKind::Normal(ptr::P(NormalAttr {
|
item: AttrItem {
|
||||||
item: AttrItem {
|
path: Path::from_ident(Ident::new(sym::track_caller, span)),
|
||||||
path: Path::from_ident(Ident::new(sym::track_caller, span)),
|
args: AttrArgs::Empty,
|
||||||
args: AttrArgs::Empty,
|
|
||||||
tokens: None,
|
|
||||||
},
|
|
||||||
tokens: None,
|
tokens: None,
|
||||||
})),
|
},
|
||||||
id: self.tcx.sess.parse_sess.attr_id_generator.mk_attr_id(),
|
tokens: None,
|
||||||
style: AttrStyle::Outer,
|
})),
|
||||||
span: unstable_span,
|
id: self.tcx.sess.parse_sess.attr_id_generator.mk_attr_id(),
|
||||||
}],
|
style: AttrStyle::Outer,
|
||||||
);
|
span: unstable_span,
|
||||||
}
|
}],
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let generator = hir::Expr { hir_id, kind: generator_kind, span: self.lower_span(span) };
|
let generator = hir::Expr { hir_id, kind: generator_kind, span: self.lower_span(span) };
|
||||||
|
|
|
@ -351,7 +351,7 @@ lint_builtin_mutable_transmutes =
|
||||||
lint_builtin_unstable_features = unstable feature
|
lint_builtin_unstable_features = unstable feature
|
||||||
|
|
||||||
lint_ungated_async_fn_track_caller = `#[track_caller]` on async functions is a no-op
|
lint_ungated_async_fn_track_caller = `#[track_caller]` on async functions is a no-op
|
||||||
.suggestion = enable this unstable feature
|
.label = this function will not propagate the caller location
|
||||||
|
|
||||||
lint_builtin_unreachable_pub = unreachable `pub` {$what}
|
lint_builtin_unreachable_pub = unreachable `pub` {$what}
|
||||||
.suggestion = consider restricting its visibility
|
.suggestion = consider restricting its visibility
|
||||||
|
|
|
@ -1416,33 +1416,27 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller {
|
||||||
span: Span,
|
span: Span,
|
||||||
hir_id: HirId,
|
hir_id: HirId,
|
||||||
) {
|
) {
|
||||||
if fn_kind.asyncness() == IsAsync::Async && !cx.tcx.features().closure_track_caller {
|
if fn_kind.asyncness() == IsAsync::Async
|
||||||
|
&& !cx.tcx.features().closure_track_caller
|
||||||
|
&& let attrs = cx.tcx.hir().attrs(hir_id)
|
||||||
// Now, check if the function has the `#[track_caller]` attribute
|
// Now, check if the function has the `#[track_caller]` attribute
|
||||||
let attrs = cx.tcx.hir().attrs(hir_id);
|
&& let Some(attr) = attrs.iter().find(|attr| attr.has_name(sym::track_caller))
|
||||||
let maybe_track_caller = attrs.iter().find(|attr| attr.has_name(sym::track_caller));
|
{
|
||||||
if let Some(attr) = maybe_track_caller {
|
|
||||||
cx.struct_span_lint(
|
cx.struct_span_lint(
|
||||||
UNGATED_ASYNC_FN_TRACK_CALLER,
|
UNGATED_ASYNC_FN_TRACK_CALLER,
|
||||||
attr.span,
|
attr.span,
|
||||||
fluent::lint_ungated_async_fn_track_caller,
|
fluent::lint_ungated_async_fn_track_caller,
|
||||||
|lint| {
|
|lint| {
|
||||||
lint.span_label(
|
lint.span_label(span, fluent::label);
|
||||||
span,
|
rustc_session::parse::add_feature_diagnostics(
|
||||||
"this function will not propagate the caller location",
|
lint,
|
||||||
|
&cx.tcx.sess.parse_sess,
|
||||||
|
sym::closure_track_caller,
|
||||||
);
|
);
|
||||||
if cx.tcx.sess.is_nightly_build() {
|
|
||||||
lint.span_suggestion(
|
|
||||||
attr.span,
|
|
||||||
fluent::suggestion,
|
|
||||||
"closure_track_caller",
|
|
||||||
Applicability::MachineApplicable,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
lint
|
lint
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
// check-pass
|
|
||||||
// edition:2021
|
|
||||||
|
|
||||||
#[track_caller] //~ WARN `#[track_caller]` on async functions is a no-op
|
|
||||||
async fn foo() {}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
foo();
|
|
||||||
}
|
|
|
@ -1,12 +0,0 @@
|
||||||
warning: `#[track_caller]` on async functions is a no-op
|
|
||||||
--> $DIR/issue-104588-no-op-track-caller.rs:4:1
|
|
||||||
|
|
|
||||||
LL | #[track_caller]
|
|
||||||
| ^^^^^^^^^^^^^^^ help: enable this unstable feature: `closure_track_caller`
|
|
||||||
LL | async fn foo() {}
|
|
||||||
| ----------------- this function will not propagate the caller location
|
|
||||||
|
|
|
||||||
= note: `#[warn(ungated_async_fn_track_caller)]` on by default
|
|
||||||
|
|
||||||
warning: 1 warning emitted
|
|
||||||
|
|
|
@ -2,23 +2,28 @@ warning: `#[track_caller]` on async functions is a no-op
|
||||||
--> $DIR/panic-track-caller.rs:50:1
|
--> $DIR/panic-track-caller.rs:50:1
|
||||||
|
|
|
|
||||||
LL | #[track_caller]
|
LL | #[track_caller]
|
||||||
| ^^^^^^^^^^^^^^^ help: enable this unstable feature: `closure_track_caller`
|
| ^^^^^^^^^^^^^^^
|
||||||
LL | / async fn bar_track_caller() {
|
LL | / async fn bar_track_caller() {
|
||||||
LL | | panic!()
|
LL | | panic!()
|
||||||
LL | | }
|
LL | | }
|
||||||
| |_- this function will not propagate the caller location
|
| |_- this function will not propagate the caller location
|
||||||
|
|
|
|
||||||
|
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
|
||||||
|
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
|
||||||
= note: `#[warn(ungated_async_fn_track_caller)]` on by default
|
= note: `#[warn(ungated_async_fn_track_caller)]` on by default
|
||||||
|
|
||||||
warning: `#[track_caller]` on async functions is a no-op
|
warning: `#[track_caller]` on async functions is a no-op
|
||||||
--> $DIR/panic-track-caller.rs:62:5
|
--> $DIR/panic-track-caller.rs:62:5
|
||||||
|
|
|
|
||||||
LL | #[track_caller]
|
LL | #[track_caller]
|
||||||
| ^^^^^^^^^^^^^^^ help: enable this unstable feature: `closure_track_caller`
|
| ^^^^^^^^^^^^^^^
|
||||||
LL | / async fn bar_assoc() {
|
LL | / async fn bar_assoc() {
|
||||||
LL | | panic!();
|
LL | | panic!();
|
||||||
LL | | }
|
LL | | }
|
||||||
| |_____- this function will not propagate the caller location
|
| |_____- this function will not propagate the caller location
|
||||||
|
|
|
||||||
|
= note: see issue #87417 <https://github.com/rust-lang/rust/issues/87417> for more information
|
||||||
|
= help: add `#![feature(closure_track_caller)]` to the crate attributes to enable
|
||||||
|
|
||||||
warning: 2 warnings emitted
|
warning: 2 warnings emitted
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue