-
Notifications
You must be signed in to change notification settings - Fork 1.7k
new lint: doc_comment_double_space_linebreaks
#12876
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f94f64f
new lint: `doc_comment_double_space_linebreak`
Jacherr 2fd51b8
`doc_comment_double_space_linebreaks`: lint per line instead of by block
Jacherr 60191cf
`doc_comment_double_space_linebreak`: Use multi spans and suggestions
xFrednet 7fe160a
Rename `doc_comment_double_space_linebreak` -> `doc_comment_double_sp…
xFrednet 45e4487
`doc_comment_double_space_linebreaks`: Fix tests
xFrednet 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
33 changes: 33 additions & 0 deletions
33
clippy_lints/src/doc/doc_comment_double_space_linebreaks.rs
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,33 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::LateContext; | ||
use rustc_span::{BytePos, Span}; | ||
|
||
use super::DOC_COMMENT_DOUBLE_SPACE_LINEBREAKS; | ||
|
||
pub fn check(cx: &LateContext<'_>, collected_breaks: &[Span]) { | ||
if collected_breaks.is_empty() { | ||
return; | ||
} | ||
|
||
let breaks: Vec<_> = collected_breaks | ||
.iter() | ||
.map(|span| span.with_hi(span.lo() + BytePos(2))) | ||
.collect(); | ||
|
||
span_lint_and_then( | ||
cx, | ||
DOC_COMMENT_DOUBLE_SPACE_LINEBREAKS, | ||
breaks.clone(), | ||
"doc comment uses two spaces for a hard line break", | ||
|diag| { | ||
let suggs: Vec<_> = breaks.iter().map(|span| (*span, "\\".to_string())).collect(); | ||
diag.tool_only_multipart_suggestion( | ||
"replace this double space with a backslash:", | ||
suggs, | ||
Applicability::MachineApplicable, | ||
); | ||
diag.help("replace this double space with a backslash: `\\`"); | ||
}, | ||
); | ||
} |
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,98 @@ | ||
#![feature(custom_inner_attributes)] | ||
#![rustfmt::skip] | ||
|
||
#![warn(clippy::doc_comment_double_space_linebreaks)] | ||
#![allow(unused, clippy::empty_docs)] | ||
|
||
//~v doc_comment_double_space_linebreaks | ||
//! 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 | ||
*/ | ||
} | ||
|
||
//~v doc_comment_double_space_linebreaks | ||
/// 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() {} | ||
|
||
//~v doc_comment_double_space_linebreaks | ||
/// 🌹 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() {} | ||
|
||
//~v doc_comment_double_space_linebreaks | ||
/// 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 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,98 @@ | ||
#![feature(custom_inner_attributes)] | ||
#![rustfmt::skip] | ||
|
||
#![warn(clippy::doc_comment_double_space_linebreaks)] | ||
#![allow(unused, clippy::empty_docs)] | ||
|
||
//~v doc_comment_double_space_linebreaks | ||
//! 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 | ||
*/ | ||
} | ||
|
||
//~v doc_comment_double_space_linebreaks | ||
/// 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() {} | ||
|
||
//~v doc_comment_double_space_linebreaks | ||
/// 🌹 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() {} | ||
|
||
//~v doc_comment_double_space_linebreaks | ||
/// 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.
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.