Commit Graph

8767 Commits

Author SHA1 Message Date
bors
3816f9a6f9 Auto merge of #10137 - euclio:dbg-macro, r=giraffate
reword dbg_macro labels

---

This change rewords the `dbg_macro` lint labels to be clearer.

changelog: none
<!-- changelog_checked -->
2023-01-06 05:25:34 +00:00
bors
4f4c96155a Auto merge of #10142 - EricWu2003:drop_ref-FP, r=llogiq
[`drop_ref`]: don't lint idiomatic in match arm

fixes #10122

As established in issue #9482, it is idiomatic to use a single `drop()` expression in a match arm to achieve a side-effect of a function while discarding its output. This should also apply to cases where the function returns a reference.

The change to the lint's code was less than 1 line, because all the heavy lifting was done in PR #9491.

---

changelog: FP: [`drop_ref`]: No longer lints idiomatic expression in `match` arms
[#10142](https://github.com/rust-lang/rust-clippy/pull/10142)
<!-- changelog_checked -->
2023-01-05 23:36:49 +00:00
Kyle Huey
1c42dbba60 Expand derivable-impls to cover enums with a default unit variant. 2023-01-04 19:08:37 -08:00
bors
d5d8ef13d7 Auto merge of #10159 - khuey:iter_kv_map_ref_mut, r=llogiq
Make the iter_kv_map lint handle ref/mut annotations.

For the degenerate (`map(|(k, _)| k)`/`map(|(_, v)| v)`) cases a mut annotation is superfluous and a ref annotation won't compile, so no additional handling is required. For cases where the `map` call must be preserved ref/mut annotations should also be presereved so that the map body continues to work as expected.

*Please write a short comment explaining your change (or "none" for internal only changes)*

changelog: [`iter_kv_map`]: handle ref/mut annotations
2023-01-04 23:13:39 +00:00
Kyle Huey
755ae3fa29 Fix spelling while we're in the neighborhood. 2023-01-04 14:58:07 -08:00
Kyle Huey
d0c1605d51 Make the iter_kv_map lint handle ref/mut annotations.
For the degenerate (`map(|(k, _)| k)`/`map(|(_, v)| v)`) cases a mut annotation is superfluous and a ref annotation won't compile, so no additional handling is required. For cases where the `map` call must be preserved ref/mut annotations should also be presereved so that the map body continues to work as expected.
2023-01-04 13:32:34 -08:00
bors
a385d34fc2 Auto merge of #10107 - tylerjw:suggest_path, r=Alexendoo
Suggest using Path for comparing extensions

fixes #10042

changelog: Sugg: [`case_sensitive_file_extension_comparisons`]: Now displays a suggestion with `Path`
[#10107](https://github.com/rust-lang/rust-clippy/pull/10107)
<!-- changelog_checked -->
2023-01-04 19:27:11 +00:00
Tyler Weaver
ea6ff7ed04
Apply changes suggested in review 2023-01-04 07:06:07 -07:00
bors
8b1ac4539d Auto merge of #10141 - FoseFx:fosefx/9887, r=Jarcho
fix empty_structs_with_brackets suggestion errors

fixes #9887

I refer to [my comment](https://github.com/rust-lang/rust-clippy/issues/9887#issuecomment-1368495395) to explain this PR.

---

changelog: Sugg: [`empty_structs_with_brackets`]: The suggestion is no longer machine applicable, to avoid errors when accessing struct fields
[#10141](https://github.com/rust-lang/rust-clippy/pull/10141)
<!-- changelog_checked -->
2023-01-03 03:57:19 +00:00
bors
43349195f7 Auto merge of #10143 - EricWu2003:field_reassign_with_default-FP, r=Manishearth
don't lint field_reassign when field in closure

fixes #10136

This change makes the ContainsName struct visit all interior expressions, which means that ContainsName will return true even if `name` is used in a closure within `expr`.

---

changelog: FP: [`field_reassign_with_default`]: No longer lints cases, where values are initializes from closures capturing struct values
[#10143](https://github.com/rust-lang/rust-clippy/pull/10143)
<!-- changelog_checked -->
2023-01-03 03:16:18 +00:00
Tyler Weaver
0cee2c5095
Don't trigger lint if last method is to_lower/upper 2023-01-02 16:42:56 -07:00
Tyler Weaver
0aa7d73df3
Only remove method from end of recv, indent suggestion source
Signed-off-by: Tyler Weaver <maybe@tylerjw.dev>
2023-01-02 16:27:46 -07:00
Tyler Weaver
bd83650e91
Suggest using Path for comparing extensions
Signed-off-by: Tyler Weaver <maybe@tylerjw.dev>
2023-01-02 16:27:46 -07:00
bors
1a46dc0b9f Auto merge of #10138 - smoelius:unused_self-typo, r=xFrednet
Fix typo in `unused_self` diagnostic message

changelog: Msg: [`unused_self`]: The message now correctly uses *an*
[#10138](https://github.com/rust-lang/rust-clippy/pull/10138)
<!-- changelog_checked -->
2023-01-02 22:57:27 +00:00
bors
653f5d98d8 Auto merge of #10139 - chansuke:chore/fix-if_chain_ident, r=Jarcho
chore: fix identation of `if_chain` in `filter_map`

This is a really small fix.
If someone could take a look at it, I would appreciate it🙏

---

changelog: none
<!-- changelog_checked -->
2023-01-02 22:44:15 +00:00
Nilstrieb
f53c6e2f15 Correct Gankra's name in the linkedlist lint 2023-01-02 19:58:00 +01:00
Andy Russell
9aef1a264a
reword dbg_macro labels 2023-01-02 12:09:26 -05:00
Max Baumann
7a1a9c8188
empty_structs_with_brackets: not MachineApplicable anymore 2023-01-02 14:58:10 +01:00
Eric Wu
01a2a9df43 [drop_ref]: don't lint idiomatic in match arm 2023-01-01 22:33:18 -05:00
Eric Wu
8de011fdf7 don't lint field_reassign when field in closure
This commit makes the ContainsName struct visit all interior
expressions, which means that ContainsName will return true
even if `name` is used in a closure within `expr`.
2023-01-01 21:41:46 -05:00
chansuke
5b46f2db59 chore: fix identation of if_chain in filter_map 2023-01-02 00:17:52 +09:00
bors
a85e480dd1 Auto merge of #10110 - Niki4tap:needless_anyhow_return, r=Alexendoo
Fix FN in `needless_return`

Fixes #10051

changelog: Enhancement: [`needless_return`]: Now detects more cases for returns of owned values
[#10110](https://github.com/rust-lang/rust-clippy/pull/10110)
<!-- changelog_checked -->
2023-01-01 13:55:29 +00:00
Samuel Moelius
da7c99b81e Fix typo in unused_self diagnostic message 2023-01-01 06:21:28 -05:00
bors
6ccd4ebd5b Auto merge of #10115 - rdrpenguin04:master, r=flip1995
Move `mutex_atomic` to `restriction`

By #4295, the general consensus seems to be that `mutex_atomic` is not a useful lint in most cases. If anything, it could be useful as a restriction on code that for whatever reason can't use atomics. Keeping it in `clippy::nursery` is harmful to people attempting to use clippy for soundness.

---

changelog: Moved [`mutex_atomic`] to `restriction`
[#10115](https://github.com/rust-lang/rust-clippy/pull/10115)
<!-- chnagelog_checked -->
2022-12-30 15:49:12 +00:00
Ray Redondo
0298095ac2 add comment about mutex_atomic issue to description 2022-12-29 14:48:36 -06:00
bors
22af8fe683 Auto merge of #10060 - alex-semenyuk:match_single_binding_fix, r=llogiq
Fix [match_single_binding] suggestion introduced an extra semicolon

Fix #9725

---

changelog: [`match_single_binding`]: suggestion no longer introduces unneeded semicolons
[#10060](https://github.com/rust-lang/rust-clippy/pull/10060)
<!-- changelog_checked -->
2022-12-29 09:23:03 +00:00
bors
266eef7701 Auto merge of #10119 - koka831:doc/config-default-value, r=xFrednet
fix codeblocks in the document

While I've looked into https://github.com/rust-lang/rust-clippy/issues/9990, I found broken code blocks in the document.

This patch not only improves visibility, but also fixes a potential bug. When a lint description ends with code block, the string will have three backquotes at the end.
Since the current implementation prints the default value immediately after that, the markdown renderer is unable to properly close the code block.

e.g. `arithmetic_side_effects`, we can see code block is not rendered properly, and (I think) it's bit hard to understand what ``"defaults to `[]`"`` is meant.

![2022-12-26_01-51](https://user-images.githubusercontent.com/14945055/209476342-4d0b2e18-44b6-4c74-8c3c-4f4f0904e8ca.png)

In this PR, it will be rendered as:

![image](https://user-images.githubusercontent.com/14945055/209476353-07587b86-1100-445f-946d-41f62f741e51.png)

changelog: none

r? `@xFrednet`
2022-12-28 13:53:34 +00:00
bors
d98e714988 Auto merge of #10113 - EricWu2003:suboptimal_flops_incorrect_suggestion, r=Jarcho
fix incorrect suggestion in `suboptimal_flops`

fixes #10003

There was an error when trying to negate an expression like `x - 1.0`. We used to format it as `-x - 1.0` whereas a proper negation would be `-(x - 1.0)`.

Therefore, we add parentheses around the expression when it is `ExprKind::Binary`.

We also add parentheses around multiply and divide expressions, even though this is not strictly necessary.

changelog: [`suboptimal_flops`]: fix incorrect suggestion caused by an incorrect negation of floating point expressions.
2022-12-26 02:00:26 +00:00
Eric Wu
6bb6dd64d4 fix incorrect suggestion in suboptimal_flops
There was an error when trying to negate an expression
like `x - 1.0`. We used to format it as `-x - 1.0` whereas
a proper negation would be `-(x - 1.0)`.

Therefore, we add parentheses around the expression when it is a
Binary ExprKind.

We also add parentheses around multiply and divide expressions,
even though this is not strictly necessary.
2022-12-25 16:56:46 -05:00
koka
fae19a9a79
Place default values near its definitions
This patch not only improves visibility, but also fixes a potential bug.
When a lint description ends with code block, the string will have three
backquotes at the end.
Since the current implementation prints the default value immediately
after that, the markdown renderer is unable to properly close the code
block.
2022-12-26 01:51:46 +09:00
Trevor Gross
12f2dea229 not_unsafe_ptr_arg_deref update documentation 2022-12-25 07:30:21 -05:00
Ray Redondo
0e5fcb7b7f move mutex_atomic to clippy::restriction 2022-12-24 23:47:14 -06:00
bors
e8703a0ce2 Auto merge of #10098 - lukaslueg:size_of_ref, r=Jarcho
Add size_of_ref lint

This addresses #9995, which is likely raising a valid point about `std::mem::size_of_val()`: It's [very easy to use double-references as the argument](https://github.com/apache/arrow-datafusion/pull/4371#discussion_r1032385224), which the function will happily accept and give back the size of _the reference_, not the size of the value _behind_ the reference. In the worst case, if the value matches the programmer's expectation, this seems to work, while in fact, everything will go horribly wrong e.g. on a different platform.

The size of a `&T` is independent of what `T` is, and people might want to use `std::mem::size_of_val()` to actually get the size of _any_ reference (e.g. via `&&()`). I would rather suggest that this is always bad behavior, though ([instead](https://doc.rust-lang.org/reference/type-layout.html#pointers-and-references-layout), [and](https://doc.rust-lang.org/stable/std/primitive.usize.html#associatedconstant.BITS)). I, therefore, put this lint into `correctness`.

Since the problem is usually easily fixed by removing extra `&`, I went light on suggesting code.

---

changelog: New lint: [`size_of_ref`]
[#10098](https://github.com/rust-lang/rust-clippy/pull/10098)
<!-- changelog_checked -->
2022-12-24 23:33:13 +00:00
Lukas Lueg
d7b9e195c2 Add size_of_ref lint
Fixes #9995
2022-12-24 23:39:54 +01:00
Niki4tap
a935826071 Fix the actual bug 2022-12-23 00:57:28 +03:00
Niki4tap
0cd9b06125 Small code style adjustments 2022-12-23 00:56:50 +03:00
bors
4fe3727c39 Auto merge of #9701 - smoelius:improve-possible-borrower, r=Jarcho
Improve `possible_borrower`

This PR makes several improvements to `clippy_uitls::mir::possible_borrower`. These changes benefit both `needless_borrow` and `redundant clone`.

1. **Use the compiler's `MaybeStorageLive` analysis**

I could spot not functional differences between the one in the compiler and the one in Clippy's repository. So, I removed the latter in favor of the the former.

2. **Make `PossibleBorrower` a dataflow analysis instead of a visitor**

The main benefit of this change is that allows `possible_borrower` to take advantage of statements' relative locations, which is easier to do in an analysis than in a visitor.

This is easier to illustrate with an example, so consider this one:
```rust
    fn foo(cx: &LateContext<'_>, lint: &'static Lint) {
        cx.struct_span_lint(lint, rustc_span::Span::default(), "", |diag| diag.note(&String::new()));
        //                                                                          ^
    }
```
We would like to flag the `&` pointed to by the `^` for removal. `foo`'s MIR begins like this:
```rust
fn span_lint::foo::{closure#0}(_1: [closure@$DIR/needless_borrow.rs:396:68: 396:74], _2: &mut rustc_errors::diagnostic_builder::DiagnosticBuilder<'_, ()>) -> &mut rustc_errors::diagnostic_builder::DiagnosticBuilder<'_, ()> {
    debug diag => _2;                    // in scope 0 at $DIR/needless_borrow.rs:396:69: 396:73
    let mut _0: &mut rustc_errors::diagnostic_builder::DiagnosticBuilder<'_, ()>; // return place in scope 0 at $DIR/needless_borrow.rs:396:75: 396:75
    let mut _3: &mut rustc_errors::diagnostic_builder::DiagnosticBuilder<'_, ()>; // in scope 0 at $DIR/needless_borrow.rs:396:75: 396:100
    let mut _4: &mut rustc_errors::diagnostic_builder::DiagnosticBuilder<'_, ()>; // in scope 0 at $DIR/needless_borrow.rs:396:75: 396:100
    let mut _5: &std::string::String;    // in scope 0 at $DIR/needless_borrow.rs:396:85: 396:99
    let _6: std::string::String;         // in scope 0 at $DIR/needless_borrow.rs:396:86: 396:99

    bb0: {
        StorageLive(_3);                 // scope 0 at $DIR/needless_borrow.rs:396:75: 396:100
        StorageLive(_4);                 // scope 0 at $DIR/needless_borrow.rs:396:75: 396:100
        _4 = &mut (*_2);                 // scope 0 at $DIR/needless_borrow.rs:396:75: 396:100
        StorageLive(_5);                 // scope 0 at $DIR/needless_borrow.rs:396:85: 396:99
        StorageLive(_6);                 // scope 0 at $DIR/needless_borrow.rs:396:86: 396:99
        _6 = std::string::String::new() -> bb1; // scope 0 at $DIR/needless_borrow.rs:396:86: 396:99
                                         // mir::Constant
                                         // + span: $DIR/needless_borrow.rs:396:86: 396:97
                                         // + literal: Const { ty: fn() -> std::string::String {std::string::String::new}, val: Value(<ZST>) }
    }

    bb1: {
        _5 = &_6;                        // scope 0 at $DIR/needless_borrow.rs:396:85: 396:99
        _3 = rustc_errors::diagnostic_builder::DiagnosticBuilder::<'_, ()>::note::<&std::string::String>(move _4, move _5) -> [return: bb2, unwind: bb4]; // scope 0 at $DIR/needless_borrow.rs:396:75: 396:100
                                         // mir::Constant
                                         // + span: $DIR/needless_borrow.rs:396:80: 396:84
                                         // + literal: Const { ty: for<'a> fn(&'a mut rustc_errors::diagnostic_builder::DiagnosticBuilder<'_, ()>, &std::string::String) -> &'a mut rustc_errors::diagnostic_builder::DiagnosticBuilder<'_, ()> {rustc_errors::diagnostic_builder::DiagnosticBuilder::<'_, ()>::note::<&std::string::String>}, val: Value(<ZST>) }
    }
```
The call to `diag.note` appears in `bb1` on the line beginning with `_3 =`. The `String` is owned by `_6`. So, in the call to `diag.note`, we would like to know whether there are any references to `_6` besides `_5`.

The old, visitor approach did not consider the relative locations of statements. So all borrows were treated the same, *even if they occurred after the location of interest*.

For example, before the `_3 = ...` call, the possible borrowers of `_6` would be just `_5`. But after the call, the possible borrowers would include `_2`, `_3`, and `_4`.

So, in a sense, the call from which we are try to remove the needless borrow is trying to prevent us from removing the needless borrow(!).

With an analysis, things do not get so muddled. We can determine the set of possible borrowers at any specific location, e.g., using a `ResultsCursor`.

3. **Change `only_borrowers` to `at_most_borrowers`**

`possible_borrowers` exposed a function `only_borrowers` that determined whether the borrowers of some local were *exactly* some set `S`. But, from what I can tell, this was overkill. For the lints that currently use `possible_borrower` (`needless_borrow` and `redundant_clone`), all we really want to know is whether there are borrowers *other than* those in `S`. (Put another way, we only care about the subset relation in one direction.) The new function `at_most_borrowers` takes this more tailored approach.

4. **Compute relations "on the fly" rather than using `transitive_relation`**

The visitor would compute and store the transitive closure of the possible borrower relation for an entire MIR body.

But with an analysis, there is effectively a different possible borrower relation at each location in the body. Computing and storing a transitive closure at each location would not be practical.

So the new approach is to compute the transitive closure on the fly, as needed. But the new approach might actually be more efficient, as I now explain.

In all current uses of `at_most_borrowers` (previously `only_borrowers`), the size of the set of borrowers `S` is at most 2. So you need only check at most three borrowers to determine whether the subset relation holds. That is, once you have found a third borrower, you can stop, since you know the relation cannot hold.

Note that `transitive_relation` is still used by `clippy_uitls::mir::possible_origin` (a kind of "subroutine" of `possible_borrower`).

cc: `@Jarcho`

---

changelog: [`needless_borrow`], [`redundant_clone`]: Now track references better and detect more cases
[#9701](https://github.com/rust-lang/rust-clippy/pull/9701)
<!-- changelog_checked -->
2022-12-22 15:08:04 +00:00
bors
8a6e6fd623 Auto merge of #10056 - koka831:fix/9993, r=Jarcho
Avoid `match_wildcard_for_single_variants` on guarded wild matches

fix #9993

changelog: FP: [`match_wildcard_for_single_variants`]: No longer lints on wildcards with a guard
[#10056](https://github.com/rust-lang/rust-clippy/pull/10056)
<!-- changelog_checked -->

r? `@Jarcho`
2022-12-22 14:54:50 +00:00
Niki4tap
b6882f6107 Fix FP in needless_return when using yeet 2022-12-22 12:47:39 +03:00
bors
065c6f78e7 Auto merge of #10091 - EricWu2003:manual-filter-FP, r=llogiq
fix manual_filter false positive

fixes #10088
fixes #9766

changelog: FP: [`manual_filter`]: Now ignores if expressions where the else branch has side effects or doesn't return `None`
[#10091](https://github.com/rust-lang/rust-clippy/pull/10091)
<!-- changelog_checked -->
2022-12-21 19:39:32 +00:00
bors
4a09068f87 Auto merge of #10063 - chansuke:issue-9702, r=Alexendoo
add [`permissions_set_readonly_false`] #9702

Add slight modification on [this PR](https://github.com/rust-lang/rust-clippy/pull/9744).

---

changelog: New lint [`permissions_set_readonly_false`]
[#10063](https://github.com/rust-lang/rust-clippy/pull/10063)
<!-- changelog_checked -->
2022-12-20 16:03:11 +00:00
chansuke
b21cc36ee9 hotfix: add help dialog for PermissionExt 2022-12-20 23:43:34 +09:00
Samuel Moelius
26df55112f Fix adjacent code 2022-12-20 05:12:13 -05:00
Samuel Moelius
ed519ad746 Improve possible_borrower 2022-12-20 05:12:13 -05:00
chansuke
e1d3c1e8f1 refactor: fix style 2022-12-20 00:37:53 +09:00
dboso
30e6e85508 add [permissions_set_readonly_false] #9702 2022-12-20 00:37:51 +09:00
Niki4tap
d0ac6ba0b1 Fix overflow ICE in large_stack/const_arrays 2022-12-19 18:27:42 +03:00
bors
b3145fea6a Auto merge of #10099 - Niki4tap:null_fn_lints, r=llogiq
Null fn lints

Adds lints to check for code, that assumes nullable `fn()`.

### Lint examples:

`transmute_null_to_fn`:
```rust
error: transmuting a known null pointer into a function pointer
  --> $DIR/transmute_null_to_fn.rs:9:23
   |
LL |         let _: fn() = std::mem::transmute(std::ptr::null::<()>());
   |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this transmute results in undefined behavior
   |
   = help: try wrapping your function pointer type in `Option<T>` instead, and using `None` as a null pointer value
```

`fn_null_check`:
```rust
error: function pointer assumed to be nullable, even though it isn't
  --> $DIR/fn_null_check.rs:13:8
   |
LL |     if (fn_ptr as *mut ()).is_null() {}
   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: try wrapping your function pointer type in `Option<T>` instead, and using `is_none` to check for null pointer value
```

Closes #1644

---

changelog: Improvement: [`transmuting_null`]: Now detects `const` pointers to all types
[#10099](https://github.com/rust-lang/rust-clippy/pull/10099)
changelog: New lint: [`transmute_null_to_fn`]
[#10099](https://github.com/rust-lang/rust-clippy/pull/10099)
changelog: New lint: [`fn_null_check`]
[#10099](https://github.com/rust-lang/rust-clippy/pull/10099)
<!-- changelog_checked (This is just a flag for me, please don't add it manually) -->
2022-12-19 12:44:23 +00:00
Niki4tap
691df70bbc Inline some consts 2022-12-19 15:34:55 +03:00
Philipp Krones
62061b8a05
Move manual_clamp to nursery 2022-12-19 09:44:56 +01:00