-
Notifications
You must be signed in to change notification settings - Fork 1.7k
doc_suspicious_footnotes: lint text that looks like a footnote #14708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use rustc_ast::token::CommentKind; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{AttrStyle, Attribute}; | ||
use rustc_lint::{LateContext, LintContext}; | ||
use rustc_resolve::rustdoc::DocFragmentKind; | ||
|
||
use std::ops::Range; | ||
|
||
use super::{DOC_SUSPICIOUS_FOOTNOTES, Fragments}; | ||
|
||
pub fn check(cx: &LateContext<'_>, doc: &str, range: Range<usize>, fragments: &Fragments<'_>, attrs: &[Attribute]) { | ||
for i in doc[range.clone()] | ||
.bytes() | ||
.enumerate() | ||
.filter_map(|(i, c)| if c == b'[' { Some(i) } else { None }) | ||
{ | ||
let start = i + range.start; | ||
if doc.as_bytes().get(start + 1) == Some(&b'^') | ||
&& let Some(end) = all_numbers_upto_brace(doc, start + 2) | ||
&& doc.as_bytes().get(end) != Some(&b':') | ||
&& doc.as_bytes().get(start - 1) != Some(&b'\\') | ||
&& let Some(this_fragment) = { | ||
// the `doc` string contains all fragments concatenated together | ||
// figure out which one this suspicious footnote comes from | ||
let mut starting_position = 0; | ||
let mut found_fragment = fragments.fragments.last(); | ||
for fragment in fragments.fragments { | ||
if start >= starting_position && start < starting_position + fragment.doc.as_str().len() { | ||
found_fragment = Some(fragment); | ||
break; | ||
} | ||
starting_position += fragment.doc.as_str().len(); | ||
} | ||
found_fragment | ||
} | ||
{ | ||
let span = fragments.span(cx, start..end).unwrap_or(this_fragment.span); | ||
span_lint_and_then( | ||
cx, | ||
DOC_SUSPICIOUS_FOOTNOTES, | ||
span, | ||
"looks like a footnote ref, but has no matching footnote", | ||
|diag| { | ||
if this_fragment.kind == DocFragmentKind::SugaredDoc { | ||
let (doc_attr, (_, doc_attr_comment_kind)) = attrs | ||
.iter() | ||
.filter(|attr| attr.span().overlaps(this_fragment.span)) | ||
.rev() | ||
.find_map(|attr| Some((attr, attr.doc_str_and_comment_kind()?))) | ||
.unwrap(); | ||
let (to_add, terminator) = match (doc_attr_comment_kind, doc_attr.style()) { | ||
(CommentKind::Line, AttrStyle::Outer) => ("\n///\n/// ", ""), | ||
blyxyas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(CommentKind::Line, AttrStyle::Inner) => ("\n//!\n//! ", ""), | ||
(CommentKind::Block, AttrStyle::Outer) => ("\n/** ", " */"), | ||
(CommentKind::Block, AttrStyle::Inner) => ("\n/*! ", " */"), | ||
}; | ||
diag.span_suggestion_verbose( | ||
doc_attr.span().shrink_to_hi(), | ||
"add footnote definition", | ||
format!( | ||
"{to_add}{label}: <!-- description -->{terminator}", | ||
label = &doc[start..end] | ||
), | ||
Applicability::HasPlaceholders, | ||
); | ||
} else { | ||
let is_file_include = cx | ||
.sess() | ||
.source_map() | ||
.span_to_snippet(this_fragment.span) | ||
.as_ref() | ||
.map(|vdoc| vdoc.trim()) | ||
== Ok(doc); | ||
if is_file_include { | ||
// if this is a file include, then there's no quote marks | ||
diag.span_suggestion_verbose( | ||
this_fragment.span.shrink_to_hi(), | ||
"add footnote definition", | ||
format!("\n\n{label}: <!-- description -->", label = &doc[start..end],), | ||
Applicability::HasPlaceholders, | ||
); | ||
} else { | ||
// otherwise, we wrap in a string | ||
diag.span_suggestion_verbose( | ||
this_fragment.span, | ||
"add footnote definition", | ||
format!( | ||
"r#\"{doc}\n\n{label}: <!-- description -->\"#", | ||
doc = this_fragment.doc, | ||
label = &doc[start..end], | ||
), | ||
Applicability::HasPlaceholders, | ||
); | ||
} | ||
} | ||
}, | ||
); | ||
} | ||
} | ||
} | ||
|
||
fn all_numbers_upto_brace(text: &str, i: usize) -> Option<usize> { | ||
for (j, c) in text.as_bytes()[i..].iter().copied().enumerate().take(64) { | ||
if c == b']' && j != 0 { | ||
return Some(i + j + 1); | ||
} | ||
if !c.is_ascii_digit() || j >= 64 { | ||
break; | ||
} | ||
} | ||
None | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
notriddle marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
#![warn(clippy::doc_suspicious_footnotes)] | ||
#![allow(clippy::needless_raw_string_hashes)] | ||
//! This is not a footnote[^1]. | ||
//! | ||
//! [^1]: <!-- description --> | ||
//~^ doc_suspicious_footnotes | ||
//! | ||
//! This is not a footnote[^either], but it doesn't warn. | ||
//! | ||
//! This is not a footnote\[^1], but it also doesn't warn. | ||
//! | ||
//! This is not a footnote[^1\], but it also doesn't warn. | ||
//! | ||
//! This is not a `footnote[^1]`, but it also doesn't warn. | ||
//! | ||
//! This is a footnote[^2]. | ||
//! | ||
//! [^2]: hello world | ||
|
||
/// This is not a footnote[^1]. | ||
/// | ||
/// [^1]: <!-- description --> | ||
//~^ doc_suspicious_footnotes | ||
/// | ||
/// This is not a footnote[^either], but it doesn't warn. | ||
/// | ||
/// This is not a footnote\[^1], but it also doesn't warn. | ||
/// | ||
/// This is not a footnote[^1\], but it also doesn't warn. | ||
/// | ||
/// This is not a `footnote[^1]`, but it also doesn't warn. | ||
/// | ||
/// This is a footnote[^2]. | ||
/// | ||
/// [^2]: hello world | ||
pub fn footnotes() { | ||
// test code goes here | ||
} | ||
|
||
pub struct Foo; | ||
#[rustfmt::skip] | ||
impl Foo { | ||
#[doc = r#"This is not a footnote[^1]. | ||
|
||
[^1]: <!-- description -->"#] | ||
//~^ doc_suspicious_footnotes | ||
#[doc = r#""#] | ||
#[doc = r#"This is not a footnote[^either], but it doesn't warn."#] | ||
blyxyas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[doc = r#""#] | ||
#[doc = r#"This is not a footnote\[^1], but it also doesn't warn."#] | ||
#[doc = r#""#] | ||
#[doc = r#"This is not a footnote[^1\], but it also doesn't warn."#] | ||
#[doc = r#""#] | ||
#[doc = r#"This is not a `footnote[^1]`, but it also doesn't warn."#] | ||
#[doc = r#""#] | ||
#[doc = r#"This is a footnote[^2]."#] | ||
#[doc = r#""#] | ||
#[doc = r#"[^2]: hello world"#] | ||
pub fn footnotes() { | ||
// test code goes here | ||
} | ||
#[doc = r#"This is not a footnote[^1]. | ||
|
||
This is not a footnote[^either], but it doesn't warn. | ||
|
||
This is not a footnote\[^1], but it also doesn't warn. | ||
|
||
This is not a footnote[^1\], but it also doesn't warn. | ||
|
||
This is not a `footnote[^1]`, but it also doesn't warn. | ||
|
||
This is a footnote[^2]. | ||
|
||
[^2]: hello world | ||
|
||
|
||
[^1]: <!-- description -->"#] | ||
//~^^^^^^^^^^^^^^ doc_suspicious_footnotes | ||
pub fn footnotes2() { | ||
// test code goes here | ||
} | ||
#[cfg_attr( | ||
not(FALSE), | ||
doc = r#"This is not a footnote[^1]. | ||
|
||
This is not a footnote[^either], but it doesn't warn. | ||
|
||
[^1]: <!-- description -->"# | ||
//~^ doc_suspicious_footnotes | ||
)] | ||
pub fn footnotes3() { | ||
// test code goes here | ||
} | ||
#[doc = "My footnote [^foot\note]"] | ||
pub fn footnote4() { | ||
// test code goes here | ||
} | ||
#[doc = "Hihi"]pub fn footnote5() { | ||
// test code goes here | ||
} | ||
} | ||
|
||
#[doc = r#"This is not a footnote[^1]. | ||
|
||
[^1]: <!-- description -->"#] | ||
//~^ doc_suspicious_footnotes | ||
#[doc = r""] | ||
#[doc = r"This is not a footnote[^either], but it doesn't warn."] | ||
#[doc = r""] | ||
#[doc = r"This is not a footnote\[^1], but it also doesn't warn."] | ||
#[doc = r""] | ||
#[doc = r"This is not a footnote[^1\], but it also doesn't warn."] | ||
#[doc = r""] | ||
#[doc = r"This is not a `footnote[^1]`, but it also doesn't warn."] | ||
#[doc = r""] | ||
#[doc = r"This is a footnote[^2]."] | ||
#[doc = r""] | ||
#[doc = r"[^2]: hello world"] | ||
pub fn footnotes_attrs() { | ||
// test code goes here | ||
} | ||
|
||
pub mod multiline { | ||
/*! | ||
* This is not a footnote[^1]. //~ doc_suspicious_footnotes | ||
* | ||
* This is not a footnote\[^1], but it doesn't warn. | ||
* | ||
* This is a footnote[^2]. | ||
* | ||
* These give weird results, but correct ones, so it works. | ||
* | ||
* [^2]: hello world | ||
*/ | ||
/*! [^1]: <!-- description --> */ | ||
/** | ||
* This is not a footnote[^1]. //~ doc_suspicious_footnotes | ||
* | ||
* This is not a footnote\[^1], but it doesn't warn. | ||
* | ||
* This is a footnote[^2]. | ||
* | ||
* These give weird results, but correct ones, so it works. | ||
* | ||
* [^2]: hello world | ||
*/ | ||
/** [^1]: <!-- description --> */ | ||
pub fn foo() {} | ||
} | ||
|
||
/// This is not a footnote [^1] | ||
/// | ||
/// [^1]: <!-- description --> | ||
//~^ doc_suspicious_footnotes | ||
/// | ||
/// This one is [^2] | ||
/// | ||
/// [^2]: contents | ||
#[doc = r#"This is not a footnote [^3] | ||
|
||
[^3]: <!-- description -->"#] | ||
//~^ doc_suspicious_footnotes | ||
#[doc = ""] | ||
#[doc = "This one is [^4]"] | ||
#[doc = ""] | ||
#[doc = "[^4]: contents"] | ||
pub struct MultiFragmentFootnote; | ||
|
||
#[doc(inline)] | ||
/// This is not a footnote [^5] | ||
/// | ||
/// [^5]: <!-- description --> | ||
//~^ doc_suspicious_footnotes | ||
/// | ||
/// This one is [^6] | ||
/// | ||
/// [^6]: contents | ||
#[doc = r#"This is not a footnote [^7] | ||
|
||
[^7]: <!-- description -->"#] | ||
//~^ doc_suspicious_footnotes | ||
#[doc = ""] | ||
#[doc = "This one is [^8]"] | ||
#[doc = ""] | ||
#[doc = "[^8]: contents"] | ||
pub use MultiFragmentFootnote as OtherInlinedFootnote; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.