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:
Bryan Garza 2022-12-21 03:13:28 +00:00
parent f702e89f9d
commit 9650a4168f
6 changed files with 38 additions and 62 deletions

View File

@ -662,29 +662,27 @@ impl<'hir> LoweringContext<'_, 'hir> {
span,
self.allow_gen_future.clone(),
);
if self.tcx.features().closure_track_caller {
let track_caller = self
.attrs
.get(&outer_hir_id.local_id)
.map_or(false, |attrs| attrs.into_iter().any(|attr| attr.has_name(sym::track_caller)));
if track_caller {
self.lower_attrs(
hir_id,
&[Attribute {
kind: AttrKind::Normal(ptr::P(NormalAttr {
item: AttrItem {
path: Path::from_ident(Ident::new(sym::track_caller, span)),
args: AttrArgs::Empty,
tokens: None,
},
if self.tcx.features().closure_track_caller
&& let Some(attrs) = self.attrs.get(&outer_hir_id.local_id)
&& attrs.into_iter().any(|attr| attr.has_name(sym::track_caller))
{
self.lower_attrs(
hir_id,
&[Attribute {
kind: AttrKind::Normal(ptr::P(NormalAttr {
item: AttrItem {
path: Path::from_ident(Ident::new(sym::track_caller, span)),
args: AttrArgs::Empty,
tokens: None,
})),
id: self.tcx.sess.parse_sess.attr_id_generator.mk_attr_id(),
style: AttrStyle::Outer,
span: unstable_span,
}],
);
}
},
tokens: None,
})),
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) };

View File

@ -351,7 +351,7 @@ lint_builtin_mutable_transmutes =
lint_builtin_unstable_features = unstable feature
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}
.suggestion = consider restricting its visibility

View File

@ -1416,33 +1416,27 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller {
span: Span,
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
let attrs = cx.tcx.hir().attrs(hir_id);
let maybe_track_caller = attrs.iter().find(|attr| attr.has_name(sym::track_caller));
if let Some(attr) = maybe_track_caller {
&& let Some(attr) = attrs.iter().find(|attr| attr.has_name(sym::track_caller))
{
cx.struct_span_lint(
UNGATED_ASYNC_FN_TRACK_CALLER,
attr.span,
fluent::lint_ungated_async_fn_track_caller,
|lint| {
lint.span_label(
span,
"this function will not propagate the caller location",
lint.span_label(span, fluent::label);
rustc_session::parse::add_feature_diagnostics(
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
},
);
}
}
}
}

View File

@ -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();
}

View File

@ -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

View File

@ -2,23 +2,28 @@ warning: `#[track_caller]` on async functions is a no-op
--> $DIR/panic-track-caller.rs:50:1
|
LL | #[track_caller]
| ^^^^^^^^^^^^^^^ help: enable this unstable feature: `closure_track_caller`
| ^^^^^^^^^^^^^^^
LL | / async fn bar_track_caller() {
LL | | panic!()
LL | | }
| |_- 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
warning: `#[track_caller]` on async functions is a no-op
--> $DIR/panic-track-caller.rs:62:5
|
LL | #[track_caller]
| ^^^^^^^^^^^^^^^ help: enable this unstable feature: `closure_track_caller`
| ^^^^^^^^^^^^^^^
LL | / async fn bar_assoc() {
LL | | panic!();
LL | | }
| |_____- 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