-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
new lint: doc_comment_double_space_linebreak
#12876
Open
Jacherr
wants to merge
9
commits into
rust-lang:master
Choose a base branch
from
Jacherr:issue-12163
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.
+356
−0
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7bef775
new restriction lint: `doc_comment_double_space_linebreak`
Jacherr d7bb600
fix typo
Jacherr 1e69f82
change replacement character in example, remove extraneous space from…
Jacherr 42ad278
add additional testcases; check doc comment not from expansion
e646eda
do not lint on macros, add more testcases
Jacherr 7babc2a
fix wording, remove commented out code, add additonal testcase
Jacherr 4519980
uibless
Jacherr ef247f9
fix doc comments, use optional snippets
Jacherr 7ad48d8
Remove unneeded additional space
Jacherr 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 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 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
41 changes: 41 additions & 0 deletions
41
clippy_lints/src/doc/doc_comment_double_space_linebreak.rs
This file contains 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,41 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::source::snippet_opt; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::LateContext; | ||
use rustc_span::Span; | ||
|
||
use super::DOC_COMMENT_DOUBLE_SPACE_LINEBREAK; | ||
|
||
pub fn check(cx: &LateContext<'_>, collected_breaks: &[Span]) { | ||
let replacements: Vec<_> = collect_doc_replacements(cx, collected_breaks); | ||
|
||
if let Some((&(lo_span, _), &(hi_span, _))) = replacements.first().zip(replacements.last()) { | ||
span_lint_and_then( | ||
cx, | ||
DOC_COMMENT_DOUBLE_SPACE_LINEBREAK, | ||
lo_span.to(hi_span), | ||
"doc comment uses two spaces for a hard line break", | ||
|diag| { | ||
diag.multipart_suggestion( | ||
"replace this double space with a backslash", | ||
replacements, | ||
Applicability::MachineApplicable, | ||
); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
fn collect_doc_replacements(cx: &LateContext<'_>, spans: &[Span]) -> Vec<(Span, String)> { | ||
spans | ||
.iter() | ||
.map(|span| { | ||
// we already made sure the snippet exists when collecting spans | ||
let s = snippet_opt(cx, *span).expect("snippet was already validated to exist"); | ||
let after_newline = s.trim_start_matches(' '); | ||
|
||
let new_comment = format!("\\{after_newline}"); | ||
(*span, new_comment) | ||
}) | ||
.collect() | ||
} |
This file contains 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 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,94 @@ | ||
#![feature(custom_inner_attributes)] | ||
#![rustfmt::skip] | ||
|
||
#![warn(clippy::doc_comment_double_space_linebreak)] | ||
#![allow(unused, clippy::empty_docs)] | ||
|
||
//! Should warn on double space linebreaks\ | ||
//! in file/module doc comment | ||
|
||
/// Should not warn on single-line doc comments | ||
fn single_line() {} | ||
|
||
/// Should not warn on single-line doc comments | ||
/// split across multiple lines | ||
fn single_line_split() {} | ||
|
||
// Should not warn on normal comments | ||
|
||
// note: cargo fmt can remove double spaces from normal and block comments | ||
// Should not warn on normal comments | ||
// with double spaces at the end of a line | ||
|
||
#[doc = "This is a doc attribute, which should not be linted"] | ||
fn normal_comment() { | ||
/* | ||
Should not warn on block comments | ||
*/ | ||
|
||
/* | ||
Should not warn on block comments | ||
with double space at the end of a line | ||
*/ | ||
} | ||
|
||
/// Should warn when doc comment uses double space\ | ||
/// as a line-break, even when there are multiple\ | ||
/// in a row | ||
fn double_space_doc_comment() {} | ||
|
||
/// Should not warn when back-slash is used \ | ||
/// as a line-break | ||
fn back_slash_doc_comment() {} | ||
|
||
/// 🌹 are 🟥\ | ||
/// 🌷 are 🟦\ | ||
/// 📎 is 😎\ | ||
/// and so are 🫵\ | ||
/// (hopefully no formatting weirdness linting this) | ||
fn multi_byte_chars_tada() {} | ||
|
||
macro_rules! macro_that_makes_function { | ||
() => { | ||
/// Shouldn't lint on this! | ||
/// (hopefully) | ||
fn my_macro_created_function() {} | ||
} | ||
} | ||
|
||
macro_that_makes_function!(); | ||
|
||
// dont lint when its alone on a line | ||
/// | ||
fn alone() {} | ||
|
||
/// | First column | Second column | | ||
/// | ------------ | ------------- | | ||
/// | Not a line | break when | | ||
/// | after a line | in a table | | ||
fn table() {} | ||
|
||
/// ```text | ||
/// It's also not a hard line break if | ||
/// there's two spaces at the end of a | ||
/// line in a block code. | ||
/// ``` | ||
fn codeblock() {} | ||
|
||
/// It's also not a hard line break `if | ||
/// there's` two spaces in the middle of inline code. | ||
fn inline() {} | ||
|
||
/// It's also not a hard line break [when]( | ||
/// https://example.com) in a URL. | ||
fn url() {} | ||
|
||
/// here we mix\ | ||
/// double spaces\ | ||
/// and also\ | ||
/// adding backslash\ | ||
/// to some of them\ | ||
/// to see how that looks | ||
fn mixed() {} | ||
|
||
fn main() {} |
This file contains 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,94 @@ | ||
#![feature(custom_inner_attributes)] | ||
#![rustfmt::skip] | ||
|
||
#![warn(clippy::doc_comment_double_space_linebreak)] | ||
#![allow(unused, clippy::empty_docs)] | ||
|
||
//! Should warn on double space linebreaks | ||
//! in file/module doc comment | ||
|
||
/// Should not warn on single-line doc comments | ||
fn single_line() {} | ||
|
||
/// Should not warn on single-line doc comments | ||
/// split across multiple lines | ||
fn single_line_split() {} | ||
|
||
// Should not warn on normal comments | ||
|
||
// note: cargo fmt can remove double spaces from normal and block comments | ||
// Should not warn on normal comments | ||
// with double spaces at the end of a line | ||
|
||
#[doc = "This is a doc attribute, which should not be linted"] | ||
fn normal_comment() { | ||
Jacherr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/* | ||
Should not warn on block comments | ||
*/ | ||
|
||
/* | ||
Should not warn on block comments | ||
with double space at the end of a line | ||
*/ | ||
} | ||
|
||
/// Should warn when doc comment uses double space | ||
/// as a line-break, even when there are multiple | ||
/// in a row | ||
fn double_space_doc_comment() {} | ||
|
||
/// Should not warn when back-slash is used \ | ||
/// as a line-break | ||
fn back_slash_doc_comment() {} | ||
|
||
/// 🌹 are 🟥 | ||
/// 🌷 are 🟦 | ||
/// 📎 is 😎 | ||
/// and so are 🫵 | ||
/// (hopefully no formatting weirdness linting this) | ||
fn multi_byte_chars_tada() {} | ||
|
||
macro_rules! macro_that_makes_function { | ||
() => { | ||
/// Shouldn't lint on this! | ||
/// (hopefully) | ||
fn my_macro_created_function() {} | ||
} | ||
} | ||
|
||
macro_that_makes_function!(); | ||
|
||
// dont lint when its alone on a line | ||
/// | ||
fn alone() {} | ||
|
||
/// | First column | Second column | | ||
/// | ------------ | ------------- | | ||
/// | Not a line | break when | | ||
/// | after a line | in a table | | ||
fn table() {} | ||
|
||
/// ```text | ||
/// It's also not a hard line break if | ||
/// there's two spaces at the end of a | ||
/// line in a block code. | ||
/// ``` | ||
fn codeblock() {} | ||
|
||
/// It's also not a hard line break `if | ||
/// there's` two spaces in the middle of inline code. | ||
fn inline() {} | ||
|
||
/// It's also not a hard line break [when]( | ||
/// https://example.com) in a URL. | ||
fn url() {} | ||
|
||
/// here we mix | ||
/// double spaces\ | ||
/// and also | ||
/// adding backslash\ | ||
/// to some of them | ||
/// to see how that looks | ||
fn mixed() {} | ||
|
||
fn main() {} |
Oops, something went wrong.
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.
Should probably not have a space, see also #12876 (comment)
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.
@Jacherr, I think this is still open, or did you intentionally keep it this way?
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.
Yep, somehow missed this 😅. Should be resolved now.