Commit Graph

7358 Commits

Author SHA1 Message Date
asquared31415
af9dfa3692 fix ICE by using a type to return the info we want and also fix some bugs in displaying an extra mut when a TypeAndMut was wrong 2022-04-23 13:07:13 -04:00
asquared31415
c922bb9443 fix ICE 2022-04-18 22:19:34 -04:00
bors
cbdf17c884 Auto merge of #8707 - OneSignal:await-invalid-types, r=llogiq
Add `await_holding_invalid_type` lint

changelog: [`await_holding_invalid_type`]

This lint allows users to create a denylist of types which are not allowed to be
held across await points. This is essentially a re-implementation of the
language-level [`must_not_suspend`
lint](https://github.com/rust-lang/rust/issues/83310). That lint has a lot of
work still to be done before it will reach Rust stable, and in the meantime
there are a lot of types which can trip up developers if they are used
improperly.

I originally implemented this specifically for `tracing::span::Entered`, until I discovered #8434 and read the commentary on that PR. Given this implementation is fully user configurable, doesn't tie clippy to any one particular crate, and introduces no additional dependencies, it seems more appropriate.
2022-04-18 18:36:50 +00:00
Lily Mara
05086858c4 fixup! Add await_holding_invalid_type lint 2022-04-18 11:32:28 -07:00
Lily Mara
8eccbbed68 fixup! Add await_holding_invalid_type lint 2022-04-18 11:30:59 -07:00
Lily Mara
7e26edce65 fixup! Add await_holding_invalid_type lint 2022-04-18 11:16:35 -07:00
Lily Mara
a511072c2c fixup! Add await_holding_invalid_type lint 2022-04-18 10:34:25 -07:00
Lily Mara
ee3ebb35f4
Update clippy_lints/src/await_holding_invalid.rs
Co-authored-by: llogiq <bogusandre@gmail.com>
2022-04-18 10:32:11 -07:00
Lily Mara
228ce783bc
Update clippy_lints/src/await_holding_invalid.rs
Co-authored-by: llogiq <bogusandre@gmail.com>
2022-04-18 10:32:03 -07:00
Lily Mara
6d0baf2b16
Update clippy_lints/src/await_holding_invalid.rs
Co-authored-by: llogiq <bogusandre@gmail.com>
2022-04-18 10:31:57 -07:00
bors
95de4dcb5e Auto merge of #8701 - xFrednet:0000-clippy-print-hir-attr, r=flip1995
Rework `#[clippy::dump]` attribute for debugging

Hey `@rust-lang/clippy,` this adds a new `#[clippy::print_hir]` attribute that prints the node to the console using `{:#?}`. Personally, I use print debugging quite a lot while working on Clippy, and this is a simple shortcut that also works in the playground (Once this has been synced). The question is now, if we want to have this attribute. Are there any concerns? I think it's similar to our `#[clippy::author]` attribute.

I haven't added a test, as the `.stdout` file would require updates with every HIR change inside rustc. Here are some examples, for the current implementation

<details>
<summary>`do_something(&map);`</summary>

```rs
Expr {
    hir_id: HirId {
        owner: DefId(0:7 ~ aaa[995b]::main),
        local_id: 21,
    },
    kind: Call(
        Expr {
            hir_id: HirId {
                owner: DefId(0:7 ~ aaa[995b]::main),
                local_id: 17,
            },
            kind: Path(
                Resolved(
                    None,
                    Path {
                        span: tests/ui/aaa.rs:23:5: 23:17 (#0),
                        res: Def(
                            Fn,
                            DefId(0:6 ~ aaa[995b]::do_something),
                        ),
                        segments: [
                            PathSegment {
                                ident: do_something#0,
                                hir_id: Some(
                                    HirId {
                                        owner: DefId(0:7 ~ aaa[995b]::main),
                                        local_id: 16,
                                    },
                                ),
                                res: Some(
                                    Err,
                                ),
                                args: None,
                                infer_args: true,
                            },
                        ],
                    },
                ),
            ),
            span: tests/ui/aaa.rs:23:5: 23:17 (#0),
        },
        [
            Expr {
                hir_id: HirId {
                    owner: DefId(0:7 ~ aaa[995b]::main),
                    local_id: 20,
                },
                kind: AddrOf(
                    Ref,
                    Not,
                    Expr {
                        hir_id: HirId {
                            owner: DefId(0:7 ~ aaa[995b]::main),
                            local_id: 19,
                        },
                        kind: Path(
                            Resolved(
                                None,
                                Path {
                                    span: tests/ui/aaa.rs:23:19: 23:22 (#0),
                                    res: Local(
                                        HirId {
                                            owner: DefId(0:7 ~ aaa[995b]::main),
                                            local_id: 15,
                                        },
                                    ),
                                    segments: [
                                        PathSegment {
                                            ident: map#0,
                                            hir_id: Some(
                                                HirId {
                                                    owner: DefId(0:7 ~ aaa[995b]::main),
                                                    local_id: 18,
                                                },
                                            ),
                                            res: Some(
                                                Local(
                                                    HirId {
                                                        owner: DefId(0:7 ~ aaa[995b]::main),
                                                        local_id: 15,
                                                    },
                                                ),
                                            ),
                                            args: None,
                                            infer_args: true,
                                        },
                                    ],
                                },
                            ),
                        ),
                        span: tests/ui/aaa.rs:23:19: 23:22 (#0),
                    },
                ),
                span: tests/ui/aaa.rs:23:18: 23:22 (#0),
            },
        ],
    ),
    span: tests/ui/aaa.rs:23:5: 23:23 (#0),
}
```
</details>

<details>
<summary>`use std::collections::HashMap;`</summary>

```rs
Item {
    ident: HashMap#0,
    def_id: DefId(0:5 ~ aaa[995b]::{misc#1}),
    kind: Use(
        Path {
            span: tests/ui/aaa.rs:8:5: 8:30 (#0),
            res: Def(
                Struct,
                DefId(1:1294 ~ std[928b]::collections:#️⃣:map::HashMap),
            ),
            segments: [
                PathSegment {
                    ident: std#0,
                    hir_id: Some(
                        HirId {
                            owner: DefId(0:5 ~ aaa[995b]::{misc#1}),
                            local_id: 1,
                        },
                    ),
                    res: Some(
                        Def(
                            Mod,
                            DefId(1:0 ~ std[928b]),
                        ),
                    ),
                    args: None,
                    infer_args: false,
                },
                PathSegment {
                    ident: collections#0,
                    hir_id: Some(
                        HirId {
                            owner: DefId(0:5 ~ aaa[995b]::{misc#1}),
                            local_id: 2,
                        },
                    ),
                    res: Some(
                        Def(
                            Mod,
                            DefId(1:1193 ~ std[928b]::collections),
                        ),
                    ),
                    args: None,
                    infer_args: false,
                },
                PathSegment {
                    ident: HashMap#0,
                    hir_id: Some(
                        HirId {
                            owner: DefId(0:5 ~ aaa[995b]::{misc#1}),
                            local_id: 3,
                        },
                    ),
                    res: Some(
                        Err,
                    ),
                    args: None,
                    infer_args: false,
                },
            ],
        },
        Single,
    ),
    vis: Spanned {
        node: Inherited,
        span: tests/ui/aaa.rs:8:1: 8:1 (#0),
    },
    span: tests/ui/aaa.rs:8:1: 8:31 (#0),
}
```

</details>

<details>
<summary>`"100"`</summary>

```rs
Expr {
    hir_id: HirId {
        owner: DefId(0:7 ~ aaa[995b]::main),
        local_id: 27,
    },
    kind: Lit(
        Spanned {
            node: Str(
                "100",
                Cooked,
            ),
            span: tests/ui/aaa.rs:28:9: 28:14 (#0),
        },
    ),
    span: tests/ui/aaa.rs:28:9: 28:14 (#0),
}
```
</details>

---

changelog: Added `[clippy::print_hir]` to inspect rustc's internal representation
2022-04-18 13:06:03 +00:00
xFrednet
ccedc64e3a
Add #[clippy::print_hir] attribute for debugging 2022-04-18 00:51:28 +02:00
bors
e5ebece910 Auto merge of #8665 - InfRandomness:option_take_on_temporary, r=llogiq
Introduce needless_option_take lint

- \[x] Followed [lint naming conventions][lint_naming]
- \[x] Added passing UI tests (including committed `.stderr` file)
- \[x] `cargo test` passes locally
- \[x] Executed `cargo dev update_lints`
- \[x] Added lint documentation
- \[x] Run `cargo dev fmt`

Fixes #8618

changelog: Introduce [`needless_option_take`] lint
2022-04-17 18:34:16 +00:00
bors
b2959dbf69 Auto merge of #8706 - lupd:cast-abs-to-unsigned, r=xFrednet
Fix formatting of `cast_abs_to_unsigned` docs

The "use instead" section of the example was not being formatted as Rust code, and the "configuration" documentation was being formatted as Rust code.

changelog: `[cast_abs_to_unsigned]` Fix example/configuration formatting
2022-04-16 09:12:15 +00:00
Lily Mara
534419282e fixup! Add await_holding_invalid_type lint 2022-04-15 14:54:19 -07:00
Lily Mara
4844325faf Add await_holding_invalid_type lint
changelog: [`await_holding_invalid_type`]

This lint allows users to create a denylist of types which are not allowed to be
held across await points. This is essentially a re-implementation of the
language-level [`must_not_suspend`
lint](https://github.com/rust-lang/rust/issues/83310). That lint has a lot of
work still to be done before it will reach Rust stable, and in the meantime
there are a lot of types which can trip up developers if they are used
improperly.
2022-04-15 14:39:10 -07:00
whodi
2be7ad5b39 initialization misspell 2022-04-15 14:19:00 -07:00
whodi
fb1934b63f infallible <> infalliable in filename 2022-04-15 14:18:09 -07:00
whodi
29ef80c78a adding spell checking 2022-04-15 14:18:09 -07:00
dlup
d1a78f3ea5 Fix formatting of cast_abs_to_unsigned docs 2022-04-15 16:34:44 -04:00
Jeong YunWon
b592cc659b Fix typo in comment 2022-04-15 19:33:16 +09:00
Jason Newcomb
70f7c624e4 Allow more complex expressions in let_unit_value 2022-04-14 21:34:33 -04:00
Jason Newcomb
48bcc1d95f Move let_unit_value back into style 2022-04-14 21:33:32 -04:00
Jason Newcomb
d68ac9ccce Don't lint let_unit_value when needed for type inferenece 2022-04-14 21:32:51 -04:00
bors
aade96f902 Auto merge of #8626 - pitaj:format_add_string, r=llogiq
New lint `format_add_strings`

Closes #6261

changelog: Added [`format_add_string`]: recommend using `write!` instead of appending the result of  `format!`
2022-04-14 14:29:22 +00:00
bors
ecb3c3fc7e Auto merge of #8677 - xFrednet:8213-manual-bits-suggestion, r=giraffate
Add `usize` cast to `clippy::manual_bits` suggestion

A fix for the suggestion from https://github.com/rust-lang/rust-clippy/pull/8213

changelog: [`manual_bits`]: The suggestion now includes a cast for proper type conversion
2022-04-14 12:59:23 +00:00
infrandomness
2903b56f17 Add tests and docs
This adds test to make sure correct behavior of lint
- The first test's option variable is not a temporary variable
- The second test does not make usage of `take()`
- The third test makes usage of `take()` and uses a temporary variable
2022-04-14 13:16:46 +02:00
infrandomness
822993675f Modify lint description 2022-04-14 13:16:34 +02:00
infrandomness
76268c0d55 Introduce new lint check
This checks if the expression has one of `core`, `option`, `Option` or
`as_ref` in its path, this avoids false positives
2022-04-14 13:16:20 +02:00
infrandomness
b52bc9b96b Swap type checked expression
Instead of type checking the entire expression (causing a false
positive), only type check for a subset of the expression (the receiver of
the matched function: `take()`)
2022-04-14 13:16:20 +02:00
infrandomness
cf83e18106 Swap the category of the lint
This changes the lint from the suspicious category to the complexity category
2022-04-14 13:15:54 +02:00
infrandomness
f8f144117d Swap span_lint for span_lint_and_sugg
This implements a machine applicable suggestion to any matched usage of
`.as_ref().take()``
2022-04-14 13:15:51 +02:00
infrandomness
ee9281d7a2 Implement checks to the expression
The implemented checks are for checking if the expression is either of
type `Option` and isn't a syntactical place
2022-04-14 13:13:55 +02:00
infrandomness
262b35ea2c Introduce option_take_on_temporary lints
This lint checks if Option::take() is used on a temporary value (a value
that is not of type &mut Option and that is not a Place expression) to
suggest omitting take()
2022-04-14 12:41:47 +02:00
Fridtjof Stoldt
3bd0ac7a8d
Update lint description of clippy::manual_bits to include the as usize cast 2022-04-14 07:02:59 +02:00
Peter Jaszkowiak
67badbeef6 New lint format_add_strings 2022-04-13 22:48:36 -06:00
bors
38ba05508c Auto merge of #8676 - Alexendoo:local-used-across-loop, r=xFrednet
Check for loops/closures in `local_used_after_expr`

Follow up to #8646, catches when a local is used multiple times because it's in a loop or a closure

changelog: none
2022-04-14 04:44:33 +00:00
xFrednet
ba15cdd3c1
Add usize cast to clippy::manual_bits suggestion 2022-04-13 19:31:24 +02:00
bors
b6645d022e Auto merge of #8670 - yoav-lavi:main, r=giraffate
`pub_use` restriction

[`pub_use`]

Fixes https://github.com/rust-lang/rust-clippy/issues/8545

- \[x] Followed [lint naming conventions][lint_naming]
- \[x] Added passing UI tests (including committed `.stderr` file)
- \[x] `cargo test` passes locally
- \[x] Executed `cargo dev update_lints`
- \[x] Added lint documentation
- \[x] Run `cargo dev fmt`

[lint_naming]: https://rust-lang.github.io/rfcs/0344-conventions-galore.html#lints

changelog: Adds a lint called `pub_use` that restricts the usage of `pub use ...`
2022-04-13 13:03:51 +00:00
Yoav Lavi
66d253f0f2 pub_use 2022-04-13 13:48:27 +02:00
bors
f70c73f5c4 Auto merge of #8692 - kyoto7250:fixing_unnecessary_to_owned, r=giraffate
fix unnecessary_to_owned about msrv

This PR fixes ``[`unnecessary_owned`]``.

## What

```rust
# sample code
fn _msrv_1_35() {
    #![clippy::msrv = "1.35"]
    let _ = &["x"][..].to_vec().into_iter();
}

fn _msrv_1_36() {
    #![clippy::msrv = "1.36"]
    let _ = &["x"][..].to_vec().into_iter();
}
```

If we will check this code using clippy, ``[`unnecessary_owned`]`` will modify the code as follows.

```rust
error: unnecessary use of `to_vec`
  --> $DIR/unnecessary_to_owned.rs:219:14
   |
LL |     let _ = &["x"][..].to_vec().into_iter();
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `["x"][..].iter().copied()`

error: unnecessary use of `to_vec`
  --> $DIR/unnecessary_to_owned.rs:224:14
   |
LL |     let _ = &["x"][..].to_vec().into_iter();
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `["x"][..].iter().copied()`
```

This is incorrect. Because `Iterator::copied` was estabilished in 1.36.

## Why

This bug was caused by not separating "copied" and "clone" by reference to msrv.

89ee6aa6e3/clippy_lints/src/methods/unnecessary_to_owned.rs (L195)

So, I added a conditional branch and described the corresponding test.

Thank you in advance.

changelog: fix wrong suggestions about msrv in [`unnecessary_to_owned`]

r! `@giraffate`
2022-04-13 00:57:08 +00:00
bors
06b1695814 Auto merge of #8647 - Jarcho:mut_from_ref_6326, r=giraffate
Only lint `mut_from_ref` when unsafe code is used

fixes #6326

changelog: Only lint `mut_from_ref` when unsafe code is used.
2022-04-13 00:38:54 +00:00
bors
d8c97e6cf3 Auto merge of #8645 - Jarcho:manual_non_exhaustive_5714, r=Jarcho
Don't lint `manual_non_exhaustive` when the enum variant is used

fixes #5714

changelog: Don't lint `manual_non_exhaustive` when the enum variant is used
2022-04-12 18:38:45 +00:00
bors
849668ad71 Auto merge of #8690 - mucinoab:DoNot-rest_pat_in_fully_bound_structs-OnNonExhaustive, r=Manishearth
Do not trigger ``[`rest_pat_in_fully_bound_structs`]`` on `#[non_exhaustive]` structs

fixes #8029

Just adds an additional check to ensure that the`ty::VariantDef` is not marked as `#[non_exhaustive]`.

changelog: Do not apply ``[`rest_pat_in_fully_bound_structs`]`` on structs marked as non exhaustive.
2022-04-12 16:14:13 +00:00
kyoto7250
dfdc5ad7d8 fix unnecessary_to_owned about msrv 2022-04-12 22:59:25 +09:00
bors
f08a9c0369 Auto merge of #8691 - flip1995:infinite_recursion_only_in_recursion, r=llogiq
Prevent infinite (exponential) recursion in only_used_in_recursion

This simplifies the visitor code a bit and prevents checking expressions
multiple times. I still think this lint should be removed for now,
because its code isn't really tested.

Fixes #8689

**NOTE:** Before merging this, we should talk about removing and revisiting this lint. See my comment in #8689

changelog: prevent infinite recursion in [`only_used_in_recursion`]
2022-04-12 13:33:54 +00:00
flip1995
214fba7ed4
Prevent infinite (exponential) recursion in only_used_in_recursion
This simplifies the visitor code a bit and prevents checking expressions
multiple times. I still think this lint should be removed for now,
because its code isn't really tested.
2022-04-12 15:27:28 +02:00
bors
b3bd03afcd Auto merge of #8686 - Jarcho:undocumented_unsafe_blocks_8681, r=flip1995
Fix ICE in `undocumented_unsafe_blocks`

fixes #8681

changelog: Fix ICE in `undocumented_unsafe_blocks`
2022-04-12 07:17:57 +00:00
Bruno A. Muciño
739f273739 Do not apply rest_pat_in_fully_bound_structs on #[non_exhaustive] structs 2022-04-11 22:47:04 -05:00
bors
bc069efb1f Auto merge of #8688 - kyoto7250:adding_condition_for_map_clone, r=giraffate
adding condition for map_clone message

This PR fixes the message about `map_clone`.

if msrv >= 1.36, the message is correct.

```bash
$ cat main.rs
fn main() {
  let x: Vec<&i32> = vec![&1, &2];
  let y: Vec<_>  = x.iter().map(|i| *i).collect();
  println!("{:?}", y);
}

$ cargo clippy
warning: you are using an explicit closure for copying elements
 --> main.rs:3:20
  |
3 |   let y: Vec<_>  = x.iter().map(|i| *i).collect();
  |                    ^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `copied` method: `x.iter().copied()`
  |
  = note: `#[warn(clippy::map_clone)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_clone

warning: `test` (build script) generated 1 warning
warning: `test` (bin "test") generated 1 warning (1 duplicate)
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
```

but, if msrv < 1.36, the suggestion is `cloned`, but the message is `copying`.
```bash
$ cat clippy.toml
msrv = "1.35"

$ cargo clippy
warning: you are using an explicit closure for copying elements
 --> main.rs:3:20
  |
3 |   let y: Vec<_>  = x.iter().map(|i| *i).collect();
  |                    ^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `cloned` method: `x.iter().cloned()`
```

I think  the separation of messages will make it more user-friendly.

thank you in advance.

changelog: Fixed a message in map_clone.
2022-04-12 01:11:54 +00:00