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

False positive on standard read!() usage #4709

Closed
EverlastingBugstopper opened this issue Oct 21, 2019 · 1 comment · Fixed by #4721
Closed

False positive on standard read!() usage #4709

EverlastingBugstopper opened this issue Oct 21, 2019 · 1 comment · Fixed by #4721
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing L-suggestion Lint: Improving, adding or fixing lint suggestions T-macros Type: Issues with macros and macro expansion

Comments

@EverlastingBugstopper
Copy link

Hey! I've recently been getting this strange error for (what I think) is a fairly common usage of the text_io crate's read macro:

fn interactive_delete(prompt_string: &str) -> Result<bool, failure::Error> {
    println!("{} [y/n]", prompt_string);
    let mut response: String = read!("{}\n");
    response = response.split_whitespace().collect(); // remove whitespace
    response.make_ascii_lowercase(); // ensure response is all lowercase
    response.truncate(INTERACTIVE_RESPONSE_LEN); // at this point, all valid input will be "y" or "n"
    match response.as_ref() {
        YES => Ok(true),
        NO => Ok(false),
        _ => failure::bail!("Response must either be \"y\" for yes or \"n\" for no"),
    }
}

When running clippy I get the following warning:

$ cargo clippy
   Compiling wrangler v1.5.0-rc.0 (/Users/me/wrangler)
warning: returning an `Err(_)` with the `?` operator
   --> src/commands/kv/mod.rs:106:32
    |
106 |     let mut response: String = read!("{}\n");
    |                                ^^^^^^^^^^^^^
    |
    = note: `#[warn(clippy::try_err)]` on by default
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#try_err
    = note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

If I then use nightly and run with the recommend backtrace, I get this which is rather unintelligible to me. It seems as if clippy wants me to fix something in an external crate?

$ cargo clippy -- -Z external-macro-backtrace
warning: returning an `Err(_)` with the `?` operator
   --> <::text_io::try_scan macros>:8:45
    |
1   |  /     ($ pattern : expr, $ ($ arg : expr), *) =>
2   |  |     {
3   |  |         use :: std :: io :: Read ; try_scan !
    |  |____________________________________-
4   | ||         (:: std :: io :: stdin () . bytes () . map (| c | c . unwrap ()) => $
5   | ||          pattern, $ ($ arg), *) ; format_args ! ($ pattern, $ ($ arg), *) ;
    | ||_________________________________- in this macro invocation (#4)
6   |  |     } ; ($ input : expr => $ pattern : expr, $ ($ arg : expr), *) =>
7   |  |     { { try_scan ! (@ impl question_mark ; $ input => $ pattern, $ ($ arg), *) } }
    |  |         ---------------------------------------------------------------------- in this macro invocation (#5)
8   |  |     ; (@ question_mark : $ ($ e : tt) +) => { { ($ ($ e) +) ? } } ;
    |  |                                                 ^^^^^^^^^^^^^
...    |
31  |  |                          return try_scan !
    |  |_________________________________-
32  | ||                          (@ $ action : Err (Error :: MissingClosingBrace)),
    | ||__________________________________________________________________________- in this macro invocation (#6)
...    |
38  |  |         }
39  |  |     } ;
    |  |       -
    |  |       |
    |  |       in this expansion of `try_scan!` (#3)
    |  |       in this expansion of `try_scan!` (#4)
    |  |_______in this expansion of `try_scan!` (#5)
    |          in this expansion of `try_scan!` (#6)
    | 
   ::: src/commands/kv/mod.rs:106:32
    |
106 |            let mut response: String = read!("{}\n");
    |                                       ------------- in this macro invocation (#1)
    | 
   ::: <::text_io::try_read macros>:1:1
    |
1   |      / () => { try_read ! ("{}") } ; ($ text : expr) =>
2   |      | {
3   |      |     {
4   |      |         (|| -> Result < _, $ crate :: Error >
5   |      |          {
6   |      |              let __try_read_var__ ; try_scan ! ($ text, __try_read_var__) ; Ok
    |      |                                     --------------------------------------- in this macro invocation (#3)
...        |
18  |      |     }
19  |      | } ;
    |      |___- in this expansion of `try_read!` (#2)
    | 
   ::: <::text_io::read macros>:1:1
    |
1   |        ($ ($ arg : tt) *) => { try_read ! ($ ($ arg) *) . unwrap () } ;
    |        ----------------------------------------------------------------
    |        |                       |
    |        |                       in this macro invocation (#2)
    |        in this expansion of `read!` (#1)
    |
    = note: `#[warn(clippy::try_err)]` on by default
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#try_err

If you would like to reproduce this easily, the repo is here: https://github.com/cloudflare/wrangler

Thanks so much for reading this and please let me know if I'm being silly or y'all need more information :)

@EverlastingBugstopper EverlastingBugstopper changed the title Unfixable warning False positive on standard read!() usage Oct 21, 2019
@phansch phansch added L-suggestion Lint: Improving, adding or fixing lint suggestions C-bug Category: Clippy is not doing the correct thing labels Oct 22, 2019
@phansch phansch self-assigned this Oct 22, 2019
@phansch
Copy link
Member

phansch commented Oct 22, 2019

Thanks for the report! This is a bug and we should not emit the lint in this case.

@phansch phansch added the T-macros Type: Issues with macros and macro expansion label Oct 22, 2019
@bors bors closed this as completed in bfef48f Oct 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing L-suggestion Lint: Improving, adding or fixing lint suggestions T-macros Type: Issues with macros and macro expansion
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants