rust-lang / rust-lang/rust-clippy

Detect inconsistent "style" for safety-related docs/comments

Open
#15,033 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint G-Rust-for-Linux
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Projects typically use a single convention for safety docs and comments, typically # Safety sections in docs and // SAFETY: comments. However, it is possible for developers to mix up the two styles from time to time. It would be useful to have a lint that detects that.

When combined with other safety-related Clippy lints, this becomes extra effective. For instance, we had a case in Linux of safe functions that should have been unsafe fn: the safety "section" was there but using the incorrect style, and thus Clippy didn't lint about it.

The other safety-related lints may be enough to cover all cases (or extending them otherwise). For instance, if https://github.com/rust-lang/rust-clippy/issues/15034 is a false negative (i.e. if that lint is supposed to catch even the /// case and not just //) and it is fixed, then we would have caught the case in the patch above. Thus this lint may be unneeded for projects that enable all the safety-related lints.

Advantage
  • May actually catch extra issues when combined with other safety-related lints (please see above).
  • More consistency in docs.
  • Less time spent by reviewers.
Drawbacks

No response

Example
/// SAFETY: ...
fn f() {}

Should be written as:

/// # Safety
///
/// ...
fn f() {}

Note that the lint should trigger even if the function is safe, because that allows the other safety-related lints to detect other mistakes (please see above).

Similarly:

fn f() {
    // # Safety
    //
    // ...
    unsafe { g() }
}

Should be written as:

fn f() {
    /// SAFETY: ...
    unsafe { g() }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the proposed examples with the existing safety-related Clippy lints and the linked issue about /// handling. Determine whether those lints already cover the inconsistent styles, then define the cases this lint should diagnose and verify that the intended examples are reported without duplicating existing diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.