1use crate::inline_config::{DisableSite, collect_disable_sites, normalize_rule_name};
27use crate::lint_context::LintContext;
28use crate::rule::{FixCapability, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity, SuppressionReport};
29
30#[derive(Debug, Clone, Default)]
31pub struct MD087UnusedDisableComment;
32
33impl MD087UnusedDisableComment {
34 pub fn new() -> Self {
35 Self
36 }
37
38 fn unused_rules(&self, site: &DisableSite, report: &SuppressionReport) -> Vec<String> {
43 let mut unused: Vec<String> = Vec::new();
44
45 for written in &site.rules {
46 let canonical = normalize_rule_name(written);
50 if canonical == self.name() || !report.judged_rules.contains(&canonical) {
53 continue;
54 }
55 let used = report
56 .suppressed
57 .iter()
58 .any(|warning| warning.rule_name == canonical && site.scope.carries(warning.layer, warning.line));
59 if !used && !unused.contains(written) {
60 unused.push(written.clone());
61 }
62 }
63
64 unused
65 }
66
67 fn warning(&self, ctx: &LintContext, site: &DisableSite, unused: &[String]) -> LintWarning {
68 let line_offset = ctx.line_info(site.line).map_or(0, |info| info.byte_offset);
69 let (line, column) = ctx.offset_to_line_col(line_offset + site.span.start);
70 let (_, end_column) = ctx.offset_to_line_col(line_offset + site.span.end);
71 let names = unused.join(", ");
72 let message = if site.kind == "configure-file" {
75 format!("Unused configure-file disable: {names}")
76 } else {
77 format!("Unused {} comment: {names}", site.kind)
78 };
79 LintWarning {
80 rule_name: Some(self.name().to_string()),
81 severity: Severity::Warning,
82 line,
83 column,
84 end_line: line,
85 end_column,
86 message,
87 fix: None,
88 }
89 }
90}
91
92impl Rule for MD087UnusedDisableComment {
93 fn name(&self) -> &'static str {
94 "MD087"
95 }
96
97 fn description(&self) -> &'static str {
98 "Inline disable comments should suppress something"
99 }
100
101 fn category(&self) -> RuleCategory {
102 RuleCategory::Other
103 }
104
105 fn should_skip(&self, ctx: &LintContext) -> bool {
106 !ctx.content.contains("<!--")
107 }
108
109 fn check(&self, _ctx: &LintContext) -> LintResult {
110 Ok(Vec::new())
114 }
115
116 fn observes_suppressions(&self) -> bool {
117 true
118 }
119
120 fn check_suppressions(&self, ctx: &LintContext, report: &SuppressionReport) -> LintResult {
121 let mut warnings = Vec::new();
122 let code_spans = crate::lint_context::code_span_byte_ranges(&ctx.code_spans());
123 for site in collect_disable_sites(ctx.content, &ctx.code_blocks, &code_spans) {
124 let unused = self.unused_rules(&site, report);
125 if unused.is_empty() {
126 continue;
127 }
128 warnings.push(self.warning(ctx, &site, &unused));
129 }
130 Ok(warnings)
131 }
132
133 fn fix_capability(&self) -> FixCapability {
134 FixCapability::Unfixable
135 }
136
137 fn fix(&self, ctx: &LintContext) -> Result<String, LintError> {
138 Ok(ctx.content.to_string())
139 }
140
141 fn as_any(&self) -> &dyn std::any::Any {
142 self
143 }
144
145 fn from_config(_config: &crate::config::Config) -> Box<dyn Rule>
146 where
147 Self: Sized,
148 {
149 Box::new(Self)
150 }
151}
152
153#[cfg(test)]
154mod tests {
155 use super::*;
156 use crate::config::MarkdownFlavor;
157 use crate::inline_config::DisableLayer;
158 use crate::rule::SuppressedWarning;
159 use std::collections::HashSet;
160
161 fn report(suppressed: &[(&str, DisableLayer, usize)], judged: &[&str]) -> SuppressionReport {
162 SuppressionReport {
163 suppressed: suppressed
164 .iter()
165 .map(|&(rule_name, layer, line)| SuppressedWarning {
166 rule_name: rule_name.to_string(),
167 line,
168 layer,
169 })
170 .collect(),
171 judged_rules: judged.iter().map(|name| (*name).to_string()).collect::<HashSet<_>>(),
172 }
173 }
174
175 fn check(content: &str, suppressed: &[(&str, DisableLayer, usize)], judged: &[&str]) -> Vec<LintWarning> {
176 check_in(MarkdownFlavor::Standard, content, suppressed, judged)
177 }
178
179 fn check_in(
180 flavor: MarkdownFlavor,
181 content: &str,
182 suppressed: &[(&str, DisableLayer, usize)],
183 judged: &[&str],
184 ) -> Vec<LintWarning> {
185 let ctx = LintContext::new(content, flavor, None);
186 MD087UnusedDisableComment::new()
187 .check_suppressions(&ctx, &report(suppressed, judged))
188 .unwrap()
189 }
190
191 #[test]
192 fn reports_a_disable_line_comment_that_suppressed_nothing() {
193 let content = "# Title\n\nA short line <!-- rumdl-disable-line MD013 -->\n";
194 let warnings = check(content, &[], &["MD013"]);
195 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
196 assert_eq!(warnings[0].message, "Unused disable-line comment: MD013");
197 assert_eq!((warnings[0].line, warnings[0].column), (3, 14));
198 assert_eq!(warnings[0].end_column, 47, "the warning spans the comment");
199 assert!(
200 warnings[0].fix.is_none(),
201 "removing an authored comment is not automatic"
202 );
203 }
204
205 #[test]
206 fn keeps_quiet_when_the_comment_suppressed_a_finding() {
207 let content = "# Title\n\nA short line <!-- rumdl-disable-line MD013 -->\n";
208 let warnings = check(content, &[("MD013", DisableLayer::Line, 3)], &["MD013"]);
209 assert!(warnings.is_empty(), "got: {warnings:?}");
210 }
211
212 #[test]
213 fn a_disable_line_comment_is_judged_on_its_own_line_only() {
214 let content = "<!-- rumdl-disable-line MD013 -->\nA long line\n";
215 let warnings = check(content, &[("MD013", DisableLayer::Line, 2)], &["MD013"]);
216 assert_eq!(warnings.len(), 1, "a finding on line 2 is not this comment's doing");
217 assert_eq!(warnings[0].line, 1);
218 }
219
220 #[test]
221 fn a_disable_next_line_comment_is_judged_on_the_following_line() {
222 let content = "<!-- rumdl-disable-next-line MD013 -->\nA long line\n";
223 assert!(
224 check(content, &[("MD013", DisableLayer::Line, 2)], &["MD013"]).is_empty(),
225 "the suppression on line 2 is what the comment is for"
226 );
227 let warnings = check(content, &[("MD013", DisableLayer::Line, 1)], &["MD013"]);
228 assert_eq!(warnings.len(), 1, "a finding on line 1 is not this comment's doing");
229 assert_eq!(warnings[0].message, "Unused disable-next-line comment: MD013");
230 }
231
232 #[test]
233 fn a_block_disable_reaches_the_end_of_the_document() {
234 let content = "<!-- rumdl-disable MD013 -->\n\ntext\n\n<!-- rumdl-enable MD013 -->\n\nmore\n";
235 assert!(
236 check(content, &[("MD013", DisableLayer::Block, 7)], &["MD013"]).is_empty(),
237 "a scope wider than the truth may only under-report"
238 );
239 let warnings = check(content, &[], &["MD013"]);
240 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
241 assert_eq!(warnings[0].message, "Unused disable comment: MD013");
242 }
243
244 #[test]
245 fn a_disable_file_comment_covers_a_finding_above_it() {
246 let content = "A long line\n\n<!-- rumdl-disable-file MD013 -->\n";
247 assert!(
248 check(content, &[("MD013", DisableLayer::File, 1)], &["MD013"]).is_empty(),
249 "disable-file applies to the whole document, including lines above it"
250 );
251 }
252
253 #[test]
254 fn a_comment_a_wider_one_already_covers_is_reported() {
255 let content = "<!-- rumdl-disable-file MD013 -->\n\nA long line <!-- rumdl-disable-line MD013 -->\n";
256 let warnings = check(content, &[("MD013", DisableLayer::File, 3)], &["MD013"]);
257 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
258 assert_eq!(warnings[0].message, "Unused disable-line comment: MD013");
259 assert_eq!(warnings[0].line, 3, "the file-wide comment is the one doing the work");
260 }
261
262 #[test]
263 fn the_wider_comment_is_the_one_reported_when_the_narrow_one_does_the_work() {
264 let content = "<!-- rumdl-disable MD013 -->\n<!-- rumdl-enable MD013 -->\nA long line <!-- rumdl-disable-line MD013 -->\n";
267 let warnings = check(content, &[("MD013", DisableLayer::Line, 3)], &["MD013"]);
268 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
269 assert_eq!(warnings[0].message, "Unused disable comment: MD013");
270 assert_eq!(warnings[0].line, 1);
271 }
272
273 #[test]
274 fn only_the_unused_names_of_a_multi_rule_comment_are_reported() {
275 let content = "text <!-- rumdl-disable-line MD013 MD033 MD009 -->\n";
276 let warnings = check(
277 content,
278 &[("MD033", DisableLayer::Line, 1)],
279 &["MD009", "MD013", "MD033"],
280 );
281 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
282 assert_eq!(warnings[0].message, "Unused disable-line comment: MD013, MD009");
283 }
284
285 #[test]
286 fn a_rule_the_run_does_not_carry_is_not_judged() {
287 let content = "text <!-- rumdl-disable-line MD013 -->\n";
288 assert!(
289 check(content, &[], &["MD009"]).is_empty(),
290 "MD013 produced nothing because it did not run"
291 );
292 }
293
294 #[test]
295 fn an_unknown_rule_name_is_not_judged() {
296 let content = "text <!-- rumdl-disable-line MD999 -->\n";
297 assert!(
298 check(content, &[], &["MD013"]).is_empty(),
299 "MD999 is not a rule the run carries"
300 );
301 }
302
303 #[test]
304 fn a_comment_naming_no_rule_is_never_reported() {
305 let content = "text <!-- rumdl-disable-line -->\n<!-- rumdl-disable -->\n";
306 assert!(
307 check(content, &[], &["MD013"]).is_empty(),
308 "a bare comment disables rules this run may not carry"
309 );
310 }
311
312 #[test]
313 fn prettier_ignore_belongs_to_another_formatter() {
314 let content = "<!-- prettier-ignore -->\n| a | b |\n";
315 assert!(
316 check(content, &[], &["MD013"]).is_empty(),
317 "not rumdl's comment to judge"
318 );
319 }
320
321 #[test]
322 fn a_comment_inside_a_code_block_configures_nothing() {
323 let content = "# Title\n\n```markdown\n<!-- rumdl-disable-line MD013 -->\n```\n";
324 assert!(
325 check(content, &[], &["MD013"]).is_empty(),
326 "a fenced example documents a comment rather than writing one"
327 );
328 }
329
330 #[test]
331 fn a_comment_inside_a_code_span_configures_nothing() {
332 let content = "# Title\n\nSee `<!-- rumdl-disable-line MD013 -->` here.\n";
333 assert!(
334 check(content, &[], &["MD013"]).is_empty(),
335 "backticks show a comment rather than writing one"
336 );
337 }
338
339 #[test]
340 fn a_comment_beside_one_in_a_code_span_is_still_judged() {
341 let content = "`<!-- rumdl-disable-line MD013 -->` <!-- rumdl-disable-line MD033 -->\n";
344 let warnings = check(content, &[], &["MD013", "MD033"]);
345 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
346 assert_eq!(warnings[0].message, "Unused disable-line comment: MD033");
347 }
348
349 #[test]
350 fn a_comment_in_an_indented_container_body_is_judged() {
351 let content = "# Title\n\n!!! example\n\n A short line <!-- rumdl-disable-line MD013 -->\n";
355 let warnings = check_in(MarkdownFlavor::MkDocs, content, &[], &["MD013"]);
356 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
357 assert_eq!(warnings[0].line, 5);
358 assert!(
359 check_in(MarkdownFlavor::Standard, content, &[], &["MD013"]).is_empty(),
360 "without admonitions the same lines are an indented code block"
361 );
362 }
363
364 #[test]
365 fn an_alias_is_reported_as_the_author_wrote_it() {
366 let content = "text <!-- rumdl-disable-line line-length -->\n";
367 let warnings = check(content, &[], &["MD013"]);
368 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
369 assert_eq!(warnings[0].message, "Unused disable-line comment: line-length");
370 }
371
372 #[test]
373 fn a_markdownlint_comment_is_judged_the_same_way() {
374 let content = "text <!-- markdownlint-disable-line MD013 -->\n";
375 let warnings = check(content, &[], &["MD013"]);
376 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
377 assert_eq!(warnings[0].message, "Unused disable-line comment: MD013");
378 }
379
380 #[test]
381 fn a_configure_file_entry_turning_a_rule_off_is_judged_like_a_disable() {
382 let content = "<!-- rumdl-configure-file { \"MD013\": false } -->\n\ntext\n";
383 let warnings = check(content, &[], &["MD013"]);
384 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
385 assert_eq!(warnings[0].message, "Unused configure-file disable: MD013");
386 assert!(
387 check(content, &[("MD013", DisableLayer::File, 3)], &["MD013"]).is_empty(),
388 "the entry turned the rule off for the whole file"
389 );
390 }
391
392 #[test]
393 fn a_configure_file_entry_carrying_options_is_not_a_disable() {
394 let content = "<!-- rumdl-configure-file { \"MD013\": { \"line_length\": 200 } } -->\n\ntext\n";
395 assert!(
396 check(content, &[], &["MD013"]).is_empty(),
397 "configuring a rule is not suppressing it"
398 );
399 }
400
401 #[test]
402 fn this_rule_never_judges_a_comment_silencing_itself() {
403 let content = "text <!-- rumdl-disable-line MD087 -->\n";
404 assert!(
405 check(content, &[], &["MD013", "MD087"]).is_empty(),
406 "MD087 findings are raised after the report is assembled"
407 );
408 }
409
410 #[test]
411 fn the_column_is_measured_in_characters() {
412 let content = "héllo wörld <!-- rumdl-disable-line MD013 -->\n";
413 let warnings = check(content, &[], &["MD013"]);
414 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
415 assert_eq!(warnings[0].column, 13, "two multi-byte characters precede the comment");
416 }
417
418 #[test]
419 fn check_reports_nothing_on_its_own() {
420 let content = "text <!-- rumdl-disable-line MD013 -->\n";
421 let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
422 assert!(
423 MD087UnusedDisableComment::new().check(&ctx).unwrap().is_empty(),
424 "the verdict needs the run's suppressions"
425 );
426 }
427}