-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add new comment_within_doc
lint
#15260
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
Open
GuillaumeGomez
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
GuillaumeGomez:comment_within_doc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+238
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,107 @@ | ||
use rustc_ast::token::CommentKind; | ||
use rustc_ast::{AttrKind, AttrStyle}; | ||
use rustc_errors::Applicability; | ||
use rustc_lexer::{TokenKind, tokenize}; | ||
use rustc_lint::{EarlyContext, LintContext}; | ||
use rustc_span::source_map::SourceMap; | ||
use rustc_span::{BytePos, Span}; | ||
|
||
use clippy_utils::diagnostics::span_lint_and_then; | ||
|
||
use super::COMMENT_WITHIN_DOC; | ||
|
||
struct AttrInfo { | ||
line: usize, | ||
is_outer: bool, | ||
span: Span, | ||
file_span_pos: BytePos, | ||
} | ||
|
||
impl AttrInfo { | ||
fn new(source_map: &SourceMap, attr: &rustc_ast::Attribute) -> Option<Self> { | ||
let span_info = source_map.span_to_lines(attr.span).ok()?; | ||
// If we cannot get the line for any reason, no point in building this item. | ||
let line = span_info.lines.last()?.line_index; | ||
Some(Self { | ||
line, | ||
is_outer: attr.style == AttrStyle::Outer, | ||
span: attr.span, | ||
file_span_pos: span_info.file.start_pos, | ||
}) | ||
} | ||
} | ||
|
||
// Returns a `Vec` of `TokenKind` if the span only contains comments, otherwise returns `None`. | ||
fn snippet_contains_only_comments(snippet: &str) -> Option<Vec<TokenKind>> { | ||
let mut tokens = Vec::new(); | ||
for token in tokenize(snippet) { | ||
match token.kind { | ||
TokenKind::Whitespace => {}, | ||
TokenKind::BlockComment { .. } | TokenKind::LineComment { .. } => tokens.push(token.kind), | ||
_ => return None, | ||
} | ||
} | ||
Some(tokens) | ||
} | ||
|
||
pub(super) fn check(cx: &EarlyContext<'_>, attrs: &[rustc_ast::Attribute]) { | ||
let mut stored_prev_attr = None; | ||
let source_map = cx.sess().source_map(); | ||
for attr in attrs | ||
.iter() | ||
// We ignore `#[doc = "..."]` and `/** */` attributes. | ||
.filter(|attr| matches!(attr.kind, AttrKind::DocComment(CommentKind::Line, _))) | ||
{ | ||
let Some(attr) = AttrInfo::new(source_map, attr) else { | ||
stored_prev_attr = None; | ||
continue; | ||
}; | ||
let Some(prev_attr) = stored_prev_attr else { | ||
stored_prev_attr = Some(attr); | ||
continue; | ||
}; | ||
// First we check if they are from the same file and if they are the same kind of doc | ||
// comments. | ||
if attr.file_span_pos != prev_attr.file_span_pos || attr.is_outer != prev_attr.is_outer { | ||
stored_prev_attr = Some(attr); | ||
continue; | ||
} | ||
let diff = attr.line - (prev_attr.line + 1); | ||
// Then we check if they follow each other. | ||
if diff == 0 || diff > 1 { | ||
// If there is no line between them or there are more than 1, we skip this check. | ||
stored_prev_attr = Some(attr); | ||
continue; | ||
} | ||
let span_between = prev_attr.span.between(attr.span); | ||
// If there is one line between the two doc comments and it contains only one line comment, | ||
// then we lint. | ||
if diff == 1 | ||
&& let Ok(snippet) = source_map.span_to_snippet(span_between) | ||
&& let Some(comments) = snippet_contains_only_comments(&snippet) | ||
&& let &[TokenKind::LineComment { .. }] = comments.as_slice() | ||
{ | ||
let offset_begin = snippet.trim_start().len(); | ||
let offset_end = snippet.trim_end().len(); | ||
let span = span_between | ||
.with_hi(span_between.lo() + BytePos(offset_begin.try_into().unwrap())) | ||
.with_hi(span_between.hi() - BytePos(offset_end.try_into().unwrap())); | ||
let comment_kind = if attr.is_outer { '/' } else { '!' }; | ||
span_lint_and_then( | ||
cx, | ||
COMMENT_WITHIN_DOC, | ||
span, | ||
"code comment surrounded by doc comments", | ||
|diag| { | ||
diag.span_suggestion( | ||
span.with_hi(span.lo() + BytePos(2)), | ||
"did you mean to make it a doc comment?", | ||
format!("//{comment_kind}"), | ||
Applicability::MaybeIncorrect, | ||
); | ||
}, | ||
); | ||
} | ||
stored_prev_attr = Some(attr); | ||
} | ||
} |
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,23 @@ | ||
#![warn(clippy::comment_within_doc)] | ||
|
||
//! Hello//!/ oups | ||
//! tadam | ||
//~^^^ comment_within_doc | ||
|
||
/// Hello//// oups | ||
/// hehe | ||
//~^^^ comment_within_doc | ||
struct Bar; | ||
|
||
mod b { | ||
//! targe//! // oups | ||
//! hello | ||
// | ||
/// nope/// // oups | ||
/// yep | ||
//~^^^ comment_within_doc | ||
//~^^^^^^^^ comment_within_doc | ||
struct Bar; | ||
} | ||
|
||
fn main() {} |
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,27 @@ | ||
#![warn(clippy::comment_within_doc)] | ||
|
||
//! Hello | ||
// oups | ||
//! tadam | ||
//~^^^ comment_within_doc | ||
|
||
/// Hello | ||
// oups | ||
/// hehe | ||
//~^^^ comment_within_doc | ||
struct Bar; | ||
|
||
mod b { | ||
//! targe | ||
// oups | ||
//! hello | ||
// | ||
/// nope | ||
// oups | ||
/// yep | ||
//~^^^ comment_within_doc | ||
//~^^^^^^^^ comment_within_doc | ||
struct Bar; | ||
} | ||
|
||
fn main() {} |
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,51 @@ | ||
error: code comment surrounded by doc comments | ||
--> tests/ui/comment_within_doc.rs:3:10 | ||
| | ||
LL | //! Hello | ||
| __________^ | ||
| |__________| | ||
LL | || // oups | ||
| || ^ | ||
| ||_| | ||
| |_help: did you mean to make it a doc comment?: `//!` | ||
| | ||
| | ||
= note: `-D clippy::comment-within-doc` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::comment_within_doc)]` | ||
|
||
error: code comment surrounded by doc comments | ||
--> tests/ui/comment_within_doc.rs:8:10 | ||
| | ||
LL | /// Hello | ||
| __________^ | ||
| |__________| | ||
LL | || // oups | ||
| || ^ | ||
| ||_| | ||
| |_help: did you mean to make it a doc comment?: `///` | ||
| | ||
|
||
error: code comment surrounded by doc comments | ||
--> tests/ui/comment_within_doc.rs:15:14 | ||
| | ||
LL | //! targe | ||
| ______________^ | ||
| |______________| | ||
LL | || // oups | ||
| ||_-__^ | ||
| |__| | ||
| help: did you mean to make it a doc comment?: `//!` | ||
|
||
error: code comment surrounded by doc comments | ||
--> tests/ui/comment_within_doc.rs:19:13 | ||
| | ||
LL | /// nope | ||
| _____________^ | ||
| |_____________| | ||
LL | || // oups | ||
| ||_-__^ | ||
| |__| | ||
| help: did you mean to make it a doc comment?: `///` | ||
|
||
error: aborting due to 4 previous errors | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That "fix" is wrong, isn't it?