Skip to content

Commit fa73b61

Browse files
committed
clean things up
1 parent 1bd6b98 commit fa73b61

File tree

7 files changed

+48
-29
lines changed

7 files changed

+48
-29
lines changed

src/librustc_expand/expand.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_parse::configure;
1414
use rustc_parse::parser::Parser;
1515
use rustc_parse::validate_attr;
1616
use rustc_parse::DirectoryOwnership;
17+
use rustc_session::lint::BuiltinLintDiagnostics;
1718
use rustc_session::lint::builtin::UNUSED_DOC_COMMENTS;
1819
use rustc_session::parse::{feature_err, ParseSess};
1920
use rustc_span::source_map::respan;
@@ -1093,7 +1094,12 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
10931094
}
10941095

10951096
if attr.doc_str().is_some() {
1096-
self.cx.parse_sess.buffer_lint(&UNUSED_DOC_COMMENTS, attr.span, ast::CRATE_NODE_ID, "yep, it's unused");
1097+
self.cx.parse_sess.buffer_lint_with_diagnostic(
1098+
&UNUSED_DOC_COMMENTS,
1099+
attr.span,
1100+
ast::CRATE_NODE_ID,
1101+
"unused doc comment",
1102+
BuiltinLintDiagnostics::UnusedDocComment(attr.span));
10971103
}
10981104
}
10991105
}

src/librustc_lint/builtin.rs

+7-22
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,6 @@ trait UnusedDocCommentExt {
744744
cx: &EarlyContext<'_>,
745745
node_span: Span,
746746
node_kind: &str,
747-
is_macro_expansion: bool,
748747
attrs: &[ast::Attribute],
749748
);
750749
}
@@ -755,7 +754,6 @@ impl UnusedDocCommentExt for UnusedDocComment {
755754
cx: &EarlyContext<'_>,
756755
node_span: Span,
757756
node_kind: &str,
758-
is_macro_expansion: bool,
759757
attrs: &[ast::Attribute],
760758
) {
761759
let mut attrs = attrs.into_iter().peekable();
@@ -783,12 +781,6 @@ impl UnusedDocCommentExt for UnusedDocComment {
783781
node_span,
784782
format!("rustdoc does not generate documentation for {}", node_kind),
785783
);
786-
if is_macro_expansion {
787-
err.help(
788-
"to document an item produced by a macro, \
789-
the macro must produce the documentation as part of its expansion",
790-
);
791-
}
792784
err.emit();
793785
});
794786
}
@@ -797,31 +789,24 @@ impl UnusedDocCommentExt for UnusedDocComment {
797789
}
798790

799791
impl EarlyLintPass for UnusedDocComment {
800-
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
801-
if let ast::ItemKind::Mac(..) = item.kind {
802-
self.warn_if_doc(cx, item.span, "macro expansions", true, &item.attrs);
803-
}
804-
}
805-
806792
fn check_stmt(&mut self, cx: &EarlyContext<'_>, stmt: &ast::Stmt) {
807-
let (kind, is_macro_expansion) = match stmt.kind {
808-
ast::StmtKind::Local(..) => ("statements", false),
809-
ast::StmtKind::Item(..) => ("inner items", false),
810-
ast::StmtKind::Mac(..) => ("macro expansions", true),
793+
let kind = match stmt.kind {
794+
ast::StmtKind::Local(..) => "statements",
795+
ast::StmtKind::Item(..) => "inner items",
811796
// expressions will be reported by `check_expr`.
812-
ast::StmtKind::Semi(..) | ast::StmtKind::Expr(..) => return,
797+
ast::StmtKind::Semi(..) | ast::StmtKind::Expr(..) | ast::StmtKind::Mac(..) => return,
813798
};
814799

815-
self.warn_if_doc(cx, stmt.span, kind, is_macro_expansion, stmt.kind.attrs());
800+
self.warn_if_doc(cx, stmt.span, kind, stmt.kind.attrs());
816801
}
817802

818803
fn check_arm(&mut self, cx: &EarlyContext<'_>, arm: &ast::Arm) {
819804
let arm_span = arm.pat.span.with_hi(arm.body.span.hi());
820-
self.warn_if_doc(cx, arm_span, "match arms", false, &arm.attrs);
805+
self.warn_if_doc(cx, arm_span, "match arms", &arm.attrs);
821806
}
822807

823808
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
824-
self.warn_if_doc(cx, expr.span, "expressions", false, &expr.attrs);
809+
self.warn_if_doc(cx, expr.span, "expressions", &expr.attrs);
825810
}
826811
}
827812

src/librustc_lint/context.rs

+5
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,11 @@ pub trait LintContext: Sized {
565565
BuiltinLintDiagnostics::DeprecatedMacro(suggestion, span) => {
566566
stability::deprecation_suggestion(&mut db, suggestion, span)
567567
}
568+
BuiltinLintDiagnostics::UnusedDocComment(span) => {
569+
db.span_label(span, "rustdoc does not generate documentation for macros");
570+
db.help("to document an item produced by a macro, \
571+
the macro must produce the documentation as part of its expansion");
572+
}
568573
}
569574
// Rewrap `db`, and pass control to the user.
570575
decorate(LintDiagnosticBuilder::new(db));

src/librustc_session/lint.rs

+1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ pub enum BuiltinLintDiagnostics {
190190
UnusedImports(String, Vec<(Span, String)>),
191191
RedundantImport(Vec<(Span, bool)>, Ident),
192192
DeprecatedMacro(Option<Symbol>, Span),
193+
UnusedDocComment(Span),
193194
}
194195

195196
/// Lints that are buffered up early on in the `Session` before the

src/librustc_session/parse.rs

+19
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,25 @@ impl ParseSess {
176176
});
177177
}
178178

179+
pub fn buffer_lint_with_diagnostic(
180+
&self,
181+
lint: &'static Lint,
182+
span: impl Into<MultiSpan>,
183+
node_id: NodeId,
184+
msg: &str,
185+
diagnostic: BuiltinLintDiagnostics,
186+
) {
187+
self.buffered_lints.with_lock(|buffered_lints| {
188+
buffered_lints.push(BufferedEarlyLint {
189+
span: span.into(),
190+
node_id,
191+
msg: msg.into(),
192+
lint_id: LintId::of(lint),
193+
diagnostic,
194+
});
195+
});
196+
}
197+
179198
/// Extend an error with a suggestion to wrap an expression with parentheses to allow the
180199
/// parser to continue parsing the following operation as part of the same expression.
181200
pub fn expr_parentheses_needed(

src/test/ui/useless-comment.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ macro_rules! mac {
66
() => {}
77
}
88

9-
/// foo //~ ERROR yep, it's unused
9+
/// foo //~ ERROR unused doc comment
1010
mac!();
1111

1212
fn foo() {
@@ -29,7 +29,7 @@ fn foo() {
2929
#[doc = "bar"] //~ ERROR unused doc comment
3030
3;
3131

32-
/// bar //~ ERROR yep, it's unused
32+
/// bar //~ ERROR unused doc comment
3333
mac!();
3434

3535
let x = /** comment */ 47; //~ ERROR unused doc comment

src/test/ui/useless-comment.stderr

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
error: yep, it's unused
1+
error: unused doc comment
22
--> $DIR/useless-comment.rs:9:1
33
|
44
LL | /// foo
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ rustdoc does not generate documentation for macros
66
|
77
note: the lint level is defined here
88
--> $DIR/useless-comment.rs:3:9
99
|
1010
LL | #![deny(unused_doc_comments)]
1111
| ^^^^^^^^^^^^^^^^^^^
12+
= help: to document an item produced by a macro, the macro must produce the documentation as part of its expansion
1213

13-
error: yep, it's unused
14+
error: unused doc comment
1415
--> $DIR/useless-comment.rs:32:5
1516
|
1617
LL | /// bar
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ rustdoc does not generate documentation for macros
19+
|
20+
= help: to document an item produced by a macro, the macro must produce the documentation as part of its expansion
1821

1922
error: unused doc comment
2023
--> $DIR/useless-comment.rs:13:5

0 commit comments

Comments
 (0)