Skip to content

Commit

Permalink
Add warning for a parameter list with an attribute but no parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed Feb 7, 2019
1 parent dbc7924 commit 9ad04b9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
26 changes: 19 additions & 7 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -5545,13 +5545,25 @@ impl<'a> Parser<'a> {
params.push(self.parse_ty_param(attrs)?);
} else {
// Check for trailing attributes and stop parsing.
if !attrs.is_empty() && !params.is_empty() {
self.struct_span_err(
attrs[0].span,
&format!("trailing attribute after generic parameter"),
)
.span_label(attrs[0].span, "attributes must go before parameters")
.emit();
if !attrs.is_empty() {
if !params.is_empty() {
self.struct_span_err(
attrs[0].span,
&format!("trailing attribute after generic parameter"),
)
.span_label(attrs[0].span, "attributes must go before parameters")
.emit();
} else {
self.struct_span_err(
attrs[0].span,
&format!("attribute without generic parameters"),
)
.span_label(
attrs[0].span,
"attributes are only permitted when preceding parameters",
)
.emit();
}
}
break
}
Expand Down
4 changes: 1 addition & 3 deletions src/test/ui/attribute-with-no-generics-in-parameter-list.rs
@@ -1,5 +1,3 @@
// run-pass

fn foo<#[attr]>() {} // ok
fn foo<#[attr]>() {} //~ ERROR attribute without generic parameters

fn main() {}
@@ -0,0 +1,8 @@
error: attribute without generic parameters
--> $DIR/attribute-with-no-generics-in-parameter-list.rs:1:8
|
LL | fn foo<#[attr]>() {} //~ ERROR attribute without generic parameters
| ^^^^^^^ attributes are only permitted when preceding parameters

error: aborting due to previous error

0 comments on commit 9ad04b9

Please sign in to comment.