Fix clippy.

This commit is contained in:
Camille GILLOT 2023-09-03 14:34:40 +00:00
parent c3170771f3
commit d5f0f443b9

View File

@ -12,7 +12,7 @@
use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::{BytePos, RelativeBytePos,Pos, Span, SyntaxContext}; use rustc_span::{BytePos, Pos, RelativeBytePos, Span, SyntaxContext};
declare_clippy_lint! { declare_clippy_lint! {
/// ### What it does /// ### What it does
@ -514,7 +514,7 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSaf
match text_has_safety_comment( match text_has_safety_comment(
src, src,
&lines[comment_start_line.line + 1..=unsafe_line.line], &lines[comment_start_line.line + 1..=unsafe_line.line],
unsafe_line.sf.start_pos.to_usize(), unsafe_line.sf.start_pos,
) { ) {
Some(b) => HasSafetyComment::Yes(b), Some(b) => HasSafetyComment::Yes(b),
None => HasSafetyComment::No, None => HasSafetyComment::No,
@ -558,7 +558,7 @@ fn stmt_has_safety_comment(cx: &LateContext<'_>, span: Span, hir_id: HirId) -> H
match text_has_safety_comment( match text_has_safety_comment(
src, src,
&lines[comment_start_line.line + 1..=unsafe_line.line], &lines[comment_start_line.line + 1..=unsafe_line.line],
unsafe_line.sf.start_pos.to_usize(), unsafe_line.sf.start_pos,
) { ) {
Some(b) => HasSafetyComment::Yes(b), Some(b) => HasSafetyComment::Yes(b),
None => HasSafetyComment::No, None => HasSafetyComment::No,
@ -619,7 +619,7 @@ fn span_from_macro_expansion_has_safety_comment(cx: &LateContext<'_>, span: Span
match text_has_safety_comment( match text_has_safety_comment(
src, src,
&lines[macro_line.line + 1..=unsafe_line.line], &lines[macro_line.line + 1..=unsafe_line.line],
unsafe_line.sf.start_pos.to_usize(), unsafe_line.sf.start_pos,
) { ) {
Some(b) => HasSafetyComment::Yes(b), Some(b) => HasSafetyComment::Yes(b),
None => HasSafetyComment::No, None => HasSafetyComment::No,
@ -675,7 +675,7 @@ fn span_in_body_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool {
body_line.line < unsafe_line.line && text_has_safety_comment( body_line.line < unsafe_line.line && text_has_safety_comment(
src, src,
&lines[body_line.line + 1..=unsafe_line.line], &lines[body_line.line + 1..=unsafe_line.line],
unsafe_line.sf.start_pos.to_usize(), unsafe_line.sf.start_pos,
).is_some() ).is_some()
}) })
} else { } else {
@ -688,13 +688,13 @@ fn span_in_body_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool {
} }
/// Checks if the given text has a safety comment for the immediately proceeding line. /// Checks if the given text has a safety comment for the immediately proceeding line.
fn text_has_safety_comment(src: &str, line_starts: &[RelativeBytePos], offset: usize) -> Option<BytePos> { fn text_has_safety_comment(src: &str, line_starts: &[RelativeBytePos], start_pos: BytePos) -> Option<BytePos> {
let mut lines = line_starts let mut lines = line_starts
.array_windows::<2>() .array_windows::<2>()
.rev() .rev()
.map_while(|[start, end]| { .map_while(|[start, end]| {
let start = start.to_usize() - offset; let start = start.to_usize();
let end = end.to_usize() - offset; let end = end.to_usize();
let text = src.get(start..end)?; let text = src.get(start..end)?;
let trimmed = text.trim_start(); let trimmed = text.trim_start();
Some((start + (text.len() - trimmed.len()), trimmed)) Some((start + (text.len() - trimmed.len()), trimmed))
@ -709,9 +709,7 @@ fn text_has_safety_comment(src: &str, line_starts: &[RelativeBytePos], offset: u
let (mut line, mut line_start) = (line, line_start); let (mut line, mut line_start) = (line, line_start);
loop { loop {
if line.to_ascii_uppercase().contains("SAFETY:") { if line.to_ascii_uppercase().contains("SAFETY:") {
return Some(BytePos( return Some(start_pos + BytePos(u32::try_from(line_start).unwrap()));
u32::try_from(line_start).unwrap() + u32::try_from(offset).unwrap(),
));
} }
match lines.next() { match lines.next() {
Some((s, x)) if x.starts_with("//") => (line, line_start) = (x, s), Some((s, x)) if x.starts_with("//") => (line, line_start) = (x, s),
@ -724,15 +722,13 @@ fn text_has_safety_comment(src: &str, line_starts: &[RelativeBytePos], offset: u
let (mut line_start, mut line) = (line_start, line); let (mut line_start, mut line) = (line_start, line);
loop { loop {
if line.starts_with("/*") { if line.starts_with("/*") {
let src = &src[line_start..line_starts.last().unwrap().to_usize() - offset]; let src = &src[line_start..line_starts.last().unwrap().to_usize()];
let mut tokens = tokenize(src); let mut tokens = tokenize(src);
return (src[..tokens.next().unwrap().len as usize] return (src[..tokens.next().unwrap().len as usize]
.to_ascii_uppercase() .to_ascii_uppercase()
.contains("SAFETY:") .contains("SAFETY:")
&& tokens.all(|t| t.kind == TokenKind::Whitespace)) && tokens.all(|t| t.kind == TokenKind::Whitespace))
.then_some(BytePos( .then_some(start_pos + BytePos(u32::try_from(line_start).unwrap()));
u32::try_from(line_start).unwrap() + u32::try_from(offset).unwrap(),
));
} }
match lines.next() { match lines.next() {
Some(x) => (line_start, line) = x, Some(x) => (line_start, line) = x,