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

Incorrect clippy::op_ref warning on &s[0] #4645

Closed
jsgf opened this issue Oct 9, 2019 · 2 comments
Closed

Incorrect clippy::op_ref warning on &s[0] #4645

jsgf opened this issue Oct 9, 2019 · 2 comments

Comments

@jsgf
Copy link
Contributor

jsgf commented Oct 9, 2019

Given the code:

pub fn inrange<T: Ord>(s: &[T], n: &T) -> bool {
    s.last().map_or(false, |last| &s[0] <= n && n <= last)
}

Clippy warns:

 --> src/lib.rs:2:35
  |
2 |     s.last().map_or(false, |last| &s[0] <= n && n <= last)
  |                                   -----^^^^^
  |                                   |
  |                                   help: use the left value directly: `s[0]`
  |
  = note: `#[warn(clippy::op_ref)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#op_ref

However, this is incorrect, as n has reference type &T. Changing it to s[0] <= n fails to compile:

error[E0308]: mismatched types
 --> src/lib.rs:2:43
  |
2 |     s.last().map_or(false, |last| s[0] <= n && n <= last)
  |                                           ^ expected type parameter, found &T
  |
  = note: expected type `T`
             found type `&T`
  = help: type parameters must be constrained to match other types
  = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

@hellow554
Copy link
Contributor

hellow554 commented Oct 9, 2019

The span is also too big?

@flip1995
Copy link
Member

flip1995 commented Oct 9, 2019

Duplicate of #2597

Than span is also too big?

The span is fine. Usually the span of the suggestion == span of error message (especially in Clippy), this is then displayed with just ^^^. In this case the span of the suggestion is shorter than the span of the error message, which is displayed with --- (for the sugg span) and ^^^ (continuation for the error span).

@flip1995 flip1995 closed this as completed Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants