Commit graph

8304 commits

Author SHA1 Message Date
bors
8e87d39f99 Auto merge of #9572 - Nilstrieb:as-ptr-cast-mut, r=dswij
Add `as_ptr_cast_mut` lint

This lint detects calls to a `&self`-taking `as_ptr` method, where the result is then immediately cast to a `*mut T`. Code like this is probably invalid, as that pointer will not have write permissions, and `*mut T` is usually used to write through.

Examples of broken code with this pattern:
https://miri.saethlin.dev/ub?crate=lol_alloc&version=0.1.3
https://miri.saethlin.dev/ub?crate=sophon-wasm&version=0.19.0
https://miri.saethlin.dev/ub?crate=polars-core&version=0.24.2
https://miri.saethlin.dev/ub?crate=ach-cell&version=0.1.17

changelog: Add [`as_ptr_cast_mut`]
2022-10-11 08:08:17 +00:00
bors
272bbfb857 Auto merge of #9386 - smoelius:further-enhance-needless-borrow, r=Jarcho
Further enhance `needless_borrow`, mildly refactor `redundant_clone`

This PR does the following:
* Moves some code from `redundant_clone` into a new `clippy_utils` module called `mir`, and wraps that code in a function called `dropped_without_further_use`.
* Relaxes the "is copyable" condition condition from #9136 by also suggesting to remove borrows from values dropped without further use. The changes involve the just mentioned function.
* Separates `redundant_clone` into modules.

Strictly speaking, the last bullet is independent of the others. `redundant_clone` is somewhat hairy, IMO. Separating it into modules makes it slightly less so, by helping to delineate what depends upon what.

I've tried to break everything up into digestible commits.

r? `@Jarcho`

(`@Jarcho` I hope you don't mind.)

changelog: continuation of #9136
2022-10-08 21:24:54 +00:00
bors
292e313259 Auto merge of #9451 - kraktus:manual_filter2, r=dswij
Add `manual_filter` lint for `Option`

Share much of its implementation with `manual_map` and should greatly benefit from its previous feedback.
I'm sure it's possible to even more refactor both and would gladly take input on that as well as any clippy idiomatic usage, since this is my first lint addition.

I've added the lint to the complexity section for now, I don't know if every new lint needs to go in nursery first.

The matching could be expanded to more than `Some(<value>)` to lint on arbitrary struct matching inside the `Some` but I've left it like it was for `manual_map` for now. `needless_match::pat_same_as_expr` provides a more generic match example.

close https://github.com/rust-lang/rust-clippy/issues/8822

changelog: Add lint [`manual_filter`] for `Option`
2022-10-08 15:58:51 +00:00
bors
2c8e473ffe Auto merge of #9585 - rust-lang:extend-box-default, r=Alexendoo
extend `box-default` lint, add suggestion

This extends the recently added `box-default` lint to also cover `Box::new(vec![])`, `Box::new(String::from(""))` and `Box::new(Vec::from([]))`. Also the lint now suggests a suitable replacement. I did not find a simple way to check whether the type is fully determined by the outside, so I at least checked for some variations to remove the turbofish in those cases.

---

changelog: none
2022-10-07 14:49:54 +00:00
Samuel Moelius
9cc8da222b Fix adjacent code 2022-10-07 05:07:44 -04:00
Samuel Moelius
037f698147 needless_borrow uses used_exactly_once 2022-10-07 05:07:44 -04:00
bors
65ae6666c8 Auto merge of #9595 - Alexendoo:author-let-chains, r=Jarcho
Replace if_chain with let chains in `clippy::author` output

Should help nudge new contributors towards let chains

changelog: none
2022-10-07 02:52:47 +00:00
Evan Typanski
39164acf6e Fix flipped variable that made it through 2022-10-06 15:36:28 -04:00
Evan Typanski
23b16998c3 Add curlies to scrutinees with side effects 2022-10-06 15:14:55 -04:00
bors
8f1ebdd18b Auto merge of #9593 - Andy-Python-Programmer:master, r=llogiq
lint::unsafe_removed_from_name: fix false positive result when allowed

changelog: [`unsafe_removed_from_name`] Fix allowing on imports produces a false positive on `useless_attribute`.

Fixes: #9197

Signed-off-by: Andy-Python-Programmer <andypythonappdeveloper@gmail.com>
2022-10-06 14:39:26 +00:00
Andre Bogus
d3c041a086 extend box-default lint, add suggestion 2022-10-06 16:00:45 +02:00
Yuri Astrakhan
cfd6c8d19d Add a temporary workaround for multiline formart arg inlining
per suggestion in
https://github.com/rust-lang/rust/pull/102729#discussion_r988990080

workaround for an internal crash when handling multi-line format
argument inlining.
2022-10-06 09:24:57 -04:00
Yuri Astrakhan
c251f8d8dd lint: fix a few comments 2022-10-06 08:54:07 -04:00
Philipp Krones
e2808afd60
Merge remote-tracking branch 'upstream/master' into rustup 2022-10-06 09:19:29 +02:00
Alex Macleod
9e70a0ff12 Replace if_chain with let chains in clippy::author output 2022-10-05 17:21:32 +00:00
bors
887ba0c5a4 Auto merge of #9586 - Alexendoo:format-args-commas, r=Manishearth
FormatArgsExpn: Find comma spans and ignore weird proc macro spans

Fixes the following cases:

A missing `, 1` from the `expect_fun_call` suggestion:

```rust
Some(()).expect(&format!("{x} {}", 1));
```
```
warning: use of `expect` followed by a function call
 --> t.rs:7:14
  |
7 |     Some(()).expect(&format!("{x} {}", 1));
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("{x} {}"))`
```

The suggestion removing from the comma in the comment rather than the one after the format string:

```rust
println!(
    "{}",
    // a comment, with a comma in it
    x
);
```
```
warning: variables can be used directly in the `format!` string
  --> t.rs:9:5
   |
9  | /     println!(
10 | |         "{}",
11 | |         // a comment, with a comma in it
12 | |         x
13 | |     );
   | |_____^
   |
help: change this to
   |
10 ~         "{x}",
11 ~         // a comment
   |
```

It also no longer accepts expansions where a format string or argument has a "weird" proc macro span, that is one where the literal/expression it outputs has the span of one of its inputs. Kind of like a `format_args` specific `clippy_utils::is_from_proc_macro`, e.g. `format!(indoc! {" ... "})`

changelog: [`expect_fun_call`]: Fix suggestion for `format!` using captured variables
changelog: [`print_literal`], [`write_literal`], [`uninlined_format_args`]: Fix suggestion when following a comment including a comma
2022-10-05 16:27:03 +00:00
Alex Macleod
9226066bcb FormatArgsExpn: Find comma spans and ignore weird proc macro spans 2022-10-05 16:10:52 +00:00
Alex Macleod
86c86c3742 Add disallowed_macros lint 2022-10-05 13:44:06 +00:00
Andy-Python-Programmer
14f9f2b69d
lint::unsafe_removed_from_name: fix false positive result when allowed
* Allowing `unsafe_removed_from_name` on imports produces a false
positive on `useless_attribute`.

Fixes: #9197

Signed-off-by: Andy-Python-Programmer <andypythonappdeveloper@gmail.com>
2022-10-05 18:59:01 +11:00
Oli Scherer
da16cc1da9 It's not about types or consts, but the lack of regions 2022-10-04 14:10:44 +00:00
Caio
a44914fcd3 Dogfood 2022-10-03 20:10:00 -03:00
Caio
2a6a98f95b [arithmetic-side-effects] Do not ignore literal references 2022-10-03 19:49:03 -03:00
bors
e8c1b5478c Auto merge of #9559 - c410-f3r:arith, r=Alexendoo
Fix #9544

Fix #9544

r? `@Alexendoo`

changelog: [`arithmetic_side_effects`]: Fix false negative for `CustomType / usize`, `CustomType * float` and similar
2022-10-03 21:54:08 +00:00
Caio
99363fef65 Address comments 2022-10-03 18:36:12 -03:00
bors
5825ae7bd4 Auto merge of #9581 - ebobrow:mul-add-negative, r=Manishearth
`suboptimal_flops` lint for multiply and subtract

fixes #9526

changelog: [`suboptimal_flops`] lint for multiply and subtract
2022-10-03 17:12:10 +00:00
royrustdev
f1c831ad17 add implicit_saturating_add lint 2022-10-03 16:46:38 +05:30
Elliot Bobrow
15431b36bb
Merge branch 'master' into mul-add-negative 2022-10-02 21:27:57 -07:00
Elliot Bobrow
7747032b77 suboptimal_flops lint for multiply and subtract 2022-10-02 21:00:51 -07:00
TennyZhuang
bf18768219 let upper_case_acronyms check the enum name
Signed-off-by: TennyZhuang <zty0826@gmail.com>
2022-10-03 10:11:57 +08:00
Jason Newcomb
38236a7135 Use for_each_expr in place of some visitors 2022-10-02 17:15:19 -04:00
Jason Newcomb
649d443646 Replace expr_visitor with for_each_expr 2022-10-02 17:04:22 -04:00
bors
bef93d3b14 Auto merge of #7962 - Jarcho:fix_match_type_on_diagnostic_items, r=llogiq
Fix and improve `match_type_on_diagnostic_item`

This extracts the fix for the lint out of #7647. There's still a couple of other functions to check, but at least this will get lint working again.

The two added util functions (`is_diagnostic_item` and `is_lang_item`) are needed to handle `DefId` for unit and tuple struct/variant constructors. The `rustc_diagnostic_item` and `lang` attributes are attached to the struct/variant `DefId`, but most of the time they are used through their constructors which have a different `DefId`. The two utility functions will check if the `DefId` is for a constructor and switch to the associated struct/variant `DefId`.

There does seem to be a bug on rustc's side where constructor `DefId`s from external crates seem to be returning `DefKind::Variant` instead of `DefKind::Ctor()`. There's a workaround put in right.

changelog: None
2022-10-02 19:46:46 +00:00
bors
0341198ba1 Auto merge of #9574 - Alexendoo:unused-fixed, r=Jarcho
Remove unused `.fixed` files, only run asm_syntax doctests on x86

Two small changes, removes some unused `.fixed` and makes `clippy_lints` doctests pass on non x86 arches

changelog: none
2022-10-02 19:06:58 +00:00
Jason Newcomb
162aa19793 Fix and improve internal lint checking for match_type usages
* Check for `const`s and `static`s from external crates
* Check for `LangItem`s
* Handle inherent functions which have the same name as a field
* Also check the following functions:
    * `match_trait_method`
    * `match_def_path`
    * `is_expr_path_def_path`
    * `is_qpath_def_path`
* Handle checking for a constructor to a diagnostic item or `LangItem`
2022-10-02 15:02:55 -04:00
Jason Newcomb
8e7af6b429 Replace is_lang_ctor with is_res_lang_ctor 2022-10-02 14:50:37 -04:00
kraktus
830fdf2b56 update rust version introduction 2022-10-02 17:45:44 +02:00
kraktus
b89ac0cefc refactor manual_filter
Move common functions to `manual_utils.rs`, better arm matching, use clippy utils `contains_unsafe_block`
2022-10-03 14:13:15 +02:00
bors
f8ba19287d Auto merge of #9576 - TennyZhuang:unnecessary_cast_for_non_literal_expr, r=llogiq
let unnecessary_cast work for trivial non_literal expressions

Signed-off-by: TennyZhuang <zty0826@gmail.com>

---

changelog: [`unnecessary_cast`]: fix for trivial non_literal expressions

Fixes #9562
2022-10-02 15:26:45 +00:00
bors
11a6d19c59 Auto merge of #9479 - kraktus:manual_assert2, r=llogiq
[`manual_assert`]: Preserve comments in the suggestion

close https://github.com/rust-lang/rust-clippy/issues/7730

changelog: [`manual_assert`]: Preserve comments in the suggestion
2022-10-02 15:13:32 +00:00
TennyZhuang
c9b93143d5 fix some logics
Signed-off-by: TennyZhuang <zty0826@gmail.com>
2022-10-02 23:02:13 +08:00
TennyZhuang
bff811bfdf extract common codes
Signed-off-by: TennyZhuang <zty0826@gmail.com>
2022-10-02 23:02:13 +08:00
TennyZhuang
081f73954b let unnecessary_cast work for trivial non_literal expressions
Signed-off-by: TennyZhuang <zty0826@gmail.com>
2022-10-02 23:02:11 +08:00
bors
ac12011315 Auto merge of #9577 - kraktus:unnecessary_cast, r=llogiq
[`unnecessary_cast`] add parenthesis when negative number uses a method

fix #9563

The issue was probably introduced by 90fe3bea52

changelog: [`unnecessary_cast`] add parenthesis when negative number uses a method

r? llogiq
2022-10-02 14:18:41 +00:00
bors
d6d5ecd625 Auto merge of #9571 - rust-lang:refactor-lit-ints, r=Jarcho
use `is_integer_literal` more

I noticed that we have the `is_integer_literal` function in our `clippy_utils`, yet almost everywhere people still match int literal expressions manually. So I searched for instances to replace and shorten the code a bit.

---

changelog: none
2022-10-02 13:11:21 +00:00
kraktus
22be60b4f0 fix indentation 2022-10-02 15:03:48 +02:00
kraktus
a35734c172 revert manual_assert suggestion refactor
Because `Sugg` helper does not simplify multiple negations
2022-10-02 15:03:48 +02:00
kraktus
2c04c1a188 [manual_assert]: Preserve comments in the suggestion 2022-10-02 15:03:48 +02:00
kraktus
90b446fd38 [unnecessary_cast] add parenthesis when negative number uses a method 2022-10-02 14:35:38 +02:00
Alex Macleod
52a68dc097 lint nested patterns and slice patterns in needless_borrowed_reference 2022-10-01 22:31:10 +00:00
Alex Macleod
a834ac9800 Only run x86 asm doctests on x86 2022-10-01 22:27:38 +00:00