Skip to content

Commit 7ea307e

Browse files
Also emit future compatibility warning if there is a dot in the codeblock tag
1 parent 6a78d59 commit 7ea307e

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

src/librustdoc/passes/check_custom_code_classes.rs

+37-26
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ pub(crate) const CHECK_CUSTOM_CODE_CLASSES: Pass = Pass {
2121
};
2222

2323
pub(crate) fn check_custom_code_classes(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
24+
if cx.tcx.features().custom_code_classes_in_docs {
25+
// Nothing to check here if the feature is enabled.
26+
return krate;
27+
}
2428
let mut coll = CustomCodeClassLinter { cx };
2529

2630
coll.fold_crate(krate)
@@ -40,49 +44,56 @@ impl<'a, 'tcx> DocFolder for CustomCodeClassLinter<'a, 'tcx> {
4044
#[derive(Debug)]
4145
struct TestsWithCustomClasses {
4246
custom_classes_found: Vec<String>,
47+
contains_dot: bool,
4348
}
4449

4550
impl crate::doctest::Tester for TestsWithCustomClasses {
4651
fn add_test(&mut self, _: String, config: LangString, _: usize) {
52+
self.contains_dot |= config.original.contains('.');
4753
self.custom_classes_found.extend(config.added_classes.into_iter());
4854
}
4955
}
5056

57+
fn emit_warning(cx: &DocContext<'_>, item: &Item, custom_classes: Option<&[String]>) {
58+
let span = item.attr_span(cx.tcx);
59+
let sess = &cx.tcx.sess.parse_sess;
60+
let mut err = sess
61+
.span_diagnostic
62+
.struct_span_warn(span, "custom classes in code blocks will change behaviour");
63+
add_feature_diagnostics_for_issue(
64+
&mut err,
65+
sess,
66+
sym::custom_code_classes_in_docs,
67+
GateIssue::Language,
68+
false,
69+
);
70+
71+
if let Some(custom_classes) = custom_classes {
72+
err.note(
73+
// This will list the wrong items to make them more easily searchable.
74+
// To ensure the most correct hits, it adds back the 'class:' that was stripped.
75+
format!("found these custom classes: class={}", custom_classes.join(",class=")),
76+
);
77+
}
78+
79+
// A later feature_err call can steal and cancel this warning.
80+
err.stash(span, StashKey::EarlySyntaxWarning);
81+
}
82+
5183
pub(crate) fn look_for_custom_classes<'tcx>(cx: &DocContext<'tcx>, item: &Item) {
5284
if !item.item_id.is_local() {
5385
// If non-local, no need to check anything.
5486
return;
5587
}
5688

57-
let mut tests = TestsWithCustomClasses { custom_classes_found: vec![] };
89+
let mut tests = TestsWithCustomClasses { custom_classes_found: vec![], contains_dot: false };
5890

5991
let dox = item.attrs.doc_value();
6092
find_codes(&dox, &mut tests, ErrorCodes::No, false, None, true, true);
6193

62-
if !tests.custom_classes_found.is_empty() && !cx.tcx.features().custom_code_classes_in_docs {
63-
let span = item.attr_span(cx.tcx);
64-
let sess = &cx.tcx.sess.parse_sess;
65-
let mut err = sess
66-
.span_diagnostic
67-
.struct_span_warn(span, "custom classes in code blocks will change behaviour");
68-
add_feature_diagnostics_for_issue(
69-
&mut err,
70-
sess,
71-
sym::custom_code_classes_in_docs,
72-
GateIssue::Language,
73-
false,
74-
);
75-
76-
err.note(
77-
// This will list the wrong items to make them more easily searchable.
78-
// To ensure the most correct hits, it adds back the 'class:' that was stripped.
79-
format!(
80-
"found these custom classes: class={}",
81-
tests.custom_classes_found.join(",class=")
82-
),
83-
);
84-
85-
// A later feature_err call can steal and cancel this warning.
86-
err.stash(span, StashKey::EarlySyntaxWarning);
94+
if !tests.custom_classes_found.is_empty() {
95+
emit_warning(cx, item, Some(&tests.custom_classes_found));
96+
} else if tests.contains_dot {
97+
emit_warning(cx, item, None);
8798
}
8899
}

0 commit comments

Comments
 (0)