Skip to content

Commit cbec8fb

Browse files
committed
fix doc comments, use optional snippets
1 parent 1dbf6f9 commit cbec8fb

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

clippy_lints/src/doc/doc_comment_double_space_linebreak.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::source::snippet;
2+
use clippy_utils::source::snippet_opt;
33
use rustc_errors::Applicability;
44
use rustc_lint::LateContext;
55
use rustc_span::Span;
@@ -30,7 +30,8 @@ fn collect_doc_replacements(cx: &LateContext<'_>, spans: &[Span]) -> Vec<(Span,
3030
spans
3131
.iter()
3232
.map(|span| {
33-
let s = snippet(cx, *span, "..");
33+
// we already made sure the snippet exists when collecting spans
34+
let s = snippet_opt(cx, *span).expect("snippet was already validated to exist");
3435
let after_newline = s.trim_start_matches(' ');
3536

3637
let new_comment = format!("\\{after_newline}");

clippy_lints/src/doc/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_config::Conf;
55
use clippy_utils::attrs::is_doc_hidden;
66
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
77
use clippy_utils::macros::{is_panic, root_macro_call_first_node};
8-
use clippy_utils::source::snippet;
8+
use clippy_utils::source::snippet_opt;
99
use clippy_utils::ty::is_type_diagnostic_item;
1010
use clippy_utils::visitors::Visitable;
1111
use clippy_utils::{is_entrypoint_fn, is_trait_impl_item, method_chain_args};
@@ -535,11 +535,11 @@ declare_clippy_lint! {
535535
}
536536

537537
declare_clippy_lint! {
538-
/// Detects doc comment linebreaks that use double spaces to separate lines, instead of back-slash (\).
538+
/// Detects doc comment linebreaks that use double spaces to separate lines, instead of back-slash (`\`).
539539
///
540540
/// ### Why is this bad?
541541
/// Double spaces, when used as doc comment linebreaks, can be difficult to see, and may
542-
/// accidentally be removed during automatic formatting or manual refactoring. The use of a back-slash (\)
542+
/// accidentally be removed during automatic formatting or manual refactoring. The use of a back-slash (`\`)
543543
/// is clearer in this regard.
544544
///
545545
/// ### Example
@@ -814,6 +814,7 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
814814
let mut code_level = 0;
815815
let mut blockquote_level = 0;
816816
let mut collected_breaks: Vec<Span> = Vec::new();
817+
let mut is_first_paragraph = true;
817818

818819
let mut containers = Vec::new();
819820

@@ -934,7 +935,7 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
934935

935936
if let Some(span) = fragments.span(cx, range.clone())
936937
&& !span.from_expansion()
937-
&& let snippet = snippet(cx, span, "..")
938+
&& let Some(snippet) = snippet_opt(cx, span)
938939
&& !snippet.trim().starts_with('\\')
939940
&& event == HardBreak {
940941
collected_breaks.push(span);

0 commit comments

Comments
 (0)