Skip to content

Commit c049735

Browse files
committed
Support ignore for lint examples.
1 parent ce014be commit c049735

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

compiler/rustc_session/src/lint.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,10 @@ impl LintBuffer {
319319
///
320320
/// The `{{produces}}` tag will be automatically replaced with the output from
321321
/// the example by the build system. You can build and view the rustc book
322-
/// with `x.py doc --stage=1 src/doc/rustc --open` (use --stage=0 if just
323-
/// changing the wording of an existing lint).
322+
/// with `x.py doc --stage=1 src/doc/rustc --open`. If the lint example is too
323+
/// complex to run as a simple example (for example, it needs an extern
324+
/// crate), mark it with `ignore` and manually paste the expected output below
325+
/// the example.
324326
#[macro_export]
325327
macro_rules! declare_lint {
326328
($(#[$attr:meta])* $vis: vis $NAME: ident, $Level: ident, $desc: expr) => (

compiler/rustc_session/src/lint/builtin.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ declare_lint! {
128128
///
129129
/// ### Example
130130
///
131-
/// ```rust,compile_fail
131+
/// ```rust,ignore (needs separate file)
132132
/// fn main() {
133133
/// include!("foo.txt");
134134
/// }
@@ -344,7 +344,7 @@ declare_lint! {
344344
///
345345
/// ### Example
346346
///
347-
/// ```rust,compile_fail
347+
/// ```rust,ignore (needs extern crate)
348348
/// #![deny(unused_crate_dependencies)]
349349
/// ```
350350
///
@@ -1984,7 +1984,7 @@ declare_lint! {
19841984
///
19851985
/// ### Example
19861986
///
1987-
/// ```rust,compile_fail
1987+
/// ```rust,ignore (needs extern crate)
19881988
/// #![deny(macro_use_extern_crate)]
19891989
///
19901990
/// #[macro_use]
@@ -2378,7 +2378,19 @@ declare_lint! {
23782378
/// }
23792379
/// ```
23802380
///
2381-
/// {{produces}}
2381+
/// This will produce:
2382+
///
2383+
/// ```text
2384+
/// warning: formatting may not be suitable for sub-register argument
2385+
/// --> src/main.rs:6:19
2386+
/// |
2387+
/// 6 | asm!("mov {0}, {0}", in(reg) 0i16);
2388+
/// | ^^^ ^^^ ---- for this argument
2389+
/// |
2390+
/// = note: `#[warn(asm_sub_register)]` on by default
2391+
/// = help: use the `x` modifier to have the register formatted as `ax`
2392+
/// = help: or use the `r` modifier to keep the default formatting of `rax`
2393+
/// ```
23822394
///
23832395
/// ### Explanation
23842396
///

src/tools/lint-docs/src/lib.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ impl Lint {
1919
fn doc_contains(&self, text: &str) -> bool {
2020
self.doc.iter().any(|line| line.contains(text))
2121
}
22+
23+
fn is_ignored(&self) -> bool {
24+
self.doc
25+
.iter()
26+
.filter(|line| line.starts_with("```rust"))
27+
.all(|line| line.contains(",ignore"))
28+
}
2229
}
2330

2431
#[derive(Clone, Copy, PartialEq)]
@@ -208,13 +215,8 @@ fn generate_output_example(
208215
// try to avoid adding to this list.
209216
if matches!(
210217
lint.name.as_str(),
211-
"unused_features"
212-
| "unstable_features"
213-
| "incomplete_include"
214-
| "unused_crate_dependencies"
215-
| "exported_private_dependencies"
216-
| "proc_macro_derive_resolution_fallback"
217-
| "macro_use_extern_crate"
218+
"unused_features" // broken lint
219+
| "unstable_features" // deprecated
218220
) {
219221
return Ok(());
220222
}
@@ -223,13 +225,22 @@ fn generate_output_example(
223225
return Ok(());
224226
}
225227
check_style(lint)?;
226-
replace_produces(lint, rustc_path, verbose)?;
228+
// Unfortunately some lints have extra requirements that this simple test
229+
// setup can't handle (like extern crates). An alternative is to use a
230+
// separate test suite, and use an include mechanism such as mdbook's
231+
// `{{#rustdoc_include}}`.
232+
if !lint.is_ignored() {
233+
replace_produces(lint, rustc_path, verbose)?;
234+
}
227235
Ok(())
228236
}
229237

230238
/// Checks the doc style of the lint.
231239
fn check_style(lint: &Lint) -> Result<(), Box<dyn Error>> {
232-
for expected in &["### Example", "### Explanation", "{{produces}}"] {
240+
for &expected in &["### Example", "### Explanation", "{{produces}}"] {
241+
if expected == "{{produces}}" && lint.is_ignored() {
242+
continue;
243+
}
233244
if !lint.doc_contains(expected) {
234245
return Err(format!("lint docs should contain the line `{}`", expected).into());
235246
}

0 commit comments

Comments
 (0)