Skip to content
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

Don't look for safety comments in doc tests #12066

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion clippy_lints/src/undocumented_unsafe_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,11 +681,19 @@ fn text_has_safety_comment(src: &str, line_starts: &[RelativeBytePos], start_pos
.filter(|(_, text)| !text.is_empty());

let (line_start, line) = lines.next()?;
let mut in_codeblock = false;
// Check for a sequence of line comments.
if line.starts_with("//") {
let (mut line, mut line_start) = (line, line_start);
loop {
if line.to_ascii_uppercase().contains("SAFETY:") {
// Don't lint if the safety comment is part of a codeblock in a doc comment.
// It may or may not be required, and we can't very easily check it (and we shouldn't, since
// the safety comment isn't referring to the node we're currently checking)
if line.trim_start_matches("///").trim_start().starts_with("```") {
in_codeblock = !in_codeblock;
}

if line.to_ascii_uppercase().contains("SAFETY:") && !in_codeblock {
return Some(start_pos + BytePos(u32::try_from(line_start).unwrap()));
}
match lines.next() {
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/unnecessary_safety_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,25 @@ mod issue_10084 {
}
}

mod issue_12048 {
pub const X: u8 = 0;

/// Returns a pointer to five.
///
/// # Examples
///
/// ```
/// use foo::point_to_five;
///
/// let five_pointer = point_to_five();
/// // Safety: this pointer always points to a valid five.
/// let five = unsafe { *five_pointer };
/// assert_eq!(five, 5);
/// ```
pub fn point_to_five() -> *const u8 {
static FIVE: u8 = 5;
&FIVE
}
}

fn main() {}