rust-lang / rust-lang/rust-clippy

`clippy::no_effect` trips when instantiating an uninhabited type

Open
#8,033 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Lint name: clippy::no_effect

Continuing the trend of technically-false positives that almost nobody will encounter, I tried this code (playground):

fn main() {
    unsafe {
        *(1 as *mut std::convert::Infallible);
    }
}

Running cargo clippy produces the following diagnostic:

warning: statement with no effect
 --> src/main.rs:3:9
  |
3 |         *(1 as *mut std::convert::Infallible);
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(clippy::no_effect)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#no_effect

However, Infallible is uninhabited, which seems to indicate that this statement causes undefined behaviour, which is most definitely an effect (and a very unpleasant one at that).

$ cargo miri run
   Compiling clippy-issue v0.1.0 (/tmp/clippy-issue)
    Finished dev [unoptimized + debuginfo] target(s) in 0.24s
     Running `/home/patchmixolydic/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo-miri target/miri/x86_64-unknown-linux-gnu/debug/clippy-issue`
error: Undefined Behavior: type validation failed: encountered a value of uninhabited type std::convert::Infallible
 --> src/main.rs:3:9
  |
3 |         *(1 as *mut std::convert::Infallible);
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of uninhabited type std::convert::Infallible
  |
  = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
  = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
          
  = note: inside `main` at src/main.rs:3:9
...

This may cause confusion if someone unknowingly tries to construct an uninhabited type, forgets to assign it to anything, and takes the Clippy warning to mean that it doesn't cause undefined behaviour, but this is astronomically unlikely. Anyone who writes code like this is likely knowingly trying to invoke UB. I doubt this is worth fixing, but just in case...

Meta

Rust version (rustc -Vv):

rustc 1.58.0-nightly (8b09ba6a5 2021-11-09)
binary: rustc
commit-hash: 8b09ba6a5d5c644fe0f1c27c7f9c80b334241707
commit-date: 2021-11-09
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.0

Also reproduced on 1.58.0-nightly (2021-11-24 b426445c60b4faab6e96).

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 at the clippy::no_effect lint implementation and reproduce the report's Infallible example with cargo clippy. Inspect how the lint handles dereferenced expressions, then add a regression test if the project's existing lint tests cover it. Done means the example no longer receives an incorrect no_effect diagnostic while ordinary no-effect statements remain covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
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.