rust-lang / rust-lang/rust-clippy

expect_fun_call: should not suggest using format arg capture if edition != 2021

Open
#9,019 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

If compiled with edition 2018, clippy will suggest the third line for the second line, but the third line will cause the warning about unused format arg... since this is only feature of edition 2021 apparently :/

Lint Name

expect_fun_call

Reproducer

I tried this code:

pub fn main() {
    let x = 123;
    Option::<i32>::None.expect(&format!("{x}"));
    Option::<i32>::None.unwrap_or_else(|| panic!("{x}"));
}

I saw this happen:

   Checking playground v0.0.1 (/playground)
warning: use of `expect` followed by a function call
 --> src/main.rs:3:25
  |
3 |     Option::<i32>::None.expect(&format!("{x}"));
  |                         ^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("{x}"))`
  |
  = note: `#[warn(clippy::expect_fun_call)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#expect_fun_call

warning: panic message contains an unused formatting placeholder
 --> src/main.rs:4:51
  |
4 |     Option::<i32>::None.unwrap_or_else(|| panic!("{x}"));
  |                                                   ^^^
  |
  = note: `#[warn(non_fmt_panics)]` on by default
  = note: this message is not used as a format string when given without arguments, but will be in Rust 2021
help: add the missing argument
  |
4 |     Option::<i32>::None.unwrap_or_else(|| panic!("{x}", ...));
  |                                                       +++++
help: or add a "{}" format string to use the message literally
  |
4 |     Option::<i32>::None.unwrap_or_else(|| panic!("{}", "{x}"));
  |                                                  +++++

warning: `playground` (bin "playground") generated 2 warnings
    Finished dev [unoptimized + debuginfo] target(s) in 0.42s

Version
clippy 0.1.63 (ca122c7 2022-06-13)
Additional Labels

No response

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

The entry point is the expect_fun_call lint; first reproduce the supplied Rust example under edition 2018 and compare its diagnostic with edition 2021. Done means the suggestion is edition-aware and a regression case covers the reported behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.