Clippy contradicting rustc? (Solved)

#1

Hi there,

I’m trying to resolve clippy’s errors and warning and on the last clippy warning, if I solve it, the file doesn’t compile anymore, who’s wrong?

The file: https://github.com/cobalt-org/cobalt.rs/blob/72589b1e7327b7bf4fb75b6427b8c7627469c2e0/src/pagination/tags.rs#L58

Clippy’s warning:

warning: redundant closure found
  --> src\pagination\tags.rs:58:18
   |
58 |         .or_else(|e: Error| Err(e))?;
   |                  ^^^^^^^^^^^^^^^^^ help: remove closure as shown: `Err`
   |
   = note: #[warn(clippy::redundant_closure)] on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure

If I solve like suggested (or_else(Err)?;), rustc is not happy:

error[E0282]: type annotations needed
  --> src\pagination\tags.rs:48:38
   |
48 |               let cur_tag_paginators = create_all_paginators(
   |  ______________________________________^
49 | |                 posts,
50 | |                 doc,
51 | |                 &pagination_cfg,
52 | |                 Some(&liquid::value::Value::scalar(tag.to_owned())),
53 | |             )?;
   | |______________^ cannot infer type

What can I do?

EDIT: for the time being, I’m putting #[allow(clippy::redundant_closure)] on this function :slight_smile:

0 Likes

#2

I’m guessing the type annotation in e: Error is necessary for inference to succeed, because usually you don’t have to annotate closures. You could probably get this to succeed like:

    .or_else(Result::<_, Error>::Err)
1 Like

#3

Thanks for your answer!
I have an alias on Result so I need only the Ok type but I get

error: enum variants on type aliases are experimental
  --> src\pagination\tags.rs:60:18
   |
60 |         .or_else(Result::<_>::Err)?;
   |                  ^^^^^^^^^^^^^^^^

So maybe it’s not solvable in stable yet :smiley:

0 Likes

#4

I guess you can bypass the alias: std::result::Result::<_, Error>::Err

… or just keep your closure. :smile:

2 Likes

#5

Why I didn’t though of this myself? ^^’ Thanks a lot!

0 Likes

#6

.or_else(Err::<_, Error>) should also work

5 Likes