Skip to content

Commit 217c073

Browse files
committed
move list parsing into its own function
1 parent 57fd974 commit 217c073

10 files changed

Lines changed: 51 additions & 106 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/diagnostic/mod.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,44 @@ fn merge<T, S: Stage>(
9595
}
9696
}
9797

98+
fn parse_list<'p, S: Stage>(
99+
cx: &mut AcceptContext<'_, '_, S>,
100+
args: &'p ArgParser,
101+
mode: Mode,
102+
) -> Option<&'p MetaItemListParser> {
103+
let span = cx.attr_span;
104+
match args {
105+
ArgParser::List(items) if items.len() != 0 => return Some(items),
106+
ArgParser::List(list) => {
107+
// We're dealing with `#[diagnostic::attr()]`.
108+
// This can be because that is what the user typed, but that's also what we'd see
109+
// if the user used non-metaitem syntax. See `ArgParser::from_attr_args`.
110+
cx.emit_lint(
111+
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
112+
AttributeLintKind::NonMetaItemDiagnosticAttribute,
113+
list.span,
114+
);
115+
}
116+
ArgParser::NoArgs => {
117+
cx.emit_lint(
118+
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
119+
AttributeLintKind::MissingOptionsForDiagnosticAttribute {
120+
attribute: mode.as_str(),
121+
},
122+
span,
123+
);
124+
}
125+
ArgParser::NameValue(_) => {
126+
cx.emit_lint(
127+
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
128+
AttributeLintKind::MalFormedDiagnosticAttribute { attribute: mode.as_str(), span },
129+
span,
130+
);
131+
}
132+
}
133+
None
134+
}
135+
98136
fn parse_directive_items<'p, S: Stage>(
99137
cx: &mut AcceptContext<'_, '_, S>,
100138
mode: Mode,

compiler/rustc_attr_parsing/src/attributes/diagnostic/on_const.rs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use rustc_hir::attrs::diagnostic::Directive;
2-
use rustc_hir::lints::AttributeLintKind;
3-
use rustc_session::lint::builtin::MALFORMED_DIAGNOSTIC_ATTRIBUTES;
42

53
use crate::attributes::diagnostic::*;
64
use crate::attributes::prelude::*;
@@ -24,30 +22,7 @@ impl<S: Stage> AttributeParser<S> for OnConstParser {
2422
this.span = Some(span);
2523
let mode = Mode::DiagnosticOnConst;
2624

27-
let items = match args {
28-
ArgParser::List(items) if items.len() != 0 => items,
29-
ArgParser::NoArgs | ArgParser::List(_) => {
30-
cx.emit_lint(
31-
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
32-
AttributeLintKind::MissingOptionsForDiagnosticAttribute {
33-
attribute: mode.as_str(),
34-
},
35-
span,
36-
);
37-
return;
38-
}
39-
ArgParser::NameValue(_) => {
40-
cx.emit_lint(
41-
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
42-
AttributeLintKind::MalFormedDiagnosticAttribute {
43-
attribute: mode.as_str(),
44-
span,
45-
},
46-
span,
47-
);
48-
return;
49-
}
50-
};
25+
let Some(items) = parse_list(cx, args, mode) else { return };
5126

5227
let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) else {
5328
return;

compiler/rustc_attr_parsing/src/attributes/diagnostic/on_move.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use rustc_feature::template;
22
use rustc_hir::attrs::AttributeKind;
3-
use rustc_hir::lints::AttributeLintKind;
4-
use rustc_session::lint::builtin::MALFORMED_DIAGNOSTIC_ATTRIBUTES;
53
use rustc_span::sym;
64

75
use crate::attributes::diagnostic::*;
@@ -29,27 +27,10 @@ impl OnMoveParser {
2927

3028
let span = cx.attr_span;
3129
self.span = Some(span);
32-
let Some(list) = args.list() else {
33-
cx.emit_lint(
34-
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
35-
AttributeLintKind::MissingOptionsForDiagnosticAttribute {
36-
attribute: mode.as_str(),
37-
},
38-
span,
39-
);
40-
return;
41-
};
4230

43-
if list.is_empty() {
44-
cx.emit_lint(
45-
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
46-
AttributeLintKind::OnMoveMalformedAttrExpectedLiteralOrDelimiter,
47-
list.span,
48-
);
49-
return;
50-
}
31+
let Some(items) = parse_list(cx, args, mode) else { return };
5132

52-
if let Some(directive) = parse_directive_items(cx, mode, list.mixed(), true) {
33+
if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
5334
merge_directives(cx, &mut self.directive, (span, directive));
5435
}
5536
}

compiler/rustc_attr_parsing/src/attributes/diagnostic/on_unimplemented.rs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use rustc_hir::attrs::diagnostic::Directive;
2-
use rustc_hir::lints::AttributeLintKind;
3-
use rustc_session::lint::builtin::MALFORMED_DIAGNOSTIC_ATTRIBUTES;
42

53
use crate::attributes::diagnostic::*;
64
use crate::attributes::prelude::*;
@@ -29,30 +27,7 @@ impl OnUnimplementedParser {
2927
return;
3028
}
3129

32-
let items = match args {
33-
ArgParser::List(items) if items.len() != 0 => items,
34-
ArgParser::NoArgs | ArgParser::List(_) => {
35-
cx.emit_lint(
36-
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
37-
AttributeLintKind::MissingOptionsForDiagnosticAttribute {
38-
attribute: mode.as_str(),
39-
},
40-
span,
41-
);
42-
return;
43-
}
44-
ArgParser::NameValue(_) => {
45-
cx.emit_lint(
46-
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
47-
AttributeLintKind::MalFormedDiagnosticAttribute {
48-
attribute: mode.as_str(),
49-
span,
50-
},
51-
span,
52-
);
53-
return;
54-
}
55-
};
30+
let Some(items) = parse_list(cx, args, mode) else { return };
5631

5732
if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
5833
merge_directives(cx, &mut self.directive, (span, directive));

compiler/rustc_attr_parsing/src/attributes/diagnostic/on_unknown.rs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rustc_hir::attrs::diagnostic::Directive;
2-
use rustc_session::lint::builtin::MALFORMED_DIAGNOSTIC_ATTRIBUTES;
32

43
use crate::attributes::diagnostic::*;
54
use crate::attributes::prelude::*;
@@ -23,30 +22,7 @@ impl OnUnknownParser {
2322
let span = cx.attr_span;
2423
self.span = Some(span);
2524

26-
let items = match args {
27-
ArgParser::List(items) if !items.is_empty() => items,
28-
ArgParser::NoArgs | ArgParser::List(_) => {
29-
cx.emit_lint(
30-
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
31-
AttributeLintKind::MissingOptionsForDiagnosticAttribute {
32-
attribute: mode.as_str(),
33-
},
34-
span,
35-
);
36-
return;
37-
}
38-
ArgParser::NameValue(_) => {
39-
cx.emit_lint(
40-
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
41-
AttributeLintKind::MalFormedDiagnosticAttribute {
42-
attribute: mode.as_str(),
43-
span,
44-
},
45-
span,
46-
);
47-
return;
48-
}
49-
};
25+
let Some(items) = parse_list(cx, args, mode) else { return };
5026

5127
if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
5228
merge_directives(cx, &mut self.directive, (span, directive));

compiler/rustc_lint/src/early/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ impl<'a> Diagnostic<'a, ()> for DecorateAttrLint<'_, '_, '_> {
204204
&AttributeLintKind::OnMoveMalformedFormatLiterals { name } => {
205205
lints::OnMoveMalformedFormatLiterals { name }.into_diag(dcx, level)
206206
}
207-
&AttributeLintKind::OnMoveMalformedAttrExpectedLiteralOrDelimiter => {
208-
lints::OnMoveMalformedAttrExpectedLiteralOrDelimiter.into_diag(dcx, level)
207+
&AttributeLintKind::NonMetaItemDiagnosticAttribute => {
208+
lints::NonMetaItemDiagnosticAttribute.into_diag(dcx, level)
209209
}
210210
}
211211
}

compiler/rustc_lint/src/lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3607,7 +3607,7 @@ pub(crate) struct OnMoveMalformedFormatLiterals {
36073607
#[help(
36083608
"only literals are allowed as values for the `message`, `note` and `label` options. These options must be separated by a comma"
36093609
)]
3610-
pub(crate) struct OnMoveMalformedAttrExpectedLiteralOrDelimiter;
3610+
pub(crate) struct NonMetaItemDiagnosticAttribute;
36113611

36123612
#[derive(Diagnostic)]
36133613
#[diag("malformed `{$attribute}` attribute")]

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ pub enum AttributeLintKind {
756756
OnMoveMalformedFormatLiterals {
757757
name: Symbol,
758758
},
759-
OnMoveMalformedAttrExpectedLiteralOrDelimiter,
759+
NonMetaItemDiagnosticAttribute,
760760
}
761761

762762
#[derive(Debug, Clone, HashStable_Generic)]

tests/ui/diagnostic_namespace/on_move/report_warning_on_invalid_meta_item_syntax.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(diagnostic_on_move)]
22

33
#[diagnostic::on_move = "foo"]
4-
//~^WARN missing options for `diagnostic::on_move` attribute [malformed_diagnostic_attributes]
4+
//~^WARN malformed `diagnostic::on_move` attribute [malformed_diagnostic_attributes]
55
struct Foo;
66

77
fn takes_foo(_: Foo) {}

tests/ui/diagnostic_namespace/on_move/report_warning_on_invalid_meta_item_syntax.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
warning: missing options for `diagnostic::on_move` attribute
1+
warning: malformed `diagnostic::on_move` attribute
22
--> $DIR/report_warning_on_invalid_meta_item_syntax.rs:3:1
33
|
44
LL | #[diagnostic::on_move = "foo"]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid option found here
66
|
7-
= help: at least one of the `message`, `note` and `label` options are expected
7+
= help: only `message`, `note` and `label` are allowed as options
88
= note: `#[warn(malformed_diagnostic_attributes)]` (part of `#[warn(unknown_or_malformed_diagnostic_attributes)]`) on by default
99

1010
error[E0382]: use of moved value: `foo`

0 commit comments

Comments
 (0)