@@ -7,6 +7,7 @@ use clippy_config::Conf;
7
7
use clippy_utils:: attrs:: is_doc_hidden;
8
8
use clippy_utils:: diagnostics:: { span_lint, span_lint_and_help, span_lint_and_then} ;
9
9
use clippy_utils:: macros:: { is_panic, root_macro_call_first_node} ;
10
+ use clippy_utils:: source:: snippet_opt;
10
11
use clippy_utils:: ty:: is_type_diagnostic_item;
11
12
use clippy_utils:: visitors:: Visitable ;
12
13
use clippy_utils:: { is_entrypoint_fn, is_trait_impl_item, method_chain_args} ;
@@ -33,6 +34,7 @@ use rustc_span::{Span, sym};
33
34
use std:: ops:: Range ;
34
35
use url:: Url ;
35
36
37
+ mod doc_comment_double_space_linebreak;
36
38
mod include_in_doc_without_cfg;
37
39
mod link_with_quotes;
38
40
mod markdown;
@@ -567,6 +569,38 @@ declare_clippy_lint! {
567
569
"link reference defined in list item or quote"
568
570
}
569
571
572
+ declare_clippy_lint ! {
573
+ /// Detects doc comment linebreaks that use double spaces to separate lines, instead of back-slash (`\`).
574
+ ///
575
+ /// ### Why is this bad?
576
+ /// Double spaces, when used as doc comment linebreaks, can be difficult to see, and may
577
+ /// accidentally be removed during automatic formatting or manual refactoring. The use of a back-slash (`\`)
578
+ /// is clearer in this regard.
579
+ ///
580
+ /// ### Example
581
+ /// The two replacement dots in this example represent a double space.
582
+ /// ```no_run
583
+ /// /// This command takes two numbers as inputs and··
584
+ /// /// adds them together, and then returns the result.
585
+ /// fn add(l: i32, r: i32) -> i32 {
586
+ /// l + r
587
+ /// }
588
+ /// ```
589
+ ///
590
+ /// Use instead:
591
+ /// ```no_run
592
+ /// /// This command takes two numbers as inputs and\
593
+ /// /// adds them together, and then returns the result.
594
+ /// fn add(l: i32, r: i32) -> i32 {
595
+ /// l + r
596
+ /// }
597
+ /// ```
598
+ #[ clippy:: version = "1.80.0" ]
599
+ pub DOC_COMMENT_DOUBLE_SPACE_LINEBREAK ,
600
+ pedantic,
601
+ "double-space used for doc comment linebreak instead of `\\ `"
602
+ }
603
+
570
604
pub struct Documentation {
571
605
valid_idents : FxHashSet < String > ,
572
606
check_private_items : bool ,
@@ -598,6 +632,7 @@ impl_lint_pass!(Documentation => [
598
632
DOC_OVERINDENTED_LIST_ITEMS ,
599
633
TOO_LONG_FIRST_DOC_PARAGRAPH ,
600
634
DOC_INCLUDE_WITHOUT_CFG ,
635
+ DOC_COMMENT_DOUBLE_SPACE_LINEBREAK
601
636
] ) ;
602
637
603
638
impl EarlyLintPass for Documentation {
@@ -737,6 +772,8 @@ fn check_attrs(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs: &[
737
772
return None ;
738
773
}
739
774
775
+ suspicious_doc_comments:: check ( cx, attrs) ;
776
+
740
777
let ( fragments, _) = attrs_to_doc_fragments (
741
778
attrs. iter ( ) . filter_map ( |attr| {
742
779
if attr. doc_str_and_comment_kind ( ) . is_none ( ) || attr. span ( ) . in_external_macro ( cx. sess ( ) . source_map ( ) ) {
@@ -894,6 +931,7 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
894
931
let mut paragraph_range = 0 ..0 ;
895
932
let mut code_level = 0 ;
896
933
let mut blockquote_level = 0 ;
934
+ let mut collected_breaks: Vec < Span > = Vec :: new ( ) ;
897
935
let mut is_first_paragraph = true ;
898
936
899
937
let mut containers = Vec :: new ( ) ;
@@ -1069,6 +1107,14 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
1069
1107
& containers[ ..] ,
1070
1108
) ;
1071
1109
}
1110
+
1111
+ if let Some ( span) = fragments. span ( cx, range. clone ( ) )
1112
+ && !span. from_expansion ( )
1113
+ && let Some ( snippet) = snippet_opt ( cx, span)
1114
+ && !snippet. trim ( ) . starts_with ( '\\' )
1115
+ && event == HardBreak {
1116
+ collected_breaks. push ( span) ;
1117
+ }
1072
1118
} ,
1073
1119
Text ( text) => {
1074
1120
paragraph_range. end = range. end ;
@@ -1119,6 +1165,9 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
1119
1165
FootnoteReference ( _) => { }
1120
1166
}
1121
1167
}
1168
+
1169
+ doc_comment_double_space_linebreak:: check ( cx, & collected_breaks) ;
1170
+
1122
1171
headers
1123
1172
}
1124
1173
0 commit comments