Commit graph

4128 commits

Author SHA1 Message Date
Philipp Krones
378ba2e03e
Rollup merge of #5846 - dima74:map_flatten.map_to_option, r=flip1995
Handle mapping to Option in `map_flatten` lint

Fixes #4496

The existing [`map_flatten`](https://rust-lang.github.io/rust-clippy/master/index.html#map_flatten) lint suggests changing `expr.map(...).flatten()` to `expr.flat_map(...)` when `expr` is `Iterator`. This PR changes suggestion to `filter_map` instead of `flat_map` when mapping to `Option`, because it is more natural

Also here are some questions:
* If expression has type which implements `Iterator` trait (`match_trait_method(cx, expr, &paths::ITERATOR) == true`), how can I get type of iterator elements? Currently I use return type of closure inside `map`, but probably it is not good way
* I would like to change suggestion range to cover only `.map(...).flatten()`, that is from:
```
    let _: Vec<_> = vec![5_i8; 6].into_iter().map(|x| 0..x).flatten().collect();
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using `flat_map` instead: `vec![5_i8; 6].into_iter().flat_map
```
to
```
    let _: Vec<_> = vec![5_i8; 6].into_iter().map(|x| 0..x).flatten().collect();
                                             ^^^^^^^^^^^^^^^^^^^^^^^^ help: try using `flat_map` instead: `.flat_map(|x| 0..x)`
```
Is it ok?
* Is `map_flatten` lint intentionally in `pedantic` category, or could it be moved to `complexity`?

changelog: Handle mapping to Option in [`map_flatten`](https://rust-lang.github.io/rust-clippy/master/index.html#map_flatten) lint
2020-08-04 12:06:39 +02:00
Philipp Krones
ca2a25d966
Rollup merge of #5837 - JarredAllen:needless_collect, r=phansch
needless_collect: catch x: Vec<_> = iter.collect(); x.into_iter() ...

changelog: Expand the needless_collect lint as suggested in #5627 (WIP).

This PR is WIP because I can't figure out how to make the multi-part suggestion include its changes in the source code (the fixed is identical to the source, despite the lint making suggestions). Aside from that one issue, I think this should be good.
2020-08-04 12:06:38 +02:00
bors
1968aede0f Auto merge of #5867 - flip1995:rustup, r=flip1995
Rustup

r? @ghost

changelog: none
2020-08-04 09:25:05 +00:00
Vadim Petrochenkov
52a9c157d0 rustc_ast: (Nested)MetaItem::check_name -> has_name
For consistency with `Attribute::has_name` which doesn't mark the attribute as used either.

Replace all uses of `check_name` with `has_name` outside of rustc
2020-08-04 00:34:11 +03:00
Cameron Steffen
0ccdf2913a
Remove obsolete known problems unnecessary_fold 2020-08-03 16:23:20 -05:00
Tomasz Miąsko
e9677105bf try_err: Consider Try impl for Poll when generating suggestions
There are two different implementation of Try trait for Poll type;
Poll<Result<T, E>> and Poll<Option<Result<T, E>>>. Take them into
account when generating suggestions.

For example, for Err(e)? suggest either return Poll::Ready(Err(e)) or
return Poll::Ready(Some(Err(e))) as appropriate.
2020-08-03 20:48:18 +02:00
JarredAllen
25abd7ae76 Create stable_sort_primitive lint 2020-08-03 11:17:43 -07:00
Christoph Walcher
e521c67e5f
early return on empty parameters/where clause 2020-08-03 12:32:23 +02:00
JarredAllen
5e10b039a3 Implement review suggestions 2020-08-02 21:46:18 -07:00
JarredAllen
bb2c14e92b Fix a bug causing it to be too trigger-happy 2020-08-02 21:34:17 -07:00
JarredAllen
a849483294 Fix formatting and dogfood fallout 2020-08-02 21:34:17 -07:00
JarredAllen
3657c92ac9 Check for other things which can be used indirectly 2020-08-02 21:34:17 -07:00
JarredAllen
3ee61373fe Write the lint and write tests 2020-08-02 21:34:17 -07:00
Eduardo Broto
e336fe80d2 manual_async_fn: take input lifetimes into account
The anonymous future returned from an `async fn` captures all input
lifetimes. This was not being taken into account.

See https://github.com/rust-lang/rfcs/blob/master/text/2394-async_await.md#lifetime-capture-in-the-anonymous-future
2020-08-03 00:36:28 +02:00
Philipp Hansch
bb6e857980
fmt 2020-08-02 14:22:54 +02:00
Philipp Hansch
cb00cdf0d7
Remove old Symbol reexport
I couldn't really tell what it was meant to improve. It seems more clear
without the renaming to `Name`?
2020-08-02 11:25:03 +02:00
Valentin Lazureanu
aa3d9ca0e9 Rename HAIR to THIR (Typed HIR). 2020-07-31 22:15:12 +00:00
Dmitry Murzin
d4ba561aaf
Review fixes 2020-07-31 00:28:21 +03:00
Dmitry Murzin
a427c99f3d
Handle mapping to Option in map_flatten lint 2020-07-30 23:23:33 +03:00
Christoph Walcher
2b7fde6a4b
typo fix 2020-07-29 16:10:15 +02:00
bors
2e0f8b6cc6 Auto merge of #5843 - dima74:iter_skip_next.add-suggestion, r=phansch
Add suggestion for `iter_skip_next` lint

changelog: Add suggestion for [`iter_skip_next`](https://rust-lang.github.io/rust-clippy/master/index.html#iter_skip_next) lint
2020-07-29 06:10:55 +00:00
flip1995
04f4471761
Merge remote-tracking branch 'upstream/master' into rustup 2020-07-29 03:17:23 +02:00
Christoph Walcher
94c50bc8c9
Lint duplicate methods of trait bounds
Fixes #5777
2020-07-28 16:42:26 +02:00
Bastian Kauschke
133e1d6773 clippy 2020-07-27 21:17:28 +02:00
Bastian Kauschke
6ce37fab95 introduce PredicateAtom 2020-07-27 21:07:37 +02:00
Bastian Kauschke
88787083f4 this might be unqualified, but at least it's now quantified 2020-07-27 21:06:36 +02:00
Bastian Kauschke
dfa1af2059 clippy 2020-07-27 21:06:36 +02:00
Takayuki Nakata
3a9ccffed8 chmod 644 clippy_lints/src/utils/ast_utils.rs 2020-07-27 22:27:54 +09:00
Ryan1729
94b10a6e5a run cargo dev update_lints 2020-07-27 00:31:09 -06:00
Ryan1729
12a6eee045 fill in lint description for DERIVE_ORD_XOR_PARTIAL_ORD 2020-07-27 00:22:39 -06:00
Ryan1729
ca03f2b650 s/pord/partial_ord/ to fix dogfood failure 2020-07-27 00:21:11 -06:00
Ryan1729
668b7474b4 run cargo dev fmt and fix overly long line 2020-07-26 23:30:00 -06:00
Ryan1729
431924ccf6 add description for derive_ord_xor_partial_ord 2020-07-26 23:15:36 -06:00
Ryan1729
7dc974815e remove is_local check since getting the def_id directly makes it unnecessary 2020-07-26 23:06:36 -06:00
Ryan1729
a8d6eda930 use get_trait_def_id to check for Ord trait 2020-07-26 23:04:04 -06:00
Ryan1729
068acbd27b initial implementation based on code for derive_hash_xor_partial_eq which is showing one error when there should be four 2020-07-26 22:04:46 -06:00
Ryan1729
fc20ee63a1 move derive_ord_xor_partial_ord into derive mod so we can reuse derive_hash_xor_partial_eq code later 2020-07-26 20:54:04 -06:00
Ryan1729
5a644964fc run cargo dev new_lint
specifically:
cargo dev new_lint --name derive_ord_xor_partial_ord --category correctness --pass late
2020-07-26 20:40:57 -06:00
bors
f5d429cd76 Auto merge of #5820 - ThibsG:FixSuspiciousArithmeticImpl, r=flip1995
Fix FP for `suspicious_arithmetic_impl` from `suspicious_trait_impl` …

As discussed in #3215, the `suspicious_trait_impl` lint causes too many false positives, as it is complex to find out if binary operations are suspicious or not.

This PR restricts the number of binary operations to at most one, otherwise we don't lint.
This can be seen as very conservative, but at least FP can be reduced to bare minimum.

Fixes: #3215

changelog: limit the `suspicious_arithmetic_impl` lint to one binop, to avoid many FPs
2020-07-26 19:48:17 +00:00
flip1995
d164ab65f7 Merge commit 'da5a6fb1b65ec6581a67e942a3850f6bc15a552c' into clippyup 2020-07-26 21:07:07 +02:00
Takayuki Nakata
c81bbd05b9 Fix FP useless_conversion
Fix #5833.
2020-07-25 23:58:22 +09:00
Dmitry Murzin
b375f1dd20
Add suggestion for iter_skip_next lint 2020-07-25 17:11:55 +03:00
bors
79f948ec0a Auto merge of #5829 - JohnTitor:epsilon, r=flip1995
Use `(std::)f64::EPSILON` in the examples as suggested in the lints

`float_cmp(_const)` suggests using `{f32|f64}::EPSILON` and it'd be great if the docs mentioned it.

changelog: none
2020-07-23 18:48:35 +00:00
bors
8c83d5f484 Auto merge of #5830 - flip1995:rustup, r=flip1995
trait_sel: only test predicates w/ no substs

r? @ghost
changelog: none
2020-07-21 22:10:04 +00:00
Yuki Okushi
142a273441
Use (std::)f64::EPSILON in the examples as suggested in the lints 2020-07-22 05:23:55 +09:00
David Wood
b7c8b96e66 trait_sel: only test predicates w/ no substs
This commit modifies the `substitute_normalize_and_test_predicates`
query, renaming it to `impossible_predicates` and only checking
predicates which do not require substs. By making this change,
polymorphization doesn't have to explicitly support vtables.

Signed-off-by: David Wood <david@davidtw.co>
2020-07-20 11:23:26 +01:00
bors
61e3d8a55c Auto merge of #5824 - tmiasko:manually-drop-clone, r=Manishearth
Ignore not really redundant clones of ManuallyDrop

"Redundant" clones of `ManuallyDrop` are sometimes used for the side effect of
invoking the clone, without running the drop implementation of the inner type.
In other words, they aren't really redundant. For example, futures-rs crate:

```rust
#[allow(clippy::redundant_clone)] // The clone here isn't actually redundant.
unsafe fn increase_refcount<T: ArcWake>(data: *const ()) {
    // Retain Arc, but don't touch refcount by wrapping in ManuallyDrop
    let arc = mem::ManuallyDrop::new(Arc::<T>::from_raw(data as *const T));
    // Now increase refcount, but don't drop new refcount either
    let _arc_clone: mem::ManuallyDrop<_> = arc.clone();
}
```

changelog: Ignore redundant clone lint for ManuallyDrop.
2020-07-20 01:36:21 +00:00
Tomasz Miąsko
a5cdd4aeb1 Ignore not really redundant clones of ManuallyDrop
"Redundant" clones of `ManuallyDrop` are sometimes used for the side effect of
invoking the clone, without running the drop implementation of the inner type.
In other words, they aren't really redundant. For example, futures-rs crate:

```rust
#[allow(clippy::redundant_clone)] // The clone here isn't actually redundant.
unsafe fn increase_refcount<T: ArcWake>(data: *const ()) {
    // Retain Arc, but don't touch refcount by wrapping in ManuallyDrop
    let arc = mem::ManuallyDrop::new(Arc::<T>::from_raw(data as *const T));
    // Now increase refcount, but don't drop new refcount either
    let _arc_clone: mem::ManuallyDrop<_> = arc.clone();
}
```

Ignore redundant clone lint for ManuallyDrop.
2020-07-20 00:56:27 +02:00
Tim Nielens
1ac8b85c9f redundant_closure_call - pr review 2020-07-20 00:36:31 +02:00
Tim Nielens
9603d9652b redundant_closure_call - add support for shadowed closures 2020-07-20 00:30:43 +02:00
Tim Nielens
0fecaf1abc redundant_closure_call - extract lint from misc_early.rs, adapt to LatePass 2020-07-20 00:30:43 +02:00
Tim Nielens
c720d823e1 redundant_closure_call - don't lint when used more than once 2020-07-20 00:30:43 +02:00
ThibsG
442c8ae23b Fix FP for suspicious_arithmetic_impl from suspicious_trait_impl lint 2020-07-19 00:00:17 +02:00
bors
8cf4219c16 Auto merge of #5815 - JarredAllen:redundant_pattern_bugfix, r=flip1995
Redundant pattern bugfix

changelog: Fixes the bug reported #5766
2020-07-18 14:43:36 +00:00
flip1995
0f501ac1db
Merge remote-tracking branch 'upstream/master' into rustup 2020-07-18 15:09:58 +02:00
bors
9a945c7413 Auto merge of #5817 - flip1995:rustup, r=flip1995
Rustup

r? @ghost

Preparation for emergency Clippyup to fix an ICE #5816

changelog: none
2020-07-17 19:41:39 +00:00
flip1995
3d3a13d871
Fix sync fallout (fmt) 2020-07-17 21:39:05 +02:00
JarredAllen
e85b590936 Fix bug in lint 2020-07-17 10:40:01 -07:00
Takayuki Nakata
e5105e82d3 Fix typo 2020-07-17 22:51:57 +09:00
Valentin Lazureanu
5a20489c5c Rename TypeckTables to TypeckResults. 2020-07-17 08:47:04 +00:00
bors
57678c8315 Auto merge of #5811 - JarredAllen:panic_multiple_args, r=phansch
Panic multiple args

changelog: Fixes bug with `panic` lint reported in #5767. I also did the same changes to the lints for `todo`, `unimplemented` and `unreachable`, so those lints should now also detect calls to those macros with a message.
2020-07-17 06:41:20 +00:00
Matthias Krüger
3618b97f59 fix typos (found by codespell) 2020-07-17 01:58:41 +02:00
JarredAllen
07867fde59 Clean up dogfood fallout 2020-07-16 16:58:21 -07:00
JarredAllen
70a41a9281 Enable detecting multiple-argument panics 2020-07-16 16:51:12 -07:00
Nicholas Nethercote
62db617e40 Avoid storing SymbolStr in a struct.
It's intended only for very temporary use.
2020-07-17 08:28:22 +10:00
bors
9349441863 Auto merge of #5788 - Leulz:shadow-unrelated-fix-rhs, r=flip1995
Removing RHS snippet from SHADOW_UNRELATED message.

Fixes #5703

I am not sure if I reinvented the wheel here, but I could not really find a snippet function that did this truncation, so I created the function. Please tell me if there was a more obvious way to do this, I am new here. 😄

changelog: Truncates multi-line RHS in shadow_unrelated message if it has more than 5 lines.
2020-07-15 19:09:21 +00:00
flip1995
d9bf74e2f2
Merge remote-tracking branch 'upstream/master' into rustup 2020-07-15 20:15:57 +02:00
Leo Meira Vital
a0640457a9 Removing snippet from SHADOW_UNRELATED message. 2020-07-15 11:12:38 -03:00
Bastian Kauschke
bf3104ec24 WithOptConstParam::dummy -> WithOptConstParam::unknown 2020-07-15 13:06:47 +02:00
Bastian Kauschke
c5b9463d26 improve naming 2020-07-15 13:06:47 +02:00
Bastian Kauschke
29de25b8a2 const_eval_resolve 2020-07-15 12:58:32 +02:00
Nicholas Nethercote
f2f99be2ff Remove lots of Symbol::as_str() calls.
In various ways, such as changing functions to take a `Symbol` instead
of a `&str`.
2020-07-15 09:01:35 +10:00
bors
128c5dec92 Auto merge of #5793 - warner:5783-nth-zero-next, r=flip1995
improve advice in iter_nth_zero

fixes #5783

*Please keep the line below*
changelog:  For iter_nth_zero, the "use .next()" replacement advice is on the last line of the code snippet, where it is vulnerable to truncation. Display that advice at the beginning instead.
2020-07-14 16:58:23 +00:00
Brian Warner
e83b3eb993 formatting nits 2020-07-14 09:20:19 -07:00
Matthias Krüger
126790999a new lint: Returning unit from closures expecting Ord
This lint catches cases where the last statement of a closure expecting
an instance of Ord has a trailing semi-colon. It compiles since the
closure ends up return () which also implements Ord but causes
unexpected results in cases such as sort_by_key.

Fixes #5080

reprise: rebase, update and address all concerns
2020-07-14 15:54:04 +02:00
flip1995
6f25adbd5a Merge commit '2ca58e7dda4a9eb142599638c59dc04d15961175' into clippyup 2020-07-14 14:59:59 +02:00
bors
2ca58e7dda Auto merge of #5732 - bjorn3:patch-1, r=flip1995
Rename collapsable_if fix suggestion to "collapse nested if block"

The name "try" is confusing when shown as quick fix by rust-analyzer

changelog: Rename `collapsable_if` fix suggestion to "collapse nested if block"
2020-07-14 09:39:01 +00:00
bors
12df6384b9 Auto merge of #5773 - giraffate:repeat_once, r=flip1995
Add a lint for `.repeat(1)`

changelog: New lint `repeat_once`

fix #3028.
2020-07-14 09:13:58 +00:00
Takayuki Nakata
b4091032ab Use .is_some() not Some(_) 2020-07-14 08:08:13 +09:00
Brian Warner
201999ccfd improve advice in iter_nth_zero
The "use .next()" replacement advice is on the last line of the code snippet,
where it is vulnerable to truncation. Display that advice at the beginning
instead.

closes #5783
2020-07-13 10:59:45 -07:00
bjorn3
ff796b6d70 Rename collapsable_if fix suggestion to "collapse nested if block"
The name "try" is confusing when shown as quick fix by rust-analyzer
2020-07-13 17:54:57 +02:00
Takayuki Nakata
f2419b9d62 Refactoring to use `constant_context
Use `constant_context`, `.is_str()` and `builtin_index()` to simplify.
2020-07-14 00:23:20 +09:00
bors
4b8700879c Auto merge of #5792 - flip1995:rollup-torc1we, r=flip1995
Rollup of 5 pull requests

Successful merges:

 - #5443 (Some accuracy lints for floating point operations)
 - #5752 (Move range_minus_one to pedantic)
 - #5756 (unnecessary_sort_by: avoid linting if key borrows)
 - #5784 (Fix out of bounds access by checking length equality BEFORE accessing by index.)
 - #5786 (fix phrase in new_lint issue template)

Failed merges:

r? @ghost

changelog: rollup
2020-07-13 14:21:20 +00:00
Philipp Krones
26ede3115f
Rollup merge of #5784 - matthiaskrgr:ice_5780, r=phansch
Fix out of bounds access by checking length equality BEFORE accessing by index.

Fixes #5780

changelog: fix out of bounds access in unnested_or_patterns lint.

Edit: I did not bother reducing a testcase from `librustc_typeck` crate but I can confirm that with the change the crash no longer occurs.
2020-07-13 15:59:45 +02:00
Philipp Krones
32ef448bdb
Rollup merge of #5756 - ebroto:5754_sort_by, r=flip1995
unnecessary_sort_by: avoid linting if key borrows

changelog: Avoid linting if key borrows in [`unnecessary_sort_by`]

Fixes #5754
Closes #2313
2020-07-13 15:59:44 +02:00
Philipp Krones
019e281d15
Rollup merge of #5752 - chrisduerr:pedantic_ranges, r=flip1995
Move range_minus_one to pedantic

This moves the range_minus_one lint to the pedantic category, so there
will not be any warnings emitted by default. This should work around
problems where the suggestion is impossible to resolve due to the range
consumer only accepting a specific range implementation, rather than the
`RangeBounds` trait (see #3307).

While it is possible to work around this by extracting the boundary into
a variable, I don't think clippy should encourage people to disable or
work around lints, but instead the lints should be fixable. So hopefully
this will help until a proper implementation checks what the range is
used for.

*Please keep the line below*
changelog: move [`range_minus_one`] to pedantic
2020-07-13 15:59:42 +02:00
Philipp Krones
75d43aac73
Rollup merge of #5443 - thiagoarrais:issue-2040, r=flip1995
Some accuracy lints for floating point operations

This will add some lints for accuracy on floating point operations suggested by @clarfon in #2040 (fixes #2040).

These are the remaining lints:

- [x] x.powi(2) => x * x
- [x] x.logN() / y.logN() => x.logbase(y)
- [x] x.logbase(E) => x.log()
- [x] x.logbase(10) => x.log10()
- [x] x.logbase(2) => x.log2().
- [x] x * PI / 180 => x.to_radians()
- [x] x * 180 / PI => x.to_degrees()
- [x] (x + 1).log() => x.log_1p()
- [x] sqrt(x * x + y * y) => x.hypot(y)

changelog: Included some accuracy lints for floating point operations
2020-07-13 15:59:41 +02:00
bors
fa4a737fba Auto merge of #5790 - flip1995:rustup, r=flip1995
Rustup

r? @ghost

changelog: none
2020-07-13 13:53:11 +00:00
Matthias Krüger
1b3bc16533 Fix out of bounds access by checking length equality BEFORE accessing by index.
Fixes #5780
2020-07-11 12:30:18 +02:00
bors
7d611d9224 Auto merge of #5769 - robojumper:match_like_matches_macro, r=phansch
new lint: match_like_matches_macro

Suggests using the `matches!` macro from `std` where appropriate.

`redundant_pattern_matching` has been moved into the `matches` pass to allow suppressing the suggestion where `is_some` and friends are a better replacement.

changelog: new lint: `match_like_matches_macro`
2020-07-10 18:10:08 +00:00
Takayuki Nakata
780a4c87de Fix typo 2020-07-10 23:53:15 +09:00
Christian Duerr
ba2a85dadc
Run update_lints 2020-07-10 15:20:00 +02:00
Christian Duerr
298a1fa3bd
Move range_minus_one to pedantic
This moves the range_minus_one lint to the pedantic category, so there
will not be any warnings emitted by default. This should work around
problems where the suggestion is impossible to resolve due to the range
consumer only accepting a specific range implementation, rather than the
`RangeBounds` trait (see #3307).

While it is possible to work around this by extracting the boundary into
a variable, I don't think clippy should encourage people to disable or
work around lints, but instead the lints should be fixable. So hopefully
this will help until a proper implementation checks what the range is
used for.
2020-07-10 15:20:00 +02:00
Tamir Duberstein
cf91c54cc6 Avoid "whitelist"
Other terms are more inclusive and precise.
2020-07-10 07:39:28 -04:00
bors
45eea9a822 Auto merge of #5771 - montrivo:bugfix/single-match-else, r=matthiaskrgr
single_match_else - single expr/stmt else block corner case

One approach to fix #3489.
See discussion in the issue.

changelog: single_match_else - single expr/stmt else block corner case fix
2020-07-09 12:03:14 +00:00
Tim Nielens
dac19e3afc single_match_else - single expr/stmt else block corner case 2020-07-09 12:04:27 +02:00
Eduardo Broto
db1c946aaa unnecessary_sort_by: avoid linting if key borrows 2020-07-08 21:31:59 +02:00
robojumper
37d75da266 make match_like_matches_macro only apply to matches with a wildcard 2020-07-08 18:37:20 +02:00
bors
e12a316b01 Auto merge of #5776 - flip1995:rustup, r=flip1995
Rustup

changelog: none

r? @ghost
2020-07-08 15:56:23 +00:00
Takayuki Nakata
5307cb5614 Add a lint for .repeat(1)
fix #3028.
2020-07-07 23:13:39 +09:00