From 70b3d0efa60b9c63df371b7d99c5f0ed926ffb97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Kohlgr=C3=BCber?= Date: Tue, 12 Mar 2019 13:38:46 +0100 Subject: [PATCH 001/244] add syntax-tree-patterns RFC --- rfcs/0001-syntax-tree-patterns.md | 797 ++++++++++++++++++++++++++++++ 1 file changed, 797 insertions(+) create mode 100644 rfcs/0001-syntax-tree-patterns.md diff --git a/rfcs/0001-syntax-tree-patterns.md b/rfcs/0001-syntax-tree-patterns.md new file mode 100644 index 0000000000000..8d285b2ef44c7 --- /dev/null +++ b/rfcs/0001-syntax-tree-patterns.md @@ -0,0 +1,797 @@ +- Feature Name: syntax-tree-patterns +- Start Date: 2019-03-12 +- RFC PR: (leave this empty) +- Rust Issue: (leave this empty) + +> Note: This project is part of my Master's Thesis (supervised by [@oli-obk](https://github.com/oli-obk)) + +# Summary +[summary]: #summary + +Introduce a domain-specific language (similar to regular expressions) that allows to describe lints using *syntax tree patterns*. + + +# Motivation +[motivation]: #motivation + + +Finding parts of a syntax tree (AST, HIR, ...) that have certain properties (e.g. "*an if that has a block as its condition*") is a major task when writing lints. For non-trivial lints, it often requires nested pattern matching of AST / HIR nodes. For example, testing that an expression is a boolean literal requires the following checks: + +``` +if let ast::ExprKind::Lit(lit) = &expr.node { + if let ast::LitKind::Bool(_) = &lit.node { + ... + } +} +``` + +Writing this kind of matching code quickly becomes a complex task and the resulting code is often hard to comprehend. The code below shows a simplified version of the pattern matching required by the `collapsible_if` lint: + +``` +// simplified version of the collapsible_if lint +if let ast::ExprKind::If(check, then, None) = &expr.node { + if then.stmts.len() == 1 { + if let ast::StmtKind::Expr(inner) | ast::StmtKind::Semi(inner) = &then.stmts[0].node { + if let ast::ExprKind::If(check_inner, content, None) = &inner.node { + ... + } + } + } +} +``` + +The `if_chain` macro can improve readability by flattening the nested if statements, but the resulting code is still quite hard to read: + +``` +// simplified version of the collapsible_if lint +if_chain! { + if let ast::ExprKind::If(check, then, None) = &expr.node; + if then.stmts.len() == 1; + if let ast::StmtKind::Expr(inner) | ast::StmtKind::Semi(inner) = &then.stmts[0].node; + if let ast::ExprKind::If(check_inner, content, None) = &inner.node; + then { + ... + } +} +``` + +The code above matches if expressions that contain only another if expression (where both ifs don't have an else branch). While it's easy to explain what the lint does, it's hard to see that from looking at the code samples above. + +Following the motivation above, the first goal this RFC is to **simplify writing and reading lints**. + +The second part of the motivation is clippy's dependence on unstable compiler-internal data structures. Clippy lints are currently written against the compiler's AST / HIR which means that even small changes in these data structures might break a lot of lints. The second goal of this RFC is to **make lints independant of the compiler's AST / HIR data structures**. + +# Approach + +A lot of complexity in writing lints currently seems to come from having to manually implement the matching logic (see code samples above). It's an imparative style that describes *how* to match a syntax tree node instead of specifying *what* should be matched against declaratively. In other areas, it's common to use declarative patterns to describe desired information and let the implementation do the actual matching. A well-known example of this approach are [regular expressions](https://en.wikipedia.org/wiki/Regular_expression). Instead of writing code that detects certain character sequences, one can describe a search pattern using a domain-specific language and search for matches using that pattern. The advantage of using a declarative domain-specific language is that its limited domain (e.g. matching character sequences in the case of regular expressions) allows to express entities in that domain in a very natural and expressive way. + +While regular expressions are very useful when searching for patterns in flat character sequences, they cannot easily be applied to hierarchical data structures like syntax trees. This RFC therefore proposes a pattern matching system that is inspired by regular expressions and designed for hierarchical syntax trees. + +# Guide-level explanation +[guide-level-explanation]: #guide-level-explanation + +This proposal adds a `pattern!` macro that can be used to specify a syntax tree pattern to search for. A simple pattern is shown below: + +``` +pattern!{ + my_pattern: Expr = + Lit(Bool(false)) +} +``` + +This macro call defines a pattern named `my_pattern` that can be matched against an `Expr` syntax tree node. The actual pattern (`Lit(Bool(false))` in this case) defines which syntax trees should match the pattern. This pattern matches expressions that are boolean literals with value `false`. + +The pattern can then be used to implement lints in the following way: + +``` +... + +impl EarlyLintPass for MyAwesomeLint { + fn check_expr(&mut self, cx: &EarlyContext, expr: &syntax::ast::Expr) { + + if my_pattern(expr).is_some() { + cx.span_lint( + MY_AWESOME_LINT, + expr.span, + "This is a match for a simple pattern. Well done!", + ); + } + + } +} +``` + +The `pattern!` macro call expands to a function `my_pattern` that expects a syntax tree expression as its argument and returns an `Option` that indicates whether the pattern matched. + +> Note: The result type is explained in more detail in [a later section](#the-result-type). For now, it's enough to know that the result is `Some` if the pattern matched and `None` otherwise. + +## Pattern syntax + +The following examples demonstate the pattern syntax: + + +#### Any (`_`) + +The simplest pattern is the any pattern. It matches anything and is therefore similar to regex's `*`. + +``` +pattern!{ + // matches any expression + my_pattern: Expr = + _ +} +``` + +#### Node (`()`) + +Nodes are used to match a specific variant of an AST node. A node has a name and a number of arguments that depends on the node type. For example, the `Lit` node has a single argument that describes the type of the literal. As another example, the `If` node has three arguments describing the if's condition, then block and else block. + +``` +pattern!{ + // matches any expression that is a literal + my_pattern: Expr = + Lit(_) +} + +pattern!{ + // matches any expression that is a boolean literal + my_pattern: Expr = + Lit(Bool(_)) +} + +pattern!{ + // matches if expressions that have a boolean literal in their condition + // Note: The `_?` syntax here means that the else branch is optional and can be anything. + // This is discussed in more detail in the section `Repetition`. + my_pattern: Expr = + If( Lit(Bool(_)) , _, _?) +} +``` + + +#### Literal (``) + +A pattern can also contain Rust literals. These literals match themselves. + +``` +pattern!{ + // matches the boolean literal false + my_pattern: Expr = + Lit(Bool(false)) +} + +pattern!{ + // matches the character literal 'x' + my_pattern: Expr = + Lit(Char('x')) +} +``` + +#### Alternations (`a | b`) + +``` +pattern!{ + // matches if the literal is a boolean or integer literal + my_pattern: Lit = + Bool(_) | Int(_) +} + +pattern!{ + // matches if the expression is a char literal with value 'x' or 'y' + my_pattern: Expr = + Lit( Char('x' | 'y') ) +} +``` + +#### Empty (`()`) + +The empty pattern represents an empty sequence or the `None` variant of an optional. + +``` +pattern!{ + // matches if the expression is an empty array + my_pattern: Expr = + Array( () ) +} + +pattern!{ + // matches if expressions that don't have an else clause + my_pattern: Expr = + If(_, _, ()) +} +``` + +#### Sequence (` `) + +``` +pattern!{ + // matches the array [true, false] + my_pattern: Expr = + Array( Lit(Bool(true)) Lit(Bool(false)) ) +} +``` + +#### Repetition (`*`, `+`, `?`, `{n}`, `{n,m}`, `{n,}`) + +Elements may be repeated. The syntax for specifying repetitions is identical to [regex's syntax](https://docs.rs/regex/1.1.2/regex/#repetitions). + +``` +pattern!{ + // matches arrays that contain 2 'x's as their last or second-last elements + // Examples: + // ['x', 'x'] match + // ['x', 'x', 'y'] match + // ['a', 'b', 'c', 'x', 'x', 'y'] match + // ['x', 'x', 'y', 'z'] no match + my_pattern: Expr = + Array( _* Lit(Char('x')){2} _? ) +} + +pattern!{ + // matches if expressions that **may or may not** have an else block + // Attn: `If(_, _, _)` matches only ifs that **have** an else block + // + // | if with else block | if witout else block + // If(_, _, _) | match | no match + // If(_, _, _?) | match | match + // If(_, _, ()) | no match | match + my_pattern: Expr = + If(_, _, _?) +} +``` + +#### Named submatch (`#`) + +``` +pattern!{ + // matches character literals and gives the literal the name foo + my_pattern: Expr = + Lit(Char(_)#foo) +} + +pattern!{ + // matches character literals and gives the char the name bar + my_pattern: Expr = + Lit(Char(_#bar)) +} + +pattern!{ + // matches character literals and gives the expression the name baz + my_pattern: Expr = + Lit(Char(_))#baz +} +``` + +The reason for using named submatches is described in the section [The result type](#the-result-type). + +### Summary + +The following table gives an summary of the pattern syntax: + +| Syntax | Concept | Examples | +|-------------------------|------------------|--------------------------------------------| +|`_` | Any | `_` | +|`()` | Node | `Lit(Bool(true))`, `If(_, _, _)` | +|`` | Literal | `'x'`, `false`, `101` | +|` \| ` | Alternation | `Char(_) \| Bool(_)` | +|`()` | Empty | `Array( () )` | +|` ` | Sequence | `Tuple( Lit(Bool(_)) Lit(Int(_)) Lit(_) )` | +|`*`
`
+`
`
?`
`
{n}`
`
{n,m}`
`
{n,}` | Repetition





| `Array( _* )`,
`Block( Semi(_)+ )`,
`If(_, _, Block(_)?)`,
`Array( Lit(_){10} )`,
`Lit(_){5,10}`,
`Lit(Bool(_)){10,}` | +|`
#` | Named submatch | `Lit(Int(_))#foo` `Lit(Int(_#bar))` | + + +## The result type +[the-result-type]: #the-result-type + +A lot of lints require checks that go beyond what the pattern syntax described above can express. For example, a lint might want to check whether a node was created as part of a macro expansion or whether there's no comment above a node. Another example would be a lint that wants to match two nodes that have the same value (as needed by lints like `almost_swapped`). Instead of allowing users to write these checks into the pattern directly (which might make patterns hard to read), the proposed solution allows users to assign names to parts of a pattern expression. When matching a pattern against a syntax tree node, the return value will contain references to all nodes that were matched by these named subpatterns. This is similar to capture groups in regular expressions. + +For example, given the following pattern + +``` +pattern!{ + // matches character literals + my_pattern: Expr = + Lit(Char(_#val_inner)#val)#val_outer +} +``` + +one could get references to the nodes that matched the subpatterns in the following way: + +``` +... +fn check_expr(expr: &syntax::ast::Expr) { + if let Some(result) = my_pattern(expr) { + result.val_inner // type: &char + result.val // type: &syntax::ast::Lit + result.val_outer // type: &syntax::ast::Expr + } +} +``` + +The types in the `result` struct depend on the pattern. For example, the following pattern + +``` +pattern!{ + // matches arrays of character literals + my_pattern_seq: Expr = + Array( Lit(_)*#foo ) +} +``` + +matches arrays that consist of any number of literal expressions. Because those expressions are named `foo`, the result struct contains a `foo` attribute which is a vector of expressions: + +``` +... +if let Some(result) = my_pattern_seq(expr) { + result.foo // type: Vec<&syntax::ast::Expr> +} +``` + +Another result type occurs when a name is only defined in one branch of an alternation: + +``` +pattern!{ + // matches if expression is a boolean or integer literal + my_pattern_alt: Expr = + Lit( Bool(_#bar) | Int(_) ) +} +``` + +In the pattern above, the `bar` name is only defined if the pattern matches a boolean literal. If it matches an integer literal, the name isn't set. To account fot this, the result struct's `bar` attribute is an option type: + +``` +... +if let Some(result) = my_pattern_alt(expr) { + result.bar // type: Option<&bool> +} +``` + +It's also possible to use a name in multiple alternation branches if they have compatible types: + +``` +pattern!{ + // matches if expression is a boolean or integer literal + my_pattern_mult: Expr = + Lit(_#baz) | Array( Lit(_#baz) ) +} +... +if let Some(result) = my_pattern_mult(expr) { + result.baz // type: &syntax::ast::Lit +} +``` + +Named submatches are a **flat** namespace and this is intended. In the example above, two different sub-structures are assigned to a flat name. I expect that for most lints, a flat namespace is sufficient and easier to work with than a hierarchical one. + +#### Two stages + +Using named subpatterns, users can write lints in two stages. First, a coarse selection of possible matches is produced by the pattern syntax. In the second stage, the named subpattern references can be used to do additional tests like asserting that a node hasn't been created as part of a macro expansion. + +## Implementing clippy lints using patterns + +As a "real-world" example, I re-implemented the `collapsible_if` lint using patterns. The code can be found [here](https://github.com/fkohlgrueber/rust-clippy-pattern/blob/039b07ecccaf96d6aa7504f5126720d2c9cceddd/clippy_lints/src/collapsible_if.rs#L88-L163). The pattern-based version passes all test cases that were written for `collapsible_if`. + + +# Reference-level explanation +[reference-level-explanation]: #reference-level-explanation + +## Overview + +The following diagram shows the dependencies between the main parts of the proposed solution: + +``` + Pattern syntax + | + | parsing / lowering + v + PatternTree + ^ + | + | + IsMatch trait + | + | + +---------------+-----------+---------+ + | | | | + v v v v + syntax::ast rustc::hir syn ... +``` + +The pattern syntax described in the previous section is parsed / lowered into the so-called *PatternTree* data structure that represents a valid syntax tree pattern. Matching a *PatternTree* against an actual syntax tree (e.g. rust ast / hir or the syn ast, ...) is done using the *IsMatch* trait. + +The *PatternTree* and the *IsMatch* trait are introduced in more detail in the following sections. + +## PatternTree + +The core data structure of this RFC is the **PatternTree**. + +It's a data structure similar to rust's AST / HIR, but with the following differences: + +- The PatternTree doesn't contain parsing information like `Span`s +- The PatternTree can represent alternatives, sequences and optionals + +The code below shows a simplified version of the current PatternTree: + +> Note: The current implementation can be found [here](https://github.com/fkohlgrueber/pattern-matching/blob/dfb3bc9fbab69cec7c91e72564a63ebaa2ede638/pattern-match/src/pattern_tree.rs#L50-L96). + + +``` +pub enum Expr { + Lit(Alt), + Array(Seq), + Block_(Alt), + If(Alt, Alt, Opt), + IfLet( + Alt, + Opt, + ), +} + +pub enum Lit { + Char(Alt), + Bool(Alt), + Int(Alt), +} + +pub enum Stmt { + Expr(Alt), + Semi(Alt), +} + +pub enum BlockType { + Block(Seq), +} +``` + +The `Alt`, `Seq` and `Opt` structs look like these: + +> Note: The current implementation can be found [here](https://github.com/fkohlgrueber/pattern-matching/blob/dfb3bc9fbab69cec7c91e72564a63ebaa2ede638/pattern-match/src/matchers.rs#L35-L60). + +``` +pub enum Alt { + Any, + Elmt(Box), + Alt(Box, Box), + Named(Box, ...) +} + +pub enum Opt { + Any, // anything, but not None + Elmt(Box), + None, + Alt(Box, Box), + Named(Box, ...) +} + +pub enum Seq { + Any, + Empty, + Elmt(Box), + Repeat(Box, RepeatRange), + Seq(Box, Box), + Alt(Box, Box), + Named(Box, ...) +} + +pub struct RepeatRange { + pub start: usize, + pub end: Option // exclusive +} +``` + +## Parsing / Lowering + +The input of a `pattern!` macro call is parsed into a `ParseTree` first and then lowered to a `PatternTree`. + +Valid patterns depend on the *PatternTree* definitions. For example, the pattern `Lit(Bool(_)*)` isn't valid because the parameter type of the `Lit` variant of the `Expr` enum is `Any` and therefore doesn't support repetition (`*`). As another example, `Array( Lit(_)* )` is a valid pattern because the parameter of `Array` is of type `Seq` which allows sequences and repetitions. + +> Note: names in the pattern syntax correspond to *PatternTree* enum **variants**. For example, the `Lit` in the pattern above refers to the `Lit` variant of the `Expr` enum (`Expr::Lit`), not the `Lit` enum. + +## The IsMatch Trait + +The pattern syntax and the *PatternTree* are independant of specific syntax tree implementations (rust ast / hir, syn, ...). When looking at the different pattern examples in the previous sections, it can be seen that the patterns don't contain any information specific to a certain syntax tree implementation. In contrast, clippy lints currently match against ast / hir syntax tree nodes and therefore directly depend on their implementation. + +The connection between the *PatternTree* and specific syntax tree implementations is the `IsMatch` trait. It defines how to match *PatternTree* nodes against specific syntax tree nodes. A simplified implementation of the `IsMatch` trait is shown below: + +``` +pub trait IsMatch { + fn is_match(&self, other: &'o O) -> bool; +} +``` + +This trait needs to be implemented on each enum of the *PatternTree* (for the corresponding syntax tree types). For example, the `IsMatch` implementation for matching `ast::LitKind` against the *PatternTree's* `Lit` enum might look like this: + +``` +impl IsMatch for Lit { + fn is_match(&self, other: &ast::LitKind) -> bool { + match (self, other) { + (Lit::Char(i), ast::LitKind::Char(j)) => i.is_match(j), + (Lit::Bool(i), ast::LitKind::Bool(j)) => i.is_match(j), + (Lit::Int(i), ast::LitKind::Int(j, _)) => i.is_match(j), + _ => false, + } + } +} +``` + +All `IsMatch` implementations for matching the current *PatternTree* against `syntax::ast` can be found [here](https://github.com/fkohlgrueber/pattern-matching/blob/dfb3bc9fbab69cec7c91e72564a63ebaa2ede638/pattern-match/src/ast_match.rs). + + +# Drawbacks +[drawbacks]: #drawbacks + +#### Performance + +The pattern matching code is currently not optimized for performance, so it might be slower than hand-written matching code. +Additionally, the two-stage approach (matching against the coarse pattern first and checking for additional properties later) might be slower than the current practice of checking for structure and additional properties in one pass. For example, the following lint + +``` +pattern!{ + pat_if_without_else: Expr = + If( + _, + Block( + Expr( If(_, _, ())#inner ) + | Semi( If(_, _, ())#inner ) + )#then, + () + ) +} +... +fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) { + if let Some(result) = pat_if_without_else(expr) { + if !block_starts_with_comment(cx, result.then) { + ... + } +} +``` + +first matches against the pattern and then checks that the `then` block doesn't start with a comment. Using clippy's current approach, it's possible to check for these conditions earlier: + +``` +fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) { + if_chain! { + if let ast::ExprKind::If(ref check, ref then, None) = expr.node; + if !block_starts_with_comment(cx, then); + if let Some(inner) = expr_block(then); + if let ast::ExprKind::If(ref check_inner, ref content, None) = inner.node; + then { + ... + } + } +} +``` + +Whether or not this causes performance regressions depends on actual patterns. If it turns out to be a problem, the pattern matching algorithms could be extended to allow "early filtering" (see the [Early Filtering](#early-filtering) section in Future Possibilities). + +That being said, I don't see any conceptual limitations regarding pattern matching performance. + +#### Applicability + +Even though I'd expect that a lot of lints can be written using the proposed pattern syntax, it's unlikely that all lints can be expressed using patterns. I suspect that there will still be lints that need to be implemented by writing custom pattern matching code. This would lead to mix within clippy's codebase where some lints are implemented using patterns and others aren't. This inconsistency might be considered a drawback. + + +# Rationale and alternatives +[rationale-and-alternatives]: #rationale-and-alternatives + +Specifying lints using syntax tree patterns has a couple of advantages compared to the current approach of manually writing matching code. First, syntax tree patterns allow users to describe patterns in a simple and expressive way. This makes it easier to write new lints for both novices and experts and also makes reading / modifying existing lints simpler. + +Another advantage is that lints are independent of specific syntax tree implementations (e.g. AST / HIR, ...). When these syntax tree implementations change, only the `IsMatch` trait implementations need to be adapted and existing lints can remain unchanged. This also means that if the `IsMatch` trait implementations were integrated into the compiler, updating the `IsMatch` implementations would be required for the compiler to compile successfully. This could reduce the number of times clippy breaks because of changes in the compiler. Another advantage of the pattern's independence is that converting an `EarlyLintPass` lint into a `LatePassLint` wouldn't require rewriting the whole pattern matching code. In fact, the pattern might work just fine without any adaptions. + + + +## Alternatives + +### Rust-like pattern syntax + +The proposed pattern syntax requires users to know the structure of the `PatternTree` (which is very similar to the AST's / HIR's structure) and also the pattern syntax. An alternative would be to introduce a pattern syntax that is similar to actual Rust syntax (probably like the `quote!` macro). For example, a pattern that matches `if` expressions that have `false` in their condition could look like this: + +``` +if false { + #[*] +} +``` + +#### Problems + +Extending Rust syntax (which is quite complex by itself) with additional syntax needed for specifying patterns (alternations, sequences, repetisions, named submatches, ...) might become difficult to read and really hard to parse properly. + +For example, a pattern that matches a binary operation that has `0` on both sides might look like this: + +``` +0 #[*:BinOpKind] 0 +``` + +Now consider this slightly more complex example: + +``` +1 + 0 #[*:BinOpKind] 0 +``` + +The parser would need to know the precedence of `#[*:BinOpKind]` because it affects the structure of the resulting AST. `1 + 0 + 0` is parsed as `(1 + 0) + 0` while `1 + 0 * 0` is parsed as `1 + (0 * 0)`. Since the pattern could be any `BinOpKind`, the precedence cannot be known in advance. + +Another example of a problem would be named submatches. Take a look at this pattern: + +``` +fn test() { + 1 #foo +} +``` + +Which node is `#foo` referring to? `int`, `ast::Lit`, `ast::Expr`, `ast::Stmt`? Naming subpatterns in a rust-like syntax is difficult because a lot of AST nodes don't have a syntactic element that can be used to put the name tag on. In these situations, the only sensible option would be to assign the name tag to the outermost node (`ast::Stmt` in the example above), because the information of all child nodes can be retrieved through the outermost node. The problem with this then would be that accessing inner nodes (like `ast::Lit`) would again require manual pattern matching. + +In general, Rust syntax contains a lot of code structure implicitly. This structure is reconstructed during parsing (e.g. binary operations are reconstructed using operator precedence and left-to-right) and is one of the reasons why parsing is a complex task. The advantage of this approach is that writing code is simpler for users. + +When writing *syntax tree patterns*, each element of the hierarchy might have alternatives, repetitions, etc.. Respecting that while still allowing human-friendly syntax that contains structure implicitly seems to be really complex, if not impossible. + +Developing such a syntax would also require to maintain a custom parser that is at least as complex as the Rust parser itself. Additionally, future changes in the Rust syntax might be incompatible with such a syntax. + +In summary, I think that developing such a syntax would introduce a lot of complexity to solve a relatively minor problem. + +The issue of users not knowing about the *PatternTree* structure could be solved by a tool that, given a rust program, generates a pattern that matches only this program (similar to the clippy author lint). + +For some simple cases (like the first example above), it might be possible to successfully mix Rust and pattern syntax. This space could be further explored in a future extension. + +# Prior art +[prior-art]: #prior-art + +The pattern syntax is heavily inspired by regular expressions (repetitions, alternatives, sequences, ...). + +From what I've seen until now, other linters also implement lints that directly work on syntax tree data structures, just like clippy does currently. I would therefore consider the pattern syntax to be *new*, but please correct me if I'm wrong. + +# Unresolved questions +[unresolved-questions]: #unresolved-questions + +#### How to handle multiple matches? + +When matching a syntax tree node against a pattern, there are possibly multiple ways in which the pattern can be matched. A simple example of this would be the following pattern: + +``` +pattern!{ + my_pattern: Expr = + Array( _* Lit(_)+#literals) +} +``` + +This pattern matches arrays that end with at least one literal. Now given the array `[x, 1, 2]`, should `1` be matched as part of the `_*` or the `Lit(_)+` part of the pattern? The difference is important because the named submatch `#literals` would contain 1 or 2 elements depending how the pattern is matched. In regular expressions, this problem is solved by matching "greedy" by default and "non-greedy" optionally. + +I haven't looked much into this yet because I don't know how relevant it is for most lints. The current implementation simply returns the first match it finds. + +# Future possibilities +[future-possibilities]: #future-possibilities + +#### Implement rest of Rust Syntax + +The current project only implements a small part of the Rust syntax. In the future, this should incrementally be extended to more syntax to allow implementing more lints. Implementing more of the Rust syntax requires extending the `PatternTree` and `IsMatch` implementations, but should be relatively straight-forward. + +#### Early filtering + +As described in the *Drawbacks/Performance* section, allowing additional checks during the pattern matching might be beneficial. + +The pattern below shows how this could look like: + +``` +pattern!{ + pat_if_without_else: Expr = + If( + _, + Block( + Expr( If(_, _, ())#inner ) + | Semi( If(_, _, ())#inner ) + )#then, + () + ) + where + !in_macro(#then.span); +} +``` + +The difference compared to the currently proposed two-stage filtering is that using early filtering, the condition (`!in_macro(#then.span)` in this case) would be evaluated as soon as the `Block(_)#then` was matched. + +Another idea in this area would be to introduce a syntax for backreferences. They could be used to require that multiple parts of a pattern should match the same value. For example, the `assign_op_pattern` lint that searches for `a = a op b` and recommends changing it to `a op= b` requires that both occurrances of `a` are the same. Using `=#...` as syntax for backreferences, the lint could be implemented like this: + +``` +pattern!{ + assign_op_pattern: Expr = + Assign(_#target, Binary(_, =#target, _) +} +``` + +#### Match descendant + +A lot of lints currently implement custom visitors that check whether any subtree (which might not be a direct descendant) of the current node matches some properties. This cannot be expressed with the proposed pattern syntax. Extending the pattern syntax to allow patterns like "a function that contains at least two return statements" could be a practical addition. + +#### Negation operator for alternatives + +For patterns like "a literal that is not a boolean literal" one currently needs to list all alternatives except the boolean case. Introducing a negation operator that allows to write `Lit(!Bool(_))` might be a good idea. This pattern would be eqivalent to `Lit( Char(_) | Int(_) )` (given that currently only three literal types are implemented). + +#### Functional composition + +Patterns currently don't have any concept of composition. This leads to repetitions within patterns. For example, one of the collapsible-if patterns currently has to be written like this: + +``` +pattern!{ + pat_if_else: Expr = + If( + _, + _, + Block_( + Block( + Expr((If(_, _, _?) | IfLet(_, _?))#else_) | + Semi((If(_, _, _?) | IfLet(_, _?))#else_) + )#block_inner + )#block + ) | + IfLet( + _, + Block_( + Block( + Expr((If(_, _, _?) | IfLet(_, _?))#else_) | + Semi((If(_, _, _?) | IfLet(_, _?))#else_) + )#block_inner + )#block + ) +} +``` + +If patterns supported defining functions of subpatterns, the code could be simplified as follows: + +``` +pattern!{ + fn expr_or_semi(expr: Expr) -> Stmt { + Expr(expr) | Semi(expr) + } + fn if_or_if_let(then: Block, else: Opt) -> Expr { + If(_, then, else) | IfLet(then, else) + } + pat_if_else: Expr = + if_or_if_let( + _, + Block_( + Block( + expr_or_semi( if_or_if_let(_, _?)#else_ ) + )#block_inner + )#block + ) +} +``` + +Additionally, common patterns like `expr_or_semi` could be shared between different lints. + +#### Clippy Pattern Author + +Another improvement could be to create a tool that, given some valid Rust syntax, generates a pattern that matches this syntax exactly. This would make starting to write a pattern easier. A user could take a look at the patterns generated for a couple of Rust code examples and use that information to write a pattern that matches all of them. + +This is similar to clippy's author lint. + +#### Supporting other syntaxes + +Most of the proposed system is language-agnostic. For example, the pattern syntax could also be used to describe patterns for other programming languages. + +In order to support other languages' syntaxes, one would need to implement another `PatternTree` that sufficiently describes the languages' AST and implement `IsMatch` for this `PatternTree` and the languages' AST. + +One aspect of this is that it would even be possible to write lints that work on the pattern syntax itself. For example, when writing the following pattern + + +``` +pattern!{ + my_pattern: Expr = + Array( Lit(Bool(false)) Lit(Bool(false)) ) +} +``` + +a lint that works on the pattern syntax's AST could suggest using this pattern instead: + +``` +pattern!{ + my_pattern: Expr = + Array( Lit(Bool(false)){2} ) +} +``` + +In the future, clippy could use this system to also provide lints for custom syntaxes like those found in macros. + +# Final Note + +This is the first RFC I've ever written and it might be a little rough around the edges. + +I'm looking forward to hearing your feedback and discussing the proposal. From f87953f01a5ba5966327055c935cc21eb2fc1762 Mon Sep 17 00:00:00 2001 From: Ali MJ Al-Nasrawy Date: Wed, 27 Jul 2022 18:34:46 +0300 Subject: [PATCH 002/244] fix universe map in ifcx.instantiate_canonical_* Previously, `infcx.instantiate_canonical_*` maps the root universe in `canonical` into `ty::UniverseIndex::Root`, I think because it assumes it works with a fresh `infcx` but this is not true for the use cases in mir typeck. Now the root universe is mapped into `infcx.universe()`. I catched this accidentally while reviewing the code. I'm not sure if this is the right fix or if it is really a bug! --- compiler/rustc_infer/src/infer/canonical/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_infer/src/infer/canonical/mod.rs b/compiler/rustc_infer/src/infer/canonical/mod.rs index f251d561c6087..270c05a237075 100644 --- a/compiler/rustc_infer/src/infer/canonical/mod.rs +++ b/compiler/rustc_infer/src/infer/canonical/mod.rs @@ -41,7 +41,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> { /// inference variables and applies it to the canonical value. /// Returns both the instantiated result *and* the substitution S. /// - /// This is only meant to be invoked as part of constructing an + /// This can be invoked as part of constructing an /// inference context at the start of a query (see /// `InferCtxtBuilder::enter_with_canonical`). It basically /// brings the canonical value "into scope" within your new infcx. @@ -63,8 +63,8 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> { // in them, so this code has no effect, but it is looking // forward to the day when we *do* want to carry universes // through into queries. - let universes: IndexVec = std::iter::once(ty::UniverseIndex::ROOT) - .chain((0..canonical.max_universe.as_u32()).map(|_| self.create_next_universe())) + let universes: IndexVec = std::iter::once(self.universe()) + .chain((1..=canonical.max_universe.as_u32()).map(|_| self.create_next_universe())) .collect(); let canonical_inference_vars = From 70200ac1907812a0f2abb7e9534e2bc26ae9f103 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Mon, 10 Oct 2022 16:51:36 -0400 Subject: [PATCH 003/244] Update compiler/rustc_infer/src/infer/canonical/mod.rs --- compiler/rustc_infer/src/infer/canonical/mod.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compiler/rustc_infer/src/infer/canonical/mod.rs b/compiler/rustc_infer/src/infer/canonical/mod.rs index 270c05a237075..5b37dda12336d 100644 --- a/compiler/rustc_infer/src/infer/canonical/mod.rs +++ b/compiler/rustc_infer/src/infer/canonical/mod.rs @@ -63,6 +63,9 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> { // in them, so this code has no effect, but it is looking // forward to the day when we *do* want to carry universes // through into queries. + // + // Instantiate the root-universe content into the current universe, + // and create fresh universes for the higher universes. let universes: IndexVec = std::iter::once(self.universe()) .chain((1..=canonical.max_universe.as_u32()).map(|_| self.create_next_universe())) .collect(); From bb04e7e2a2bd0f1a8e0034f6f93bb00673b4c210 Mon Sep 17 00:00:00 2001 From: Nixon Enraght-Moony Date: Fri, 14 Oct 2022 19:57:01 +0100 Subject: [PATCH 004/244] rustdoc-json: Document and Test that args can be patterns. --- src/rustdoc-json-types/lib.rs | 4 ++++ src/test/rustdoc-json/fns/pattern_arg.rs | 7 +++++++ 2 files changed, 11 insertions(+) create mode 100644 src/test/rustdoc-json/fns/pattern_arg.rs diff --git a/src/rustdoc-json-types/lib.rs b/src/rustdoc-json-types/lib.rs index 7379b04ad1677..dbbdeecd99d3e 100644 --- a/src/rustdoc-json-types/lib.rs +++ b/src/rustdoc-json-types/lib.rs @@ -618,6 +618,10 @@ pub struct FunctionPointer { #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct FnDecl { + /// List of argument names and their type. + /// + /// Note that not all names will be valid identifiers, as some of + /// them may be patterns. pub inputs: Vec<(String, Type)>, pub output: Option, pub c_variadic: bool, diff --git a/src/test/rustdoc-json/fns/pattern_arg.rs b/src/test/rustdoc-json/fns/pattern_arg.rs new file mode 100644 index 0000000000000..32b7da0fae4e2 --- /dev/null +++ b/src/test/rustdoc-json/fns/pattern_arg.rs @@ -0,0 +1,7 @@ +// @is "$.index[*][?(@.name=='fst')].inner.decl.inputs[0][0]" '"(x, _)"' +pub fn fst((x, _): (X, Y)) -> X { + x +} + +// @is "$.index[*][?(@.name=='drop_int')].inner.decl.inputs[0][0]" '"_"' +pub fn drop_int(_: i32) {} From 329dc4715bb152c98d4a5d59747604f3e79a2bb6 Mon Sep 17 00:00:00 2001 From: Matthew Ingwersen Date: Fri, 28 Oct 2022 15:48:04 -0400 Subject: [PATCH 005/244] Fix `redundant_closure_for_method_calls` suggestion Certain types must be enclosed in angle brackets and must have generic arguments substituted to create a working suggestion. For example, if `s` has type `&[u8]`, then `|s| s.len()` may be replaced with `<[u8]>::len`. Previously, Clippy erroneously suggested `[T]::len`. --- clippy_lints/src/eta_reduction.rs | 15 ++++++++++++--- tests/ui/eta.fixed | 22 ++++++++++++++++++++++ tests/ui/eta.rs | 22 ++++++++++++++++++++++ tests/ui/eta.stderr | 26 +++++++++++++++++++++++++- 4 files changed, 81 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/eta_reduction.rs b/clippy_lints/src/eta_reduction.rs index 7b9786d7e570f..9588be7364d82 100644 --- a/clippy_lints/src/eta_reduction.rs +++ b/clippy_lints/src/eta_reduction.rs @@ -11,7 +11,7 @@ use rustc_hir::{Closure, Expr, ExprKind, Param, PatKind, Unsafety}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow}; use rustc_middle::ty::binding::BindingMode; -use rustc_middle::ty::{self, Ty, TypeVisitable}; +use rustc_middle::ty::{self, EarlyBinder, SubstsRef, Ty, TypeVisitable}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::symbol::sym; @@ -150,7 +150,7 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction { if check_sig(cx, closure_ty, call_ty); then { span_lint_and_then(cx, REDUNDANT_CLOSURE_FOR_METHOD_CALLS, expr.span, "redundant closure", |diag| { - let name = get_ufcs_type_name(cx, method_def_id); + let name = get_ufcs_type_name(cx, method_def_id, substs); diag.span_suggestion( expr.span, "replace the closure with the method itself", @@ -220,7 +220,7 @@ fn check_sig<'tcx>(cx: &LateContext<'tcx>, closure_ty: Ty<'tcx>, call_ty: Ty<'tc cx.tcx.erase_late_bound_regions(closure_sig) == cx.tcx.erase_late_bound_regions(call_sig) } -fn get_ufcs_type_name(cx: &LateContext<'_>, method_def_id: DefId) -> String { +fn get_ufcs_type_name<'tcx>(cx: &LateContext<'tcx>, method_def_id: DefId, substs: SubstsRef<'tcx>) -> String { let assoc_item = cx.tcx.associated_item(method_def_id); let def_id = assoc_item.container_id(cx.tcx); match assoc_item.container { @@ -229,6 +229,15 @@ fn get_ufcs_type_name(cx: &LateContext<'_>, method_def_id: DefId) -> String { let ty = cx.tcx.type_of(def_id); match ty.kind() { ty::Adt(adt, _) => cx.tcx.def_path_str(adt.did()), + ty::Array(..) + | ty::Dynamic(..) + | ty::Never + | ty::RawPtr(_) + | ty::Ref(..) + | ty::Slice(_) + | ty::Tuple(_) => { + format!("<{}>", EarlyBinder(ty).subst(cx.tcx, substs)) + }, _ => ty.to_string(), } }, diff --git a/tests/ui/eta.fixed b/tests/ui/eta.fixed index a9cc80aaaf623..dc129591eac4e 100644 --- a/tests/ui/eta.fixed +++ b/tests/ui/eta.fixed @@ -316,3 +316,25 @@ pub fn mutable_impl_fn_mut(mut f: impl FnMut(), mut f_used_once: impl FnMut()) - move || takes_fn_mut(&mut f_used_once) } + +impl dyn TestTrait + '_ { + fn method_on_dyn(&self) -> bool { + false + } +} + +// https://github.com/rust-lang/rust-clippy/issues/7746 +fn angle_brackets_and_substs() { + let array_opt: Option<&[u8; 3]> = Some(&[4, 8, 7]); + array_opt.map(<[u8; 3]>::as_slice); + + let slice_opt: Option<&[u8]> = Some(b"slice"); + slice_opt.map(<[u8]>::len); + + let ptr_opt: Option<*const usize> = Some(&487); + ptr_opt.map(<*const usize>::is_null); + + let test_struct = TestStruct { some_ref: &487 }; + let dyn_opt: Option<&dyn TestTrait> = Some(&test_struct); + dyn_opt.map(::method_on_dyn); +} diff --git a/tests/ui/eta.rs b/tests/ui/eta.rs index cc99906ccd667..025fd6a0b7afd 100644 --- a/tests/ui/eta.rs +++ b/tests/ui/eta.rs @@ -316,3 +316,25 @@ pub fn mutable_impl_fn_mut(mut f: impl FnMut(), mut f_used_once: impl FnMut()) - move || takes_fn_mut(|| f_used_once()) } + +impl dyn TestTrait + '_ { + fn method_on_dyn(&self) -> bool { + false + } +} + +// https://github.com/rust-lang/rust-clippy/issues/7746 +fn angle_brackets_and_substs() { + let array_opt: Option<&[u8; 3]> = Some(&[4, 8, 7]); + array_opt.map(|a| a.as_slice()); + + let slice_opt: Option<&[u8]> = Some(b"slice"); + slice_opt.map(|s| s.len()); + + let ptr_opt: Option<*const usize> = Some(&487); + ptr_opt.map(|p| p.is_null()); + + let test_struct = TestStruct { some_ref: &487 }; + let dyn_opt: Option<&dyn TestTrait> = Some(&test_struct); + dyn_opt.map(|d| d.method_on_dyn()); +} diff --git a/tests/ui/eta.stderr b/tests/ui/eta.stderr index 434706b7e258f..a521fb868607a 100644 --- a/tests/ui/eta.stderr +++ b/tests/ui/eta.stderr @@ -134,5 +134,29 @@ error: redundant closure LL | move || takes_fn_mut(|| f_used_once()) | ^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `&mut f_used_once` -error: aborting due to 22 previous errors +error: redundant closure + --> $DIR/eta.rs:329:19 + | +LL | array_opt.map(|a| a.as_slice()); + | ^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `<[u8; 3]>::as_slice` + +error: redundant closure + --> $DIR/eta.rs:332:19 + | +LL | slice_opt.map(|s| s.len()); + | ^^^^^^^^^^^ help: replace the closure with the method itself: `<[u8]>::len` + +error: redundant closure + --> $DIR/eta.rs:335:17 + | +LL | ptr_opt.map(|p| p.is_null()); + | ^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `<*const usize>::is_null` + +error: redundant closure + --> $DIR/eta.rs:339:17 + | +LL | dyn_opt.map(|d| d.method_on_dyn()); + | ^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `::method_on_dyn` + +error: aborting due to 26 previous errors From f27ca5c00a96d2dbd1e2b5dceee918d829891bc1 Mon Sep 17 00:00:00 2001 From: Samuel Moelius Date: Fri, 4 Nov 2022 16:01:44 +0000 Subject: [PATCH 006/244] Fix #9771 (`unnecessary_to_owned` false positive) --- .../src/methods/unnecessary_to_owned.rs | 18 +++++++++--- tests/ui/unnecessary_to_owned.fixed | 29 +++++++++++++++++++ tests/ui/unnecessary_to_owned.rs | 29 +++++++++++++++++++ 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/methods/unnecessary_to_owned.rs b/clippy_lints/src/methods/unnecessary_to_owned.rs index 4b4f2f47b1d92..460bd1cfbd38b 100644 --- a/clippy_lints/src/methods/unnecessary_to_owned.rs +++ b/clippy_lints/src/methods/unnecessary_to_owned.rs @@ -19,7 +19,6 @@ use rustc_middle::ty::{self, ParamTy, PredicateKind, ProjectionPredicate, TraitP use rustc_semver::RustcVersion; use rustc_span::{sym, Symbol}; use rustc_trait_selection::traits::{query::evaluate_obligation::InferCtxtExt as _, Obligation, ObligationCause}; -use std::cmp::max; use super::UNNECESSARY_TO_OWNED; @@ -263,11 +262,22 @@ fn check_other_call_arg<'tcx>( if let Some(as_ref_trait_id) = cx.tcx.get_diagnostic_item(sym::AsRef); if trait_predicate.def_id() == deref_trait_id || trait_predicate.def_id() == as_ref_trait_id; let receiver_ty = cx.typeck_results().expr_ty(receiver); - if can_change_type(cx, maybe_arg, receiver_ty); // We can't add an `&` when the trait is `Deref` because `Target = &T` won't match // `Target = T`. - if n_refs > 0 || is_copy(cx, receiver_ty) || trait_predicate.def_id() != deref_trait_id; - let n_refs = max(n_refs, usize::from(!is_copy(cx, receiver_ty))); + if let Some((n_refs, receiver_ty)) = if n_refs > 0 || is_copy(cx, receiver_ty) { + Some((n_refs, receiver_ty)) + } else if trait_predicate.def_id() != deref_trait_id { + Some((1, cx.tcx.mk_ref( + cx.tcx.lifetimes.re_erased, + ty::TypeAndMut { + ty: receiver_ty, + mutbl: Mutability::Not, + }, + ))) + } else { + None + }; + if can_change_type(cx, maybe_arg, receiver_ty); if let Some(receiver_snippet) = snippet_opt(cx, receiver.span); then { span_lint_and_sugg( diff --git a/tests/ui/unnecessary_to_owned.fixed b/tests/ui/unnecessary_to_owned.fixed index fe09aad06bc84..fadcf7f9c9e86 100644 --- a/tests/ui/unnecessary_to_owned.fixed +++ b/tests/ui/unnecessary_to_owned.fixed @@ -426,3 +426,32 @@ mod issue_9504 { foo(std::path::PathBuf::new().to_string_lossy().to_string()).await; } } + +mod issue_9771a { + #![allow(dead_code)] + + use std::marker::PhantomData; + + pub struct Key, V: ?Sized>(K, PhantomData); + + impl, V: ?Sized> Key { + pub fn new(key: K) -> Key { + Key(key, PhantomData) + } + } + + pub fn pkh(pkh: &[u8]) -> Key, String> { + Key::new([b"pkh-", pkh].concat().to_vec()) + } +} + +mod issue_9771b { + #![allow(dead_code)] + + pub struct Key>(K); + + pub fn from(c: &[u8]) -> Key> { + let v = [c].concat(); + Key(v.to_vec()) + } +} diff --git a/tests/ui/unnecessary_to_owned.rs b/tests/ui/unnecessary_to_owned.rs index 3de6d0903c0f1..fe6864b9e07f9 100644 --- a/tests/ui/unnecessary_to_owned.rs +++ b/tests/ui/unnecessary_to_owned.rs @@ -426,3 +426,32 @@ mod issue_9504 { foo(std::path::PathBuf::new().to_string_lossy().to_string()).await; } } + +mod issue_9771a { + #![allow(dead_code)] + + use std::marker::PhantomData; + + pub struct Key, V: ?Sized>(K, PhantomData); + + impl, V: ?Sized> Key { + pub fn new(key: K) -> Key { + Key(key, PhantomData) + } + } + + pub fn pkh(pkh: &[u8]) -> Key, String> { + Key::new([b"pkh-", pkh].concat().to_vec()) + } +} + +mod issue_9771b { + #![allow(dead_code)] + + pub struct Key>(K); + + pub fn from(c: &[u8]) -> Key> { + let v = [c].concat(); + Key(v.to_vec()) + } +} From d6f88b544d3ee219c20ca487b309e3e76c7d0cfd Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Wed, 16 Nov 2022 13:14:09 -0800 Subject: [PATCH 007/244] Expand a style-guide principle: readability in plain text --- src/doc/style-guide/src/principles.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/doc/style-guide/src/principles.md b/src/doc/style-guide/src/principles.md index b02b3c0471f28..21668973194ed 100644 --- a/src/doc/style-guide/src/principles.md +++ b/src/doc/style-guide/src/principles.md @@ -8,7 +8,9 @@ following principles (in rough priority order): - avoiding misleading formatting - accessibility - readable and editable by users using the the widest variety of hardware, including non-visual accessibility interfaces - - readability of code when quoted in rustc error messages + - readability of code in contexts without syntax highlighting or IDE + assistance, such as rustc error messages, diffs, grep, and other + plain-text contexts * aesthetics - sense of 'beauty' From 9ed297769a03eca2c7782272ac20ce61fc225c4f Mon Sep 17 00:00:00 2001 From: David Koloski Date: Wed, 16 Nov 2022 21:23:28 -0500 Subject: [PATCH 008/244] [fuchsia] Document more settings for building Rust --- src/ci/docker/scripts/fuchsia-test-runner.py | 0 src/doc/rustc/src/platform-support/fuchsia.md | 83 ++++++++++++++----- 2 files changed, 60 insertions(+), 23 deletions(-) mode change 100644 => 100755 src/ci/docker/scripts/fuchsia-test-runner.py diff --git a/src/ci/docker/scripts/fuchsia-test-runner.py b/src/ci/docker/scripts/fuchsia-test-runner.py old mode 100644 new mode 100755 diff --git a/src/doc/rustc/src/platform-support/fuchsia.md b/src/doc/rustc/src/platform-support/fuchsia.md index 62cad19d0ec33..66e7f9a32970c 100644 --- a/src/doc/rustc/src/platform-support/fuchsia.md +++ b/src/doc/rustc/src/platform-support/fuchsia.md @@ -189,17 +189,45 @@ Fuchsia as well. A recent version (14+) of clang should be sufficient to compile Rust for Fuchsia. x86-64 and AArch64 Fuchsia targets can be enabled using the following -configuration. - -In `config.toml`, add: +configuration in `config.toml`: ```toml [build] target = ["", "aarch64-fuchsia", "x86_64-fuchsia"] + +[rust] +lld = true + +[target.x86_64-fuchsia] +cc = "clang" +cxx = "clang++" + +[target.aarch64-fuchsia] +cc = "clang" +cxx = "clang++" +``` + +Though not strictly required, you may also want to use `clang` for your host +target as well: + +```toml +[target.] +cc = "clang" +cxx = "clang++" +``` + +By default, the Rust compiler installs itself to `/usr/local` on most UNIX +systems. You may want to install it to another location (e.g. a local `install` +directory) by setting a custom prefix in `config.toml`: + +```toml +[install] +# Make sure to use the absolute path to your install directory +prefix = "/install" ``` -Additionally, the following environment variables must be configured (for -example, using a script like `config-env.sh`): +Next, the following environment variables must be configured. For example, using +a script we name `config-env.sh`: ```sh # Configure this environment variable to be the path to the downloaded SDK @@ -215,8 +243,11 @@ export LDFLAGS_x86_64_fuchsia="--target=x86_64-fuchsia --sysroot=${SDK_PATH}/arc export CARGO_TARGET_X86_64_FUCHSIA_RUSTFLAGS="-C link-arg=--sysroot=${SDK_PATH}/arch/x64/sysroot -Lnative=${SDK_PATH}/arch/x64/sysroot/lib -Lnative=${SDK_PATH}/arch/x64/lib" ``` -These can be run together in a shell environment by executing -`(source config-env.sh && ./x.py install)`. +Finally, the Rust compiler can be built and installed: + +```sh +(source config-env.sh && ./x.py install) +``` Once `rustc` is installed, we can create a new working directory to work from, `hello_fuchsia` along with `hello_fuchsia/src`: @@ -641,31 +672,38 @@ available on the [Fuchsia devsite]. ### Running the compiler test suite -Pre-requisites for running the Rust test suite on Fuchsia are: -1. Checkout of Rust source. -1. Setup of `config-env.sh` and `config.toml` from "[Targeting Fuchsia with a compiler built from source](#targeting-fuchsia-with-a-compiler-built-from-source)". -1. Download of the Fuchsia SDK. Minimum supported SDK version is [9.20220726.1.1](https://chrome-infra-packages.appspot.com/p/fuchsia/sdk/core/linux-amd64/+/version:9.20220726.1.1) +The commands in this section assume that they are being run from inside your +local Rust source checkout: + +```sh +cd ${RUST_SRC_PATH} +``` + +To run the Rust test suite on an emulated Fuchsia device, you must install the +Rust compiler locally. See "[Targeting Fuchsia with a compiler built from source](#targeting-fuchsia-with-a-compiler-built-from-source)" +for the steps to build locally. -Interfacing with the Fuchsia emulator is handled by our test runner script located -at `${RUST_SRC_PATH}/src/ci/docker/scripts/fuchsia-test-runner.py`. +You'll also need to download a copy of the Fuchsia SDK. The current minimum +supported SDK version is [9.20220726.1.1](https://chrome-infra-packages.appspot.com/p/fuchsia/sdk/core/linux-amd64/+/version:9.20220726.1.1). -We start by activating our Fuchsia test environment. From a terminal: +Fuchsia's test runner interacts with the Fuchsia emulator and is located at +`src/ci/docker/scripts/fuchsia-test-runner.py`. We can use it to start our +test environment with: -**Issue command from ${RUST_SRC_PATH}** ```sh src/ci/docker/scripts/fuchsia-test-runner.py start - --rust . + --rust ${RUST_SRC_PATH}/install --sdk ${SDK_PATH} --target-arch {x64,arm64} ``` -Next, for ease of commands, we copy `config-env.sh` and `config.toml` into our Rust source -code path, `${RUST_SRC_PATH}`. +Where `${RUST_SRC_PATH}/install` is the `prefix` set in `config.toml` and +`${SDK_PATH}` is the path to the downloaded and unzipped SDK. -From there, we utilize `x.py` to run our tests, using the test runner script to -run the tests on our emulator. To run the full `src/test/ui` test suite: +Once our environment is started, we can run our tests using `x.py` as usual. The +test runner script will run the compiled tests on an emulated Fuchsia device. To +run the full `src/test/ui` test suite: -**Run from ${RUST_SRC_PATH}** ```sh ( \ source config-env.sh && \ @@ -689,9 +727,8 @@ run the tests on our emulator. To run the full `src/test/ui` test suite: *Note: The test suite cannot be run in parallel at the moment, so `x.py` must be run with `--jobs 1` to ensure only one test runs at a time.* -When finished, stop the test environment: +When finished, the test runner can be used to stop the test environment: -**Issue command from ${RUST_SRC_PATH}** ```sh src/ci/docker/scripts/fuchsia-test-runner.py stop ``` From 5dd073e7e1794bd5267051a3b3320139c98022c3 Mon Sep 17 00:00:00 2001 From: Florian Bartels Date: Thu, 17 Nov 2022 09:24:19 +0100 Subject: [PATCH 009/244] Encourage developers not to use periods in target names They can cause issues in e.g. cargo. --- src/doc/rustc/src/target-tier-policy.md | 2 ++ src/tools/tier-check/src/main.rs | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/doc/rustc/src/target-tier-policy.md b/src/doc/rustc/src/target-tier-policy.md index df9131ce84afd..95932db14e1f6 100644 --- a/src/doc/rustc/src/target-tier-policy.md +++ b/src/doc/rustc/src/target-tier-policy.md @@ -158,6 +158,8 @@ approved by the appropriate team for that shared code before acceptance. the name of the target makes people extremely likely to form incorrect beliefs about what it targets, the name should be changed or augmented to disambiguate it. + - If possible, use only letters, numbers, dashes and underscores for the name. + Periods (`.`) are known to cause issues in Cargo. - Tier 3 targets may have unusual requirements to build or use, but must not create legal issues or impose onerous legal terms for the Rust project or for Rust developers or users. diff --git a/src/tools/tier-check/src/main.rs b/src/tools/tier-check/src/main.rs index a41e2d6e3aa45..c74d37c61e887 100644 --- a/src/tools/tier-check/src/main.rs +++ b/src/tools/tier-check/src/main.rs @@ -44,7 +44,23 @@ fn main() { target, filename, src ); } - if !missing.is_empty() || !extra.is_empty() { + // Check target names for unwanted characters like `.` that can cause problems e.g. in Cargo. + // See also Tier 3 target policy. + // If desired, target names can ignore this check. + let ignore_target_names = + vec!["thumbv8m.base-none-eabi", "thumbv8m.main-none-eabi", "thumbv8m.main-none-eabihf"]; + let mut invalid_target_name_found = false; + for target in &target_list { + if !ignore_target_names.contains(target) + && !target.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_') + { + invalid_target_name_found = true; + eprintln!( + "error: Target name `{target}` contains other characters than ASCII alphanumeric (a-z, A-Z, 0-9), dash (-) or underscore (_)." + ); + } + } + if !missing.is_empty() || !extra.is_empty() || invalid_target_name_found { std::process::exit(1); } } From 9c3555d5c2dc3d1e5025c35db3873c2776054185 Mon Sep 17 00:00:00 2001 From: Florian Bartels Date: Thu, 17 Nov 2022 11:25:28 +0100 Subject: [PATCH 010/244] Remove periods from QNX/nto target names --- compiler/rustc_target/src/spec/mod.rs | 4 ++-- src/doc/rustc/src/platform-support.md | 4 ++-- src/doc/rustc/src/platform-support/nto-qnx.md | 18 +++++++++--------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 664592b02a124..9afbeadb1abf9 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -1244,8 +1244,8 @@ supported_targets! { ("mips64-openwrt-linux-musl", mips64_openwrt_linux_musl), - ("aarch64-unknown-nto-qnx7.1.0", aarch64_unknown_nto_qnx_710), - ("x86_64-pc-nto-qnx7.1.0", x86_64_pc_nto_qnx710), + ("aarch64-unknown-nto-qnx710", aarch64_unknown_nto_qnx_710), + ("x86_64-pc-nto-qnx710", x86_64_pc_nto_qnx710), } /// Cow-Vec-Str: Cow<'static, [Cow<'static, str>]> diff --git a/src/doc/rustc/src/platform-support.md b/src/doc/rustc/src/platform-support.md index 28929acb9b48d..09d1dac7c58b5 100644 --- a/src/doc/rustc/src/platform-support.md +++ b/src/doc/rustc/src/platform-support.md @@ -214,7 +214,7 @@ target | std | host | notes [`aarch64-kmc-solid_asp3`](platform-support/kmc-solid.md) | ✓ | | ARM64 SOLID with TOPPERS/ASP3 [`aarch64-nintendo-switch-freestanding`](platform-support/aarch64-nintendo-switch-freestanding.md) | * | | ARM64 Nintendo Switch, Horizon [`aarch64-pc-windows-gnullvm`](platform-support/pc-windows-gnullvm.md) | ✓ | ✓ | -[`aarch64-unknown-nto-qnx7.1.0`](platform-support/nto-qnx.md) | ? | | ARM64 QNX Neutrino 7.1 RTOS | +[`aarch64-unknown-nto-qnx710`](platform-support/nto-qnx.md) | ? | | ARM64 QNX Neutrino 7.1 RTOS | `aarch64-unknown-freebsd` | ✓ | ✓ | ARM64 FreeBSD `aarch64-unknown-hermit` | ✓ | | ARM64 HermitCore `aarch64-unknown-linux-gnu_ilp32` | ✓ | ✓ | ARM64 Linux (ILP32 ABI) @@ -304,7 +304,7 @@ target | std | host | notes `x86_64-apple-ios-macabi` | ✓ | | Apple Catalyst on x86_64 `x86_64-apple-tvos` | * | | x86 64-bit tvOS [`x86_64-apple-watchos-sim`](platform-support/apple-watchos.md) | ✓ | | x86 64-bit Apple WatchOS simulator -[`x86_64-pc-nto-qnx7.1.0`](platform-support/nto-qnx.md) | ? | | x86 64-bit QNX Neutrino 7.1 RTOS | +[`x86_64-pc-nto-qnx710`](platform-support/nto-qnx.md) | ? | | x86 64-bit QNX Neutrino 7.1 RTOS | [`x86_64-pc-windows-gnullvm`](platform-support/pc-windows-gnullvm.md) | ✓ | ✓ | `x86_64-pc-windows-msvc` | * | | 64-bit Windows XP support `x86_64-sun-solaris` | ? | | Deprecated target for 64-bit Solaris 10/11, illumos diff --git a/src/doc/rustc/src/platform-support/nto-qnx.md b/src/doc/rustc/src/platform-support/nto-qnx.md index 2f6ea94d113a9..37d0c31976c77 100644 --- a/src/doc/rustc/src/platform-support/nto-qnx.md +++ b/src/doc/rustc/src/platform-support/nto-qnx.md @@ -93,15 +93,15 @@ Run the following: ```bash env \ - CC_aarch64-unknown-nto-qnx7.1.0="qcc" \ - CFLAGS_aarch64-unknown-nto-qnx7.1.0="-Vgcc_ntoaarch64le_cxx" \ - CXX_aarch64-unknown-nto-qnx7.1.0="qcc" \ - AR_aarch64_unknown_nto_qnx7.1.0="ntoaarch64-ar" \ - CC_x86_64-pc-nto-qnx7.1.0="qcc" \ - CFLAGS_x86_64-pc-nto-qnx7.1.0="-Vgcc_ntox86_64_cxx" \ - CXX_x86_64-pc-nto-qnx7.1.0="qcc" \ - AR_x86_64_pc_nto_qnx7.1.0="ntox86_64-ar" \ - ./x.py build --target aarch64-unknown-nto-qnx7.1.0 --target x86_64-pc-nto-qnx7.1.0 --target x86_64-unknown-linux-gnu rustc library/core library/alloc/ + CC_aarch64-unknown-nto-qnx710="qcc" \ + CFLAGS_aarch64-unknown-nto-qnx710="-Vgcc_ntoaarch64le_cxx" \ + CXX_aarch64-unknown-nto-qnx710="qcc" \ + AR_aarch64_unknown_nto_qnx710="ntoaarch64-ar" \ + CC_x86_64-pc-nto-qnx710="qcc" \ + CFLAGS_x86_64-pc-nto-qnx710="-Vgcc_ntox86_64_cxx" \ + CXX_x86_64-pc-nto-qnx710="qcc" \ + AR_x86_64_pc_nto_qnx710="ntox86_64-ar" \ + ./x.py build --target aarch64-unknown-nto-qnx710 --target x86_64-pc-nto-qnx710 --target x86_64-unknown-linux-gnu rustc library/core library/alloc/ ``` ## Building Rust programs From a09143866f7359826a4b7cd7125d1a7c4cff981f Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 19 Nov 2022 03:28:56 +0000 Subject: [PATCH 011/244] drive-by: PolyExistentialPredicate --- clippy_lints/src/ptr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/ptr.rs b/clippy_lints/src/ptr.rs index c8c6f32c6c989..180d534636e0f 100644 --- a/clippy_lints/src/ptr.rs +++ b/clippy_lints/src/ptr.rs @@ -687,7 +687,7 @@ fn check_ptr_arg_usage<'tcx>(cx: &LateContext<'tcx>, body: &'tcx Body<'_>, args: fn matches_preds<'tcx>( cx: &LateContext<'tcx>, ty: Ty<'tcx>, - preds: &'tcx [Binder<'tcx, ExistentialPredicate<'tcx>>], + preds: &'tcx [ty::PolyExistentialPredicate<'tcx>], ) -> bool { let infcx = cx.tcx.infer_ctxt().build(); preds.iter().all(|&p| match cx.tcx.erase_late_bound_regions(p) { From 6ee0dd97e3c5b353f4798b43bb956b9c9ccb5d64 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sat, 19 Nov 2022 21:27:57 +0100 Subject: [PATCH 012/244] Add unstable `type_ascribe` macro This macro serves as a placeholder for future type ascription syntax to make sure that the semantic implementation keeps working. --- compiler/rustc_builtin_macros/src/lib.rs | 2 ++ .../rustc_builtin_macros/src/type_ascribe.rs | 35 +++++++++++++++++++ compiler/rustc_span/src/symbol.rs | 1 + library/core/src/macros/mod.rs | 23 ++++++++++++ library/core/src/prelude/v1.rs | 7 ++++ library/std/src/prelude/v1.rs | 8 +++++ 6 files changed, 76 insertions(+) create mode 100644 compiler/rustc_builtin_macros/src/type_ascribe.rs diff --git a/compiler/rustc_builtin_macros/src/lib.rs b/compiler/rustc_builtin_macros/src/lib.rs index 1cbbfb432647c..75cfac7238485 100644 --- a/compiler/rustc_builtin_macros/src/lib.rs +++ b/compiler/rustc_builtin_macros/src/lib.rs @@ -45,6 +45,7 @@ mod log_syntax; mod source_util; mod test; mod trace_macros; +mod type_ascribe; mod util; pub mod asm; @@ -92,6 +93,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) { unreachable: edition_panic::expand_unreachable, stringify: source_util::expand_stringify, trace_macros: trace_macros::expand_trace_macros, + type_ascribe: type_ascribe::expand_type_ascribe, } register_attr! { diff --git a/compiler/rustc_builtin_macros/src/type_ascribe.rs b/compiler/rustc_builtin_macros/src/type_ascribe.rs new file mode 100644 index 0000000000000..72b85af148638 --- /dev/null +++ b/compiler/rustc_builtin_macros/src/type_ascribe.rs @@ -0,0 +1,35 @@ +use rustc_ast::ptr::P; +use rustc_ast::tokenstream::TokenStream; +use rustc_ast::{token, Expr, ExprKind, Ty}; +use rustc_errors::PResult; +use rustc_expand::base::{self, DummyResult, ExtCtxt, MacEager}; +use rustc_span::Span; + +pub fn expand_type_ascribe( + cx: &mut ExtCtxt<'_>, + span: Span, + tts: TokenStream, +) -> Box { + let (expr, ty) = match parse_ascribe(cx, tts) { + Ok(parsed) => parsed, + Err(mut err) => { + err.emit(); + return DummyResult::any(span); + } + }; + + let asc_expr = cx.expr(span, ExprKind::Type(expr, ty)); + + return MacEager::expr(asc_expr); +} + +fn parse_ascribe<'a>(cx: &mut ExtCtxt<'a>, stream: TokenStream) -> PResult<'a, (P, P)> { + let mut parser = cx.new_parser_from_tts(stream); + + let expr = parser.parse_expr()?; + parser.expect(&token::Comma)?; + + let ty = parser.parse_ty()?; + + Ok((expr, ty)) +} diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 199b2d32b9d4c..5bf11b1a3f432 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1485,6 +1485,7 @@ symbols! { ty, type_alias_enum_variants, type_alias_impl_trait, + type_ascribe, type_ascription, type_changing_struct_update, type_id, diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs index c20ca69a1c6f1..0cd9426209fd2 100644 --- a/library/core/src/macros/mod.rs +++ b/library/core/src/macros/mod.rs @@ -1546,6 +1546,29 @@ pub(crate) mod builtin { /* compiler built-in */ } + /// Unstable placeholder for type ascription. + #[rustc_builtin_macro] + #[unstable( + feature = "type_ascription", + issue = "23416", + reason = "placeholder syntax for type ascription" + )] + #[cfg(not(bootstrap))] + pub macro type_ascribe($expr:expr, $ty:ty) { + /* compiler built-in */ + } + + /// Unstable placeholder for type ascription. + #[unstable( + feature = "type_ascription", + issue = "23416", + reason = "placeholder syntax for type ascription" + )] + #[cfg(bootstrap)] + pub macro type_ascribe($expr:expr, $ty:ty) { + $expr: $ty + } + /// Unstable implementation detail of the `rustc` compiler, do not use. #[rustc_builtin_macro] #[stable(feature = "rust1", since = "1.0.0")] diff --git a/library/core/src/prelude/v1.rs b/library/core/src/prelude/v1.rs index d3d255a802d7f..a28d14b14a8e6 100644 --- a/library/core/src/prelude/v1.rs +++ b/library/core/src/prelude/v1.rs @@ -98,3 +98,10 @@ pub use crate::macros::builtin::cfg_accessible; reason = "`cfg_eval` is a recently implemented feature" )] pub use crate::macros::builtin::cfg_eval; + +#[unstable( + feature = "type_ascription", + issue = "23416", + reason = "placeholder syntax for type ascription" +)] +pub use crate::macros::builtin::type_ascribe; diff --git a/library/std/src/prelude/v1.rs b/library/std/src/prelude/v1.rs index d5ac16e6b94e2..4ab4229598ecc 100644 --- a/library/std/src/prelude/v1.rs +++ b/library/std/src/prelude/v1.rs @@ -85,6 +85,14 @@ pub use core::prelude::v1::cfg_accessible; )] pub use core::prelude::v1::cfg_eval; +// Do not `doc(no_inline)` either. +#[unstable( + feature = "type_ascription", + issue = "23416", + reason = "placeholder syntax for type ascription" +)] +pub use core::prelude::v1::type_ascribe; + // The file so far is equivalent to src/libcore/prelude/v1.rs, // and below to src/liballoc/prelude.rs. // Those files are duplicated rather than using glob imports From 2c7d32b4f487d5712d8520d97434aa9616ad7267 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sat, 19 Nov 2022 22:11:00 +0100 Subject: [PATCH 013/244] Use `type_ascribe!` in many UI tests Use it in all UI tests that are about the semantics and not the syntax of type ascription. --- src/test/ui/associated-consts/issue-93835.rs | 8 +- .../ui/associated-consts/issue-93835.stderr | 65 +++++--------- src/test/ui/closures/issue-90871.rs | 4 +- src/test/ui/closures/issue-90871.stderr | 23 +++-- .../coerce-expect-unsized-ascribed.rs | 32 +++---- .../coerce-expect-unsized-ascribed.stderr | 90 +++++++++---------- .../const_in_pattern/accept_structural.rs | 2 +- .../const_in_pattern/reject_non_structural.rs | 2 +- src/test/ui/enum/issue-67945-2.rs | 2 +- src/test/ui/enum/issue-67945-2.stderr | 6 +- .../lint/unused/issue-88519-unused-paren.rs | 15 +--- src/test/ui/mir/mir_ascription_coercion.rs | 2 +- .../issue-57731-ascibed-coupled-types.rs | 6 +- .../issue-57731-ascibed-coupled-types.stderr | 4 +- .../type_ascription_static_lifetime.rs | 2 +- .../type_ascription_static_lifetime.stderr | 10 +-- src/test/ui/raw-ref-op/raw-ref-temp-deref.rs | 6 +- src/test/ui/raw-ref-op/raw-ref-temp.rs | 40 ++++----- src/test/ui/raw-ref-op/raw-ref-temp.stderr | 16 ++-- src/test/ui/reachable/expr_type.rs | 2 +- src/test/ui/reachable/expr_type.stderr | 8 +- src/test/ui/type/type-ascription-soundness.rs | 8 +- .../ui/type/type-ascription-soundness.stderr | 24 ++--- src/test/ui/type/type-ascription.rs | 20 ++--- src/test/ui/typeck/issue-91267.rs | 4 +- src/test/ui/typeck/issue-91267.stderr | 21 ++--- 26 files changed, 198 insertions(+), 224 deletions(-) diff --git a/src/test/ui/associated-consts/issue-93835.rs b/src/test/ui/associated-consts/issue-93835.rs index 5c7b065983e19..b2a437fcbfb85 100644 --- a/src/test/ui/associated-consts/issue-93835.rs +++ b/src/test/ui/associated-consts/issue-93835.rs @@ -1,9 +1,11 @@ +#![feature(type_ascription)] + fn e() { - p:a> - //~^ ERROR comparison operators + type_ascribe!(p, a>); + //~^ ERROR cannot find type `a` in this scope //~| ERROR cannot find value //~| ERROR associated const equality - //~| ERROR associated const equality + //~| ERROR cannot find trait `p` in this scope //~| ERROR associated type bounds } diff --git a/src/test/ui/associated-consts/issue-93835.stderr b/src/test/ui/associated-consts/issue-93835.stderr index 0406a16a39732..be0573a1301af 100644 --- a/src/test/ui/associated-consts/issue-93835.stderr +++ b/src/test/ui/associated-consts/issue-93835.stderr @@ -1,65 +1,40 @@ -error: comparison operators cannot be chained - --> $DIR/issue-93835.rs:2:8 - | -LL | fn e() { - | - while parsing this struct -LL | p:a> - | ^ ^ - | - = help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments - = help: or use `(...)` if you meant to specify fn arguments - error[E0425]: cannot find value `p` in this scope - --> $DIR/issue-93835.rs:2:5 - | -LL | p:a> - | ^ not found in this scope - | -help: you might have meant to write a `struct` literal - | -LL ~ fn e() { SomeStruct { -LL | p:a> - ... -LL | -LL ~ }} + --> $DIR/issue-93835.rs:4:19 | -help: maybe you meant to write a path separator here - | -LL | p::a> - | ~~ -help: maybe you meant to write an assignment here - | -LL | let p:a> - | ~~~~~ +LL | type_ascribe!(p, a>); + | ^ not found in this scope -error[E0658]: associated const equality is incomplete - --> $DIR/issue-93835.rs:2:13 +error[E0412]: cannot find type `a` in this scope + --> $DIR/issue-93835.rs:4:22 | -LL | p:a> - | ^^^ +LL | type_ascribe!(p, a>); + | ^ not found in this scope + +error[E0405]: cannot find trait `p` in this scope + --> $DIR/issue-93835.rs:4:26 | - = note: see issue #92827 for more information - = help: add `#![feature(associated_const_equality)]` to the crate attributes to enable +LL | type_ascribe!(p, a>); + | ^ not found in this scope error[E0658]: associated const equality is incomplete - --> $DIR/issue-93835.rs:2:13 + --> $DIR/issue-93835.rs:4:28 | -LL | p:a> - | ^^^ +LL | type_ascribe!(p, a>); + | ^^^ | = note: see issue #92827 for more information = help: add `#![feature(associated_const_equality)]` to the crate attributes to enable error[E0658]: associated type bounds are unstable - --> $DIR/issue-93835.rs:2:9 + --> $DIR/issue-93835.rs:4:24 | -LL | p:a> - | ^^^^^^^^ +LL | type_ascribe!(p, a>); + | ^^^^^^^^ | = note: see issue #52662 for more information = help: add `#![feature(associated_type_bounds)]` to the crate attributes to enable error: aborting due to 5 previous errors -Some errors have detailed explanations: E0425, E0658. -For more information about an error, try `rustc --explain E0425`. +Some errors have detailed explanations: E0405, E0412, E0425, E0658. +For more information about an error, try `rustc --explain E0405`. diff --git a/src/test/ui/closures/issue-90871.rs b/src/test/ui/closures/issue-90871.rs index 9c70bbc85acfa..7ce061cd388c9 100644 --- a/src/test/ui/closures/issue-90871.rs +++ b/src/test/ui/closures/issue-90871.rs @@ -1,5 +1,7 @@ +#![feature(type_ascription)] + fn main() { - 2: n([u8; || 1]) + type_ascribe!(2, n([u8; || 1])) //~^ ERROR cannot find type `n` in this scope //~| ERROR mismatched types } diff --git a/src/test/ui/closures/issue-90871.stderr b/src/test/ui/closures/issue-90871.stderr index 1e102cc9805bd..a482750fbd016 100644 --- a/src/test/ui/closures/issue-90871.stderr +++ b/src/test/ui/closures/issue-90871.stderr @@ -1,21 +1,26 @@ error[E0412]: cannot find type `n` in this scope - --> $DIR/issue-90871.rs:2:8 + --> $DIR/issue-90871.rs:4:22 | -LL | 2: n([u8; || 1]) - | ^ expecting a type here because of type ascription +LL | type_ascribe!(2, n([u8; || 1])) + | ^ help: a trait with a similar name exists: `Fn` + | + ::: $SRC_DIR/core/src/ops/function.rs:LL:COL + | +LL | pub trait Fn: FnMut { + | -------------------------------------- similarly named trait `Fn` defined here error[E0308]: mismatched types - --> $DIR/issue-90871.rs:2:15 + --> $DIR/issue-90871.rs:4:29 | -LL | 2: n([u8; || 1]) - | ^^^^ expected `usize`, found closure +LL | type_ascribe!(2, n([u8; || 1])) + | ^^^^ expected `usize`, found closure | = note: expected type `usize` - found closure `[closure@$DIR/issue-90871.rs:2:15: 2:17]` + found closure `[closure@$DIR/issue-90871.rs:4:29: 4:31]` help: use parentheses to call this closure | -LL | 2: n([u8; (|| 1)()]) - | + +++ +LL | type_ascribe!(2, n([u8; (|| 1)()])) + | + +++ error: aborting due to 2 previous errors diff --git a/src/test/ui/coercion/coerce-expect-unsized-ascribed.rs b/src/test/ui/coercion/coerce-expect-unsized-ascribed.rs index c139e823c2aef..d7b11317ad5a8 100644 --- a/src/test/ui/coercion/coerce-expect-unsized-ascribed.rs +++ b/src/test/ui/coercion/coerce-expect-unsized-ascribed.rs @@ -6,27 +6,27 @@ use std::fmt::Debug; pub fn main() { - let _ = box { [1, 2, 3] }: Box<[i32]>; //~ ERROR mismatched types - let _ = box if true { [1, 2, 3] } else { [1, 3, 4] }: Box<[i32]>; //~ ERROR mismatched types - let _ = box match true { true => [1, 2, 3], false => [1, 3, 4] }: Box<[i32]>; + let _ = type_ascribe!(box { [1, 2, 3] }, Box<[i32]>); //~ ERROR mismatched types + let _ = type_ascribe!(box if true { [1, 2, 3] } else { [1, 3, 4] }, Box<[i32]>); //~ ERROR mismatched types + let _ = type_ascribe!(box match true { true => [1, 2, 3], false => [1, 3, 4] }, Box<[i32]>); //~^ ERROR mismatched types - let _ = box { |x| (x as u8) }: Box _>; //~ ERROR mismatched types - let _ = box if true { false } else { true }: Box; //~ ERROR mismatched types - let _ = box match true { true => 'a', false => 'b' }: Box; //~ ERROR mismatched types + let _ = type_ascribe!(box { |x| (x as u8) }, Box _>); //~ ERROR mismatched types + let _ = type_ascribe!(box if true { false } else { true }, Box); //~ ERROR mismatched types + let _ = type_ascribe!(box match true { true => 'a', false => 'b' }, Box); //~ ERROR mismatched types - let _ = &{ [1, 2, 3] }: &[i32]; //~ ERROR mismatched types - let _ = &if true { [1, 2, 3] } else { [1, 3, 4] }: &[i32]; //~ ERROR mismatched types - let _ = &match true { true => [1, 2, 3], false => [1, 3, 4] }: &[i32]; + let _ = type_ascribe!(&{ [1, 2, 3] }, &[i32]); //~ ERROR mismatched types + let _ = type_ascribe!(&if true { [1, 2, 3] } else { [1, 3, 4] }, &[i32]); //~ ERROR mismatched types + let _ = type_ascribe!(&match true { true => [1, 2, 3], false => [1, 3, 4] }, &[i32]); //~^ ERROR mismatched types - let _ = &{ |x| (x as u8) }: &dyn Fn(i32) -> _; //~ ERROR mismatched types - let _ = &if true { false } else { true }: &dyn Debug; //~ ERROR mismatched types - let _ = &match true { true => 'a', false => 'b' }: &dyn Debug; //~ ERROR mismatched types + let _ = type_ascribe!(&{ |x| (x as u8) }, &dyn Fn(i32) -> _); //~ ERROR mismatched types + let _ = type_ascribe!(&if true { false } else { true }, &dyn Debug); //~ ERROR mismatched types + let _ = type_ascribe!(&match true { true => 'a', false => 'b' }, &dyn Debug); //~ ERROR mismatched types - let _ = Box::new([1, 2, 3]): Box<[i32]>; //~ ERROR mismatched types - let _ = Box::new(|x| (x as u8)): Box _>; //~ ERROR mismatched types + let _ = type_ascribe!(Box::new([1, 2, 3]), Box<[i32]>); //~ ERROR mismatched types + let _ = type_ascribe!(Box::new(|x| (x as u8)), Box _>); //~ ERROR mismatched types - let _ = vec![ + let _ = type_ascribe!(vec![ Box::new(|x| (x as u8)), box |x| (x as i16 as u8), - ]: Vec _>>; + ], Vec _>>); } diff --git a/src/test/ui/coercion/coerce-expect-unsized-ascribed.stderr b/src/test/ui/coercion/coerce-expect-unsized-ascribed.stderr index 9d614e610ad86..44968244c4d05 100644 --- a/src/test/ui/coercion/coerce-expect-unsized-ascribed.stderr +++ b/src/test/ui/coercion/coerce-expect-unsized-ascribed.stderr @@ -1,128 +1,128 @@ error[E0308]: mismatched types - --> $DIR/coerce-expect-unsized-ascribed.rs:9:13 + --> $DIR/coerce-expect-unsized-ascribed.rs:9:27 | -LL | let _ = box { [1, 2, 3] }: Box<[i32]>; - | ^^^^^^^^^^^^^^^^^ expected slice `[i32]`, found array `[i32; 3]` +LL | let _ = type_ascribe!(box { [1, 2, 3] }, Box<[i32]>); + | ^^^^^^^^^^^^^^^^^ expected slice `[i32]`, found array `[i32; 3]` | = note: expected struct `Box<[i32]>` found struct `Box<[i32; 3]>` error[E0308]: mismatched types - --> $DIR/coerce-expect-unsized-ascribed.rs:10:13 + --> $DIR/coerce-expect-unsized-ascribed.rs:10:27 | -LL | let _ = box if true { [1, 2, 3] } else { [1, 3, 4] }: Box<[i32]>; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected slice `[i32]`, found array `[i32; 3]` +LL | let _ = type_ascribe!(box if true { [1, 2, 3] } else { [1, 3, 4] }, Box<[i32]>); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected slice `[i32]`, found array `[i32; 3]` | = note: expected struct `Box<[i32]>` found struct `Box<[i32; 3]>` error[E0308]: mismatched types - --> $DIR/coerce-expect-unsized-ascribed.rs:11:13 + --> $DIR/coerce-expect-unsized-ascribed.rs:11:27 | -LL | let _ = box match true { true => [1, 2, 3], false => [1, 3, 4] }: Box<[i32]>; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected slice `[i32]`, found array `[i32; 3]` +LL | let _ = type_ascribe!(box match true { true => [1, 2, 3], false => [1, 3, 4] }, Box<[i32]>); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected slice `[i32]`, found array `[i32; 3]` | = note: expected struct `Box<[i32]>` found struct `Box<[i32; 3]>` error[E0308]: mismatched types - --> $DIR/coerce-expect-unsized-ascribed.rs:13:13 + --> $DIR/coerce-expect-unsized-ascribed.rs:13:27 | -LL | let _ = box { |x| (x as u8) }: Box _>; - | ^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn Fn`, found closure +LL | let _ = type_ascribe!(box { |x| (x as u8) }, Box _>); + | ^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn Fn`, found closure | = note: expected struct `Box u8>` - found struct `Box<[closure@$DIR/coerce-expect-unsized-ascribed.rs:13:19: 13:22]>` + found struct `Box<[closure@$DIR/coerce-expect-unsized-ascribed.rs:13:33: 13:36]>` error[E0308]: mismatched types - --> $DIR/coerce-expect-unsized-ascribed.rs:14:13 + --> $DIR/coerce-expect-unsized-ascribed.rs:14:27 | -LL | let _ = box if true { false } else { true }: Box; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn Debug`, found `bool` +LL | let _ = type_ascribe!(box if true { false } else { true }, Box); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn Debug`, found `bool` | = note: expected struct `Box` found struct `Box` error[E0308]: mismatched types - --> $DIR/coerce-expect-unsized-ascribed.rs:15:13 + --> $DIR/coerce-expect-unsized-ascribed.rs:15:27 | -LL | let _ = box match true { true => 'a', false => 'b' }: Box; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn Debug`, found `char` +LL | let _ = type_ascribe!(box match true { true => 'a', false => 'b' }, Box); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn Debug`, found `char` | = note: expected struct `Box` found struct `Box` error[E0308]: mismatched types - --> $DIR/coerce-expect-unsized-ascribed.rs:17:13 + --> $DIR/coerce-expect-unsized-ascribed.rs:17:27 | -LL | let _ = &{ [1, 2, 3] }: &[i32]; - | ^^^^^^^^^^^^^^ expected slice `[i32]`, found array `[i32; 3]` +LL | let _ = type_ascribe!(&{ [1, 2, 3] }, &[i32]); + | ^^^^^^^^^^^^^^ expected slice `[i32]`, found array `[i32; 3]` | = note: expected reference `&[i32]` found reference `&[i32; 3]` error[E0308]: mismatched types - --> $DIR/coerce-expect-unsized-ascribed.rs:18:13 + --> $DIR/coerce-expect-unsized-ascribed.rs:18:27 | -LL | let _ = &if true { [1, 2, 3] } else { [1, 3, 4] }: &[i32]; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected slice `[i32]`, found array `[i32; 3]` +LL | let _ = type_ascribe!(&if true { [1, 2, 3] } else { [1, 3, 4] }, &[i32]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected slice `[i32]`, found array `[i32; 3]` | = note: expected reference `&[i32]` found reference `&[i32; 3]` error[E0308]: mismatched types - --> $DIR/coerce-expect-unsized-ascribed.rs:19:13 + --> $DIR/coerce-expect-unsized-ascribed.rs:19:27 | -LL | let _ = &match true { true => [1, 2, 3], false => [1, 3, 4] }: &[i32]; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected slice `[i32]`, found array `[i32; 3]` +LL | let _ = type_ascribe!(&match true { true => [1, 2, 3], false => [1, 3, 4] }, &[i32]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected slice `[i32]`, found array `[i32; 3]` | = note: expected reference `&[i32]` found reference `&[i32; 3]` error[E0308]: mismatched types - --> $DIR/coerce-expect-unsized-ascribed.rs:21:13 + --> $DIR/coerce-expect-unsized-ascribed.rs:21:27 | -LL | let _ = &{ |x| (x as u8) }: &dyn Fn(i32) -> _; - | ^^^^^^^^^^^^^^^^^^ expected trait object `dyn Fn`, found closure +LL | let _ = type_ascribe!(&{ |x| (x as u8) }, &dyn Fn(i32) -> _); + | ^^^^^^^^^^^^^^^^^^ expected trait object `dyn Fn`, found closure | = note: expected reference `&dyn Fn(i32) -> u8` - found reference `&[closure@$DIR/coerce-expect-unsized-ascribed.rs:21:16: 21:19]` + found reference `&[closure@$DIR/coerce-expect-unsized-ascribed.rs:21:30: 21:33]` error[E0308]: mismatched types - --> $DIR/coerce-expect-unsized-ascribed.rs:22:13 + --> $DIR/coerce-expect-unsized-ascribed.rs:22:27 | -LL | let _ = &if true { false } else { true }: &dyn Debug; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn Debug`, found `bool` +LL | let _ = type_ascribe!(&if true { false } else { true }, &dyn Debug); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn Debug`, found `bool` | = note: expected reference `&dyn Debug` found reference `&bool` error[E0308]: mismatched types - --> $DIR/coerce-expect-unsized-ascribed.rs:23:13 + --> $DIR/coerce-expect-unsized-ascribed.rs:23:27 | -LL | let _ = &match true { true => 'a', false => 'b' }: &dyn Debug; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn Debug`, found `char` +LL | let _ = type_ascribe!(&match true { true => 'a', false => 'b' }, &dyn Debug); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn Debug`, found `char` | = note: expected reference `&dyn Debug` found reference `&char` error[E0308]: mismatched types - --> $DIR/coerce-expect-unsized-ascribed.rs:25:13 + --> $DIR/coerce-expect-unsized-ascribed.rs:25:27 | -LL | let _ = Box::new([1, 2, 3]): Box<[i32]>; - | ^^^^^^^^^^^^^^^^^^^ expected slice `[i32]`, found array `[i32; 3]` +LL | let _ = type_ascribe!(Box::new([1, 2, 3]), Box<[i32]>); + | ^^^^^^^^^^^^^^^^^^^ expected slice `[i32]`, found array `[i32; 3]` | = note: expected struct `Box<[i32]>` found struct `Box<[i32; 3]>` error[E0308]: mismatched types - --> $DIR/coerce-expect-unsized-ascribed.rs:26:13 + --> $DIR/coerce-expect-unsized-ascribed.rs:26:27 | -LL | let _ = Box::new(|x| (x as u8)): Box _>; - | ^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn Fn`, found closure +LL | let _ = type_ascribe!(Box::new(|x| (x as u8)), Box _>); + | ^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn Fn`, found closure | = note: expected struct `Box u8>` - found struct `Box<[closure@$DIR/coerce-expect-unsized-ascribed.rs:26:22: 26:25]>` + found struct `Box<[closure@$DIR/coerce-expect-unsized-ascribed.rs:26:36: 26:39]>` error: aborting due to 14 previous errors diff --git a/src/test/ui/consts/const_in_pattern/accept_structural.rs b/src/test/ui/consts/const_in_pattern/accept_structural.rs index 5093fe5391547..1f56f581c0270 100644 --- a/src/test/ui/consts/const_in_pattern/accept_structural.rs +++ b/src/test/ui/consts/const_in_pattern/accept_structural.rs @@ -45,7 +45,7 @@ fn main() { const TUPLE: (OND, OND) = (None, None); match (None, None) { TUPLE => dbg!(TUPLE), _ => panic!("whoops"), }; - const TYPE_ASCRIPTION: OND = None: OND; + const TYPE_ASCRIPTION: OND = type_ascribe!(None, OND); match None { TYPE_ASCRIPTION => dbg!(TYPE_ASCRIPTION), _ => panic!("whoops"), }; const ARRAY: [OND; 2] = [None, None]; diff --git a/src/test/ui/consts/const_in_pattern/reject_non_structural.rs b/src/test/ui/consts/const_in_pattern/reject_non_structural.rs index 7a8169bec45be..75fde0d92de17 100644 --- a/src/test/ui/consts/const_in_pattern/reject_non_structural.rs +++ b/src/test/ui/consts/const_in_pattern/reject_non_structural.rs @@ -53,7 +53,7 @@ fn main() { match (None, Some(NoDerive)) { TUPLE => dbg!(TUPLE), _ => panic!("whoops"), }; //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` - const TYPE_ASCRIPTION: OND = Some(NoDerive): OND; + const TYPE_ASCRIPTION: OND = type_ascribe!(Some(NoDerive), OND); match Some(NoDerive) { TYPE_ASCRIPTION => dbg!(TYPE_ASCRIPTION), _ => panic!("whoops"), }; //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` diff --git a/src/test/ui/enum/issue-67945-2.rs b/src/test/ui/enum/issue-67945-2.rs index e5044468da129..2eb123b7328da 100644 --- a/src/test/ui/enum/issue-67945-2.rs +++ b/src/test/ui/enum/issue-67945-2.rs @@ -1,7 +1,7 @@ #![feature(type_ascription)] enum Bug { //~ ERROR parameter `S` is never used - Var = 0: S, + Var = type_ascribe!(0, S), //~^ ERROR generic parameters may not be used } diff --git a/src/test/ui/enum/issue-67945-2.stderr b/src/test/ui/enum/issue-67945-2.stderr index 4f5e236a37b45..63d3521afe4fb 100644 --- a/src/test/ui/enum/issue-67945-2.stderr +++ b/src/test/ui/enum/issue-67945-2.stderr @@ -1,8 +1,8 @@ error: generic parameters may not be used in const operations - --> $DIR/issue-67945-2.rs:4:14 + --> $DIR/issue-67945-2.rs:4:28 | -LL | Var = 0: S, - | ^ cannot perform const operation using `S` +LL | Var = type_ascribe!(0, S), + | ^ cannot perform const operation using `S` | = note: type parameters may not be used in const expressions = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions diff --git a/src/test/ui/lint/unused/issue-88519-unused-paren.rs b/src/test/ui/lint/unused/issue-88519-unused-paren.rs index be02fcd3f00c7..ce3d15ac183fe 100644 --- a/src/test/ui/lint/unused/issue-88519-unused-paren.rs +++ b/src/test/ui/lint/unused/issue-88519-unused-paren.rs @@ -51,22 +51,13 @@ mod casts { mod typeascription { fn outside() -> u8 { - ({ 0 }): u8 - } - fn inside() -> u8 { - ({ 0 }: u8) + type_ascribe!(({ 0 }), u8) } fn outside_match() -> u8 { - (match 0 { x => x }): u8 - } - fn inside_match() -> u8 { - (match 0 { x => x }: u8) + type_ascribe!((match 0 { x => x }), u8) } fn outside_if() -> u8 { - (if false { 0 } else { 0 }): u8 - } - fn inside_if() -> u8 { - (if false { 0 } else { 0 }: u8) + type_ascribe!((if false { 0 } else { 0 }), u8) } } diff --git a/src/test/ui/mir/mir_ascription_coercion.rs b/src/test/ui/mir/mir_ascription_coercion.rs index 0ebd20e97d7ef..9e04d60198721 100644 --- a/src/test/ui/mir/mir_ascription_coercion.rs +++ b/src/test/ui/mir/mir_ascription_coercion.rs @@ -6,5 +6,5 @@ fn main() { let x = [1, 2, 3]; // The RHS should coerce to &[i32] - let _y : &[i32] = &x : &[i32; 3]; + let _y : &[i32] = type_ascribe!(&x, &[i32; 3]); } diff --git a/src/test/ui/nll/user-annotations/issue-57731-ascibed-coupled-types.rs b/src/test/ui/nll/user-annotations/issue-57731-ascibed-coupled-types.rs index 9b3ec702c75da..95c655654eaa4 100644 --- a/src/test/ui/nll/user-annotations/issue-57731-ascibed-coupled-types.rs +++ b/src/test/ui/nll/user-annotations/issue-57731-ascibed-coupled-types.rs @@ -8,17 +8,17 @@ type PairCoupledTypes = (T, T); type PairCoupledRegions<'a, T> = (&'a T, &'a T); fn uncoupled_wilds_rhs<'a>(_x: &'a u32, s: &'static u32) -> &'static u32 { - let ((y, _z),) = ((s, _x),): (PairUncoupled<_>,); + let ((y, _z),) = type_ascribe!(((s, _x),), (PairUncoupled<_>,)); y // OK } fn coupled_wilds_rhs<'a>(_x: &'a u32, s: &'static u32) -> &'static u32 { - let ((y, _z),) = ((s, _x),): (PairCoupledTypes<_>,); + let ((y, _z),) = type_ascribe!(((s, _x),), (PairCoupledTypes<_>,)); y //~ ERROR lifetime may not live long enough } fn coupled_regions_rhs<'a>(_x: &'a u32, s: &'static u32) -> &'static u32 { - let ((y, _z),) = ((s, _x),): (PairCoupledRegions<_>,); + let ((y, _z),) = type_ascribe!(((s, _x),), (PairCoupledRegions<_>,)); y //~ ERROR lifetime may not live long enough } diff --git a/src/test/ui/nll/user-annotations/issue-57731-ascibed-coupled-types.stderr b/src/test/ui/nll/user-annotations/issue-57731-ascibed-coupled-types.stderr index c99f53c5aa4c5..8601691e88ad7 100644 --- a/src/test/ui/nll/user-annotations/issue-57731-ascibed-coupled-types.stderr +++ b/src/test/ui/nll/user-annotations/issue-57731-ascibed-coupled-types.stderr @@ -3,7 +3,7 @@ error: lifetime may not live long enough | LL | fn coupled_wilds_rhs<'a>(_x: &'a u32, s: &'static u32) -> &'static u32 { | -- lifetime `'a` defined here -LL | let ((y, _z),) = ((s, _x),): (PairCoupledTypes<_>,); +LL | let ((y, _z),) = type_ascribe!(((s, _x),), (PairCoupledTypes<_>,)); LL | y | ^ returning this value requires that `'a` must outlive `'static` @@ -12,7 +12,7 @@ error: lifetime may not live long enough | LL | fn coupled_regions_rhs<'a>(_x: &'a u32, s: &'static u32) -> &'static u32 { | -- lifetime `'a` defined here -LL | let ((y, _z),) = ((s, _x),): (PairCoupledRegions<_>,); +LL | let ((y, _z),) = type_ascribe!(((s, _x),), (PairCoupledRegions<_>,)); LL | y | ^ returning this value requires that `'a` must outlive `'static` diff --git a/src/test/ui/nll/user-annotations/type_ascription_static_lifetime.rs b/src/test/ui/nll/user-annotations/type_ascription_static_lifetime.rs index 101b5cfabb3d0..88d646dee7c74 100644 --- a/src/test/ui/nll/user-annotations/type_ascription_static_lifetime.rs +++ b/src/test/ui/nll/user-annotations/type_ascription_static_lifetime.rs @@ -3,5 +3,5 @@ fn main() { let x = 22_u32; - let y: &u32 = &x: &'static u32; //~ ERROR E0597 + let y: &u32 = type_ascribe!(&x, &'static u32); //~ ERROR E0597 } diff --git a/src/test/ui/nll/user-annotations/type_ascription_static_lifetime.stderr b/src/test/ui/nll/user-annotations/type_ascription_static_lifetime.stderr index 133bbef52311d..ccbf3c1d927c6 100644 --- a/src/test/ui/nll/user-annotations/type_ascription_static_lifetime.stderr +++ b/src/test/ui/nll/user-annotations/type_ascription_static_lifetime.stderr @@ -1,10 +1,10 @@ error[E0597]: `x` does not live long enough - --> $DIR/type_ascription_static_lifetime.rs:6:19 + --> $DIR/type_ascription_static_lifetime.rs:6:33 | -LL | let y: &u32 = &x: &'static u32; - | ^^-------------- - | | - | borrowed value does not live long enough +LL | let y: &u32 = type_ascribe!(&x, &'static u32); + | --------------^^--------------- + | | | + | | borrowed value does not live long enough | type annotation requires that `x` is borrowed for `'static` LL | } | - `x` dropped here while still borrowed diff --git a/src/test/ui/raw-ref-op/raw-ref-temp-deref.rs b/src/test/ui/raw-ref-op/raw-ref-temp-deref.rs index a814003aebf20..2e075a1b9e8bb 100644 --- a/src/test/ui/raw-ref-op/raw-ref-temp-deref.rs +++ b/src/test/ui/raw-ref-op/raw-ref-temp-deref.rs @@ -18,7 +18,7 @@ fn main() { let index_deref_ref = &raw const SLICE_REF[1]; let x = 0; - let ascribe_ref = &raw const (x: i32); - let ascribe_deref = &raw const (*ARRAY_REF: [i32; 2]); - let ascribe_index_deref = &raw const (ARRAY_REF[0]: i32); + let ascribe_ref = &raw const type_ascribe!(x, i32); + let ascribe_deref = &raw const type_ascribe!(*ARRAY_REF, [i32; 2]); + let ascribe_index_deref = &raw const type_ascribe!(ARRAY_REF[0], i32); } diff --git a/src/test/ui/raw-ref-op/raw-ref-temp.rs b/src/test/ui/raw-ref-op/raw-ref-temp.rs index 32df56468da17..10e47cb34c58d 100644 --- a/src/test/ui/raw-ref-op/raw-ref-temp.rs +++ b/src/test/ui/raw-ref-op/raw-ref-temp.rs @@ -8,24 +8,24 @@ const PAIR: (i32, i64) = (1, 2); const ARRAY: [i32; 2] = [1, 2]; fn main() { - let ref_expr = &raw const 2; //~ ERROR cannot take address - let mut_ref_expr = &raw mut 3; //~ ERROR cannot take address - let ref_const = &raw const FOUR; //~ ERROR cannot take address - let mut_ref_const = &raw mut FOUR; //~ ERROR cannot take address - - let field_ref_expr = &raw const (1, 2).0; //~ ERROR cannot take address - let mut_field_ref_expr = &raw mut (1, 2).0; //~ ERROR cannot take address - let field_ref = &raw const PAIR.0; //~ ERROR cannot take address - let mut_field_ref = &raw mut PAIR.0; //~ ERROR cannot take address - - let index_ref_expr = &raw const [1, 2][0]; //~ ERROR cannot take address - let mut_index_ref_expr = &raw mut [1, 2][0]; //~ ERROR cannot take address - let index_ref = &raw const ARRAY[0]; //~ ERROR cannot take address - let mut_index_ref = &raw mut ARRAY[1]; //~ ERROR cannot take address - - let ref_ascribe = &raw const (2: i32); //~ ERROR cannot take address - let mut_ref_ascribe = &raw mut (3: i32); //~ ERROR cannot take address - - let ascribe_field_ref = &raw const (PAIR.0: i32); //~ ERROR cannot take address - let ascribe_index_ref = &raw mut (ARRAY[0]: i32); //~ ERROR cannot take address + let ref_expr = &raw const 2; //~ ERROR cannot take address + let mut_ref_expr = &raw mut 3; //~ ERROR cannot take address + let ref_const = &raw const FOUR; //~ ERROR cannot take address + let mut_ref_const = &raw mut FOUR; //~ ERROR cannot take address + + let field_ref_expr = &raw const (1, 2).0; //~ ERROR cannot take address + let mut_field_ref_expr = &raw mut (1, 2).0; //~ ERROR cannot take address + let field_ref = &raw const PAIR.0; //~ ERROR cannot take address + let mut_field_ref = &raw mut PAIR.0; //~ ERROR cannot take address + + let index_ref_expr = &raw const [1, 2][0]; //~ ERROR cannot take address + let mut_index_ref_expr = &raw mut [1, 2][0]; //~ ERROR cannot take address + let index_ref = &raw const ARRAY[0]; //~ ERROR cannot take address + let mut_index_ref = &raw mut ARRAY[1]; //~ ERROR cannot take address + + let ref_ascribe = &raw const type_ascribe!(2, i32); //~ ERROR cannot take address + let mut_ref_ascribe = &raw mut type_ascribe!(3, i32); //~ ERROR cannot take address + + let ascribe_field_ref = &raw const type_ascribe!(PAIR.0, i32); //~ ERROR cannot take address + let ascribe_index_ref = &raw mut type_ascribe!(ARRAY[0], i32); //~ ERROR cannot take address } diff --git a/src/test/ui/raw-ref-op/raw-ref-temp.stderr b/src/test/ui/raw-ref-op/raw-ref-temp.stderr index 80dea76d5953b..b96661625170f 100644 --- a/src/test/ui/raw-ref-op/raw-ref-temp.stderr +++ b/src/test/ui/raw-ref-op/raw-ref-temp.stderr @@ -73,26 +73,26 @@ LL | let mut_index_ref = &raw mut ARRAY[1]; error[E0745]: cannot take address of a temporary --> $DIR/raw-ref-temp.rs:26:34 | -LL | let ref_ascribe = &raw const (2: i32); - | ^^^^^^^^ temporary value +LL | let ref_ascribe = &raw const type_ascribe!(2, i32); + | ^^^^^^^^^^^^^^^^^^^^^ temporary value error[E0745]: cannot take address of a temporary --> $DIR/raw-ref-temp.rs:27:36 | -LL | let mut_ref_ascribe = &raw mut (3: i32); - | ^^^^^^^^ temporary value +LL | let mut_ref_ascribe = &raw mut type_ascribe!(3, i32); + | ^^^^^^^^^^^^^^^^^^^^^ temporary value error[E0745]: cannot take address of a temporary --> $DIR/raw-ref-temp.rs:29:40 | -LL | let ascribe_field_ref = &raw const (PAIR.0: i32); - | ^^^^^^^^^^^^^ temporary value +LL | let ascribe_field_ref = &raw const type_ascribe!(PAIR.0, i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value error[E0745]: cannot take address of a temporary --> $DIR/raw-ref-temp.rs:30:38 | -LL | let ascribe_index_ref = &raw mut (ARRAY[0]: i32); - | ^^^^^^^^^^^^^^^ temporary value +LL | let ascribe_index_ref = &raw mut type_ascribe!(ARRAY[0], i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value error: aborting due to 16 previous errors diff --git a/src/test/ui/reachable/expr_type.rs b/src/test/ui/reachable/expr_type.rs index 8d32397b54248..1ceb2f8597160 100644 --- a/src/test/ui/reachable/expr_type.rs +++ b/src/test/ui/reachable/expr_type.rs @@ -6,7 +6,7 @@ fn a() { // the cast is unreachable: - let x = {return}: !; //~ ERROR unreachable + let x = type_ascribe!({return}, !); //~ ERROR unreachable } fn main() { } diff --git a/src/test/ui/reachable/expr_type.stderr b/src/test/ui/reachable/expr_type.stderr index c56c64be721fe..3cb4a32e02fac 100644 --- a/src/test/ui/reachable/expr_type.stderr +++ b/src/test/ui/reachable/expr_type.stderr @@ -1,10 +1,10 @@ error: unreachable expression --> $DIR/expr_type.rs:9:13 | -LL | let x = {return}: !; - | ^------^^^^ - | || - | |any code following this expression is unreachable +LL | let x = type_ascribe!({return}, !); + | ^^^^^^^^^^^^^^^------^^^^^ + | | | + | | any code following this expression is unreachable | unreachable expression | note: the lint level is defined here diff --git a/src/test/ui/type/type-ascription-soundness.rs b/src/test/ui/type/type-ascription-soundness.rs index d583fc2131a74..08316cdcd35ec 100644 --- a/src/test/ui/type/type-ascription-soundness.rs +++ b/src/test/ui/type/type-ascription-soundness.rs @@ -4,10 +4,10 @@ fn main() { let arr = &[1u8, 2, 3]; - let ref x = arr: &[u8]; //~ ERROR mismatched types - let ref mut x = arr: &[u8]; //~ ERROR mismatched types - match arr: &[u8] { //~ ERROR mismatched types + let ref x = type_ascribe!(arr, &[u8]); //~ ERROR mismatched types + let ref mut x = type_ascribe!(arr, &[u8]); //~ ERROR mismatched types + match type_ascribe!(arr, &[u8]) { //~ ERROR mismatched types ref x => {} } - let _len = (arr: &[u8]).len(); //~ ERROR mismatched types + let _len = type_ascribe!(arr, &[u8]).len(); //~ ERROR mismatched types } diff --git a/src/test/ui/type/type-ascription-soundness.stderr b/src/test/ui/type/type-ascription-soundness.stderr index 6ed940823af1a..522d5b2e37555 100644 --- a/src/test/ui/type/type-ascription-soundness.stderr +++ b/src/test/ui/type/type-ascription-soundness.stderr @@ -1,35 +1,35 @@ error[E0308]: mismatched types - --> $DIR/type-ascription-soundness.rs:7:17 + --> $DIR/type-ascription-soundness.rs:7:31 | -LL | let ref x = arr: &[u8]; - | ^^^ expected slice `[u8]`, found array `[u8; 3]` +LL | let ref x = type_ascribe!(arr, &[u8]); + | ^^^ expected slice `[u8]`, found array `[u8; 3]` | = note: expected reference `&[u8]` found reference `&[u8; 3]` error[E0308]: mismatched types - --> $DIR/type-ascription-soundness.rs:8:21 + --> $DIR/type-ascription-soundness.rs:8:35 | -LL | let ref mut x = arr: &[u8]; - | ^^^ expected slice `[u8]`, found array `[u8; 3]` +LL | let ref mut x = type_ascribe!(arr, &[u8]); + | ^^^ expected slice `[u8]`, found array `[u8; 3]` | = note: expected reference `&[u8]` found reference `&[u8; 3]` error[E0308]: mismatched types - --> $DIR/type-ascription-soundness.rs:9:11 + --> $DIR/type-ascription-soundness.rs:9:25 | -LL | match arr: &[u8] { - | ^^^ expected slice `[u8]`, found array `[u8; 3]` +LL | match type_ascribe!(arr, &[u8]) { + | ^^^ expected slice `[u8]`, found array `[u8; 3]` | = note: expected reference `&[u8]` found reference `&[u8; 3]` error[E0308]: mismatched types - --> $DIR/type-ascription-soundness.rs:12:17 + --> $DIR/type-ascription-soundness.rs:12:30 | -LL | let _len = (arr: &[u8]).len(); - | ^^^ expected slice `[u8]`, found array `[u8; 3]` +LL | let _len = type_ascribe!(arr, &[u8]).len(); + | ^^^ expected slice `[u8]`, found array `[u8; 3]` | = note: expected reference `&[u8]` found reference `&[u8; 3]` diff --git a/src/test/ui/type/type-ascription.rs b/src/test/ui/type/type-ascription.rs index 7adb074428ce9..e4a4c89d057f2 100644 --- a/src/test/ui/type/type-ascription.rs +++ b/src/test/ui/type/type-ascription.rs @@ -8,32 +8,32 @@ use std::mem; -const C1: u8 = 10: u8; -const C2: [u8; 1: usize] = [1]; +const C1: u8 = type_ascribe!(10, u8); +const C2: [u8; type_ascribe!(1, usize)] = [1]; struct S { a: u8 } fn main() { - assert_eq!(C1.into(): i32, 10); + assert_eq!(type_ascribe!(C1.into(), i32), 10); assert_eq!(C2[0], 1); - let s = S { a: 10: u8 }; + let s = S { a: type_ascribe!(10, u8) }; let arr = &[1u8, 2, 3]; - let mut v = arr.iter().cloned().collect(): Vec<_>; + let mut v = type_ascribe!(arr.iter().cloned().collect(), Vec<_>); v.push(4); assert_eq!(v, [1, 2, 3, 4]); - let a = 1: u8; - let b = a.into(): u16; - assert_eq!(v[a.into(): usize], 2); + let a = type_ascribe!(1, u8); + let b = type_ascribe!(a.into(), u16); + assert_eq!(v[type_ascribe!(a.into(), usize)], 2); assert_eq!(mem::size_of_val(&a), 1); assert_eq!(mem::size_of_val(&b), 2); - assert_eq!(b, 1: u16); + assert_eq!(b, type_ascribe!(1, u16)); let mut v = Vec::new(); - v: Vec = vec![1, 2, 3]; // Place expression type ascription + type_ascribe!(v, Vec) = vec![1, 2, 3]; // Place expression type ascription assert_eq!(v, [1u8, 2, 3]); } diff --git a/src/test/ui/typeck/issue-91267.rs b/src/test/ui/typeck/issue-91267.rs index f5a37e9cb86f8..4e39cfab5b439 100644 --- a/src/test/ui/typeck/issue-91267.rs +++ b/src/test/ui/typeck/issue-91267.rs @@ -1,5 +1,7 @@ +#![feature(type_ascription)] + fn main() { - 0: u8=e> + type_ascribe!(0, u8=e>) //~^ ERROR: cannot find type `e` in this scope [E0412] //~| ERROR: associated type bindings are not allowed here [E0229] //~| ERROR: mismatched types [E0308] diff --git a/src/test/ui/typeck/issue-91267.stderr b/src/test/ui/typeck/issue-91267.stderr index aac00b9b6a941..72acd9c673b63 100644 --- a/src/test/ui/typeck/issue-91267.stderr +++ b/src/test/ui/typeck/issue-91267.stderr @@ -1,25 +1,22 @@ error[E0412]: cannot find type `e` in this scope - --> $DIR/issue-91267.rs:2:16 + --> $DIR/issue-91267.rs:4:30 | -LL | 0: u8=e> - | ^ - | | - | not found in this scope - | help: maybe you meant to write an assignment here: `let e` +LL | type_ascribe!(0, u8=e>) + | ^ not found in this scope error[E0229]: associated type bindings are not allowed here - --> $DIR/issue-91267.rs:2:11 + --> $DIR/issue-91267.rs:4:25 | -LL | 0: u8=e> - | ^^^^^^ associated type not allowed here +LL | type_ascribe!(0, u8=e>) + | ^^^^^^ associated type not allowed here error[E0308]: mismatched types - --> $DIR/issue-91267.rs:2:5 + --> $DIR/issue-91267.rs:4:5 | LL | fn main() { | - expected `()` because of default return type -LL | 0: u8=e> - | ^^^^^^^^^^^^^ expected `()`, found `u8` +LL | type_ascribe!(0, u8=e>) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `u8` error: aborting due to 3 previous errors From 102a5d867c9b82df79ff7465eb8287b700af3bce Mon Sep 17 00:00:00 2001 From: Caleb Zulawski Date: Sat, 19 Nov 2022 21:22:17 -0500 Subject: [PATCH 014/244] Print all features with --print target-features, including aliases and tied features --- compiler/rustc_codegen_llvm/src/llvm_util.rs | 35 +++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index 4af1aaec0a112..8383a9c853556 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -292,30 +292,33 @@ fn llvm_target_features(tm: &llvm::TargetMachine) -> Vec<(&str, &str)> { } fn print_target_features(sess: &Session, tm: &llvm::TargetMachine) { - let mut target_features = llvm_target_features(tm); + let mut llvm_target_features = llvm_target_features(tm); + let mut known_llvm_target_features = FxHashSet::<&'static str>::default(); let mut rustc_target_features = supported_target_features(sess) .iter() - .filter_map(|(feature, _gate)| { - for llvm_feature in to_llvm_features(sess, *feature) { + .map(|(feature, _gate)| { + let desc = if let Some(llvm_feature) = to_llvm_features(sess, *feature).first() { // LLVM asserts that these are sorted. LLVM and Rust both use byte comparison for these strings. - match target_features.binary_search_by_key(&llvm_feature, |(f, _d)| f).ok().map( - |index| { - let (_f, desc) = target_features.remove(index); - (*feature, desc) - }, - ) { - Some(v) => return Some(v), - None => {} + match llvm_target_features.binary_search_by_key(&llvm_feature, |(f, _d)| f).ok() { + Some(index) => { + known_llvm_target_features.insert(llvm_feature); + llvm_target_features[index].1 + } + None => "", } - } - None + } else { + "" + }; + (*feature, desc) }) .collect::>(); rustc_target_features.extend_from_slice(&[( "crt-static", "Enables C Run-time Libraries to be statically linked", )]); - let max_feature_len = target_features + llvm_target_features.retain(|(f, _d)| !known_llvm_target_features.contains(f)); + + let max_feature_len = llvm_target_features .iter() .chain(rustc_target_features.iter()) .map(|(feature, _desc)| feature.len()) @@ -327,10 +330,10 @@ fn print_target_features(sess: &Session, tm: &llvm::TargetMachine) { println!(" {1:0$} - {2}.", max_feature_len, feature, desc); } println!("\nCode-generation features supported by LLVM for this target:"); - for (feature, desc) in &target_features { + for (feature, desc) in &llvm_target_features { println!(" {1:0$} - {2}.", max_feature_len, feature, desc); } - if target_features.is_empty() { + if llvm_target_features.is_empty() { println!(" Target features listing is not supported by this LLVM version."); } println!("\nUse +feature to enable a feature, or -feature to disable it."); From 31b83d0895d37dc8a37e195f75bb9fe7de2c5e37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Tue, 1 Nov 2022 18:39:36 +0100 Subject: [PATCH 015/244] Add missnamed_getters lint --- CHANGELOG.md | 1 + clippy_lints/src/declared_lints.rs | 1 + .../src/functions/missnamed_getters.rs | 123 ++++++++++++++++++ clippy_lints/src/functions/mod.rs | 22 ++++ tests/ui/missnamed_getters.rs | 28 ++++ tests/ui/missnamed_getters.stderr | 16 +++ 6 files changed, 191 insertions(+) create mode 100644 clippy_lints/src/functions/missnamed_getters.rs create mode 100644 tests/ui/missnamed_getters.rs create mode 100644 tests/ui/missnamed_getters.stderr diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f1f73c1fd2fe..180e7d8bedcd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4198,6 +4198,7 @@ Released 2018-09-13 [`missing_safety_doc`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_safety_doc [`missing_spin_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_spin_loop [`missing_trait_methods`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_trait_methods +[`missnamed_getters`]: https://rust-lang.github.io/rust-clippy/master/index.html#missnamed_getters [`mistyped_literal_suffixes`]: https://rust-lang.github.io/rust-clippy/master/index.html#mistyped_literal_suffixes [`mixed_case_hex_literals`]: https://rust-lang.github.io/rust-clippy/master/index.html#mixed_case_hex_literals [`mixed_read_write_in_expression`]: https://rust-lang.github.io/rust-clippy/master/index.html#mixed_read_write_in_expression diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index 0d3fc43a6443b..0c9ae6380d874 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -184,6 +184,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::functions::RESULT_UNIT_ERR_INFO, crate::functions::TOO_MANY_ARGUMENTS_INFO, crate::functions::TOO_MANY_LINES_INFO, + crate::functions::MISSNAMED_GETTERS_INFO, crate::future_not_send::FUTURE_NOT_SEND_INFO, crate::if_let_mutex::IF_LET_MUTEX_INFO, crate::if_not_else::IF_NOT_ELSE_INFO, diff --git a/clippy_lints/src/functions/missnamed_getters.rs b/clippy_lints/src/functions/missnamed_getters.rs new file mode 100644 index 0000000000000..c522bb780b3db --- /dev/null +++ b/clippy_lints/src/functions/missnamed_getters.rs @@ -0,0 +1,123 @@ +use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::source::snippet; +use rustc_errors::Applicability; +use rustc_hir::{intravisit::FnKind, Body, ExprKind, FnDecl, HirId, ImplicitSelfKind}; +use rustc_lint::LateContext; +use rustc_middle::ty; +use rustc_span::Span; + +use super::MISSNAMED_GETTERS; + +pub fn check_fn( + cx: &LateContext<'_>, + kind: FnKind<'_>, + decl: &FnDecl<'_>, + body: &Body<'_>, + _span: Span, + _hir_id: HirId, +) { + let FnKind::Method(ref ident, sig) = kind else { + return; + }; + + // Takes only &(mut) self + if decl.inputs.len() != 1 { + return; + } + + let name = ident.name.as_str(); + + let name = match sig.decl.implicit_self { + ImplicitSelfKind::ImmRef => name, + ImplicitSelfKind::MutRef => { + let Some(name) = name.strip_suffix("_mut") else { + return; + }; + name + }, + _ => return, + }; + + // Body must be &(mut) .name + // self_data is not neccessarilly self + let (self_data, used_ident, span) = if_chain! { + if let ExprKind::Block(block,_) = body.value.kind; + if block.stmts.is_empty(); + if let Some(block_expr) = block.expr; + // replace with while for as many addrof needed + if let ExprKind::AddrOf(_,_, expr) = block_expr.kind; + if let ExprKind::Field(self_data, ident) = expr.kind; + if ident.name.as_str() != name; + then { + (self_data,ident,block_expr.span) + } else { + return; + } + }; + + let ty = cx.typeck_results().expr_ty(self_data); + + let def = { + let mut kind = ty.kind(); + loop { + match kind { + ty::Adt(def, _) => break def, + ty::Ref(_, ty, _) => kind = ty.kind(), + // We don't do tuples because the function name cannot be a number + _ => return, + } + } + }; + + let variants = def.variants(); + + // We're accessing a field, so it should be an union or a struct and have one and only one variant + if variants.len() != 1 { + if cfg!(debug_assertions) { + panic!("Struct or union expected to have only one variant"); + } else { + // Don't ICE when possible + return; + } + } + + let first = variants.last().unwrap(); + let fields = &variants[first]; + + let mut used_field = None; + let mut correct_field = None; + for f in &fields.fields { + if f.name.as_str() == name { + correct_field = Some(f); + } + if f.name == used_ident.name { + used_field = Some(f); + } + } + + let Some(used_field) = used_field else { + if cfg!(debug_assertions) { + panic!("Struct doesn't contain the correct field"); + } else { + // Don't ICE when possible + return; + } + }; + let Some(correct_field) = correct_field else { + return; + }; + + if cx.tcx.type_of(used_field.did) == cx.tcx.type_of(correct_field.did) { + let snippet = snippet(cx, span, ".."); + let sugg = format!("{}{name}", snippet.strip_suffix(used_field.name.as_str()).unwrap()); + span_lint_and_sugg( + cx, + MISSNAMED_GETTERS, + span, + "getter function appears to return the wrong field", + "consider using", + sugg, + Applicability::MaybeIncorrect, + ); + } +} diff --git a/clippy_lints/src/functions/mod.rs b/clippy_lints/src/functions/mod.rs index ae0e083344638..726df02444fc8 100644 --- a/clippy_lints/src/functions/mod.rs +++ b/clippy_lints/src/functions/mod.rs @@ -1,3 +1,4 @@ +mod missnamed_getters; mod must_use; mod not_unsafe_ptr_arg_deref; mod result; @@ -260,6 +261,25 @@ declare_clippy_lint! { "function returning `Result` with large `Err` type" } +declare_clippy_lint! { + /// ### What it does + /// + /// ### Why is this bad? + /// + /// ### Example + /// ```rust + /// // example code where clippy issues a warning + /// ``` + /// Use instead: + /// ```rust + /// // example code which does not raise clippy warning + /// ``` + #[clippy::version = "1.66.0"] + pub MISSNAMED_GETTERS, + suspicious, + "default lint description" +} + #[derive(Copy, Clone)] pub struct Functions { too_many_arguments_threshold: u64, @@ -286,6 +306,7 @@ impl_lint_pass!(Functions => [ MUST_USE_CANDIDATE, RESULT_UNIT_ERR, RESULT_LARGE_ERR, + MISSNAMED_GETTERS, ]); impl<'tcx> LateLintPass<'tcx> for Functions { @@ -301,6 +322,7 @@ impl<'tcx> LateLintPass<'tcx> for Functions { too_many_arguments::check_fn(cx, kind, decl, span, hir_id, self.too_many_arguments_threshold); too_many_lines::check_fn(cx, kind, span, body, self.too_many_lines_threshold); not_unsafe_ptr_arg_deref::check_fn(cx, kind, decl, body, hir_id); + missnamed_getters::check_fn(cx, kind, decl, body, span, hir_id); } fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) { diff --git a/tests/ui/missnamed_getters.rs b/tests/ui/missnamed_getters.rs new file mode 100644 index 0000000000000..b47f6edc5ba10 --- /dev/null +++ b/tests/ui/missnamed_getters.rs @@ -0,0 +1,28 @@ +#![allow(unused)] +#![warn(clippy::missnamed_getters)] + +struct A { + a: u8, + b: u8, +} + +impl A { + fn a(&self) -> &u8 { + &self.b + } +} + +union B { + a: u8, + b: u8, +} + +impl B { + unsafe fn a(&self) -> &u8 { + &self.b + } +} + +fn main() { + // test code goes here +} diff --git a/tests/ui/missnamed_getters.stderr b/tests/ui/missnamed_getters.stderr new file mode 100644 index 0000000000000..8e31a42b97c1a --- /dev/null +++ b/tests/ui/missnamed_getters.stderr @@ -0,0 +1,16 @@ +error: getter function appears to return the wrong field + --> $DIR/missnamed_getters.rs:11:9 + | +LL | &self.b + | ^^^^^^^ help: consider using: `&self.a` + | + = note: `-D clippy::missnamed-getters` implied by `-D warnings` + +error: getter function appears to return the wrong field + --> $DIR/missnamed_getters.rs:22:9 + | +LL | &self.b + | ^^^^^^^ help: consider using: `&self.a` + +error: aborting due to 2 previous errors + From 9891af348c01f8cf14ab1e10754faccad04fcaa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Tue, 1 Nov 2022 19:07:51 +0100 Subject: [PATCH 016/244] missnamed_getters: Match owned methods --- .../src/functions/missnamed_getters.rs | 27 +++++++--- tests/ui/missnamed_getters.rs | 39 ++++++++++++++ tests/ui/missnamed_getters.stderr | 54 +++++++++++++++++-- 3 files changed, 110 insertions(+), 10 deletions(-) diff --git a/clippy_lints/src/functions/missnamed_getters.rs b/clippy_lints/src/functions/missnamed_getters.rs index c522bb780b3db..1f4eefc620bdc 100644 --- a/clippy_lints/src/functions/missnamed_getters.rs +++ b/clippy_lints/src/functions/missnamed_getters.rs @@ -35,21 +35,34 @@ pub fn check_fn( }; name }, + ImplicitSelfKind::Imm | ImplicitSelfKind::Mut => name, _ => return, }; // Body must be &(mut) .name - // self_data is not neccessarilly self - let (self_data, used_ident, span) = if_chain! { + // self_data is not neccessarilly self, to also lint sub-getters, etc… + + let block_expr = if_chain! { if let ExprKind::Block(block,_) = body.value.kind; if block.stmts.is_empty(); if let Some(block_expr) = block.expr; - // replace with while for as many addrof needed - if let ExprKind::AddrOf(_,_, expr) = block_expr.kind; + then { + block_expr + } else { + return; + } + }; + let expr_span = block_expr.span; + + let mut expr = block_expr; + if let ExprKind::AddrOf(_, _, tmp) = expr.kind { + expr = tmp; + } + let (self_data, used_ident) = if_chain! { if let ExprKind::Field(self_data, ident) = expr.kind; if ident.name.as_str() != name; then { - (self_data,ident,block_expr.span) + (self_data,ident) } else { return; } @@ -108,12 +121,12 @@ pub fn check_fn( }; if cx.tcx.type_of(used_field.did) == cx.tcx.type_of(correct_field.did) { - let snippet = snippet(cx, span, ".."); + let snippet = snippet(cx, expr_span, ".."); let sugg = format!("{}{name}", snippet.strip_suffix(used_field.name.as_str()).unwrap()); span_lint_and_sugg( cx, MISSNAMED_GETTERS, - span, + expr_span, "getter function appears to return the wrong field", "consider using", sugg, diff --git a/tests/ui/missnamed_getters.rs b/tests/ui/missnamed_getters.rs index b47f6edc5ba10..f9c2351f833d2 100644 --- a/tests/ui/missnamed_getters.rs +++ b/tests/ui/missnamed_getters.rs @@ -4,12 +4,32 @@ struct A { a: u8, b: u8, + c: u8, } impl A { fn a(&self) -> &u8 { &self.b } + fn a_mut(&mut self) -> &mut u8 { + &mut self.b + } + + fn b(self) -> u8 { + self.a + } + + fn b_mut(&mut self) -> &mut u8 { + &mut self.a + } + + fn c(&self) -> &u8 { + &self.b + } + + fn c_mut(&mut self) -> &mut u8 { + &mut self.a + } } union B { @@ -21,6 +41,25 @@ impl B { unsafe fn a(&self) -> &u8 { &self.b } + unsafe fn a_mut(&mut self) -> &mut u8 { + &mut self.b + } + + unsafe fn b(self) -> u8 { + self.a + } + + unsafe fn b_mut(&mut self) -> &mut u8 { + &mut self.a + } + + unsafe fn c(&self) -> &u8 { + &self.b + } + + unsafe fn c_mut(&mut self) -> &mut u8 { + &mut self.a + } } fn main() { diff --git a/tests/ui/missnamed_getters.stderr b/tests/ui/missnamed_getters.stderr index 8e31a42b97c1a..276096ade87f5 100644 --- a/tests/ui/missnamed_getters.stderr +++ b/tests/ui/missnamed_getters.stderr @@ -1,5 +1,5 @@ error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:11:9 + --> $DIR/missnamed_getters.rs:12:9 | LL | &self.b | ^^^^^^^ help: consider using: `&self.a` @@ -7,10 +7,58 @@ LL | &self.b = note: `-D clippy::missnamed-getters` implied by `-D warnings` error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:22:9 + --> $DIR/missnamed_getters.rs:15:9 + | +LL | &mut self.b + | ^^^^^^^^^^^ help: consider using: `&mut self.a` + +error: getter function appears to return the wrong field + --> $DIR/missnamed_getters.rs:19:9 + | +LL | self.a + | ^^^^^^ help: consider using: `self.b` + +error: getter function appears to return the wrong field + --> $DIR/missnamed_getters.rs:23:9 + | +LL | &mut self.a + | ^^^^^^^^^^^ help: consider using: `&mut self.b` + +error: getter function appears to return the wrong field + --> $DIR/missnamed_getters.rs:27:9 + | +LL | &self.b + | ^^^^^^^ help: consider using: `&self.c` + +error: getter function appears to return the wrong field + --> $DIR/missnamed_getters.rs:31:9 + | +LL | &mut self.a + | ^^^^^^^^^^^ help: consider using: `&mut self.c` + +error: getter function appears to return the wrong field + --> $DIR/missnamed_getters.rs:42:9 | LL | &self.b | ^^^^^^^ help: consider using: `&self.a` -error: aborting due to 2 previous errors +error: getter function appears to return the wrong field + --> $DIR/missnamed_getters.rs:45:9 + | +LL | &mut self.b + | ^^^^^^^^^^^ help: consider using: `&mut self.a` + +error: getter function appears to return the wrong field + --> $DIR/missnamed_getters.rs:49:9 + | +LL | self.a + | ^^^^^^ help: consider using: `self.b` + +error: getter function appears to return the wrong field + --> $DIR/missnamed_getters.rs:53:9 + | +LL | &mut self.a + | ^^^^^^^^^^^ help: consider using: `&mut self.b` + +error: aborting due to 10 previous errors From ddc49966dc4552b77582cf7e5aace8ac97d736fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Tue, 1 Nov 2022 19:28:06 +0100 Subject: [PATCH 017/244] Fix suggestion to point to the whole method --- .../src/functions/missnamed_getters.rs | 22 ++--- tests/ui/missnamed_getters.stderr | 90 ++++++++++++------- 2 files changed, 72 insertions(+), 40 deletions(-) diff --git a/clippy_lints/src/functions/missnamed_getters.rs b/clippy_lints/src/functions/missnamed_getters.rs index 1f4eefc620bdc..60922fb4ea497 100644 --- a/clippy_lints/src/functions/missnamed_getters.rs +++ b/clippy_lints/src/functions/missnamed_getters.rs @@ -1,4 +1,4 @@ -use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::source::snippet; use rustc_errors::Applicability; use rustc_hir::{intravisit::FnKind, Body, ExprKind, FnDecl, HirId, ImplicitSelfKind}; @@ -13,7 +13,7 @@ pub fn check_fn( kind: FnKind<'_>, decl: &FnDecl<'_>, body: &Body<'_>, - _span: Span, + span: Span, _hir_id: HirId, ) { let FnKind::Method(ref ident, sig) = kind else { @@ -55,6 +55,7 @@ pub fn check_fn( let expr_span = block_expr.span; let mut expr = block_expr; + // Accept &, &mut and if let ExprKind::AddrOf(_, _, tmp) = expr.kind { expr = tmp; } @@ -62,7 +63,7 @@ pub fn check_fn( if let ExprKind::Field(self_data, ident) = expr.kind; if ident.name.as_str() != name; then { - (self_data,ident) + (self_data, ident) } else { return; } @@ -121,16 +122,17 @@ pub fn check_fn( }; if cx.tcx.type_of(used_field.did) == cx.tcx.type_of(correct_field.did) { - let snippet = snippet(cx, expr_span, ".."); - let sugg = format!("{}{name}", snippet.strip_suffix(used_field.name.as_str()).unwrap()); - span_lint_and_sugg( + let left_span = block_expr.span.until(used_ident.span); + let snippet = snippet(cx, left_span, ".."); + let sugg = format!("{snippet}{name}"); + span_lint_and_then( cx, MISSNAMED_GETTERS, - expr_span, + span, "getter function appears to return the wrong field", - "consider using", - sugg, - Applicability::MaybeIncorrect, + |diag| { + diag.span_suggestion(expr_span, "consider using", sugg, Applicability::MaybeIncorrect); + }, ); } } diff --git a/tests/ui/missnamed_getters.stderr b/tests/ui/missnamed_getters.stderr index 276096ade87f5..2e3d9df34f45a 100644 --- a/tests/ui/missnamed_getters.stderr +++ b/tests/ui/missnamed_getters.stderr @@ -1,64 +1,94 @@ error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:12:9 + --> $DIR/missnamed_getters.rs:11:5 | -LL | &self.b - | ^^^^^^^ help: consider using: `&self.a` +LL | / fn a(&self) -> &u8 { +LL | | &self.b + | | ------- help: consider using: `&self.a` +LL | | } + | |_____^ | = note: `-D clippy::missnamed-getters` implied by `-D warnings` error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:15:9 + --> $DIR/missnamed_getters.rs:14:5 | -LL | &mut self.b - | ^^^^^^^^^^^ help: consider using: `&mut self.a` +LL | / fn a_mut(&mut self) -> &mut u8 { +LL | | &mut self.b + | | ----------- help: consider using: `&mut self.a` +LL | | } + | |_____^ error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:19:9 + --> $DIR/missnamed_getters.rs:18:5 | -LL | self.a - | ^^^^^^ help: consider using: `self.b` +LL | / fn b(self) -> u8 { +LL | | self.a + | | ------ help: consider using: `self.b` +LL | | } + | |_____^ error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:23:9 + --> $DIR/missnamed_getters.rs:22:5 | -LL | &mut self.a - | ^^^^^^^^^^^ help: consider using: `&mut self.b` +LL | / fn b_mut(&mut self) -> &mut u8 { +LL | | &mut self.a + | | ----------- help: consider using: `&mut self.b` +LL | | } + | |_____^ error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:27:9 + --> $DIR/missnamed_getters.rs:26:5 | -LL | &self.b - | ^^^^^^^ help: consider using: `&self.c` +LL | / fn c(&self) -> &u8 { +LL | | &self.b + | | ------- help: consider using: `&self.c` +LL | | } + | |_____^ error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:31:9 + --> $DIR/missnamed_getters.rs:30:5 | -LL | &mut self.a - | ^^^^^^^^^^^ help: consider using: `&mut self.c` +LL | / fn c_mut(&mut self) -> &mut u8 { +LL | | &mut self.a + | | ----------- help: consider using: `&mut self.c` +LL | | } + | |_____^ error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:42:9 + --> $DIR/missnamed_getters.rs:41:5 | -LL | &self.b - | ^^^^^^^ help: consider using: `&self.a` +LL | / unsafe fn a(&self) -> &u8 { +LL | | &self.b + | | ------- help: consider using: `&self.a` +LL | | } + | |_____^ error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:45:9 + --> $DIR/missnamed_getters.rs:44:5 | -LL | &mut self.b - | ^^^^^^^^^^^ help: consider using: `&mut self.a` +LL | / unsafe fn a_mut(&mut self) -> &mut u8 { +LL | | &mut self.b + | | ----------- help: consider using: `&mut self.a` +LL | | } + | |_____^ error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:49:9 + --> $DIR/missnamed_getters.rs:48:5 | -LL | self.a - | ^^^^^^ help: consider using: `self.b` +LL | / unsafe fn b(self) -> u8 { +LL | | self.a + | | ------ help: consider using: `self.b` +LL | | } + | |_____^ error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:53:9 + --> $DIR/missnamed_getters.rs:52:5 | -LL | &mut self.a - | ^^^^^^^^^^^ help: consider using: `&mut self.b` +LL | / unsafe fn b_mut(&mut self) -> &mut u8 { +LL | | &mut self.a + | | ----------- help: consider using: `&mut self.b` +LL | | } + | |_____^ error: aborting due to 10 previous errors From 81d459083499c35700f3d7fa2b28cdae2f35a636 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Tue, 1 Nov 2022 19:31:47 +0100 Subject: [PATCH 018/244] missnamed_getters: use all_fields iterator --- clippy_lints/src/functions/missnamed_getters.rs | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/clippy_lints/src/functions/missnamed_getters.rs b/clippy_lints/src/functions/missnamed_getters.rs index 60922fb4ea497..306dccdbd4899 100644 --- a/clippy_lints/src/functions/missnamed_getters.rs +++ b/clippy_lints/src/functions/missnamed_getters.rs @@ -83,24 +83,9 @@ pub fn check_fn( } }; - let variants = def.variants(); - - // We're accessing a field, so it should be an union or a struct and have one and only one variant - if variants.len() != 1 { - if cfg!(debug_assertions) { - panic!("Struct or union expected to have only one variant"); - } else { - // Don't ICE when possible - return; - } - } - - let first = variants.last().unwrap(); - let fields = &variants[first]; - let mut used_field = None; let mut correct_field = None; - for f in &fields.fields { + for f in def.all_fields() { if f.name.as_str() == name { correct_field = Some(f); } From 5fa0e07cdf5e6798a71ff252f14a2713699ae99d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Tue, 1 Nov 2022 21:36:18 +0100 Subject: [PATCH 019/244] Document missname_getters --- clippy_lints/src/declared_lints.rs | 2 +- clippy_lints/src/functions/mod.rs | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index 0c9ae6380d874..bccf8425968b4 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -177,6 +177,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::from_raw_with_void_ptr::FROM_RAW_WITH_VOID_PTR_INFO, crate::from_str_radix_10::FROM_STR_RADIX_10_INFO, crate::functions::DOUBLE_MUST_USE_INFO, + crate::functions::MISSNAMED_GETTERS_INFO, crate::functions::MUST_USE_CANDIDATE_INFO, crate::functions::MUST_USE_UNIT_INFO, crate::functions::NOT_UNSAFE_PTR_ARG_DEREF_INFO, @@ -184,7 +185,6 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::functions::RESULT_UNIT_ERR_INFO, crate::functions::TOO_MANY_ARGUMENTS_INFO, crate::functions::TOO_MANY_LINES_INFO, - crate::functions::MISSNAMED_GETTERS_INFO, crate::future_not_send::FUTURE_NOT_SEND_INFO, crate::if_let_mutex::IF_LET_MUTEX_INFO, crate::if_not_else::IF_NOT_ELSE_INFO, diff --git a/clippy_lints/src/functions/mod.rs b/clippy_lints/src/functions/mod.rs index 726df02444fc8..87792899b25ba 100644 --- a/clippy_lints/src/functions/mod.rs +++ b/clippy_lints/src/functions/mod.rs @@ -263,21 +263,38 @@ declare_clippy_lint! { declare_clippy_lint! { /// ### What it does + /// Checks for getter methods that return a field that doesn't correspond + /// to the name of the method, when there is a field's whose name matches that of the method. /// /// ### Why is this bad? + /// It is most likely that such a method is a bug caused by a typo or by copy-pasting. /// /// ### Example /// ```rust + /// struct A { + /// a: String, + /// b: String, + /// } + /// + /// impl A { + /// fn a(&self) -> &str{ + /// self.b + /// } + /// } /// // example code where clippy issues a warning /// ``` /// Use instead: /// ```rust - /// // example code which does not raise clippy warning + /// impl A { + /// fn a(&self) -> &str{ + /// self.a + /// } + /// } /// ``` #[clippy::version = "1.66.0"] pub MISSNAMED_GETTERS, suspicious, - "default lint description" + "getter method returning the wrong field" } #[derive(Copy, Clone)] From 3e2e81b2db9bbacc826ac429b52ac1173356a2e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Tue, 1 Nov 2022 21:49:20 +0100 Subject: [PATCH 020/244] Fix internal warnings --- clippy_lints/src/functions/missnamed_getters.rs | 5 ++--- clippy_lints/src/functions/mod.rs | 9 +++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/functions/missnamed_getters.rs b/clippy_lints/src/functions/missnamed_getters.rs index 306dccdbd4899..3d78679b526fc 100644 --- a/clippy_lints/src/functions/missnamed_getters.rs +++ b/clippy_lints/src/functions/missnamed_getters.rs @@ -28,15 +28,14 @@ pub fn check_fn( let name = ident.name.as_str(); let name = match sig.decl.implicit_self { - ImplicitSelfKind::ImmRef => name, ImplicitSelfKind::MutRef => { let Some(name) = name.strip_suffix("_mut") else { return; }; name }, - ImplicitSelfKind::Imm | ImplicitSelfKind::Mut => name, - _ => return, + ImplicitSelfKind::Imm | ImplicitSelfKind::Mut | ImplicitSelfKind::ImmRef => name, + ImplicitSelfKind::None => return, }; // Body must be &(mut) .name diff --git a/clippy_lints/src/functions/mod.rs b/clippy_lints/src/functions/mod.rs index 87792899b25ba..e4181a164cb75 100644 --- a/clippy_lints/src/functions/mod.rs +++ b/clippy_lints/src/functions/mod.rs @@ -278,16 +278,21 @@ declare_clippy_lint! { /// /// impl A { /// fn a(&self) -> &str{ - /// self.b + /// &self.b /// } /// } /// // example code where clippy issues a warning /// ``` /// Use instead: /// ```rust + /// struct A { + /// a: String, + /// b: String, + /// } + /// /// impl A { /// fn a(&self) -> &str{ - /// self.a + /// &self.a /// } /// } /// ``` From 3428da6e00c025b3b3141a9fe7c65ee5008e07f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Wed, 2 Nov 2022 09:01:33 +0100 Subject: [PATCH 021/244] Fix typo missnamed -> misnamed --- CHANGELOG.md | 2 +- clippy_lints/src/declared_lints.rs | 2 +- ...ssnamed_getters.rs => misnamed_getters.rs} | 4 ++-- clippy_lints/src/functions/mod.rs | 8 +++---- ...ssnamed_getters.rs => misnamed_getters.rs} | 2 +- ...getters.stderr => misnamed_getters.stderr} | 22 +++++++++---------- 6 files changed, 20 insertions(+), 20 deletions(-) rename clippy_lints/src/functions/{missnamed_getters.rs => misnamed_getters.rs} (98%) rename tests/ui/{missnamed_getters.rs => misnamed_getters.rs} (96%) rename tests/ui/{missnamed_getters.stderr => misnamed_getters.stderr} (83%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 180e7d8bedcd5..5b6b12c623af1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4188,6 +4188,7 @@ Released 2018-09-13 [`misaligned_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#misaligned_transmute [`mismatched_target_os`]: https://rust-lang.github.io/rust-clippy/master/index.html#mismatched_target_os [`mismatching_type_param_order`]: https://rust-lang.github.io/rust-clippy/master/index.html#mismatching_type_param_order +[`misnamed_getters`]: https://rust-lang.github.io/rust-clippy/master/index.html#misnamed_getters [`misrefactored_assign_op`]: https://rust-lang.github.io/rust-clippy/master/index.html#misrefactored_assign_op [`missing_const_for_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn [`missing_docs_in_private_items`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_docs_in_private_items @@ -4198,7 +4199,6 @@ Released 2018-09-13 [`missing_safety_doc`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_safety_doc [`missing_spin_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_spin_loop [`missing_trait_methods`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_trait_methods -[`missnamed_getters`]: https://rust-lang.github.io/rust-clippy/master/index.html#missnamed_getters [`mistyped_literal_suffixes`]: https://rust-lang.github.io/rust-clippy/master/index.html#mistyped_literal_suffixes [`mixed_case_hex_literals`]: https://rust-lang.github.io/rust-clippy/master/index.html#mixed_case_hex_literals [`mixed_read_write_in_expression`]: https://rust-lang.github.io/rust-clippy/master/index.html#mixed_read_write_in_expression diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index bccf8425968b4..eb3210946f119 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -177,7 +177,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::from_raw_with_void_ptr::FROM_RAW_WITH_VOID_PTR_INFO, crate::from_str_radix_10::FROM_STR_RADIX_10_INFO, crate::functions::DOUBLE_MUST_USE_INFO, - crate::functions::MISSNAMED_GETTERS_INFO, + crate::functions::MISNAMED_GETTERS_INFO, crate::functions::MUST_USE_CANDIDATE_INFO, crate::functions::MUST_USE_UNIT_INFO, crate::functions::NOT_UNSAFE_PTR_ARG_DEREF_INFO, diff --git a/clippy_lints/src/functions/missnamed_getters.rs b/clippy_lints/src/functions/misnamed_getters.rs similarity index 98% rename from clippy_lints/src/functions/missnamed_getters.rs rename to clippy_lints/src/functions/misnamed_getters.rs index 3d78679b526fc..3859c7a62ea49 100644 --- a/clippy_lints/src/functions/missnamed_getters.rs +++ b/clippy_lints/src/functions/misnamed_getters.rs @@ -6,7 +6,7 @@ use rustc_lint::LateContext; use rustc_middle::ty; use rustc_span::Span; -use super::MISSNAMED_GETTERS; +use super::MISNAMED_GETTERS; pub fn check_fn( cx: &LateContext<'_>, @@ -111,7 +111,7 @@ pub fn check_fn( let sugg = format!("{snippet}{name}"); span_lint_and_then( cx, - MISSNAMED_GETTERS, + MISNAMED_GETTERS, span, "getter function appears to return the wrong field", |diag| { diff --git a/clippy_lints/src/functions/mod.rs b/clippy_lints/src/functions/mod.rs index e4181a164cb75..3478fdd8624a8 100644 --- a/clippy_lints/src/functions/mod.rs +++ b/clippy_lints/src/functions/mod.rs @@ -1,4 +1,4 @@ -mod missnamed_getters; +mod misnamed_getters; mod must_use; mod not_unsafe_ptr_arg_deref; mod result; @@ -297,7 +297,7 @@ declare_clippy_lint! { /// } /// ``` #[clippy::version = "1.66.0"] - pub MISSNAMED_GETTERS, + pub MISNAMED_GETTERS, suspicious, "getter method returning the wrong field" } @@ -328,7 +328,7 @@ impl_lint_pass!(Functions => [ MUST_USE_CANDIDATE, RESULT_UNIT_ERR, RESULT_LARGE_ERR, - MISSNAMED_GETTERS, + MISNAMED_GETTERS, ]); impl<'tcx> LateLintPass<'tcx> for Functions { @@ -344,7 +344,7 @@ impl<'tcx> LateLintPass<'tcx> for Functions { too_many_arguments::check_fn(cx, kind, decl, span, hir_id, self.too_many_arguments_threshold); too_many_lines::check_fn(cx, kind, span, body, self.too_many_lines_threshold); not_unsafe_ptr_arg_deref::check_fn(cx, kind, decl, body, hir_id); - missnamed_getters::check_fn(cx, kind, decl, body, span, hir_id); + misnamed_getters::check_fn(cx, kind, decl, body, span, hir_id); } fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) { diff --git a/tests/ui/missnamed_getters.rs b/tests/ui/misnamed_getters.rs similarity index 96% rename from tests/ui/missnamed_getters.rs rename to tests/ui/misnamed_getters.rs index f9c2351f833d2..7d490f4b143cc 100644 --- a/tests/ui/missnamed_getters.rs +++ b/tests/ui/misnamed_getters.rs @@ -1,5 +1,5 @@ #![allow(unused)] -#![warn(clippy::missnamed_getters)] +#![warn(clippy::misnamed_getters)] struct A { a: u8, diff --git a/tests/ui/missnamed_getters.stderr b/tests/ui/misnamed_getters.stderr similarity index 83% rename from tests/ui/missnamed_getters.stderr rename to tests/ui/misnamed_getters.stderr index 2e3d9df34f45a..f4a059ab9b55f 100644 --- a/tests/ui/missnamed_getters.stderr +++ b/tests/ui/misnamed_getters.stderr @@ -1,5 +1,5 @@ error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:11:5 + --> $DIR/misnamed_getters.rs:11:5 | LL | / fn a(&self) -> &u8 { LL | | &self.b @@ -7,10 +7,10 @@ LL | | &self.b LL | | } | |_____^ | - = note: `-D clippy::missnamed-getters` implied by `-D warnings` + = note: `-D clippy::misnamed-getters` implied by `-D warnings` error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:14:5 + --> $DIR/misnamed_getters.rs:14:5 | LL | / fn a_mut(&mut self) -> &mut u8 { LL | | &mut self.b @@ -19,7 +19,7 @@ LL | | } | |_____^ error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:18:5 + --> $DIR/misnamed_getters.rs:18:5 | LL | / fn b(self) -> u8 { LL | | self.a @@ -28,7 +28,7 @@ LL | | } | |_____^ error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:22:5 + --> $DIR/misnamed_getters.rs:22:5 | LL | / fn b_mut(&mut self) -> &mut u8 { LL | | &mut self.a @@ -37,7 +37,7 @@ LL | | } | |_____^ error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:26:5 + --> $DIR/misnamed_getters.rs:26:5 | LL | / fn c(&self) -> &u8 { LL | | &self.b @@ -46,7 +46,7 @@ LL | | } | |_____^ error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:30:5 + --> $DIR/misnamed_getters.rs:30:5 | LL | / fn c_mut(&mut self) -> &mut u8 { LL | | &mut self.a @@ -55,7 +55,7 @@ LL | | } | |_____^ error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:41:5 + --> $DIR/misnamed_getters.rs:41:5 | LL | / unsafe fn a(&self) -> &u8 { LL | | &self.b @@ -64,7 +64,7 @@ LL | | } | |_____^ error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:44:5 + --> $DIR/misnamed_getters.rs:44:5 | LL | / unsafe fn a_mut(&mut self) -> &mut u8 { LL | | &mut self.b @@ -73,7 +73,7 @@ LL | | } | |_____^ error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:48:5 + --> $DIR/misnamed_getters.rs:48:5 | LL | / unsafe fn b(self) -> u8 { LL | | self.a @@ -82,7 +82,7 @@ LL | | } | |_____^ error: getter function appears to return the wrong field - --> $DIR/missnamed_getters.rs:52:5 + --> $DIR/misnamed_getters.rs:52:5 | LL | / unsafe fn b_mut(&mut self) -> &mut u8 { LL | | &mut self.a From a867c17ab36926a575a57e24a03e99db672055f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Wed, 2 Nov 2022 19:02:46 +0100 Subject: [PATCH 022/244] Improve code --- .../src/functions/misnamed_getters.rs | 13 ++++++----- tests/ui/misnamed_getters.rs | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/functions/misnamed_getters.rs b/clippy_lints/src/functions/misnamed_getters.rs index 3859c7a62ea49..0d50ec37989b9 100644 --- a/clippy_lints/src/functions/misnamed_getters.rs +++ b/clippy_lints/src/functions/misnamed_getters.rs @@ -16,7 +16,7 @@ pub fn check_fn( span: Span, _hir_id: HirId, ) { - let FnKind::Method(ref ident, sig) = kind else { + let FnKind::Method(ref ident, _) = kind else { return; }; @@ -27,7 +27,7 @@ pub fn check_fn( let name = ident.name.as_str(); - let name = match sig.decl.implicit_self { + let name = match decl.implicit_self { ImplicitSelfKind::MutRef => { let Some(name) = name.strip_suffix("_mut") else { return; @@ -53,11 +53,12 @@ pub fn check_fn( }; let expr_span = block_expr.span; - let mut expr = block_expr; // Accept &, &mut and - if let ExprKind::AddrOf(_, _, tmp) = expr.kind { - expr = tmp; - } + let expr = if let ExprKind::AddrOf(_, _, tmp) = block_expr.kind { + tmp + } else { + block_expr + }; let (self_data, used_ident) = if_chain! { if let ExprKind::Field(self_data, ident) = expr.kind; if ident.name.as_str() != name; diff --git a/tests/ui/misnamed_getters.rs b/tests/ui/misnamed_getters.rs index 7d490f4b143cc..6f16181825167 100644 --- a/tests/ui/misnamed_getters.rs +++ b/tests/ui/misnamed_getters.rs @@ -60,6 +60,29 @@ impl B { unsafe fn c_mut(&mut self) -> &mut u8 { &mut self.a } + + unsafe fn a_unchecked(&self) -> &u8 { + &self.b + } + unsafe fn a_unchecked_mut(&mut self) -> &mut u8 { + &mut self.b + } + + unsafe fn b_unchecked(self) -> u8 { + self.a + } + + unsafe fn b_unchecked_mut(&mut self) -> &mut u8 { + &mut self.a + } + + unsafe fn c_unchecked(&self) -> &u8 { + &self.b + } + + unsafe fn c_unchecked_mut(&mut self) -> &mut u8 { + &mut self.a + } } fn main() { From 3f1a186bd1fd74ad3b25bf5c642e86c8913a1149 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Wed, 2 Nov 2022 19:11:27 +0100 Subject: [PATCH 023/244] misnamed_getters: Trigger on unsafe with _unchecked --- .../src/functions/misnamed_getters.rs | 10 ++++- tests/ui/misnamed_getters.stderr | 38 ++++++++++++++++++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/functions/misnamed_getters.rs b/clippy_lints/src/functions/misnamed_getters.rs index 0d50ec37989b9..599269e2d6318 100644 --- a/clippy_lints/src/functions/misnamed_getters.rs +++ b/clippy_lints/src/functions/misnamed_getters.rs @@ -1,7 +1,7 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::source::snippet; use rustc_errors::Applicability; -use rustc_hir::{intravisit::FnKind, Body, ExprKind, FnDecl, HirId, ImplicitSelfKind}; +use rustc_hir::{intravisit::FnKind, Body, ExprKind, FnDecl, HirId, ImplicitSelfKind, Unsafety}; use rustc_lint::LateContext; use rustc_middle::ty; use rustc_span::Span; @@ -16,7 +16,7 @@ pub fn check_fn( span: Span, _hir_id: HirId, ) { - let FnKind::Method(ref ident, _) = kind else { + let FnKind::Method(ref ident, sig) = kind else { return; }; @@ -38,6 +38,12 @@ pub fn check_fn( ImplicitSelfKind::None => return, }; + let name = if sig.header.unsafety == Unsafety::Unsafe { + name.strip_suffix("_unchecked").unwrap_or(name) + } else { + name + }; + // Body must be &(mut) .name // self_data is not neccessarilly self, to also lint sub-getters, etc… diff --git a/tests/ui/misnamed_getters.stderr b/tests/ui/misnamed_getters.stderr index f4a059ab9b55f..feefb95ab4f8f 100644 --- a/tests/ui/misnamed_getters.stderr +++ b/tests/ui/misnamed_getters.stderr @@ -90,5 +90,41 @@ LL | | &mut self.a LL | | } | |_____^ -error: aborting due to 10 previous errors +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:64:5 + | +LL | / unsafe fn a_unchecked(&self) -> &u8 { +LL | | &self.b + | | ------- help: consider using: `&self.a` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:67:5 + | +LL | / unsafe fn a_unchecked_mut(&mut self) -> &mut u8 { +LL | | &mut self.b + | | ----------- help: consider using: `&mut self.a` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:71:5 + | +LL | / unsafe fn b_unchecked(self) -> u8 { +LL | | self.a + | | ------ help: consider using: `self.b` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:75:5 + | +LL | / unsafe fn b_unchecked_mut(&mut self) -> &mut u8 { +LL | | &mut self.a + | | ----------- help: consider using: `&mut self.b` +LL | | } + | |_____^ + +error: aborting due to 14 previous errors From 77374a95272dfb8fcbc75cd074b4e61b1890c724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Sat, 12 Nov 2022 22:33:46 +0100 Subject: [PATCH 024/244] Add failing test --- tests/ui/misnamed_getters.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/ui/misnamed_getters.rs b/tests/ui/misnamed_getters.rs index 6f16181825167..2c451bd3a162a 100644 --- a/tests/ui/misnamed_getters.rs +++ b/tests/ui/misnamed_getters.rs @@ -85,6 +85,18 @@ impl B { } } +struct C { + inner: Box, +} +impl C { + unsafe fn a(&self) -> &u8 { + &self.inner.b + } + unsafe fn a_mut(&mut self) -> &mut u8 { + &mut self.inner.b + } +} + fn main() { // test code goes here } From d9993cb133909eb17705cd9dc518c2c213fc779b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Sun, 20 Nov 2022 12:20:16 +0100 Subject: [PATCH 025/244] Remove error when fields use autoderef --- clippy_lints/src/functions/misnamed_getters.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/clippy_lints/src/functions/misnamed_getters.rs b/clippy_lints/src/functions/misnamed_getters.rs index 599269e2d6318..c553901059bb7 100644 --- a/clippy_lints/src/functions/misnamed_getters.rs +++ b/clippy_lints/src/functions/misnamed_getters.rs @@ -101,16 +101,14 @@ pub fn check_fn( } let Some(used_field) = used_field else { - if cfg!(debug_assertions) { - panic!("Struct doesn't contain the correct field"); - } else { - // Don't ICE when possible - return; - } - }; + // FIXME: This can be reached if the field access uses autoderef. + // `dec.all_fields()` should be replaced by something that uses autoderef. + return; + }; + let Some(correct_field) = correct_field else { return; - }; + }; if cx.tcx.type_of(used_field.did) == cx.tcx.type_of(correct_field.did) { let left_span = block_expr.span.until(used_ident.span); From 6178ddaded7f73ba413475ea27ac98336069cb11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Sun, 20 Nov 2022 13:46:30 +0100 Subject: [PATCH 026/244] misname-getters: Fix documentation --- clippy_lints/src/functions/mod.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/functions/mod.rs b/clippy_lints/src/functions/mod.rs index 3478fdd8624a8..91e6ffe644790 100644 --- a/clippy_lints/src/functions/mod.rs +++ b/clippy_lints/src/functions/mod.rs @@ -270,6 +270,7 @@ declare_clippy_lint! { /// It is most likely that such a method is a bug caused by a typo or by copy-pasting. /// /// ### Example + /// ```rust /// struct A { /// a: String, @@ -281,7 +282,7 @@ declare_clippy_lint! { /// &self.b /// } /// } - /// // example code where clippy issues a warning + /// ``` /// Use instead: /// ```rust @@ -296,7 +297,7 @@ declare_clippy_lint! { /// } /// } /// ``` - #[clippy::version = "1.66.0"] + #[clippy::version = "1.67.0"] pub MISNAMED_GETTERS, suspicious, "getter method returning the wrong field" From a1bf25e2bdbebad2f1f42118ba1d2c4a7ae4f7b0 Mon Sep 17 00:00:00 2001 From: Markus Everling Date: Thu, 13 Oct 2022 00:58:02 +0200 Subject: [PATCH 027/244] Update VecDeque implementation --- .../alloc/src/collections/vec_deque/drain.rs | 180 ++- .../alloc/src/collections/vec_deque/iter.rs | 176 +-- .../src/collections/vec_deque/iter_mut.rs | 151 ++- .../alloc/src/collections/vec_deque/mod.rs | 1142 ++++++----------- .../src/collections/vec_deque/pair_slices.rs | 67 - .../src/collections/vec_deque/ring_slices.rs | 56 - .../src/collections/vec_deque/spec_extend.rs | 72 +- .../alloc/src/collections/vec_deque/tests.rs | 146 +-- library/alloc/tests/vec_deque.rs | 13 +- src/etc/gdb_providers.py | 8 +- src/etc/lldb_providers.py | 12 +- src/test/ui/hygiene/panic-location.run.stderr | 2 +- 12 files changed, 774 insertions(+), 1251 deletions(-) delete mode 100644 library/alloc/src/collections/vec_deque/pair_slices.rs delete mode 100644 library/alloc/src/collections/vec_deque/ring_slices.rs diff --git a/library/alloc/src/collections/vec_deque/drain.rs b/library/alloc/src/collections/vec_deque/drain.rs index 41baa7102cdce..a4a96bad5e042 100644 --- a/library/alloc/src/collections/vec_deque/drain.rs +++ b/library/alloc/src/collections/vec_deque/drain.rs @@ -1,12 +1,12 @@ -use core::fmt; use core::iter::FusedIterator; use core::marker::PhantomData; -use core::mem::{self, MaybeUninit}; -use core::ptr::{self, NonNull}; +use core::mem::{self, SizedTypeProperties}; +use core::ptr::NonNull; +use core::{fmt, ptr}; use crate::alloc::{Allocator, Global}; -use super::{count, wrap_index, VecDeque}; +use super::VecDeque; /// A draining iterator over the elements of a `VecDeque`. /// @@ -20,26 +20,61 @@ pub struct Drain< T: 'a, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, > { - after_tail: usize, - after_head: usize, - ring: NonNull<[T]>, - tail: usize, - head: usize, + // We can't just use a &mut VecDeque, as that would make Drain invariant over T + // and we want it to be covariant instead deque: NonNull>, - _phantom: PhantomData<&'a T>, + // drain_start is stored in deque.len + drain_len: usize, + // index into the logical array, not the physical one (always lies in [0..deque.len)) + idx: usize, + // number of elements after the drain range + tail_len: usize, + remaining: usize, + // Needed to make Drain covariant over T + _marker: PhantomData<&'a T>, } impl<'a, T, A: Allocator> Drain<'a, T, A> { pub(super) unsafe fn new( - after_tail: usize, - after_head: usize, - ring: &'a [MaybeUninit], - tail: usize, - head: usize, - deque: NonNull>, + deque: &'a mut VecDeque, + drain_start: usize, + drain_len: usize, ) -> Self { - let ring = unsafe { NonNull::new_unchecked(ring as *const [MaybeUninit] as *mut _) }; - Drain { after_tail, after_head, ring, tail, head, deque, _phantom: PhantomData } + let orig_len = mem::replace(&mut deque.len, drain_start); + let tail_len = orig_len - drain_start - drain_len; + Drain { + deque: NonNull::from(deque), + drain_len, + idx: drain_start, + tail_len, + remaining: drain_len, + _marker: PhantomData, + } + } + + // Only returns pointers to the slices, as that's + // all we need to drop them + fn as_slices(&self) -> (*mut [T], *mut [T]) { + unsafe { + let deque = self.deque.as_ref(); + let wrapped_start = deque.wrap_idx(self.idx); + + if self.remaining <= deque.capacity() - wrapped_start { + // there's only one contigous slice + ( + ptr::slice_from_raw_parts_mut(deque.ptr().add(wrapped_start), self.remaining), + &mut [] as *mut [T], + ) + } else { + let head_len = deque.capacity() - wrapped_start; + // this will never overflow due to the if condition + let tail_len = self.remaining - head_len; + ( + ptr::slice_from_raw_parts_mut(deque.ptr().add(wrapped_start), head_len), + ptr::slice_from_raw_parts_mut(deque.ptr(), tail_len), + ) + } + } } } @@ -47,11 +82,10 @@ impl<'a, T, A: Allocator> Drain<'a, T, A> { impl fmt::Debug for Drain<'_, T, A> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("Drain") - .field(&self.after_tail) - .field(&self.after_head) - .field(&self.ring) - .field(&self.tail) - .field(&self.head) + .field(&self.drain_len) + .field(&self.idx) + .field(&self.tail_len) + .field(&self.remaining) .finish() } } @@ -68,57 +102,77 @@ impl Drop for Drain<'_, T, A> { impl<'r, 'a, T, A: Allocator> Drop for DropGuard<'r, 'a, T, A> { fn drop(&mut self) { - self.0.for_each(drop); + if self.0.remaining != 0 { + let (front, back) = self.0.as_slices(); + unsafe { + ptr::drop_in_place(front); + ptr::drop_in_place(back); + } + } let source_deque = unsafe { self.0.deque.as_mut() }; - // T = source_deque_tail; H = source_deque_head; t = drain_tail; h = drain_head - // - // T t h H - // [. . . o o x x o o . . .] - // - let orig_tail = source_deque.tail; - let drain_tail = source_deque.head; - let drain_head = self.0.after_tail; - let orig_head = self.0.after_head; + let drain_start = source_deque.len(); + let drain_len = self.0.drain_len; + let drain_end = drain_start + drain_len; - let tail_len = count(orig_tail, drain_tail, source_deque.cap()); - let head_len = count(drain_head, orig_head, source_deque.cap()); + let orig_len = self.0.tail_len + drain_end; + + if T::IS_ZST { + // no need to copy around any memory if T is a ZST + source_deque.len = orig_len - drain_len; + return; + } - // Restore the original head value - source_deque.head = orig_head; + let head_len = drain_start; + let tail_len = self.0.tail_len; - match (tail_len, head_len) { + match (head_len, tail_len) { (0, 0) => { source_deque.head = 0; - source_deque.tail = 0; + source_deque.len = 0; } (0, _) => { - source_deque.tail = drain_head; + source_deque.head = source_deque.wrap_idx(drain_len); + source_deque.len = orig_len - drain_len; } (_, 0) => { - source_deque.head = drain_tail; + source_deque.len = orig_len - drain_len; } _ => unsafe { - if tail_len <= head_len { - source_deque.tail = source_deque.wrap_sub(drain_head, tail_len); - source_deque.wrap_copy(source_deque.tail, orig_tail, tail_len); + if head_len <= tail_len { + source_deque.wrap_copy( + source_deque.head, + source_deque.wrap_idx(drain_len), + head_len, + ); + source_deque.head = source_deque.wrap_idx(drain_len); + source_deque.len = orig_len - drain_len; } else { - source_deque.head = source_deque.wrap_add(drain_tail, head_len); - source_deque.wrap_copy(drain_tail, drain_head, head_len); + source_deque.wrap_copy( + source_deque.wrap_idx(head_len + drain_len), + source_deque.wrap_idx(head_len), + tail_len, + ); + source_deque.len = orig_len - drain_len; } }, } } } - while let Some(item) = self.next() { - let guard = DropGuard(self); - drop(item); - mem::forget(guard); + let guard = DropGuard(self); + let (front, back) = guard.0.as_slices(); + unsafe { + // since idx is a logical index, we don't need to worry about wrapping. + guard.0.idx += front.len(); + guard.0.remaining -= front.len(); + ptr::drop_in_place(front); + guard.0.remaining = 0; + ptr::drop_in_place(back); } - DropGuard(self); + // Dropping `guard` handles moving the remaining elements into place. } } @@ -128,20 +182,18 @@ impl Iterator for Drain<'_, T, A> { #[inline] fn next(&mut self) -> Option { - if self.tail == self.head { + if self.remaining == 0 { return None; } - let tail = self.tail; - self.tail = wrap_index(self.tail.wrapping_add(1), self.ring.len()); - // Safety: - // - `self.tail` in a ring buffer is always a valid index. - // - `self.head` and `self.tail` equality is checked above. - unsafe { Some(ptr::read(self.ring.as_ptr().get_unchecked_mut(tail))) } + let wrapped_idx = unsafe { self.deque.as_ref().wrap_idx(self.idx) }; + self.idx += 1; + self.remaining -= 1; + Some(unsafe { self.deque.as_mut().buffer_read(wrapped_idx) }) } #[inline] fn size_hint(&self) -> (usize, Option) { - let len = count(self.tail, self.head, self.ring.len()); + let len = self.remaining; (len, Some(len)) } } @@ -150,14 +202,12 @@ impl Iterator for Drain<'_, T, A> { impl DoubleEndedIterator for Drain<'_, T, A> { #[inline] fn next_back(&mut self) -> Option { - if self.tail == self.head { + if self.remaining == 0 { return None; } - self.head = wrap_index(self.head.wrapping_sub(1), self.ring.len()); - // Safety: - // - `self.head` in a ring buffer is always a valid index. - // - `self.head` and `self.tail` equality is checked above. - unsafe { Some(ptr::read(self.ring.as_ptr().get_unchecked_mut(self.head))) } + self.remaining -= 1; + let wrapped_idx = unsafe { self.deque.as_ref().wrap_idx(self.idx + self.remaining) }; + Some(unsafe { self.deque.as_mut().buffer_read(wrapped_idx) }) } } diff --git a/library/alloc/src/collections/vec_deque/iter.rs b/library/alloc/src/collections/vec_deque/iter.rs index e696d7ed636b5..d4e66db4903c3 100644 --- a/library/alloc/src/collections/vec_deque/iter.rs +++ b/library/alloc/src/collections/vec_deque/iter.rs @@ -1,9 +1,6 @@ -use core::fmt; use core::iter::{FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce}; -use core::mem::MaybeUninit; use core::ops::Try; - -use super::{count, wrap_index, RingSlices}; +use core::{fmt, mem, slice}; /// An iterator over the elements of a `VecDeque`. /// @@ -13,30 +10,20 @@ use super::{count, wrap_index, RingSlices}; /// [`iter`]: super::VecDeque::iter #[stable(feature = "rust1", since = "1.0.0")] pub struct Iter<'a, T: 'a> { - ring: &'a [MaybeUninit], - tail: usize, - head: usize, + i1: slice::Iter<'a, T>, + i2: slice::Iter<'a, T>, } impl<'a, T> Iter<'a, T> { - pub(super) fn new(ring: &'a [MaybeUninit], tail: usize, head: usize) -> Self { - Iter { ring, tail, head } + pub(super) fn new(i1: slice::Iter<'a, T>, i2: slice::Iter<'a, T>) -> Self { + Self { i1, i2 } } } #[stable(feature = "collection_debug", since = "1.17.0")] impl fmt::Debug for Iter<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail); - // Safety: - // - `self.head` and `self.tail` in a ring buffer are always valid indices. - // - `RingSlices::ring_slices` guarantees that the slices split according to `self.head` and `self.tail` are initialized. - unsafe { - f.debug_tuple("Iter") - .field(&MaybeUninit::slice_assume_init_ref(front)) - .field(&MaybeUninit::slice_assume_init_ref(back)) - .finish() - } + f.debug_tuple("Iter").field(&self.i1.as_slice()).field(&self.i2.as_slice()).finish() } } @@ -44,7 +31,7 @@ impl fmt::Debug for Iter<'_, T> { #[stable(feature = "rust1", since = "1.0.0")] impl Clone for Iter<'_, T> { fn clone(&self) -> Self { - Iter { ring: self.ring, tail: self.tail, head: self.head } + Iter { i1: self.i1.clone(), i2: self.i2.clone() } } } @@ -52,37 +39,43 @@ impl Clone for Iter<'_, T> { impl<'a, T> Iterator for Iter<'a, T> { type Item = &'a T; + fn advance_by(&mut self, n: usize) -> Result<(), usize> { + let m = match self.i1.advance_by(n) { + Ok(_) => return Ok(()), + Err(m) => m, + }; + mem::swap(&mut self.i1, &mut self.i2); + self.i1.advance_by(n - m).map_err(|o| o + m) + } + #[inline] fn next(&mut self) -> Option<&'a T> { - if self.tail == self.head { - return None; + match self.i1.next() { + Some(val) => Some(val), + None => { + // most of the time, the iterator will either always + // call next(), or always call next_back(). By swapping + // the iterators once the first one is empty, we ensure + // that the first branch is taken as often as possible, + // without sacrificing correctness, as i1 is empty anyways + mem::swap(&mut self.i1, &mut self.i2); + self.i1.next() + } } - let tail = self.tail; - self.tail = wrap_index(self.tail.wrapping_add(1), self.ring.len()); - // Safety: - // - `self.tail` in a ring buffer is always a valid index. - // - `self.head` and `self.tail` equality is checked above. - unsafe { Some(self.ring.get_unchecked(tail).assume_init_ref()) } } #[inline] fn size_hint(&self) -> (usize, Option) { - let len = count(self.tail, self.head, self.ring.len()); + let len = self.len(); (len, Some(len)) } - fn fold(self, mut accum: Acc, mut f: F) -> Acc + fn fold(self, accum: Acc, mut f: F) -> Acc where F: FnMut(Acc, Self::Item) -> Acc, { - let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail); - // Safety: - // - `self.head` and `self.tail` in a ring buffer are always valid indices. - // - `RingSlices::ring_slices` guarantees that the slices split according to `self.head` and `self.tail` are initialized. - unsafe { - accum = MaybeUninit::slice_assume_init_ref(front).iter().fold(accum, &mut f); - MaybeUninit::slice_assume_init_ref(back).iter().fold(accum, &mut f) - } + let accum = self.i1.fold(accum, &mut f); + self.i2.fold(accum, f) } fn try_fold(&mut self, init: B, mut f: F) -> R @@ -91,35 +84,8 @@ impl<'a, T> Iterator for Iter<'a, T> { F: FnMut(B, Self::Item) -> R, R: Try, { - let (mut iter, final_res); - if self.tail <= self.head { - // Safety: single slice self.ring[self.tail..self.head] is initialized. - iter = unsafe { MaybeUninit::slice_assume_init_ref(&self.ring[self.tail..self.head]) } - .iter(); - final_res = iter.try_fold(init, &mut f); - } else { - // Safety: two slices: self.ring[self.tail..], self.ring[..self.head] both are initialized. - let (front, back) = self.ring.split_at(self.tail); - - let mut back_iter = unsafe { MaybeUninit::slice_assume_init_ref(back).iter() }; - let res = back_iter.try_fold(init, &mut f); - let len = self.ring.len(); - self.tail = (self.ring.len() - back_iter.len()) & (len - 1); - iter = unsafe { MaybeUninit::slice_assume_init_ref(&front[..self.head]).iter() }; - final_res = iter.try_fold(res?, &mut f); - } - self.tail = self.head - iter.len(); - final_res - } - - fn nth(&mut self, n: usize) -> Option { - if n >= count(self.tail, self.head, self.ring.len()) { - self.tail = self.head; - None - } else { - self.tail = wrap_index(self.tail.wrapping_add(n), self.ring.len()); - self.next() - } + let acc = self.i1.try_fold(init, &mut f)?; + self.i2.try_fold(acc, f) } #[inline] @@ -132,8 +98,12 @@ impl<'a, T> Iterator for Iter<'a, T> { // Safety: The TrustedRandomAccess contract requires that callers only pass an index // that is in bounds. unsafe { - let idx = wrap_index(self.tail.wrapping_add(idx), self.ring.len()); - self.ring.get_unchecked(idx).assume_init_ref() + let i1_len = self.i1.len(); + if idx < i1_len { + self.i1.__iterator_get_unchecked(idx) + } else { + self.i2.__iterator_get_unchecked(idx - i1_len) + } } } } @@ -142,28 +112,36 @@ impl<'a, T> Iterator for Iter<'a, T> { impl<'a, T> DoubleEndedIterator for Iter<'a, T> { #[inline] fn next_back(&mut self) -> Option<&'a T> { - if self.tail == self.head { - return None; + match self.i2.next_back() { + Some(val) => Some(val), + None => { + // most of the time, the iterator will either always + // call next(), or always call next_back(). By swapping + // the iterators once the first one is empty, we ensure + // that the first branch is taken as often as possible, + // without sacrificing correctness, as i2 is empty anyways + mem::swap(&mut self.i1, &mut self.i2); + self.i2.next_back() + } } - self.head = wrap_index(self.head.wrapping_sub(1), self.ring.len()); - // Safety: - // - `self.head` in a ring buffer is always a valid index. - // - `self.head` and `self.tail` equality is checked above. - unsafe { Some(self.ring.get_unchecked(self.head).assume_init_ref()) } } - fn rfold(self, mut accum: Acc, mut f: F) -> Acc + fn advance_back_by(&mut self, n: usize) -> Result<(), usize> { + let m = match self.i2.advance_back_by(n) { + Ok(_) => return Ok(()), + Err(m) => m, + }; + + mem::swap(&mut self.i1, &mut self.i2); + self.i2.advance_back_by(n - m).map_err(|o| m + o) + } + + fn rfold(self, accum: Acc, mut f: F) -> Acc where F: FnMut(Acc, Self::Item) -> Acc, { - let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail); - // Safety: - // - `self.head` and `self.tail` in a ring buffer are always valid indices. - // - `RingSlices::ring_slices` guarantees that the slices split according to `self.head` and `self.tail` are initialized. - unsafe { - accum = MaybeUninit::slice_assume_init_ref(back).iter().rfold(accum, &mut f); - MaybeUninit::slice_assume_init_ref(front).iter().rfold(accum, &mut f) - } + let accum = self.i2.rfold(accum, &mut f); + self.i1.rfold(accum, f) } fn try_rfold(&mut self, init: B, mut f: F) -> R @@ -172,33 +150,19 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> { F: FnMut(B, Self::Item) -> R, R: Try, { - let (mut iter, final_res); - if self.tail <= self.head { - // Safety: single slice self.ring[self.tail..self.head] is initialized. - iter = unsafe { - MaybeUninit::slice_assume_init_ref(&self.ring[self.tail..self.head]).iter() - }; - final_res = iter.try_rfold(init, &mut f); - } else { - // Safety: two slices: self.ring[self.tail..], self.ring[..self.head] both are initialized. - let (front, back) = self.ring.split_at(self.tail); - - let mut front_iter = - unsafe { MaybeUninit::slice_assume_init_ref(&front[..self.head]).iter() }; - let res = front_iter.try_rfold(init, &mut f); - self.head = front_iter.len(); - iter = unsafe { MaybeUninit::slice_assume_init_ref(back).iter() }; - final_res = iter.try_rfold(res?, &mut f); - } - self.head = self.tail + iter.len(); - final_res + let acc = self.i2.try_rfold(init, &mut f)?; + self.i1.try_rfold(acc, f) } } #[stable(feature = "rust1", since = "1.0.0")] impl ExactSizeIterator for Iter<'_, T> { + fn len(&self) -> usize { + self.i1.len() + self.i2.len() + } + fn is_empty(&self) -> bool { - self.head == self.tail + self.i1.is_empty() && self.i2.is_empty() } } diff --git a/library/alloc/src/collections/vec_deque/iter_mut.rs b/library/alloc/src/collections/vec_deque/iter_mut.rs index b78c0d5e1b3cf..7c955663bde7e 100644 --- a/library/alloc/src/collections/vec_deque/iter_mut.rs +++ b/library/alloc/src/collections/vec_deque/iter_mut.rs @@ -1,8 +1,6 @@ -use core::fmt; use core::iter::{FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce}; -use core::marker::PhantomData; - -use super::{count, wrap_index, RingSlices}; +use core::ops::Try; +use core::{fmt, mem, slice}; /// A mutable iterator over the elements of a `VecDeque`. /// @@ -12,39 +10,20 @@ use super::{count, wrap_index, RingSlices}; /// [`iter_mut`]: super::VecDeque::iter_mut #[stable(feature = "rust1", since = "1.0.0")] pub struct IterMut<'a, T: 'a> { - // Internal safety invariant: the entire slice is dereferenceable. - ring: *mut [T], - tail: usize, - head: usize, - phantom: PhantomData<&'a mut [T]>, + i1: slice::IterMut<'a, T>, + i2: slice::IterMut<'a, T>, } impl<'a, T> IterMut<'a, T> { - pub(super) unsafe fn new( - ring: *mut [T], - tail: usize, - head: usize, - phantom: PhantomData<&'a mut [T]>, - ) -> Self { - IterMut { ring, tail, head, phantom } + pub(super) fn new(i1: slice::IterMut<'a, T>, i2: slice::IterMut<'a, T>) -> Self { + Self { i1, i2 } } } -// SAFETY: we do nothing thread-local and there is no interior mutability, -// so the usual structural `Send`/`Sync` apply. -#[stable(feature = "rust1", since = "1.0.0")] -unsafe impl Send for IterMut<'_, T> {} -#[stable(feature = "rust1", since = "1.0.0")] -unsafe impl Sync for IterMut<'_, T> {} - #[stable(feature = "collection_debug", since = "1.17.0")] impl fmt::Debug for IterMut<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail); - // SAFETY: these are the elements we have not handed out yet, so aliasing is fine. - // The `IterMut` invariant also ensures everything is dereferenceable. - let (front, back) = unsafe { (&*front, &*back) }; - f.debug_tuple("IterMut").field(&front).field(&back).finish() + f.debug_tuple("IterMut").field(&self.i1.as_slice()).field(&self.i2.as_slice()).finish() } } @@ -54,44 +33,51 @@ impl<'a, T> Iterator for IterMut<'a, T> { #[inline] fn next(&mut self) -> Option<&'a mut T> { - if self.tail == self.head { - return None; + match self.i1.next() { + Some(val) => Some(val), + None => { + // most of the time, the iterator will either always + // call next(), or always call next_back(). By swapping + // the iterators once the first one is empty, we ensure + // that the first branch is taken as often as possible, + // without sacrificing correctness, as i1 is empty anyways + mem::swap(&mut self.i1, &mut self.i2); + self.i1.next() + } } - let tail = self.tail; - self.tail = wrap_index(self.tail.wrapping_add(1), self.ring.len()); + } - unsafe { - let elem = self.ring.get_unchecked_mut(tail); - Some(&mut *elem) - } + fn advance_by(&mut self, n: usize) -> Result<(), usize> { + let m = match self.i1.advance_by(n) { + Ok(_) => return Ok(()), + Err(m) => m, + }; + mem::swap(&mut self.i1, &mut self.i2); + self.i1.advance_by(n - m).map_err(|o| o + m) } #[inline] fn size_hint(&self) -> (usize, Option) { - let len = count(self.tail, self.head, self.ring.len()); + let len = self.len(); (len, Some(len)) } - fn fold(self, mut accum: Acc, mut f: F) -> Acc + fn fold(self, accum: Acc, mut f: F) -> Acc where F: FnMut(Acc, Self::Item) -> Acc, { - let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail); - // SAFETY: these are the elements we have not handed out yet, so aliasing is fine. - // The `IterMut` invariant also ensures everything is dereferenceable. - let (front, back) = unsafe { (&mut *front, &mut *back) }; - accum = front.iter_mut().fold(accum, &mut f); - back.iter_mut().fold(accum, &mut f) + let accum = self.i1.fold(accum, &mut f); + self.i2.fold(accum, f) } - fn nth(&mut self, n: usize) -> Option { - if n >= count(self.tail, self.head, self.ring.len()) { - self.tail = self.head; - None - } else { - self.tail = wrap_index(self.tail.wrapping_add(n), self.ring.len()); - self.next() - } + fn try_fold(&mut self, init: B, mut f: F) -> R + where + Self: Sized, + F: FnMut(B, Self::Item) -> R, + R: Try, + { + let acc = self.i1.try_fold(init, &mut f)?; + self.i2.try_fold(acc, f) } #[inline] @@ -104,8 +90,12 @@ impl<'a, T> Iterator for IterMut<'a, T> { // Safety: The TrustedRandomAccess contract requires that callers only pass an index // that is in bounds. unsafe { - let idx = wrap_index(self.tail.wrapping_add(idx), self.ring.len()); - &mut *self.ring.get_unchecked_mut(idx) + let i1_len = self.i1.len(); + if idx < i1_len { + self.i1.__iterator_get_unchecked(idx) + } else { + self.i2.__iterator_get_unchecked(idx - i1_len) + } } } } @@ -114,34 +104,57 @@ impl<'a, T> Iterator for IterMut<'a, T> { impl<'a, T> DoubleEndedIterator for IterMut<'a, T> { #[inline] fn next_back(&mut self) -> Option<&'a mut T> { - if self.tail == self.head { - return None; + match self.i2.next_back() { + Some(val) => Some(val), + None => { + // most of the time, the iterator will either always + // call next(), or always call next_back(). By swapping + // the iterators once the first one is empty, we ensure + // that the first branch is taken as often as possible, + // without sacrificing correctness, as i2 is empty anyways + mem::swap(&mut self.i1, &mut self.i2); + self.i2.next_back() + } } - self.head = wrap_index(self.head.wrapping_sub(1), self.ring.len()); + } - unsafe { - let elem = self.ring.get_unchecked_mut(self.head); - Some(&mut *elem) - } + fn advance_back_by(&mut self, n: usize) -> Result<(), usize> { + let m = match self.i2.advance_back_by(n) { + Ok(_) => return Ok(()), + Err(m) => m, + }; + + mem::swap(&mut self.i1, &mut self.i2); + self.i2.advance_back_by(n - m).map_err(|o| m + o) } - fn rfold(self, mut accum: Acc, mut f: F) -> Acc + fn rfold(self, accum: Acc, mut f: F) -> Acc where F: FnMut(Acc, Self::Item) -> Acc, { - let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail); - // SAFETY: these are the elements we have not handed out yet, so aliasing is fine. - // The `IterMut` invariant also ensures everything is dereferenceable. - let (front, back) = unsafe { (&mut *front, &mut *back) }; - accum = back.iter_mut().rfold(accum, &mut f); - front.iter_mut().rfold(accum, &mut f) + let accum = self.i2.rfold(accum, &mut f); + self.i1.rfold(accum, f) + } + + fn try_rfold(&mut self, init: B, mut f: F) -> R + where + Self: Sized, + F: FnMut(B, Self::Item) -> R, + R: Try, + { + let acc = self.i2.try_rfold(init, &mut f)?; + self.i1.try_rfold(acc, f) } } #[stable(feature = "rust1", since = "1.0.0")] impl ExactSizeIterator for IterMut<'_, T> { + fn len(&self) -> usize { + self.i1.len() + self.i2.len() + } + fn is_empty(&self) -> bool { - self.head == self.tail + self.i1.is_empty() && self.i2.is_empty() } } diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 537fe22a4be72..bb0e11d6c2d20 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -10,11 +10,10 @@ use core::cmp::{self, Ordering}; use core::fmt; use core::hash::{Hash, Hasher}; -use core::iter::{repeat_n, repeat_with, FromIterator}; -use core::marker::PhantomData; -use core::mem::{ManuallyDrop, MaybeUninit, SizedTypeProperties}; +use core::iter::{repeat_n, repeat_with, ByRefSized, FromIterator}; +use core::mem::{ManuallyDrop, SizedTypeProperties}; use core::ops::{Index, IndexMut, Range, RangeBounds}; -use core::ptr::{self, NonNull}; +use core::ptr; use core::slice; // This is used in a bunch of intra-doc links. @@ -52,14 +51,6 @@ pub use self::iter::Iter; mod iter; -use self::pair_slices::PairSlices; - -mod pair_slices; - -use self::ring_slices::RingSlices; - -mod ring_slices; - use self::spec_extend::SpecExtend; mod spec_extend; @@ -67,11 +58,6 @@ mod spec_extend; #[cfg(test)] mod tests; -const INITIAL_CAPACITY: usize = 7; // 2^3 - 1 -const MINIMUM_CAPACITY: usize = 1; // 2 - 1 - -const MAXIMUM_ZST_CAPACITY: usize = 1 << (usize::BITS - 1); // Largest possible power of two - /// A double-ended queue implemented with a growable ring buffer. /// /// The "default" usage of this type as a queue is to use [`push_back`] to add to @@ -105,13 +91,8 @@ pub struct VecDeque< T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, > { - // tail and head are pointers into the buffer. Tail always points - // to the first element that could be read, Head always points - // to where data should be written. - // If tail == head the buffer is empty. The length of the ringbuffer - // is defined as the distance between the two. - tail: usize, head: usize, + len: usize, buf: RawVec, } @@ -124,18 +105,16 @@ impl Clone for VecDeque { } fn clone_from(&mut self, other: &Self) { - self.truncate(other.len()); - - let mut iter = PairSlices::from(self, other); - while let Some((dst, src)) = iter.next() { - dst.clone_from_slice(&src); - } + self.clear(); + self.head = 0; + self.reserve(other.len); - if iter.has_remainder() { - for remainder in iter.remainder() { - self.extend(remainder.iter().cloned()); - } + let (a, b) = other.as_slices(); + unsafe { + self.write_iter(0, a.iter().cloned(), &mut 0); + self.write_iter(a.len(), b.iter().cloned(), &mut 0); } + self.len = other.len; } } @@ -154,11 +133,13 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for VecDeque { } } - let (front, back) = self.as_mut_slices(); - unsafe { - let _back_dropper = Dropper(back); - // use drop for [T] - ptr::drop_in_place(front); + if mem::needs_drop::() { + let (front, back) = self.as_mut_slices(); + unsafe { + let _back_dropper = Dropper(back); + // use drop for [T] + ptr::drop_in_place(front); + } } // RawVec handles deallocation } @@ -180,41 +161,6 @@ impl VecDeque { self.buf.ptr() } - /// Marginally more convenient - #[inline] - fn cap(&self) -> usize { - if T::IS_ZST { - // For zero sized types, we are always at maximum capacity - MAXIMUM_ZST_CAPACITY - } else { - self.buf.capacity() - } - } - - /// Turn ptr into a slice, since the elements of the backing buffer may be uninitialized, - /// we will return a slice of [`MaybeUninit`]. - /// - /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and - /// incorrect usage of this method. - /// - /// [zeroed]: mem::MaybeUninit::zeroed - #[inline] - unsafe fn buffer_as_slice(&self) -> &[MaybeUninit] { - unsafe { slice::from_raw_parts(self.ptr() as *mut MaybeUninit, self.cap()) } - } - - /// Turn ptr into a mut slice, since the elements of the backing buffer may be uninitialized, - /// we will return a slice of [`MaybeUninit`]. - /// - /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and - /// incorrect usage of this method. - /// - /// [zeroed]: mem::MaybeUninit::zeroed - #[inline] - unsafe fn buffer_as_mut_slice(&mut self) -> &mut [MaybeUninit] { - unsafe { slice::from_raw_parts_mut(self.ptr() as *mut MaybeUninit, self.cap()) } - } - /// Moves an element out of the buffer #[inline] unsafe fn buffer_read(&mut self, off: usize) -> T { @@ -232,48 +178,46 @@ impl VecDeque { /// Returns `true` if the buffer is at full capacity. #[inline] fn is_full(&self) -> bool { - self.cap() - self.len() == 1 + self.len == self.capacity() } /// Returns the index in the underlying buffer for a given logical element - /// index. + /// index + addend. #[inline] - fn wrap_index(&self, idx: usize) -> usize { - wrap_index(idx, self.cap()) + fn wrap_add(&self, idx: usize, addend: usize) -> usize { + wrap_index(idx.wrapping_add(addend), self.capacity()) } - /// Returns the index in the underlying buffer for a given logical element - /// index + addend. #[inline] - fn wrap_add(&self, idx: usize, addend: usize) -> usize { - wrap_index(idx.wrapping_add(addend), self.cap()) + fn wrap_idx(&self, idx: usize) -> usize { + self.wrap_add(self.head, idx) } /// Returns the index in the underlying buffer for a given logical element /// index - subtrahend. #[inline] fn wrap_sub(&self, idx: usize, subtrahend: usize) -> usize { - wrap_index(idx.wrapping_sub(subtrahend), self.cap()) + wrap_index(idx.wrapping_sub(subtrahend).wrapping_add(self.capacity()), self.capacity()) } /// Copies a contiguous block of memory len long from src to dst #[inline] - unsafe fn copy(&self, dst: usize, src: usize, len: usize) { + unsafe fn copy(&mut self, src: usize, dst: usize, len: usize) { debug_assert!( - dst + len <= self.cap(), + dst + len <= self.capacity(), "cpy dst={} src={} len={} cap={}", dst, src, len, - self.cap() + self.capacity() ); debug_assert!( - src + len <= self.cap(), + src + len <= self.capacity(), "cpy dst={} src={} len={} cap={}", dst, src, len, - self.cap() + self.capacity() ); unsafe { ptr::copy(self.ptr().add(src), self.ptr().add(dst), len); @@ -282,22 +226,22 @@ impl VecDeque { /// Copies a contiguous block of memory len long from src to dst #[inline] - unsafe fn copy_nonoverlapping(&self, dst: usize, src: usize, len: usize) { + unsafe fn copy_nonoverlapping(&mut self, src: usize, dst: usize, len: usize) { debug_assert!( - dst + len <= self.cap(), + dst + len <= self.capacity(), "cno dst={} src={} len={} cap={}", dst, src, len, - self.cap() + self.capacity() ); debug_assert!( - src + len <= self.cap(), + src + len <= self.capacity(), "cno dst={} src={} len={} cap={}", dst, src, len, - self.cap() + self.capacity() ); unsafe { ptr::copy_nonoverlapping(self.ptr().add(src), self.ptr().add(dst), len); @@ -305,30 +249,28 @@ impl VecDeque { } /// Copies a potentially wrapping block of memory len long from src to dest. - /// (abs(dst - src) + len) must be no larger than cap() (There must be at + /// (abs(dst - src) + len) must be no larger than capacity() (There must be at /// most one continuous overlapping region between src and dest). - unsafe fn wrap_copy(&self, dst: usize, src: usize, len: usize) { - #[allow(dead_code)] - fn diff(a: usize, b: usize) -> usize { - if a <= b { b - a } else { a - b } - } + unsafe fn wrap_copy(&mut self, src: usize, dst: usize, len: usize) { debug_assert!( - cmp::min(diff(dst, src), self.cap() - diff(dst, src)) + len <= self.cap(), + cmp::min(src.abs_diff(dst), self.capacity() - src.abs_diff(dst)) + len + <= self.capacity(), "wrc dst={} src={} len={} cap={}", dst, src, len, - self.cap() + self.capacity() ); - if src == dst || len == 0 { + // If T is a ZST, don't do any copying. + if T::IS_ZST || src == dst || len == 0 { return; } let dst_after_src = self.wrap_sub(dst, src) < len; - let src_pre_wrap_len = self.cap() - src; - let dst_pre_wrap_len = self.cap() - dst; + let src_pre_wrap_len = self.capacity() - src; + let dst_pre_wrap_len = self.capacity() - dst; let src_wraps = src_pre_wrap_len < len; let dst_wraps = dst_pre_wrap_len < len; @@ -342,7 +284,7 @@ impl VecDeque { // D . . . // unsafe { - self.copy(dst, src, len); + self.copy(src, dst, len); } } (false, false, true) => { @@ -355,8 +297,8 @@ impl VecDeque { // . . D . // unsafe { - self.copy(dst, src, dst_pre_wrap_len); - self.copy(0, src + dst_pre_wrap_len, len - dst_pre_wrap_len); + self.copy(src, dst, dst_pre_wrap_len); + self.copy(src + dst_pre_wrap_len, 0, len - dst_pre_wrap_len); } } (true, false, true) => { @@ -369,8 +311,8 @@ impl VecDeque { // . . D . // unsafe { - self.copy(0, src + dst_pre_wrap_len, len - dst_pre_wrap_len); - self.copy(dst, src, dst_pre_wrap_len); + self.copy(src + dst_pre_wrap_len, 0, len - dst_pre_wrap_len); + self.copy(src, dst, dst_pre_wrap_len); } } (false, true, false) => { @@ -383,8 +325,8 @@ impl VecDeque { // D . . . // unsafe { - self.copy(dst, src, src_pre_wrap_len); - self.copy(dst + src_pre_wrap_len, 0, len - src_pre_wrap_len); + self.copy(src, dst, src_pre_wrap_len); + self.copy(0, dst + src_pre_wrap_len, len - src_pre_wrap_len); } } (true, true, false) => { @@ -397,8 +339,8 @@ impl VecDeque { // D . . . // unsafe { - self.copy(dst + src_pre_wrap_len, 0, len - src_pre_wrap_len); - self.copy(dst, src, src_pre_wrap_len); + self.copy(0, dst + src_pre_wrap_len, len - src_pre_wrap_len); + self.copy(src, dst, src_pre_wrap_len); } } (false, true, true) => { @@ -414,9 +356,9 @@ impl VecDeque { debug_assert!(dst_pre_wrap_len > src_pre_wrap_len); let delta = dst_pre_wrap_len - src_pre_wrap_len; unsafe { - self.copy(dst, src, src_pre_wrap_len); - self.copy(dst + src_pre_wrap_len, 0, delta); - self.copy(0, delta, len - dst_pre_wrap_len); + self.copy(src, dst, src_pre_wrap_len); + self.copy(0, dst + src_pre_wrap_len, delta); + self.copy(delta, 0, len - dst_pre_wrap_len); } } (true, true, true) => { @@ -432,9 +374,9 @@ impl VecDeque { debug_assert!(src_pre_wrap_len > dst_pre_wrap_len); let delta = src_pre_wrap_len - dst_pre_wrap_len; unsafe { - self.copy(delta, 0, len - src_pre_wrap_len); - self.copy(0, self.cap() - delta, delta); - self.copy(dst, src, dst_pre_wrap_len); + self.copy(0, delta, len - src_pre_wrap_len); + self.copy(self.capacity() - delta, 0, delta); + self.copy(src, dst, dst_pre_wrap_len); } } } @@ -444,8 +386,8 @@ impl VecDeque { /// Assumes capacity is sufficient. #[inline] unsafe fn copy_slice(&mut self, dst: usize, src: &[T]) { - debug_assert!(src.len() <= self.cap()); - let head_room = self.cap() - dst; + debug_assert!(src.len() <= self.capacity()); + let head_room = self.capacity() - dst; if src.len() <= head_room { unsafe { ptr::copy_nonoverlapping(src.as_ptr(), self.ptr().add(dst), src.len()); @@ -478,48 +420,96 @@ impl VecDeque { }); } + /// Writes all values from `iter` to `dst`, wrapping + /// at the end of the buffer and returns the number + /// of written values. + /// + /// # Safety + /// + /// Assumes that `iter` yields at most `len` items. + /// Assumes capacity is sufficient. + unsafe fn write_iter_wrapping( + &mut self, + dst: usize, + mut iter: impl Iterator, + len: usize, + ) -> usize { + struct Guard<'a, T, A: Allocator> { + deque: &'a mut VecDeque, + written: usize, + } + + impl<'a, T, A: Allocator> Drop for Guard<'a, T, A> { + fn drop(&mut self) { + self.deque.len += self.written; + } + } + + let head_room = self.capacity() - dst; + + let mut guard = Guard { deque: self, written: 0 }; + + if head_room >= len { + unsafe { guard.deque.write_iter(dst, iter, &mut guard.written) }; + } else { + unsafe { + guard.deque.write_iter( + dst, + ByRefSized(&mut iter).take(head_room), + &mut guard.written, + ); + guard.deque.write_iter(0, iter, &mut guard.written) + }; + } + + guard.written + } + /// Frobs the head and tail sections around to handle the fact that we /// just reallocated. Unsafe because it trusts old_capacity. #[inline] unsafe fn handle_capacity_increase(&mut self, old_capacity: usize) { - let new_capacity = self.cap(); + let new_capacity = self.capacity(); + debug_assert!(new_capacity >= old_capacity); // Move the shortest contiguous section of the ring buffer - // T H + // H L // [o o o o o o o . ] - // T H + // H L // A [o o o o o o o . . . . . . . . . ] - // H T - // [o o . o o o o o ] - // T H + // L H + // [o o o o o o o o ] + // H L // B [. . . o o o o o o o . . . . . . ] - // H T - // [o o o o o . o o ] - // H T + // L H + // [o o o o o o o o ] + // L H // C [o o o o o . . . . . . . . . o o ] - if self.tail <= self.head { + // can't use is_contiguous() because the capacity is already updated. + if self.head <= old_capacity - self.len { // A // Nop - } else if self.head < old_capacity - self.tail { - // B - unsafe { - self.copy_nonoverlapping(old_capacity, 0, self.head); - } - self.head += old_capacity; - debug_assert!(self.head > self.tail); } else { - // C - let new_tail = new_capacity - (old_capacity - self.tail); - unsafe { - self.copy_nonoverlapping(new_tail, self.tail, old_capacity - self.tail); + let head_len = old_capacity - self.head; + let tail_len = self.len - head_len; + if head_len > tail_len && new_capacity - old_capacity >= tail_len { + // B + unsafe { + self.copy_nonoverlapping(0, old_capacity, tail_len); + } + } else { + // C + let new_head = new_capacity - head_len; + unsafe { + // can't use copy_nonoverlapping here, because if e.g. head_len = 2 + // and new_capacity = old_capacity + 1, then the heads overlap. + self.copy(self.head, new_head, head_len); + } + self.head = new_head; } - self.tail = new_tail; - debug_assert!(self.head < self.tail); } - debug_assert!(self.head < self.cap()); - debug_assert!(self.tail < self.cap()); - debug_assert!(self.cap().count_ones() == 1); + debug_assert!(self.head < self.capacity() || self.capacity() == 0); } } @@ -533,6 +523,7 @@ impl VecDeque { /// /// let deque: VecDeque = VecDeque::new(); /// ``` + // FIXME: This should probably be const #[inline] #[stable(feature = "rust1", since = "1.0.0")] #[must_use] @@ -567,10 +558,11 @@ impl VecDeque { /// /// let deque: VecDeque = VecDeque::new(); /// ``` + // FIXME: This should probably be const #[inline] #[unstable(feature = "allocator_api", issue = "32838")] pub fn new_in(alloc: A) -> VecDeque { - VecDeque::with_capacity_in(INITIAL_CAPACITY, alloc) + VecDeque { head: 0, len: 0, buf: RawVec::new_in(alloc) } } /// Creates an empty deque with space for at least `capacity` elements. @@ -584,11 +576,7 @@ impl VecDeque { /// ``` #[unstable(feature = "allocator_api", issue = "32838")] pub fn with_capacity_in(capacity: usize, alloc: A) -> VecDeque { - assert!(capacity < 1_usize << usize::BITS - 1, "capacity overflow"); - // +1 since the ringbuffer always leaves one space empty - let cap = cmp::max(capacity + 1, MINIMUM_CAPACITY + 1).next_power_of_two(); - - VecDeque { tail: 0, head: 0, buf: RawVec::with_capacity_in(cap, alloc) } + VecDeque { head: 0, len: 0, buf: RawVec::with_capacity_in(capacity, alloc) } } /// Provides a reference to the element at the given index. @@ -608,8 +596,8 @@ impl VecDeque { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn get(&self, index: usize) -> Option<&T> { - if index < self.len() { - let idx = self.wrap_add(self.tail, index); + if index < self.len { + let idx = self.wrap_idx(index); unsafe { Some(&*self.ptr().add(idx)) } } else { None @@ -637,8 +625,8 @@ impl VecDeque { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn get_mut(&mut self, index: usize) -> Option<&mut T> { - if index < self.len() { - let idx = self.wrap_add(self.tail, index); + if index < self.len { + let idx = self.wrap_idx(index); unsafe { Some(&mut *self.ptr().add(idx)) } } else { None @@ -672,8 +660,8 @@ impl VecDeque { pub fn swap(&mut self, i: usize, j: usize) { assert!(i < self.len()); assert!(j < self.len()); - let ri = self.wrap_add(self.tail, i); - let rj = self.wrap_add(self.tail, j); + let ri = self.wrap_idx(i); + let rj = self.wrap_idx(j); unsafe { ptr::swap(self.ptr().add(ri), self.ptr().add(rj)) } } @@ -691,7 +679,7 @@ impl VecDeque { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn capacity(&self) -> usize { - self.cap() - 1 + if T::IS_ZST { usize::MAX } else { self.buf.capacity() } } /// Reserves the minimum capacity for at least `additional` more elements to be inserted in the @@ -718,7 +706,15 @@ impl VecDeque { /// [`reserve`]: VecDeque::reserve #[stable(feature = "rust1", since = "1.0.0")] pub fn reserve_exact(&mut self, additional: usize) { - self.reserve(additional); + let new_cap = self.len.checked_add(additional).expect("capacity overflow"); + let old_cap = self.capacity(); + + if new_cap > old_cap { + self.buf.reserve_exact(self.len, additional); + unsafe { + self.handle_capacity_increase(old_cap); + } + } } /// Reserves capacity for at least `additional` more elements to be inserted in the given @@ -739,15 +735,13 @@ impl VecDeque { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn reserve(&mut self, additional: usize) { - let old_cap = self.cap(); - let used_cap = self.len() + 1; - let new_cap = used_cap - .checked_add(additional) - .and_then(|needed_cap| needed_cap.checked_next_power_of_two()) - .expect("capacity overflow"); + let new_cap = self.len.checked_add(additional).expect("capacity overflow"); + let old_cap = self.capacity(); if new_cap > old_cap { - self.buf.reserve_exact(used_cap, new_cap - used_cap); + // we don't need to reserve_exact(), as the size doesn't have + // to be a power of 2. + self.buf.reserve(self.len, additional); unsafe { self.handle_capacity_increase(old_cap); } @@ -793,7 +787,17 @@ impl VecDeque { /// ``` #[stable(feature = "try_reserve", since = "1.57.0")] pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> { - self.try_reserve(additional) + let new_cap = + self.len.checked_add(additional).ok_or(TryReserveErrorKind::CapacityOverflow)?; + let old_cap = self.capacity(); + + if new_cap > old_cap { + self.buf.try_reserve_exact(self.len, additional)?; + unsafe { + self.handle_capacity_increase(old_cap); + } + } + Ok(()) } /// Tries to reserve capacity for at least `additional` more elements to be inserted @@ -831,15 +835,12 @@ impl VecDeque { /// ``` #[stable(feature = "try_reserve", since = "1.57.0")] pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { - let old_cap = self.cap(); - let used_cap = self.len() + 1; - let new_cap = used_cap - .checked_add(additional) - .and_then(|needed_cap| needed_cap.checked_next_power_of_two()) - .ok_or(TryReserveErrorKind::CapacityOverflow)?; + let new_cap = + self.len.checked_add(additional).ok_or(TryReserveErrorKind::CapacityOverflow)?; + let old_cap = self.capacity(); if new_cap > old_cap { - self.buf.try_reserve_exact(used_cap, new_cap - used_cap)?; + self.buf.try_reserve(self.len, additional)?; unsafe { self.handle_capacity_increase(old_cap); } @@ -890,13 +891,14 @@ impl VecDeque { /// ``` #[stable(feature = "shrink_to", since = "1.56.0")] pub fn shrink_to(&mut self, min_capacity: usize) { - let min_capacity = cmp::min(min_capacity, self.capacity()); - // We don't have to worry about an overflow as neither `self.len()` nor `self.capacity()` - // can ever be `usize::MAX`. +1 as the ringbuffer always leaves one space empty. - let target_cap = cmp::max(cmp::max(min_capacity, self.len()) + 1, MINIMUM_CAPACITY + 1) - .next_power_of_two(); + let target_cap = min_capacity.max(self.len); - if target_cap < self.cap() { + // never shrink ZSTs + if T::IS_ZST || self.capacity() <= target_cap { + return; + } + + if target_cap < self.capacity() { // There are three cases of interest: // All elements are out of desired bounds // Elements are contiguous, and head is out of desired bounds @@ -905,49 +907,49 @@ impl VecDeque { // At all other times, element positions are unaffected. // // Indicates that elements at the head should be moved. - let head_outside = self.head == 0 || self.head >= target_cap; + + let tail_outside = (target_cap + 1..=self.capacity()).contains(&(self.head + self.len)); // Move elements from out of desired bounds (positions after target_cap) - if self.tail >= target_cap && head_outside { - // T H + if self.len == 0 { + self.head = 0; + } else if self.head >= target_cap && tail_outside { + // H L // [. . . . . . . . o o o o o o o . ] - // T H + // H L // [o o o o o o o . ] unsafe { - self.copy_nonoverlapping(0, self.tail, self.len()); + // nonoverlapping because self.head >= target_cap >= self.len + self.copy_nonoverlapping(self.head, 0, self.len); } - self.head = self.len(); - self.tail = 0; - } else if self.tail != 0 && self.tail < target_cap && head_outside { - // T H + self.head = 0; + } else if self.head < target_cap && tail_outside { + // H L // [. . . o o o o o o o . . . . . . ] - // H T + // L H // [o o . o o o o o ] - let len = self.wrap_sub(self.head, target_cap); + let len = self.head + self.len - target_cap; unsafe { - self.copy_nonoverlapping(0, target_cap, len); + self.copy_nonoverlapping(target_cap, 0, len); } - self.head = len; - debug_assert!(self.head < self.tail); - } else if self.tail >= target_cap { - // H T + } else if self.head >= target_cap { + // L H // [o o o o o . . . . . . . . . o o ] - // H T + // L H // [o o o o o . o o ] - debug_assert!(self.wrap_sub(self.head, 1) < target_cap); - let len = self.cap() - self.tail; - let new_tail = target_cap - len; + let len = self.capacity() - self.head; + let new_head = target_cap - len; unsafe { - self.copy_nonoverlapping(new_tail, self.tail, len); + // can't use copy_nonoverlapping here for the same reason + // as in `handle_capacity_increase()` + self.copy(self.head, new_head, len); } - self.tail = new_tail; - debug_assert!(self.head < self.tail); + self.head = new_head; } self.buf.shrink_to_fit(target_cap); - debug_assert!(self.head < self.cap()); - debug_assert!(self.tail < self.cap()); - debug_assert!(self.cap().count_ones() == 1); + debug_assert!(self.head < self.capacity() || self.capacity() == 0); + debug_assert!(self.len <= self.capacity()); } } @@ -992,20 +994,25 @@ impl VecDeque { // * The head of the VecDeque is moved before calling `drop_in_place`, // so no value is dropped twice if `drop_in_place` panics unsafe { - if len > self.len() { + if len >= self.len { return; } - let num_dropped = self.len() - len; + + if !mem::needs_drop::() { + self.len = len; + return; + } + let (front, back) = self.as_mut_slices(); if len > front.len() { let begin = len - front.len(); let drop_back = back.get_unchecked_mut(begin..) as *mut _; - self.head = self.wrap_sub(self.head, num_dropped); + self.len = len; ptr::drop_in_place(drop_back); } else { let drop_back = back as *mut _; let drop_front = front.get_unchecked_mut(len..) as *mut _; - self.head = self.wrap_sub(self.head, num_dropped); + self.len = len; // Make sure the second half is dropped even when a destructor // in the first one panics. @@ -1039,7 +1046,8 @@ impl VecDeque { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn iter(&self) -> Iter<'_, T> { - Iter::new(unsafe { self.buffer_as_slice() }, self.tail, self.head) + let (a, b) = self.as_slices(); + Iter::new(a.iter(), b.iter()) } /// Returns a front-to-back iterator that returns mutable references. @@ -1061,11 +1069,8 @@ impl VecDeque { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn iter_mut(&mut self) -> IterMut<'_, T> { - // SAFETY: The internal `IterMut` safety invariant is established because the - // `ring` we create is a dereferenceable slice for lifetime '_. - let ring = ptr::slice_from_raw_parts_mut(self.ptr(), self.cap()); - - unsafe { IterMut::new(ring, self.tail, self.head, PhantomData) } + let (a, b) = self.as_mut_slices(); + IterMut::new(a.iter_mut(), b.iter_mut()) } /// Returns a pair of slices which contain, in order, the contents of the @@ -1097,13 +1102,17 @@ impl VecDeque { #[inline] #[stable(feature = "deque_extras_15", since = "1.5.0")] pub fn as_slices(&self) -> (&[T], &[T]) { - // Safety: - // - `self.head` and `self.tail` in a ring buffer are always valid indices. - // - `RingSlices::ring_slices` guarantees that the slices split according to `self.head` and `self.tail` are initialized. - unsafe { - let buf = self.buffer_as_slice(); - let (front, back) = RingSlices::ring_slices(buf, self.head, self.tail); - (MaybeUninit::slice_assume_init_ref(front), MaybeUninit::slice_assume_init_ref(back)) + if self.is_contiguous() { + (unsafe { slice::from_raw_parts(self.ptr().add(self.head), self.len) }, &[]) + } else { + let head_len = self.capacity() - self.head; + let tail_len = self.len - head_len; + unsafe { + ( + slice::from_raw_parts(self.ptr().add(self.head), head_len), + slice::from_raw_parts(self.ptr(), tail_len), + ) + } } } @@ -1135,15 +1144,17 @@ impl VecDeque { #[inline] #[stable(feature = "deque_extras_15", since = "1.5.0")] pub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T]) { - // Safety: - // - `self.head` and `self.tail` in a ring buffer are always valid indices. - // - `RingSlices::ring_slices` guarantees that the slices split according to `self.head` and `self.tail` are initialized. - unsafe { - let head = self.head; - let tail = self.tail; - let buf = self.buffer_as_mut_slice(); - let (front, back) = RingSlices::ring_slices(buf, head, tail); - (MaybeUninit::slice_assume_init_mut(front), MaybeUninit::slice_assume_init_mut(back)) + if self.is_contiguous() { + (unsafe { slice::from_raw_parts_mut(self.ptr().add(self.head), self.len) }, &mut []) + } else { + let head_len = self.capacity() - self.head; + let tail_len = self.len - head_len; + unsafe { + ( + slice::from_raw_parts_mut(self.ptr().add(self.head), head_len), + slice::from_raw_parts_mut(self.ptr(), tail_len), + ) + } } } @@ -1161,7 +1172,7 @@ impl VecDeque { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn len(&self) -> usize { - count(self.tail, self.head, self.cap()) + self.len } /// Returns `true` if the deque is empty. @@ -1178,17 +1189,22 @@ impl VecDeque { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn is_empty(&self) -> bool { - self.tail == self.head + self.len == 0 } - fn range_tail_head(&self, range: R) -> (usize, usize) + fn slice_ranges(&self, range: R) -> (Range, Range) where R: RangeBounds, { - let Range { start, end } = slice::range(range, ..self.len()); - let tail = self.wrap_add(self.tail, start); - let head = self.wrap_add(self.tail, end); - (tail, head) + let Range { start, end } = slice::range(range, ..self.len); + let a_len = self.len.min(self.capacity() - self.head); + if end <= a_len { + (start..end, 0..0) + } else if start >= a_len { + (0..0, start - a_len..end - a_len) + } else { + (start..a_len, 0..end - a_len) + } } /// Creates an iterator that covers the specified range in the deque. @@ -1217,9 +1233,9 @@ impl VecDeque { where R: RangeBounds, { - let (tail, head) = self.range_tail_head(range); - // The shared reference we have in &self is maintained in the '_ of Iter. - Iter::new(unsafe { self.buffer_as_slice() }, tail, head) + let (a, b) = self.as_slices(); + let (a_range, b_range) = self.slice_ranges(range); + Iter::new(a[a_range].iter(), b[b_range].iter()) } /// Creates an iterator that covers the specified mutable range in the deque. @@ -1252,13 +1268,9 @@ impl VecDeque { where R: RangeBounds, { - let (tail, head) = self.range_tail_head(range); - - // SAFETY: The internal `IterMut` safety invariant is established because the - // `ring` we create is a dereferenceable slice for lifetime '_. - let ring = ptr::slice_from_raw_parts_mut(self.ptr(), self.cap()); - - unsafe { IterMut::new(ring, tail, head, PhantomData) } + let (a_range, b_range) = self.slice_ranges(range); + let (a, b) = self.as_mut_slices(); + IterMut::new(a[a_range].iter_mut(), b[b_range].iter_mut()) } /// Removes the specified range from the deque in bulk, returning all @@ -1310,39 +1322,30 @@ impl VecDeque { // When finished, the remaining data will be copied back to cover the hole, // and the head/tail values will be restored correctly. // - let (drain_tail, drain_head) = self.range_tail_head(range); + let Range { start, end } = slice::range(range, ..self.len); + let drain_start = start; + let drain_len = end - start; // The deque's elements are parted into three segments: - // * self.tail -> drain_tail - // * drain_tail -> drain_head - // * drain_head -> self.head + // * 0 -> drain_start + // * drain_start -> drain_start+drain_len + // * drain_start+drain_len -> self.len // - // T = self.tail; H = self.head; t = drain_tail; h = drain_head + // H = self.head; T = self.head+self.len; t = drain_start+drain_len; h = drain_head // - // We store drain_tail as self.head, and drain_head and self.head as - // after_tail and after_head respectively on the Drain. This also + // We store drain_start as self.len, and drain_len and self.len as + // drain_len and orig_len respectively on the Drain. This also // truncates the effective array such that if the Drain is leaked, we // have forgotten about the potentially moved values after the start of // the drain. // - // T t h H + // H h t T // [. . . o o x x o o . . .] // - let head = self.head; - // "forget" about the values after the start of the drain until after // the drain is complete and the Drain destructor is run. - self.head = drain_tail; - - let deque = NonNull::from(&mut *self); - unsafe { - // Crucially, we only create shared references from `self` here and read from - // it. We do not write to `self` nor reborrow to a mutable reference. - // Hence the raw pointer we created above, for `deque`, remains valid. - let ring = self.buffer_as_slice(); - Drain::new(drain_head, head, ring, drain_tail, drain_head, deque) - } + unsafe { Drain::new(self, drain_start, drain_len) } } /// Clears the deque, removing all values. @@ -1361,6 +1364,8 @@ impl VecDeque { #[inline] pub fn clear(&mut self) { self.truncate(0); + // Not strictly necessary, but leaves things in a more consistent/predictable state. + self.head = 0; } /// Returns `true` if the deque contains an element equal to the @@ -1455,7 +1460,7 @@ impl VecDeque { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn back(&self) -> Option<&T> { - self.get(self.len().wrapping_sub(1)) + self.get(self.len.wrapping_sub(1)) } /// Provides a mutable reference to the back element, or `None` if the @@ -1479,7 +1484,7 @@ impl VecDeque { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn back_mut(&mut self) -> Option<&mut T> { - self.get_mut(self.len().wrapping_sub(1)) + self.get_mut(self.len.wrapping_sub(1)) } /// Removes the first element and returns it, or `None` if the deque is @@ -1503,9 +1508,10 @@ impl VecDeque { if self.is_empty() { None } else { - let tail = self.tail; - self.tail = self.wrap_add(self.tail, 1); - unsafe { Some(self.buffer_read(tail)) } + let old_head = self.head; + self.head = self.wrap_idx(1); + self.len -= 1; + Some(unsafe { self.buffer_read(old_head) }) } } @@ -1528,9 +1534,8 @@ impl VecDeque { if self.is_empty() { None } else { - self.head = self.wrap_sub(self.head, 1); - let head = self.head; - unsafe { Some(self.buffer_read(head)) } + self.len -= 1; + Some(unsafe { self.buffer_read(self.wrap_idx(self.len)) }) } } @@ -1552,10 +1557,11 @@ impl VecDeque { self.grow(); } - self.tail = self.wrap_sub(self.tail, 1); - let tail = self.tail; + self.head = self.wrap_sub(self.head, 1); + self.len += 1; + unsafe { - self.buffer_write(tail, value); + self.buffer_write(self.head, value); } } @@ -1577,16 +1583,14 @@ impl VecDeque { self.grow(); } - let head = self.head; - self.head = self.wrap_add(self.head, 1); - unsafe { self.buffer_write(head, value) } + unsafe { self.buffer_write(self.wrap_idx(self.len), value) } + self.len += 1; } #[inline] fn is_contiguous(&self) -> bool { - // FIXME: Should we consider `head == 0` to mean - // that `self` is contiguous? - self.tail <= self.head + // Do the calculation like this to avoid overflowing if len + head > usize::MAX + self.head <= self.capacity() - self.len } /// Removes an element from anywhere in the deque and returns it, @@ -1615,8 +1619,8 @@ impl VecDeque { /// ``` #[stable(feature = "deque_extras_15", since = "1.5.0")] pub fn swap_remove_front(&mut self, index: usize) -> Option { - let length = self.len(); - if length > 0 && index < length && index != 0 { + let length = self.len; + if index < length && index != 0 { self.swap(index, 0); } else if index >= length { return None; @@ -1650,7 +1654,7 @@ impl VecDeque { /// ``` #[stable(feature = "deque_extras_15", since = "1.5.0")] pub fn swap_remove_back(&mut self, index: usize) -> Option { - let length = self.len(); + let length = self.len; if length > 0 && index < length - 1 { self.swap(index, length - 1); } else if index >= length { @@ -1689,198 +1693,26 @@ impl VecDeque { self.grow(); } - // Move the least number of elements in the ring buffer and insert - // the given object - // - // At most len/2 - 1 elements will be moved. O(min(n, n-i)) - // - // There are three main cases: - // Elements are contiguous - // - special case when tail is 0 - // Elements are discontiguous and the insert is in the tail section - // Elements are discontiguous and the insert is in the head section - // - // For each of those there are two more cases: - // Insert is closer to tail - // Insert is closer to head - // - // Key: H - self.head - // T - self.tail - // o - Valid element - // I - Insertion element - // A - The element that should be after the insertion point - // M - Indicates element was moved - - let idx = self.wrap_add(self.tail, index); - - let distance_to_tail = index; - let distance_to_head = self.len() - index; - - let contiguous = self.is_contiguous(); - - match (contiguous, distance_to_tail <= distance_to_head, idx >= self.tail) { - (true, true, _) if index == 0 => { - // push_front - // - // T - // I H - // [A o o o o o o . . . . . . . . .] - // - // H T - // [A o o o o o o o . . . . . I] - // - - self.tail = self.wrap_sub(self.tail, 1); - } - (true, true, _) => { - unsafe { - // contiguous, insert closer to tail: - // - // T I H - // [. . . o o A o o o o . . . . . .] - // - // T H - // [. . o o I A o o o o . . . . . .] - // M M - // - // contiguous, insert closer to tail and tail is 0: - // - // - // T I H - // [o o A o o o o . . . . . . . . .] - // - // H T - // [o I A o o o o o . . . . . . . o] - // M M - - let new_tail = self.wrap_sub(self.tail, 1); - - self.copy(new_tail, self.tail, 1); - // Already moved the tail, so we only copy `index - 1` elements. - self.copy(self.tail, self.tail + 1, index - 1); - - self.tail = new_tail; - } - } - (true, false, _) => { - unsafe { - // contiguous, insert closer to head: - // - // T I H - // [. . . o o o o A o o . . . . . .] - // - // T H - // [. . . o o o o I A o o . . . . .] - // M M M - - self.copy(idx + 1, idx, self.head - idx); - self.head = self.wrap_add(self.head, 1); - } - } - (false, true, true) => { - unsafe { - // discontiguous, insert closer to tail, tail section: - // - // H T I - // [o o o o o o . . . . . o o A o o] - // - // H T - // [o o o o o o . . . . o o I A o o] - // M M - - self.copy(self.tail - 1, self.tail, index); - self.tail -= 1; - } - } - (false, false, true) => { - unsafe { - // discontiguous, insert closer to head, tail section: - // - // H T I - // [o o . . . . . . . o o o o o A o] - // - // H T - // [o o o . . . . . . o o o o o I A] - // M M M M - - // copy elements up to new head - self.copy(1, 0, self.head); - - // copy last element into empty spot at bottom of buffer - self.copy(0, self.cap() - 1, 1); - - // move elements from idx to end forward not including ^ element - self.copy(idx + 1, idx, self.cap() - 1 - idx); - - self.head += 1; - } - } - (false, true, false) if idx == 0 => { - unsafe { - // discontiguous, insert is closer to tail, head section, - // and is at index zero in the internal buffer: - // - // I H T - // [A o o o o o o o o o . . . o o o] - // - // H T - // [A o o o o o o o o o . . o o o I] - // M M M - - // copy elements up to new tail - self.copy(self.tail - 1, self.tail, self.cap() - self.tail); - - // copy last element into empty spot at bottom of buffer - self.copy(self.cap() - 1, 0, 1); - - self.tail -= 1; - } - } - (false, true, false) => { - unsafe { - // discontiguous, insert closer to tail, head section: - // - // I H T - // [o o o A o o o o o o . . . o o o] - // - // H T - // [o o I A o o o o o o . . o o o o] - // M M M M M M - - // copy elements up to new tail - self.copy(self.tail - 1, self.tail, self.cap() - self.tail); - - // copy last element into empty spot at bottom of buffer - self.copy(self.cap() - 1, 0, 1); - - // move elements from idx-1 to end forward not including ^ element - self.copy(0, 1, idx - 1); - - self.tail -= 1; - } + let k = self.len - index; + if k < index { + // `index + 1` can't overflow, because if index was usize::MAX, then either the + // assert would've failed, or the deque would've tried to grow past usize::MAX + // and panicked. + unsafe { + // see `remove()` for explanation why this wrap_copy() call is safe. + self.wrap_copy(self.wrap_idx(index), self.wrap_idx(index + 1), k); + self.buffer_write(self.wrap_idx(index), value); + self.len += 1; } - (false, false, false) => { - unsafe { - // discontiguous, insert closer to head, head section: - // - // I H T - // [o o o o A o o . . . . . . o o o] - // - // H T - // [o o o o I A o o . . . . . o o o] - // M M M - - self.copy(idx + 1, idx, self.head - idx); - self.head += 1; - } + } else { + let old_head = self.head; + self.head = self.wrap_sub(self.head, 1); + unsafe { + self.wrap_copy(old_head, self.head, index); + self.buffer_write(self.wrap_idx(index), value); + self.len += 1; } } - - // tail might've been changed so we need to recalculate - let new_idx = self.wrap_add(self.tail, index); - unsafe { - self.buffer_write(new_idx, value); - } } /// Removes and returns the element at `index` from the deque. @@ -1906,156 +1738,26 @@ impl VecDeque { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn remove(&mut self, index: usize) -> Option { - if self.is_empty() || self.len() <= index { + if self.len <= index { return None; } - // There are three main cases: - // Elements are contiguous - // Elements are discontiguous and the removal is in the tail section - // Elements are discontiguous and the removal is in the head section - // - special case when elements are technically contiguous, - // but self.head = 0 - // - // For each of those there are two more cases: - // Insert is closer to tail - // Insert is closer to head - // - // Key: H - self.head - // T - self.tail - // o - Valid element - // x - Element marked for removal - // R - Indicates element that is being removed - // M - Indicates element was moved - - let idx = self.wrap_add(self.tail, index); - - let elem = unsafe { Some(self.buffer_read(idx)) }; - - let distance_to_tail = index; - let distance_to_head = self.len() - index; + let wrapped_idx = self.wrap_idx(index); - let contiguous = self.is_contiguous(); + let elem = unsafe { Some(self.buffer_read(wrapped_idx)) }; - match (contiguous, distance_to_tail <= distance_to_head, idx >= self.tail) { - (true, true, _) => { - unsafe { - // contiguous, remove closer to tail: - // - // T R H - // [. . . o o x o o o o . . . . . .] - // - // T H - // [. . . . o o o o o o . . . . . .] - // M M - - self.copy(self.tail + 1, self.tail, index); - self.tail += 1; - } - } - (true, false, _) => { - unsafe { - // contiguous, remove closer to head: - // - // T R H - // [. . . o o o o x o o . . . . . .] - // - // T H - // [. . . o o o o o o . . . . . . .] - // M M - - self.copy(idx, idx + 1, self.head - idx - 1); - self.head -= 1; - } - } - (false, true, true) => { - unsafe { - // discontiguous, remove closer to tail, tail section: - // - // H T R - // [o o o o o o . . . . . o o x o o] - // - // H T - // [o o o o o o . . . . . . o o o o] - // M M - - self.copy(self.tail + 1, self.tail, index); - self.tail = self.wrap_add(self.tail, 1); - } - } - (false, false, false) => { - unsafe { - // discontiguous, remove closer to head, head section: - // - // R H T - // [o o o o x o o . . . . . . o o o] - // - // H T - // [o o o o o o . . . . . . . o o o] - // M M - - self.copy(idx, idx + 1, self.head - idx - 1); - self.head -= 1; - } - } - (false, false, true) => { - unsafe { - // discontiguous, remove closer to head, tail section: - // - // H T R - // [o o o . . . . . . o o o o o x o] - // - // H T - // [o o . . . . . . . o o o o o o o] - // M M M M - // - // or quasi-discontiguous, remove next to head, tail section: - // - // H T R - // [. . . . . . . . . o o o o o x o] - // - // T H - // [. . . . . . . . . o o o o o o .] - // M - - // draw in elements in the tail section - self.copy(idx, idx + 1, self.cap() - idx - 1); - - // Prevents underflow. - if self.head != 0 { - // copy first element into empty spot - self.copy(self.cap() - 1, 0, 1); - - // move elements in the head section backwards - self.copy(0, 1, self.head - 1); - } - - self.head = self.wrap_sub(self.head, 1); - } - } - (false, true, false) => { - unsafe { - // discontiguous, remove closer to tail, head section: - // - // R H T - // [o o x o o o o o o o . . . o o o] - // - // H T - // [o o o o o o o o o o . . . . o o] - // M M M M M - - // draw in elements up to idx - self.copy(1, 0, idx); - - // copy last element into empty spot - self.copy(0, self.cap() - 1, 1); - - // move elements from tail to end forward, excluding the last one - self.copy(self.tail + 1, self.tail, self.cap() - self.tail - 1); - - self.tail = self.wrap_add(self.tail, 1); - } - } + let k = self.len - index - 1; + // safety: due to the nature of the if-condition, whichever wrap_copy gets called, + // its length argument will be at most `self.len / 2`, so there can't be more than + // one overlapping area. + if k < index { + unsafe { self.wrap_copy(self.wrap_add(wrapped_idx, 1), wrapped_idx, k) }; + self.len -= 1; + } else { + let old_head = self.head; + self.head = self.wrap_idx(1); + unsafe { self.wrap_copy(old_head, self.head, index) }; + self.len -= 1; } elem @@ -2091,7 +1793,7 @@ impl VecDeque { where A: Clone, { - let len = self.len(); + let len = self.len; assert!(at <= len, "`at` out of bounds"); let other_len = len - at; @@ -2128,8 +1830,8 @@ impl VecDeque { } // Cleanup where the ends of the buffers are - self.head = self.wrap_sub(self.head, other_len); - other.head = other.wrap_index(other_len); + self.len = at; + other.len = other_len; other } @@ -2154,17 +1856,26 @@ impl VecDeque { #[inline] #[stable(feature = "append", since = "1.4.0")] pub fn append(&mut self, other: &mut Self) { - self.reserve(other.len()); + if T::IS_ZST { + self.len += other.len; + other.len = 0; + other.head = 0; + return; + } + + self.reserve(other.len); unsafe { let (left, right) = other.as_slices(); - self.copy_slice(self.head, left); - self.copy_slice(self.wrap_add(self.head, left.len()), right); + self.copy_slice(self.wrap_idx(self.len), left); + // no overflow, because self.capacity() >= old_cap + left.len() >= self.len + left.len() + self.copy_slice(self.wrap_idx(self.len + left.len()), right); } // SAFETY: Update pointers after copying to avoid leaving doppelganger // in case of panics. - self.head = self.wrap_add(self.head, other.len()); + self.len += other.len; // Silently drop values in `other`. - other.tail = other.head; + other.len = 0; + other.head = 0; } /// Retains only the elements specified by the predicate. @@ -2232,7 +1943,7 @@ impl VecDeque { where F: FnMut(&mut T) -> bool, { - let len = self.len(); + let len = self.len; let mut idx = 0; let mut cur = 0; @@ -2270,9 +1981,9 @@ impl VecDeque { // Extend or possibly remove this assertion when valid use-cases for growing the // buffer without it being full emerge debug_assert!(self.is_full()); - let old_cap = self.cap(); - self.buf.reserve_exact(old_cap, old_cap); - assert!(self.cap() == old_cap * 2); + let old_cap = self.capacity(); + // if the cap was 0, we need to reserve at least 1. + self.buf.reserve(old_cap, old_cap.max(1)); unsafe { self.handle_capacity_increase(old_cap); } @@ -2306,7 +2017,7 @@ impl VecDeque { /// ``` #[stable(feature = "vec_resize_with", since = "1.33.0")] pub fn resize_with(&mut self, new_len: usize, generator: impl FnMut() -> T) { - let len = self.len(); + let len = self.len; if new_len > len { self.extend(repeat_with(generator).take(new_len - len)) @@ -2372,64 +2083,53 @@ impl VecDeque { /// ``` #[stable(feature = "deque_make_contiguous", since = "1.48.0")] pub fn make_contiguous(&mut self) -> &mut [T] { + if T::IS_ZST { + self.head = 0; + } + if self.is_contiguous() { - let tail = self.tail; - let head = self.head; - // Safety: - // - `self.head` and `self.tail` in a ring buffer are always valid indices. - // - `RingSlices::ring_slices` guarantees that the slices split according to `self.head` and `self.tail` are initialized. - return unsafe { - MaybeUninit::slice_assume_init_mut( - RingSlices::ring_slices(self.buffer_as_mut_slice(), head, tail).0, - ) - }; + unsafe { return slice::from_raw_parts_mut(self.ptr().add(self.head), self.len) } } - let buf = self.buf.ptr(); - let cap = self.cap(); - let len = self.len(); + let &mut Self { head, len, .. } = self; + let ptr = self.ptr(); + let cap = self.capacity(); - let free = self.tail - self.head; - let tail_len = cap - self.tail; + let free = cap - len; + let head_len = cap - head; + let tail = len - head_len; + let tail_len = tail; - if free >= tail_len { - // there is enough free space to copy the tail in one go, - // this means that we first shift the head backwards, and then - // copy the tail to the correct position. + if free >= head_len { + // there is enough free space to copy the head in one go, + // this means that we first shift the tail backwards, and then + // copy the head to the correct position. // // from: DEFGH....ABC // to: ABCDEFGH.... unsafe { - ptr::copy(buf, buf.add(tail_len), self.head); + self.copy(0, head_len, tail_len); // ...DEFGH.ABC - ptr::copy_nonoverlapping(buf.add(self.tail), buf, tail_len); + self.copy_nonoverlapping(head, 0, head_len); // ABCDEFGH.... - - self.tail = 0; - self.head = len; } - } else if free > self.head { - // FIXME: We currently do not consider ....ABCDEFGH - // to be contiguous because `head` would be `0` in this - // case. While we probably want to change this it - // isn't trivial as a few places expect `is_contiguous` - // to mean that we can just slice using `buf[tail..head]`. - // there is enough free space to copy the head in one go, - // this means that we first shift the tail forwards, and then - // copy the head to the correct position. + self.head = 0; + } else if free >= tail_len { + // there is enough free space to copy the tail in one go, + // this means that we first shift the head forwards, and then + // copy the tail to the correct position. // // from: FGH....ABCDE // to: ...ABCDEFGH. unsafe { - ptr::copy(buf.add(self.tail), buf.add(self.head), tail_len); + self.copy(head, tail, head_len); // FGHABCDE.... - ptr::copy_nonoverlapping(buf, buf.add(self.head + tail_len), self.head); + self.copy_nonoverlapping(0, tail + head_len, tail_len); // ...ABCDEFGH. - - self.tail = self.head; - self.head = self.wrap_add(self.tail, len); } + + self.head = tail; } else { // free is smaller than both head and tail, // this means we have to slowly "swap" the tail and the head. @@ -2437,7 +2137,7 @@ impl VecDeque { // from: EFGHI...ABCD or HIJK.ABCDEFG // to: ABCDEFGHI... or ABCDEFGHIJK. let mut left_edge: usize = 0; - let mut right_edge: usize = self.tail; + let mut right_edge: usize = head; unsafe { // The general problem looks like this // GHIJKLM...ABCDEF - before any swaps @@ -2454,28 +2154,18 @@ impl VecDeque { for i in left_edge..right_edge { right_offset = (i - left_edge) % (cap - right_edge); let src = right_edge + right_offset; - ptr::swap(buf.add(i), buf.add(src)); + ptr::swap(ptr.add(i), ptr.add(src)); } let n_ops = right_edge - left_edge; left_edge += n_ops; right_edge += right_offset + 1; } - self.tail = 0; - self.head = len; + self.head = 0; } } - let tail = self.tail; - let head = self.head; - // Safety: - // - `self.head` and `self.tail` in a ring buffer are always valid indices. - // - `RingSlices::ring_slices` guarantees that the slices split according to `self.head` and `self.tail` are initialized. - unsafe { - MaybeUninit::slice_assume_init_mut( - RingSlices::ring_slices(self.buffer_as_mut_slice(), head, tail).0, - ) - } + unsafe { slice::from_raw_parts_mut(ptr.add(self.head), self.len) } } /// Rotates the double-ended queue `mid` places to the left. @@ -2513,7 +2203,7 @@ impl VecDeque { #[stable(feature = "vecdeque_rotate", since = "1.36.0")] pub fn rotate_left(&mut self, mid: usize) { assert!(mid <= self.len()); - let k = self.len() - mid; + let k = self.len - mid; if mid <= k { unsafe { self.rotate_left_inner(mid) } } else { @@ -2556,7 +2246,7 @@ impl VecDeque { #[stable(feature = "vecdeque_rotate", since = "1.36.0")] pub fn rotate_right(&mut self, k: usize) { assert!(k <= self.len()); - let mid = self.len() - k; + let mid = self.len - k; if k <= mid { unsafe { self.rotate_right_inner(k) } } else { @@ -2567,26 +2257,24 @@ impl VecDeque { // SAFETY: the following two methods require that the rotation amount // be less than half the length of the deque. // - // `wrap_copy` requires that `min(x, cap() - x) + copy_len <= cap()`, - // but than `min` is never more than half the capacity, regardless of x, + // `wrap_copy` requires that `min(x, capacity() - x) + copy_len <= capacity()`, + // but then `min` is never more than half the capacity, regardless of x, // so it's sound to call here because we're calling with something // less than half the length, which is never above half the capacity. unsafe fn rotate_left_inner(&mut self, mid: usize) { debug_assert!(mid * 2 <= self.len()); unsafe { - self.wrap_copy(self.head, self.tail, mid); + self.wrap_copy(self.head, self.wrap_idx(self.len), mid); } - self.head = self.wrap_add(self.head, mid); - self.tail = self.wrap_add(self.tail, mid); + self.head = self.wrap_idx(mid); } unsafe fn rotate_right_inner(&mut self, k: usize) { debug_assert!(k * 2 <= self.len()); self.head = self.wrap_sub(self.head, k); - self.tail = self.wrap_sub(self.tail, k); unsafe { - self.wrap_copy(self.tail, self.head, k); + self.wrap_copy(self.wrap_idx(self.len), self.head, k); } } @@ -2845,22 +2533,13 @@ impl VecDeque { /// Returns the index in the underlying buffer for a given logical element index. #[inline] fn wrap_index(index: usize, size: usize) -> usize { - // size is always a power of 2 - debug_assert!(size.is_power_of_two()); - index & (size - 1) -} - -/// Calculate the number of elements left to be read in the buffer -#[inline] -fn count(tail: usize, head: usize, size: usize) -> usize { - // size is always a power of 2 - (head.wrapping_sub(tail)) & (size - 1) + if index >= size { index - size } else { index } } #[stable(feature = "rust1", since = "1.0.0")] impl PartialEq for VecDeque { fn eq(&self, other: &Self) -> bool { - if self.len() != other.len() { + if self.len != other.len() { return false; } let (sa, sb) = self.as_slices(); @@ -2924,7 +2603,7 @@ impl Ord for VecDeque { #[stable(feature = "rust1", since = "1.0.0")] impl Hash for VecDeque { fn hash(&self, state: &mut H) { - state.write_length_prefix(self.len()); + state.write_length_prefix(self.len); // It's not possible to use Hash::hash_slice on slices // returned by as_slices method as their length can vary // in otherwise identical deques. @@ -3033,7 +2712,7 @@ impl<'a, T: 'a + Copy, A: Allocator> Extend<&'a T> for VecDeque { #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Debug for VecDeque { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_list().entries(self).finish() + f.debug_list().entries(self.iter()).finish() } } @@ -3044,31 +2723,12 @@ impl From> for VecDeque { /// [`Vec`]: crate::vec::Vec /// [`VecDeque`]: crate::collections::VecDeque /// - /// This avoids reallocating where possible, but the conditions for that are - /// strict, and subject to change, and so shouldn't be relied upon unless the - /// `Vec` came from `From>` and hasn't been reallocated. - fn from(mut other: Vec) -> Self { - let len = other.len(); - if T::IS_ZST { - // There's no actual allocation for ZSTs to worry about capacity, - // but `VecDeque` can't handle as much length as `Vec`. - assert!(len < MAXIMUM_ZST_CAPACITY, "capacity overflow"); - } else { - // We need to resize if the capacity is not a power of two, too small or - // doesn't have at least one free space. We do this while it's still in - // the `Vec` so the items will drop on panic. - let min_cap = cmp::max(MINIMUM_CAPACITY, len) + 1; - let cap = cmp::max(min_cap, other.capacity()).next_power_of_two(); - if other.capacity() != cap { - other.reserve_exact(cap - len); - } - } - - unsafe { - let (other_buf, len, capacity, alloc) = other.into_raw_parts_with_alloc(); - let buf = RawVec::from_raw_parts_in(other_buf, capacity, alloc); - VecDeque { tail: 0, head: len, buf } - } + /// In its current implementation, this is a very cheap + /// conversion. This isn't yet a guarantee though, and + /// shouldn't be relied on. + fn from(other: Vec) -> Self { + let (ptr, len, cap, alloc) = other.into_raw_parts_with_alloc(); + Self { head: 0, len, buf: unsafe { RawVec::from_raw_parts_in(ptr, cap, alloc) } } } } @@ -3110,11 +2770,11 @@ impl From> for Vec { let other = ManuallyDrop::new(other); let buf = other.buf.ptr(); let len = other.len(); - let cap = other.cap(); + let cap = other.capacity(); let alloc = ptr::read(other.allocator()); - if other.tail != 0 { - ptr::copy(buf.add(other.tail), buf, len); + if other.head != 0 { + ptr::copy(buf.add(other.head), buf, len); } Vec::from_raw_parts_in(buf, len, cap, alloc) } @@ -3141,8 +2801,8 @@ impl From<[T; N]> for VecDeque { ptr::copy_nonoverlapping(arr.as_ptr(), deq.ptr(), N); } } - deq.tail = 0; - deq.head = N; + deq.head = 0; + deq.len = N; deq } } diff --git a/library/alloc/src/collections/vec_deque/pair_slices.rs b/library/alloc/src/collections/vec_deque/pair_slices.rs deleted file mode 100644 index 6735424a3ef33..0000000000000 --- a/library/alloc/src/collections/vec_deque/pair_slices.rs +++ /dev/null @@ -1,67 +0,0 @@ -use core::cmp::{self}; -use core::mem::replace; - -use crate::alloc::Allocator; - -use super::VecDeque; - -/// PairSlices pairs up equal length slice parts of two deques -/// -/// For example, given deques "A" and "B" with the following division into slices: -/// -/// A: [0 1 2] [3 4 5] -/// B: [a b] [c d e] -/// -/// It produces the following sequence of matching slices: -/// -/// ([0 1], [a b]) -/// (\[2\], \[c\]) -/// ([3 4], [d e]) -/// -/// and the uneven remainder of either A or B is skipped. -pub struct PairSlices<'a, 'b, T> { - a0: &'a mut [T], - a1: &'a mut [T], - b0: &'b [T], - b1: &'b [T], -} - -impl<'a, 'b, T> PairSlices<'a, 'b, T> { - pub fn from(to: &'a mut VecDeque, from: &'b VecDeque) -> Self { - let (a0, a1) = to.as_mut_slices(); - let (b0, b1) = from.as_slices(); - PairSlices { a0, a1, b0, b1 } - } - - pub fn has_remainder(&self) -> bool { - !self.b0.is_empty() - } - - pub fn remainder(self) -> impl Iterator { - IntoIterator::into_iter([self.b0, self.b1]) - } -} - -impl<'a, 'b, T> Iterator for PairSlices<'a, 'b, T> { - type Item = (&'a mut [T], &'b [T]); - fn next(&mut self) -> Option { - // Get next part length - let part = cmp::min(self.a0.len(), self.b0.len()); - if part == 0 { - return None; - } - let (p0, p1) = replace(&mut self.a0, &mut []).split_at_mut(part); - let (q0, q1) = self.b0.split_at(part); - - // Move a1 into a0, if it's empty (and b1, b0 the same way). - self.a0 = p1; - self.b0 = q1; - if self.a0.is_empty() { - self.a0 = replace(&mut self.a1, &mut []); - } - if self.b0.is_empty() { - self.b0 = replace(&mut self.b1, &[]); - } - Some((p0, q0)) - } -} diff --git a/library/alloc/src/collections/vec_deque/ring_slices.rs b/library/alloc/src/collections/vec_deque/ring_slices.rs deleted file mode 100644 index dd0fa7d6074c0..0000000000000 --- a/library/alloc/src/collections/vec_deque/ring_slices.rs +++ /dev/null @@ -1,56 +0,0 @@ -use core::ptr::{self}; - -/// Returns the two slices that cover the `VecDeque`'s valid range -pub trait RingSlices: Sized { - fn slice(self, from: usize, to: usize) -> Self; - fn split_at(self, i: usize) -> (Self, Self); - - fn ring_slices(buf: Self, head: usize, tail: usize) -> (Self, Self) { - let contiguous = tail <= head; - if contiguous { - let (empty, buf) = buf.split_at(0); - (buf.slice(tail, head), empty) - } else { - let (mid, right) = buf.split_at(tail); - let (left, _) = mid.split_at(head); - (right, left) - } - } -} - -impl RingSlices for &[T] { - fn slice(self, from: usize, to: usize) -> Self { - &self[from..to] - } - fn split_at(self, i: usize) -> (Self, Self) { - (*self).split_at(i) - } -} - -impl RingSlices for &mut [T] { - fn slice(self, from: usize, to: usize) -> Self { - &mut self[from..to] - } - fn split_at(self, i: usize) -> (Self, Self) { - (*self).split_at_mut(i) - } -} - -impl RingSlices for *mut [T] { - fn slice(self, from: usize, to: usize) -> Self { - assert!(from <= to && to < self.len()); - // Not using `get_unchecked_mut` to keep this a safe operation. - let len = to - from; - ptr::slice_from_raw_parts_mut(self.as_mut_ptr().wrapping_add(from), len) - } - - fn split_at(self, mid: usize) -> (Self, Self) { - let len = self.len(); - let ptr = self.as_mut_ptr(); - assert!(mid <= len); - ( - ptr::slice_from_raw_parts_mut(ptr, mid), - ptr::slice_from_raw_parts_mut(ptr.wrapping_add(mid), len - mid), - ) - } -} diff --git a/library/alloc/src/collections/vec_deque/spec_extend.rs b/library/alloc/src/collections/vec_deque/spec_extend.rs index 97ff8b7652403..0f722ceb0823b 100644 --- a/library/alloc/src/collections/vec_deque/spec_extend.rs +++ b/library/alloc/src/collections/vec_deque/spec_extend.rs @@ -17,19 +17,31 @@ where default fn spec_extend(&mut self, mut iter: I) { // This function should be the moral equivalent of: // - // for item in iter { - // self.push_back(item); - // } - while let Some(element) = iter.next() { - if self.len() == self.capacity() { - let (lower, _) = iter.size_hint(); - self.reserve(lower.saturating_add(1)); + // for item in iter { + // self.push_back(item); + // } + loop { + let lower_bound = iter.size_hint().0; + if lower_bound != 0 { + self.reserve(lower_bound); } - let head = self.head; - self.head = self.wrap_add(self.head, 1); + match iter.next() { + Some(val) => self.push_back(val), + None => break, + } + + let room = self.capacity() - self.len; unsafe { - self.buffer_write(head, element); + // Safety: + // The iter is at most `room` items long, + // and `room == self.capacity() - self.len` + // => `self.len + room <= self.capacity()` + self.write_iter_wrapping( + self.wrap_idx(self.len), + ByRefSized(&mut iter).take(room), + room, + ); } } } @@ -39,7 +51,7 @@ impl SpecExtend for VecDeque where I: TrustedLen, { - default fn spec_extend(&mut self, mut iter: I) { + default fn spec_extend(&mut self, iter: I) { // This is the case for a TrustedLen iterator. let (low, high) = iter.size_hint(); if let Some(additional) = high { @@ -51,35 +63,11 @@ where ); self.reserve(additional); - struct WrapAddOnDrop<'a, T, A: Allocator> { - vec_deque: &'a mut VecDeque, - written: usize, - } - - impl<'a, T, A: Allocator> Drop for WrapAddOnDrop<'a, T, A> { - fn drop(&mut self) { - self.vec_deque.head = - self.vec_deque.wrap_add(self.vec_deque.head, self.written); - } - } - - let mut wrapper = WrapAddOnDrop { vec_deque: self, written: 0 }; - - let head_room = wrapper.vec_deque.cap() - wrapper.vec_deque.head; - unsafe { - wrapper.vec_deque.write_iter( - wrapper.vec_deque.head, - ByRefSized(&mut iter).take(head_room), - &mut wrapper.written, - ); - - if additional > head_room { - wrapper.vec_deque.write_iter(0, iter, &mut wrapper.written); - } - } + let written = + unsafe { self.write_iter_wrapping(self.wrap_idx(self.len), iter, additional) }; debug_assert_eq!( - additional, wrapper.written, + additional, written, "The number of items written to VecDeque doesn't match the TrustedLen size hint" ); } else { @@ -99,8 +87,8 @@ impl SpecExtend> for VecDeque { self.reserve(slice.len()); unsafe { - self.copy_slice(self.head, slice); - self.head = self.wrap_add(self.head, slice.len()); + self.copy_slice(self.wrap_idx(self.len), slice); + self.len += slice.len(); } iterator.forget_remaining_elements(); } @@ -125,8 +113,8 @@ where self.reserve(slice.len()); unsafe { - self.copy_slice(self.head, slice); - self.head = self.wrap_add(self.head, slice.len()); + self.copy_slice(self.wrap_idx(self.len), slice); + self.len += slice.len(); } } } diff --git a/library/alloc/src/collections/vec_deque/tests.rs b/library/alloc/src/collections/vec_deque/tests.rs index 6e0f83020f957..6995c5b47dbf8 100644 --- a/library/alloc/src/collections/vec_deque/tests.rs +++ b/library/alloc/src/collections/vec_deque/tests.rs @@ -10,7 +10,7 @@ fn bench_push_back_100(b: &mut test::Bencher) { deq.push_back(i); } deq.head = 0; - deq.tail = 0; + deq.len = 0; }) } @@ -22,7 +22,7 @@ fn bench_push_front_100(b: &mut test::Bencher) { deq.push_front(i); } deq.head = 0; - deq.tail = 0; + deq.len = 0; }) } @@ -35,8 +35,8 @@ fn bench_pop_back_100(b: &mut test::Bencher) { unsafe { deq.ptr().write_bytes(0u8, size + 1) }; b.iter(|| { - deq.head = size; - deq.tail = 0; + deq.head = 0; + deq.len = 100; while !deq.is_empty() { test::black_box(deq.pop_back()); } @@ -85,8 +85,8 @@ fn bench_pop_front_100(b: &mut test::Bencher) { unsafe { deq.ptr().write_bytes(0u8, size + 1) }; b.iter(|| { - deq.head = size; - deq.tail = 0; + deq.head = 0; + deq.len = 100; while !deq.is_empty() { test::black_box(deq.pop_front()); } @@ -105,9 +105,9 @@ fn test_swap_front_back_remove() { for len in 0..final_len { let expected: VecDeque<_> = if back { (0..len).collect() } else { (0..len).rev().collect() }; - for tail_pos in 0..usable_cap { - tester.tail = tail_pos; - tester.head = tail_pos; + for head_pos in 0..usable_cap { + tester.head = head_pos; + tester.len = 0; if back { for i in 0..len * 2 { tester.push_front(i); @@ -124,8 +124,8 @@ fn test_swap_front_back_remove() { assert_eq!(tester.swap_remove_front(idx), Some(len * 2 - 1 - i)); } } - assert!(tester.tail < tester.cap()); - assert!(tester.head < tester.cap()); + assert!(tester.head <= tester.capacity()); + assert!(tester.len <= tester.capacity()); assert_eq!(tester, expected); } } @@ -150,18 +150,18 @@ fn test_insert() { for len in minlen..cap { // 0, 1, 2, .., len - 1 let expected = (0..).take(len).collect::>(); - for tail_pos in 0..cap { + for head_pos in 0..cap { for to_insert in 0..len { - tester.tail = tail_pos; - tester.head = tail_pos; + tester.head = head_pos; + tester.len = 0; for i in 0..len { if i != to_insert { tester.push_back(i); } } tester.insert(to_insert, to_insert); - assert!(tester.tail < tester.cap()); - assert!(tester.head < tester.cap()); + assert!(tester.head <= tester.capacity()); + assert!(tester.len <= tester.capacity()); assert_eq!(tester, expected); } } @@ -259,9 +259,9 @@ fn test_reserve_exact() { let mut tester: VecDeque = VecDeque::with_capacity(1); assert!(tester.capacity() == 1); tester.reserve_exact(50); - assert!(tester.capacity() >= 51); + assert!(tester.capacity() >= 50); tester.reserve_exact(40); - assert!(tester.capacity() >= 51); + assert!(tester.capacity() >= 40); tester.reserve_exact(200); assert!(tester.capacity() >= 200); } @@ -323,6 +323,7 @@ fn test_contains() { #[test] fn test_rotate_left_right() { let mut tester: VecDeque<_> = (1..=10).collect(); + tester.reserve(1); assert_eq!(tester.len(), 10); @@ -478,9 +479,9 @@ fn make_contiguous_big_tail() { assert_eq!(tester.capacity(), 15); assert_eq!((&[9, 8, 7, 6, 5, 4, 3] as &[_], &[0, 1, 2] as &[_]), tester.as_slices()); - let expected_start = tester.head; + let expected_start = tester.wrap_idx(tester.len); tester.make_contiguous(); - assert_eq!(tester.tail, expected_start); + assert_eq!(tester.head, expected_start); assert_eq!((&[9, 8, 7, 6, 5, 4, 3, 0, 1, 2] as &[_], &[] as &[_]), tester.as_slices()); } @@ -499,7 +500,7 @@ fn make_contiguous_big_head() { // 01234567......98 let expected_start = 0; tester.make_contiguous(); - assert_eq!(tester.tail, expected_start); + assert_eq!(tester.head, expected_start); assert_eq!((&[9, 8, 0, 1, 2, 3, 4, 5, 6, 7] as &[_], &[] as &[_]), tester.as_slices()); } @@ -515,10 +516,12 @@ fn make_contiguous_small_free() { tester.push_front(i as char); } + assert_eq!(tester, ['M', 'L', 'K', 'J', 'I', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']); + // ABCDEFGH...MLKJI let expected_start = 0; tester.make_contiguous(); - assert_eq!(tester.tail, expected_start); + assert_eq!(tester.head, expected_start); assert_eq!( (&['M', 'L', 'K', 'J', 'I', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] as &[_], &[] as &[_]), tester.as_slices() @@ -536,7 +539,7 @@ fn make_contiguous_small_free() { // IJKLM...HGFEDCBA let expected_start = 0; tester.make_contiguous(); - assert_eq!(tester.tail, expected_start); + assert_eq!(tester.head, expected_start); assert_eq!( (&['H', 'G', 'F', 'E', 'D', 'C', 'B', 'A', 'I', 'J', 'K', 'L', 'M'] as &[_], &[] as &[_]), tester.as_slices() @@ -550,10 +553,10 @@ fn make_contiguous_head_to_end() { dq.push_front('A'); dq.push_back('C'); dq.make_contiguous(); - let expected_tail = 0; - let expected_head = 3; - assert_eq!(expected_tail, dq.tail); + let expected_head = 0; + let expected_len = 3; assert_eq!(expected_head, dq.head); + assert_eq!(expected_len, dq.len); assert_eq!((&['A', 'B', 'C'] as &[_], &[] as &[_]), dq.as_slices()); } @@ -588,10 +591,10 @@ fn test_remove() { for len in minlen..cap - 1 { // 0, 1, 2, .., len - 1 let expected = (0..).take(len).collect::>(); - for tail_pos in 0..cap { + for head_pos in 0..cap { for to_remove in 0..=len { - tester.tail = tail_pos; - tester.head = tail_pos; + tester.head = head_pos; + tester.len = 0; for i in 0..len { if i == to_remove { tester.push_back(1234); @@ -602,8 +605,8 @@ fn test_remove() { tester.push_back(1234); } tester.remove(to_remove); - assert!(tester.tail < tester.cap()); - assert!(tester.head < tester.cap()); + assert!(tester.head <= tester.capacity()); + assert!(tester.len <= tester.capacity()); assert_eq!(tester, expected); } } @@ -617,11 +620,11 @@ fn test_range() { let cap = tester.capacity(); let minlen = if cfg!(miri) { cap - 1 } else { 0 }; // Miri is too slow for len in minlen..=cap { - for tail in 0..=cap { + for head in 0..=cap { for start in 0..=len { for end in start..=len { - tester.tail = tail; - tester.head = tail; + tester.head = head; + tester.len = 0; for i in 0..len { tester.push_back(i); } @@ -642,17 +645,17 @@ fn test_range_mut() { let cap = tester.capacity(); for len in 0..=cap { - for tail in 0..=cap { + for head in 0..=cap { for start in 0..=len { for end in start..=len { - tester.tail = tail; - tester.head = tail; + tester.head = head; + tester.len = 0; for i in 0..len { tester.push_back(i); } let head_was = tester.head; - let tail_was = tester.tail; + let len_was = tester.len; // Check that we iterate over the correct values let range: VecDeque<_> = tester.range_mut(start..end).map(|v| *v).collect(); @@ -662,8 +665,8 @@ fn test_range_mut() { // We shouldn't have changed the capacity or made the // head or tail out of bounds assert_eq!(tester.capacity(), cap); - assert_eq!(tester.tail, tail_was); assert_eq!(tester.head, head_was); + assert_eq!(tester.len, len_was); } } } @@ -676,11 +679,11 @@ fn test_drain() { let cap = tester.capacity(); for len in 0..=cap { - for tail in 0..=cap { + for head in 0..=cap { for drain_start in 0..=len { for drain_end in drain_start..=len { - tester.tail = tail; - tester.head = tail; + tester.head = head; + tester.len = 0; for i in 0..len { tester.push_back(i); } @@ -693,8 +696,8 @@ fn test_drain() { // We shouldn't have changed the capacity or made the // head or tail out of bounds assert_eq!(tester.capacity(), cap); - assert!(tester.tail < tester.cap()); - assert!(tester.head < tester.cap()); + assert!(tester.head <= tester.capacity()); + assert!(tester.len <= tester.capacity()); // We should see the correct values in the VecDeque let expected: VecDeque<_> = (0..drain_start).chain(drain_end..len).collect(); @@ -721,17 +724,18 @@ fn test_shrink_to_fit() { for len in 0..=cap { // 0, 1, 2, .., len - 1 let expected = (0..).take(len).collect::>(); - for tail_pos in 0..=max_cap { - tester.tail = tail_pos; - tester.head = tail_pos; + for head_pos in 0..=max_cap { + tester.reserve(head_pos); + tester.head = head_pos; + tester.len = 0; tester.reserve(63); for i in 0..len { tester.push_back(i); } tester.shrink_to_fit(); assert!(tester.capacity() <= cap); - assert!(tester.tail < tester.cap()); - assert!(tester.head < tester.cap()); + assert!(tester.head <= tester.capacity()); + assert!(tester.len <= tester.capacity()); assert_eq!(tester, expected); } } @@ -758,17 +762,17 @@ fn test_split_off() { // at, at + 1, .., len - 1 (may be empty) let expected_other = (at..).take(len - at).collect::>(); - for tail_pos in 0..cap { - tester.tail = tail_pos; - tester.head = tail_pos; + for head_pos in 0..cap { + tester.head = head_pos; + tester.len = 0; for i in 0..len { tester.push_back(i); } let result = tester.split_off(at); - assert!(tester.tail < tester.cap()); - assert!(tester.head < tester.cap()); - assert!(result.tail < result.cap()); - assert!(result.head < result.cap()); + assert!(tester.head <= tester.capacity()); + assert!(tester.len <= tester.capacity()); + assert!(result.head <= result.capacity()); + assert!(result.len <= result.capacity()); assert_eq!(tester, expected_self); assert_eq!(result, expected_other); } @@ -785,16 +789,10 @@ fn test_from_vec() { vec.extend(0..len); let vd = VecDeque::from(vec.clone()); - assert!(vd.cap().is_power_of_two()); assert_eq!(vd.len(), vec.len()); assert!(vd.into_iter().eq(vec)); } } - - let vec = Vec::from([(); MAXIMUM_ZST_CAPACITY - 1]); - let vd = VecDeque::from(vec.clone()); - assert!(vd.cap().is_power_of_two()); - assert_eq!(vd.len(), vec.len()); } #[test] @@ -846,10 +844,6 @@ fn test_extend_impl(trusted_len: bool) { } assert_eq!(self.test, self.expected); - let (a1, b1) = self.test.as_slices(); - let (a2, b2) = self.expected.as_slices(); - assert_eq!(a1, a2); - assert_eq!(b1, b2); } fn drain + Clone>(&mut self, range: R) { @@ -872,7 +866,7 @@ fn test_extend_impl(trusted_len: bool) { let mut tester = VecDequeTester::new(trusted_len); // Initial capacity - tester.test_extend(0..tester.remaining_capacity() - 1); + tester.test_extend(0..tester.remaining_capacity()); // Grow tester.test_extend(1024..2048); @@ -880,7 +874,7 @@ fn test_extend_impl(trusted_len: bool) { // Wrap around tester.drain(..128); - tester.test_extend(0..tester.remaining_capacity() - 1); + tester.test_extend(0..tester.remaining_capacity()); // Continue tester.drain(256..); @@ -892,16 +886,6 @@ fn test_extend_impl(trusted_len: bool) { tester.test_extend(0..32); } -#[test] -#[should_panic = "capacity overflow"] -fn test_from_vec_zst_overflow() { - use crate::vec::Vec; - let vec = Vec::from([(); MAXIMUM_ZST_CAPACITY]); - let vd = VecDeque::from(vec.clone()); // no room for +1 - assert!(vd.cap().is_power_of_two()); - assert_eq!(vd.len(), vec.len()); -} - #[test] fn test_from_array() { fn test() { @@ -917,7 +901,6 @@ fn test_from_array() { assert_eq!(deq[i], i); } - assert!(deq.cap().is_power_of_two()); assert_eq!(deq.len(), N); } test::<0>(); @@ -925,11 +908,6 @@ fn test_from_array() { test::<2>(); test::<32>(); test::<35>(); - - let array = [(); MAXIMUM_ZST_CAPACITY - 1]; - let deq = VecDeque::from(array); - assert!(deq.cap().is_power_of_two()); - assert_eq!(deq.len(), MAXIMUM_ZST_CAPACITY - 1); } #[test] diff --git a/library/alloc/tests/vec_deque.rs b/library/alloc/tests/vec_deque.rs index c1b9e7af9d857..d04de5a074b7f 100644 --- a/library/alloc/tests/vec_deque.rs +++ b/library/alloc/tests/vec_deque.rs @@ -465,7 +465,6 @@ fn test_drain() { for i in 6..9 { d.push_front(i); } - assert_eq!(d.drain(..).collect::>(), [8, 7, 6, 0, 1, 2, 3, 4]); assert!(d.is_empty()); } @@ -1142,7 +1141,7 @@ fn test_reserve_exact_2() { v.push_back(16); v.reserve_exact(16); - assert!(v.capacity() >= 48) + assert!(v.capacity() >= 33) } #[test] @@ -1157,7 +1156,7 @@ fn test_try_reserve() { // * overflow may trigger when adding `len` to `cap` (in number of elements) // * overflow may trigger when multiplying `new_cap` by size_of:: (to get bytes) - const MAX_CAP: usize = (isize::MAX as usize + 1) / 2 - 1; + const MAX_CAP: usize = isize::MAX as usize; const MAX_USIZE: usize = usize::MAX; { @@ -1248,7 +1247,7 @@ fn test_try_reserve_exact() { // This is exactly the same as test_try_reserve with the method changed. // See that test for comments. - const MAX_CAP: usize = (isize::MAX as usize + 1) / 2 - 1; + const MAX_CAP: usize = isize::MAX as usize; const MAX_USIZE: usize = usize::MAX; { @@ -1391,7 +1390,8 @@ fn test_rotate_nop() { #[test] fn test_rotate_left_parts() { - let mut v: VecDeque<_> = (1..=7).collect(); + let mut v: VecDeque<_> = VecDeque::with_capacity(8); + v.extend(1..=7); v.rotate_left(2); assert_eq!(v.as_slices(), (&[3, 4, 5, 6, 7, 1][..], &[2][..])); v.rotate_left(2); @@ -1410,7 +1410,8 @@ fn test_rotate_left_parts() { #[test] fn test_rotate_right_parts() { - let mut v: VecDeque<_> = (1..=7).collect(); + let mut v: VecDeque<_> = VecDeque::with_capacity(8); + v.extend(1..=7); v.rotate_right(2); assert_eq!(v.as_slices(), (&[6, 7][..], &[1, 2, 3, 4, 5][..])); v.rotate_right(2); diff --git a/src/etc/gdb_providers.py b/src/etc/gdb_providers.py index c351c3450f582..32b8d8e24c652 100644 --- a/src/etc/gdb_providers.py +++ b/src/etc/gdb_providers.py @@ -144,20 +144,16 @@ class StdVecDequeProvider: def __init__(self, valobj): self.valobj = valobj self.head = int(valobj["head"]) - self.tail = int(valobj["tail"]) + self.size = int(valobj["len"]) self.cap = int(valobj["buf"]["cap"]) self.data_ptr = unwrap_unique_or_non_null(valobj["buf"]["ptr"]) - if self.head >= self.tail: - self.size = self.head - self.tail - else: - self.size = self.cap + self.head - self.tail def to_string(self): return "VecDeque(size={})".format(self.size) def children(self): return _enumerate_array_elements( - (self.data_ptr + ((self.tail + index) % self.cap)) for index in xrange(self.size) + (self.data_ptr + ((self.head + index) % self.cap)) for index in xrange(self.size) ) @staticmethod diff --git a/src/etc/lldb_providers.py b/src/etc/lldb_providers.py index 8a9927e7d96d7..697ad4293c3f0 100644 --- a/src/etc/lldb_providers.py +++ b/src/etc/lldb_providers.py @@ -356,7 +356,7 @@ def has_children(self): class StdVecDequeSyntheticProvider: """Pretty-printer for alloc::collections::vec_deque::VecDeque - struct VecDeque { tail: usize, head: usize, buf: RawVec } + struct VecDeque { head: usize, len: usize, buf: RawVec } """ def __init__(self, valobj, dict): @@ -373,7 +373,7 @@ def num_children(self): def get_child_index(self, name): # type: (str) -> int index = name.lstrip('[').rstrip(']') - if index.isdigit() and self.tail <= index and (self.tail + index) % self.cap < self.head: + if index.isdigit() and int(index) < self.size: return int(index) else: return -1 @@ -381,20 +381,16 @@ def get_child_index(self, name): def get_child_at_index(self, index): # type: (int) -> SBValue start = self.data_ptr.GetValueAsUnsigned() - address = start + ((index + self.tail) % self.cap) * self.element_type_size + address = start + ((index + self.head) % self.cap) * self.element_type_size element = self.data_ptr.CreateValueFromAddress("[%s]" % index, address, self.element_type) return element def update(self): # type: () -> None self.head = self.valobj.GetChildMemberWithName("head").GetValueAsUnsigned() - self.tail = self.valobj.GetChildMemberWithName("tail").GetValueAsUnsigned() + self.size = self.valobj.GetChildMemberWithName("len").GetValueAsUnsigned() self.buf = self.valobj.GetChildMemberWithName("buf") self.cap = self.buf.GetChildMemberWithName("cap").GetValueAsUnsigned() - if self.head >= self.tail: - self.size = self.head - self.tail - else: - self.size = self.cap + self.head - self.tail self.data_ptr = unwrap_unique_or_non_null(self.buf.GetChildMemberWithName("ptr")) diff --git a/src/test/ui/hygiene/panic-location.run.stderr b/src/test/ui/hygiene/panic-location.run.stderr index 216b31586da3e..0b23b1cc2f451 100644 --- a/src/test/ui/hygiene/panic-location.run.stderr +++ b/src/test/ui/hygiene/panic-location.run.stderr @@ -1,2 +1,2 @@ -thread 'main' panicked at 'capacity overflow', $SRC_DIR/alloc/src/collections/vec_deque/mod.rs:LL:COL +thread 'main' panicked at 'capacity overflow', library/alloc/src/raw_vec.rs:518:5 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace From 0411edfbbda82407c956b69db5bf687f8749766e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Sun, 20 Nov 2022 15:34:56 +0100 Subject: [PATCH 028/244] Improve diagnostic for cases where autoderef is used --- .../src/functions/misnamed_getters.rs | 4 ++-- tests/ui/misnamed_getters.stderr | 20 ++++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/functions/misnamed_getters.rs b/clippy_lints/src/functions/misnamed_getters.rs index c553901059bb7..5424c0eecfed5 100644 --- a/clippy_lints/src/functions/misnamed_getters.rs +++ b/clippy_lints/src/functions/misnamed_getters.rs @@ -75,7 +75,7 @@ pub fn check_fn( } }; - let ty = cx.typeck_results().expr_ty(self_data); + let ty = cx.typeck_results().expr_ty_adjusted(self_data); let def = { let mut kind = ty.kind(); @@ -102,7 +102,7 @@ pub fn check_fn( let Some(used_field) = used_field else { // FIXME: This can be reached if the field access uses autoderef. - // `dec.all_fields()` should be replaced by something that uses autoderef. + // `dec.all_fields()` should be replaced by something that uses autoderef on the unajusted type of `self_data` return; }; diff --git a/tests/ui/misnamed_getters.stderr b/tests/ui/misnamed_getters.stderr index feefb95ab4f8f..5210295893e8e 100644 --- a/tests/ui/misnamed_getters.stderr +++ b/tests/ui/misnamed_getters.stderr @@ -126,5 +126,23 @@ LL | | &mut self.a LL | | } | |_____^ -error: aborting due to 14 previous errors +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:92:5 + | +LL | / unsafe fn a(&self) -> &u8 { +LL | | &self.inner.b + | | ------------- help: consider using: `&self.inner.a` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:95:5 + | +LL | / unsafe fn a_mut(&mut self) -> &mut u8 { +LL | | &mut self.inner.b + | | ----------------- help: consider using: `&mut self.inner.a` +LL | | } + | |_____^ + +error: aborting due to 16 previous errors From 1f2f50c34eb304ce56213dad70ccdeb2f2901512 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Sun, 20 Nov 2022 17:03:53 +0100 Subject: [PATCH 029/244] Fix many false negatives caused by autoderef --- .../src/functions/misnamed_getters.rs | 44 +++++++++---------- tests/ui/misnamed_getters.rs | 36 ++++++++++++--- tests/ui/misnamed_getters.stderr | 36 +++++++++++---- 3 files changed, 77 insertions(+), 39 deletions(-) diff --git a/clippy_lints/src/functions/misnamed_getters.rs b/clippy_lints/src/functions/misnamed_getters.rs index 5424c0eecfed5..27acad45ccf72 100644 --- a/clippy_lints/src/functions/misnamed_getters.rs +++ b/clippy_lints/src/functions/misnamed_getters.rs @@ -6,6 +6,8 @@ use rustc_lint::LateContext; use rustc_middle::ty; use rustc_span::Span; +use std::iter; + use super::MISNAMED_GETTERS; pub fn check_fn( @@ -75,39 +77,35 @@ pub fn check_fn( } }; - let ty = cx.typeck_results().expr_ty_adjusted(self_data); - - let def = { - let mut kind = ty.kind(); - loop { - match kind { - ty::Adt(def, _) => break def, - ty::Ref(_, ty, _) => kind = ty.kind(), - // We don't do tuples because the function name cannot be a number - _ => return, - } - } - }; - let mut used_field = None; let mut correct_field = None; - for f in def.all_fields() { - if f.name.as_str() == name { - correct_field = Some(f); - } - if f.name == used_ident.name { - used_field = Some(f); + let typeck_results = cx.typeck_results(); + for adjusted_type in iter::once(typeck_results.expr_ty(self_data)) + .chain(typeck_results.expr_adjustments(self_data).iter().map(|adj| adj.target)) + { + let ty::Adt(def,_) = adjusted_type.kind() else { + continue; + }; + + for f in def.all_fields() { + if f.name.as_str() == name { + correct_field = Some(f); + } + if f.name == used_ident.name { + used_field = Some(f); + } } } let Some(used_field) = used_field else { - // FIXME: This can be reached if the field access uses autoderef. - // `dec.all_fields()` should be replaced by something that uses autoderef on the unajusted type of `self_data` + // Can happen if the field access is a tuple. We don't lint those because the getter name could not start with a number. return; }; let Some(correct_field) = correct_field else { - return; + // There is no field corresponding to the getter name. + // FIXME: This can be a false positive if the correct field is reachable trought deeper autodereferences than used_field is + return; }; if cx.tcx.type_of(used_field.did) == cx.tcx.type_of(correct_field.did) { diff --git a/tests/ui/misnamed_getters.rs b/tests/ui/misnamed_getters.rs index 2c451bd3a162a..03e7dac7df94c 100644 --- a/tests/ui/misnamed_getters.rs +++ b/tests/ui/misnamed_getters.rs @@ -85,15 +85,37 @@ impl B { } } -struct C { - inner: Box, +struct D { + d: u8, + inner: A, } -impl C { - unsafe fn a(&self) -> &u8 { - &self.inner.b + +impl core::ops::Deref for D { + type Target = A; + fn deref(&self) -> &A { + &self.inner } - unsafe fn a_mut(&mut self) -> &mut u8 { - &mut self.inner.b +} + +impl core::ops::DerefMut for D { + fn deref_mut(&mut self) -> &mut A { + &mut self.inner + } +} + +impl D { + fn a(&self) -> &u8 { + &self.b + } + fn a_mut(&mut self) -> &mut u8 { + &mut self.b + } + + fn d(&self) -> &u8 { + &self.b + } + fn d_mut(&mut self) -> &mut u8 { + &mut self.b } } diff --git a/tests/ui/misnamed_getters.stderr b/tests/ui/misnamed_getters.stderr index 5210295893e8e..1e38a83d019a6 100644 --- a/tests/ui/misnamed_getters.stderr +++ b/tests/ui/misnamed_getters.stderr @@ -127,22 +127,40 @@ LL | | } | |_____^ error: getter function appears to return the wrong field - --> $DIR/misnamed_getters.rs:92:5 + --> $DIR/misnamed_getters.rs:107:5 | -LL | / unsafe fn a(&self) -> &u8 { -LL | | &self.inner.b - | | ------------- help: consider using: `&self.inner.a` +LL | / fn a(&self) -> &u8 { +LL | | &self.b + | | ------- help: consider using: `&self.a` LL | | } | |_____^ error: getter function appears to return the wrong field - --> $DIR/misnamed_getters.rs:95:5 + --> $DIR/misnamed_getters.rs:110:5 | -LL | / unsafe fn a_mut(&mut self) -> &mut u8 { -LL | | &mut self.inner.b - | | ----------------- help: consider using: `&mut self.inner.a` +LL | / fn a_mut(&mut self) -> &mut u8 { +LL | | &mut self.b + | | ----------- help: consider using: `&mut self.a` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:114:5 + | +LL | / fn d(&self) -> &u8 { +LL | | &self.b + | | ------- help: consider using: `&self.d` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:117:5 + | +LL | / fn d_mut(&mut self) -> &mut u8 { +LL | | &mut self.b + | | ----------- help: consider using: `&mut self.d` LL | | } | |_____^ -error: aborting due to 16 previous errors +error: aborting due to 18 previous errors From 005c6dfde667654c48ec2dd873bd3e9d0a5ae66b Mon Sep 17 00:00:00 2001 From: ismailmaj Date: Mon, 21 Nov 2022 10:38:04 +0100 Subject: [PATCH 030/244] type annotate &str when stack allocating a string --- library/core/src/str/converts.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/str/converts.rs b/library/core/src/str/converts.rs index b0c55ca4f5139..5f8748206d764 100644 --- a/library/core/src/str/converts.rs +++ b/library/core/src/str/converts.rs @@ -77,7 +77,7 @@ use super::Utf8Error; /// let sparkle_heart = [240, 159, 146, 150]; /// /// // We know these bytes are valid, so just use `unwrap()`. -/// let sparkle_heart = str::from_utf8(&sparkle_heart).unwrap(); +/// let sparkle_heart: &str = str::from_utf8(&sparkle_heart).unwrap(); /// /// assert_eq!("💖", sparkle_heart); /// ``` From ed183ee9acbc8b0c13a7a1a4859db9b4e7ca7d37 Mon Sep 17 00:00:00 2001 From: kraktus Date: Sat, 29 Oct 2022 21:45:40 +0200 Subject: [PATCH 031/244] Fix [`unnecessary_lazy_eval`] when type has significant drop --- clippy_utils/src/eager_or_lazy.rs | 26 ++++++--- tests/ui/unnecessary_lazy_eval.fixed | 23 +++++--- tests/ui/unnecessary_lazy_eval.rs | 23 +++++--- tests/ui/unnecessary_lazy_eval.stderr | 84 +++++++++++++-------------- 4 files changed, 92 insertions(+), 64 deletions(-) diff --git a/clippy_utils/src/eager_or_lazy.rs b/clippy_utils/src/eager_or_lazy.rs index 95b3e651e2b53..107405609fd56 100644 --- a/clippy_utils/src/eager_or_lazy.rs +++ b/clippy_utils/src/eager_or_lazy.rs @@ -91,6 +91,16 @@ fn fn_eagerness(cx: &LateContext<'_>, fn_id: DefId, name: Symbol, have_one_arg: } } +fn res_has_significant_drop(res: Res, cx: &LateContext<'_>, e: &Expr<'_>) -> bool { + if let Res::Def(DefKind::Ctor(..) | DefKind::Variant, _) | Res::SelfCtor(_) = res { + cx.typeck_results() + .expr_ty(e) + .has_significant_drop(cx.tcx, cx.param_env) + } else { + false + } +} + #[expect(clippy::too_many_lines)] fn expr_eagerness<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> EagernessSuggestion { struct V<'cx, 'tcx> { @@ -113,13 +123,8 @@ fn expr_eagerness<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> EagernessS }, args, ) => match self.cx.qpath_res(path, hir_id) { - Res::Def(DefKind::Ctor(..) | DefKind::Variant, _) | Res::SelfCtor(_) => { - if self - .cx - .typeck_results() - .expr_ty(e) - .has_significant_drop(self.cx.tcx, self.cx.param_env) - { + res @ (Res::Def(DefKind::Ctor(..) | DefKind::Variant, _) | Res::SelfCtor(_)) => { + if res_has_significant_drop(res, self.cx, e) { self.eagerness = ForceNoChange; return; } @@ -147,6 +152,12 @@ fn expr_eagerness<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> EagernessS self.eagerness |= NoChange; return; }, + ExprKind::Path(ref path) => { + if res_has_significant_drop(self.cx.qpath_res(path, e.hir_id), self.cx, e) { + self.eagerness = ForceNoChange; + return; + } + }, ExprKind::MethodCall(name, ..) => { self.eagerness |= self .cx @@ -206,7 +217,6 @@ fn expr_eagerness<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> EagernessS | ExprKind::Match(..) | ExprKind::Closure { .. } | ExprKind::Field(..) - | ExprKind::Path(_) | ExprKind::AddrOf(..) | ExprKind::Struct(..) | ExprKind::Repeat(..) diff --git a/tests/ui/unnecessary_lazy_eval.fixed b/tests/ui/unnecessary_lazy_eval.fixed index ce4a82e021745..22e9bd8bdc510 100644 --- a/tests/ui/unnecessary_lazy_eval.fixed +++ b/tests/ui/unnecessary_lazy_eval.fixed @@ -33,6 +33,14 @@ impl Drop for Issue9427 { } } +struct Issue9427FollowUp; + +impl Drop for Issue9427FollowUp { + fn drop(&mut self) { + panic!("side effect drop"); + } +} + fn main() { let astronomers_pi = 10; let ext_arr: [usize; 1] = [2]; @@ -87,6 +95,7 @@ fn main() { // Should not lint - bool let _ = (0 == 1).then(|| Issue9427(0)); // Issue9427 has a significant drop + let _ = false.then(|| Issue9427FollowUp); // Issue9427FollowUp has a significant drop // should not lint, bind_instead_of_map takes priority let _ = Some(10).and_then(|idx| Some(ext_arr[idx])); @@ -133,13 +142,13 @@ fn main() { let _: Result = res.or(Ok(astronomers_pi)); let _: Result = res.or(Ok(ext_str.some_field)); let _: Result = res. - // some lines - // some lines - // some lines - // some lines - // some lines - // some lines - or(Ok(ext_str.some_field)); + // some lines + // some lines + // some lines + // some lines + // some lines + // some lines + or(Ok(ext_str.some_field)); // neither bind_instead_of_map nor unnecessary_lazy_eval applies here let _: Result = res.and_then(|x| Err(x)); diff --git a/tests/ui/unnecessary_lazy_eval.rs b/tests/ui/unnecessary_lazy_eval.rs index 59cdf66285463..8726d84a23fcf 100644 --- a/tests/ui/unnecessary_lazy_eval.rs +++ b/tests/ui/unnecessary_lazy_eval.rs @@ -33,6 +33,14 @@ impl Drop for Issue9427 { } } +struct Issue9427FollowUp; + +impl Drop for Issue9427FollowUp { + fn drop(&mut self) { + panic!("side effect drop"); + } +} + fn main() { let astronomers_pi = 10; let ext_arr: [usize; 1] = [2]; @@ -87,6 +95,7 @@ fn main() { // Should not lint - bool let _ = (0 == 1).then(|| Issue9427(0)); // Issue9427 has a significant drop + let _ = false.then(|| Issue9427FollowUp); // Issue9427FollowUp has a significant drop // should not lint, bind_instead_of_map takes priority let _ = Some(10).and_then(|idx| Some(ext_arr[idx])); @@ -133,13 +142,13 @@ fn main() { let _: Result = res.or_else(|_| Ok(astronomers_pi)); let _: Result = res.or_else(|_| Ok(ext_str.some_field)); let _: Result = res. - // some lines - // some lines - // some lines - // some lines - // some lines - // some lines - or_else(|_| Ok(ext_str.some_field)); + // some lines + // some lines + // some lines + // some lines + // some lines + // some lines + or_else(|_| Ok(ext_str.some_field)); // neither bind_instead_of_map nor unnecessary_lazy_eval applies here let _: Result = res.and_then(|x| Err(x)); diff --git a/tests/ui/unnecessary_lazy_eval.stderr b/tests/ui/unnecessary_lazy_eval.stderr index 8a9ece4aa7e54..0339755442c5a 100644 --- a/tests/ui/unnecessary_lazy_eval.stderr +++ b/tests/ui/unnecessary_lazy_eval.stderr @@ -1,5 +1,5 @@ error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:48:13 + --> $DIR/unnecessary_lazy_eval.rs:56:13 | LL | let _ = opt.unwrap_or_else(|| 2); | ^^^^-------------------- @@ -9,7 +9,7 @@ LL | let _ = opt.unwrap_or_else(|| 2); = note: `-D clippy::unnecessary-lazy-evaluations` implied by `-D warnings` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:49:13 + --> $DIR/unnecessary_lazy_eval.rs:57:13 | LL | let _ = opt.unwrap_or_else(|| astronomers_pi); | ^^^^--------------------------------- @@ -17,7 +17,7 @@ LL | let _ = opt.unwrap_or_else(|| astronomers_pi); | help: use `unwrap_or(..)` instead: `unwrap_or(astronomers_pi)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:50:13 + --> $DIR/unnecessary_lazy_eval.rs:58:13 | LL | let _ = opt.unwrap_or_else(|| ext_str.some_field); | ^^^^------------------------------------- @@ -25,7 +25,7 @@ LL | let _ = opt.unwrap_or_else(|| ext_str.some_field); | help: use `unwrap_or(..)` instead: `unwrap_or(ext_str.some_field)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:52:13 + --> $DIR/unnecessary_lazy_eval.rs:60:13 | LL | let _ = opt.and_then(|_| ext_opt); | ^^^^--------------------- @@ -33,7 +33,7 @@ LL | let _ = opt.and_then(|_| ext_opt); | help: use `and(..)` instead: `and(ext_opt)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:53:13 + --> $DIR/unnecessary_lazy_eval.rs:61:13 | LL | let _ = opt.or_else(|| ext_opt); | ^^^^------------------- @@ -41,7 +41,7 @@ LL | let _ = opt.or_else(|| ext_opt); | help: use `or(..)` instead: `or(ext_opt)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:54:13 + --> $DIR/unnecessary_lazy_eval.rs:62:13 | LL | let _ = opt.or_else(|| None); | ^^^^---------------- @@ -49,7 +49,7 @@ LL | let _ = opt.or_else(|| None); | help: use `or(..)` instead: `or(None)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:55:13 + --> $DIR/unnecessary_lazy_eval.rs:63:13 | LL | let _ = opt.get_or_insert_with(|| 2); | ^^^^------------------------ @@ -57,7 +57,7 @@ LL | let _ = opt.get_or_insert_with(|| 2); | help: use `get_or_insert(..)` instead: `get_or_insert(2)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:56:13 + --> $DIR/unnecessary_lazy_eval.rs:64:13 | LL | let _ = opt.ok_or_else(|| 2); | ^^^^---------------- @@ -65,7 +65,7 @@ LL | let _ = opt.ok_or_else(|| 2); | help: use `ok_or(..)` instead: `ok_or(2)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:57:13 + --> $DIR/unnecessary_lazy_eval.rs:65:13 | LL | let _ = nested_tuple_opt.unwrap_or_else(|| Some((1, 2))); | ^^^^^^^^^^^^^^^^^------------------------------- @@ -73,7 +73,7 @@ LL | let _ = nested_tuple_opt.unwrap_or_else(|| Some((1, 2))); | help: use `unwrap_or(..)` instead: `unwrap_or(Some((1, 2)))` error: unnecessary closure used with `bool::then` - --> $DIR/unnecessary_lazy_eval.rs:58:13 + --> $DIR/unnecessary_lazy_eval.rs:66:13 | LL | let _ = cond.then(|| astronomers_pi); | ^^^^^----------------------- @@ -81,7 +81,7 @@ LL | let _ = cond.then(|| astronomers_pi); | help: use `then_some(..)` instead: `then_some(astronomers_pi)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:61:13 + --> $DIR/unnecessary_lazy_eval.rs:69:13 | LL | let _ = Some(10).unwrap_or_else(|| 2); | ^^^^^^^^^-------------------- @@ -89,7 +89,7 @@ LL | let _ = Some(10).unwrap_or_else(|| 2); | help: use `unwrap_or(..)` instead: `unwrap_or(2)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:62:13 + --> $DIR/unnecessary_lazy_eval.rs:70:13 | LL | let _ = Some(10).and_then(|_| ext_opt); | ^^^^^^^^^--------------------- @@ -97,7 +97,7 @@ LL | let _ = Some(10).and_then(|_| ext_opt); | help: use `and(..)` instead: `and(ext_opt)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:63:28 + --> $DIR/unnecessary_lazy_eval.rs:71:28 | LL | let _: Option = None.or_else(|| ext_opt); | ^^^^^------------------- @@ -105,7 +105,7 @@ LL | let _: Option = None.or_else(|| ext_opt); | help: use `or(..)` instead: `or(ext_opt)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:64:13 + --> $DIR/unnecessary_lazy_eval.rs:72:13 | LL | let _ = None.get_or_insert_with(|| 2); | ^^^^^------------------------ @@ -113,7 +113,7 @@ LL | let _ = None.get_or_insert_with(|| 2); | help: use `get_or_insert(..)` instead: `get_or_insert(2)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:65:35 + --> $DIR/unnecessary_lazy_eval.rs:73:35 | LL | let _: Result = None.ok_or_else(|| 2); | ^^^^^---------------- @@ -121,7 +121,7 @@ LL | let _: Result = None.ok_or_else(|| 2); | help: use `ok_or(..)` instead: `ok_or(2)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:66:28 + --> $DIR/unnecessary_lazy_eval.rs:74:28 | LL | let _: Option = None.or_else(|| None); | ^^^^^---------------- @@ -129,7 +129,7 @@ LL | let _: Option = None.or_else(|| None); | help: use `or(..)` instead: `or(None)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:69:13 + --> $DIR/unnecessary_lazy_eval.rs:77:13 | LL | let _ = deep.0.unwrap_or_else(|| 2); | ^^^^^^^-------------------- @@ -137,7 +137,7 @@ LL | let _ = deep.0.unwrap_or_else(|| 2); | help: use `unwrap_or(..)` instead: `unwrap_or(2)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:70:13 + --> $DIR/unnecessary_lazy_eval.rs:78:13 | LL | let _ = deep.0.and_then(|_| ext_opt); | ^^^^^^^--------------------- @@ -145,7 +145,7 @@ LL | let _ = deep.0.and_then(|_| ext_opt); | help: use `and(..)` instead: `and(ext_opt)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:71:13 + --> $DIR/unnecessary_lazy_eval.rs:79:13 | LL | let _ = deep.0.or_else(|| None); | ^^^^^^^---------------- @@ -153,7 +153,7 @@ LL | let _ = deep.0.or_else(|| None); | help: use `or(..)` instead: `or(None)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:72:13 + --> $DIR/unnecessary_lazy_eval.rs:80:13 | LL | let _ = deep.0.get_or_insert_with(|| 2); | ^^^^^^^------------------------ @@ -161,7 +161,7 @@ LL | let _ = deep.0.get_or_insert_with(|| 2); | help: use `get_or_insert(..)` instead: `get_or_insert(2)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:73:13 + --> $DIR/unnecessary_lazy_eval.rs:81:13 | LL | let _ = deep.0.ok_or_else(|| 2); | ^^^^^^^---------------- @@ -169,7 +169,7 @@ LL | let _ = deep.0.ok_or_else(|| 2); | help: use `ok_or(..)` instead: `ok_or(2)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:96:28 + --> $DIR/unnecessary_lazy_eval.rs:105:28 | LL | let _: Option = None.or_else(|| Some(3)); | ^^^^^------------------- @@ -177,7 +177,7 @@ LL | let _: Option = None.or_else(|| Some(3)); | help: use `or(..)` instead: `or(Some(3))` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:97:13 + --> $DIR/unnecessary_lazy_eval.rs:106:13 | LL | let _ = deep.0.or_else(|| Some(3)); | ^^^^^^^------------------- @@ -185,7 +185,7 @@ LL | let _ = deep.0.or_else(|| Some(3)); | help: use `or(..)` instead: `or(Some(3))` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:98:13 + --> $DIR/unnecessary_lazy_eval.rs:107:13 | LL | let _ = opt.or_else(|| Some(3)); | ^^^^------------------- @@ -193,7 +193,7 @@ LL | let _ = opt.or_else(|| Some(3)); | help: use `or(..)` instead: `or(Some(3))` error: unnecessary closure used to substitute value for `Result::Err` - --> $DIR/unnecessary_lazy_eval.rs:104:13 + --> $DIR/unnecessary_lazy_eval.rs:113:13 | LL | let _ = res2.unwrap_or_else(|_| 2); | ^^^^^--------------------- @@ -201,7 +201,7 @@ LL | let _ = res2.unwrap_or_else(|_| 2); | help: use `unwrap_or(..)` instead: `unwrap_or(2)` error: unnecessary closure used to substitute value for `Result::Err` - --> $DIR/unnecessary_lazy_eval.rs:105:13 + --> $DIR/unnecessary_lazy_eval.rs:114:13 | LL | let _ = res2.unwrap_or_else(|_| astronomers_pi); | ^^^^^---------------------------------- @@ -209,7 +209,7 @@ LL | let _ = res2.unwrap_or_else(|_| astronomers_pi); | help: use `unwrap_or(..)` instead: `unwrap_or(astronomers_pi)` error: unnecessary closure used to substitute value for `Result::Err` - --> $DIR/unnecessary_lazy_eval.rs:106:13 + --> $DIR/unnecessary_lazy_eval.rs:115:13 | LL | let _ = res2.unwrap_or_else(|_| ext_str.some_field); | ^^^^^-------------------------------------- @@ -217,7 +217,7 @@ LL | let _ = res2.unwrap_or_else(|_| ext_str.some_field); | help: use `unwrap_or(..)` instead: `unwrap_or(ext_str.some_field)` error: unnecessary closure used to substitute value for `Result::Err` - --> $DIR/unnecessary_lazy_eval.rs:128:35 + --> $DIR/unnecessary_lazy_eval.rs:137:35 | LL | let _: Result = res.and_then(|_| Err(2)); | ^^^^-------------------- @@ -225,7 +225,7 @@ LL | let _: Result = res.and_then(|_| Err(2)); | help: use `and(..)` instead: `and(Err(2))` error: unnecessary closure used to substitute value for `Result::Err` - --> $DIR/unnecessary_lazy_eval.rs:129:35 + --> $DIR/unnecessary_lazy_eval.rs:138:35 | LL | let _: Result = res.and_then(|_| Err(astronomers_pi)); | ^^^^--------------------------------- @@ -233,7 +233,7 @@ LL | let _: Result = res.and_then(|_| Err(astronomers_pi)); | help: use `and(..)` instead: `and(Err(astronomers_pi))` error: unnecessary closure used to substitute value for `Result::Err` - --> $DIR/unnecessary_lazy_eval.rs:130:35 + --> $DIR/unnecessary_lazy_eval.rs:139:35 | LL | let _: Result = res.and_then(|_| Err(ext_str.some_field)); | ^^^^------------------------------------- @@ -241,7 +241,7 @@ LL | let _: Result = res.and_then(|_| Err(ext_str.some_field)) | help: use `and(..)` instead: `and(Err(ext_str.some_field))` error: unnecessary closure used to substitute value for `Result::Err` - --> $DIR/unnecessary_lazy_eval.rs:132:35 + --> $DIR/unnecessary_lazy_eval.rs:141:35 | LL | let _: Result = res.or_else(|_| Ok(2)); | ^^^^------------------ @@ -249,7 +249,7 @@ LL | let _: Result = res.or_else(|_| Ok(2)); | help: use `or(..)` instead: `or(Ok(2))` error: unnecessary closure used to substitute value for `Result::Err` - --> $DIR/unnecessary_lazy_eval.rs:133:35 + --> $DIR/unnecessary_lazy_eval.rs:142:35 | LL | let _: Result = res.or_else(|_| Ok(astronomers_pi)); | ^^^^------------------------------- @@ -257,7 +257,7 @@ LL | let _: Result = res.or_else(|_| Ok(astronomers_pi)); | help: use `or(..)` instead: `or(Ok(astronomers_pi))` error: unnecessary closure used to substitute value for `Result::Err` - --> $DIR/unnecessary_lazy_eval.rs:134:35 + --> $DIR/unnecessary_lazy_eval.rs:143:35 | LL | let _: Result = res.or_else(|_| Ok(ext_str.some_field)); | ^^^^----------------------------------- @@ -265,19 +265,19 @@ LL | let _: Result = res.or_else(|_| Ok(ext_str.some_field)); | help: use `or(..)` instead: `or(Ok(ext_str.some_field))` error: unnecessary closure used to substitute value for `Result::Err` - --> $DIR/unnecessary_lazy_eval.rs:135:35 + --> $DIR/unnecessary_lazy_eval.rs:144:35 | LL | let _: Result = res. | ___________________________________^ -LL | | // some lines -LL | | // some lines -LL | | // some lines +LL | | // some lines +LL | | // some lines +LL | | // some lines ... | -LL | | // some lines -LL | | or_else(|_| Ok(ext_str.some_field)); - | |_________----------------------------------^ - | | - | help: use `or(..)` instead: `or(Ok(ext_str.some_field))` +LL | | // some lines +LL | | or_else(|_| Ok(ext_str.some_field)); + | |_____----------------------------------^ + | | + | help: use `or(..)` instead: `or(Ok(ext_str.some_field))` error: aborting due to 34 previous errors From 386d0a5c67921b19e62522e693cba9738a08760e Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 2 Nov 2022 15:10:05 +0000 Subject: [PATCH 032/244] Add an always-ambiguous predicate to make sure that we don't accidentlally allow trait resolution to prove false things during coherence --- clippy_utils/src/qualify_min_const_fn.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/clippy_utils/src/qualify_min_const_fn.rs b/clippy_utils/src/qualify_min_const_fn.rs index 45b63a4aa5df8..b48bacb9ace6f 100644 --- a/clippy_utils/src/qualify_min_const_fn.rs +++ b/clippy_utils/src/qualify_min_const_fn.rs @@ -37,6 +37,7 @@ pub fn is_min_const_fn<'a, 'tcx>(tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, msrv: ty::PredicateKind::ClosureKind(..) => panic!("closure kind predicate on function: {predicate:#?}"), ty::PredicateKind::Subtype(_) => panic!("subtype predicate on function: {predicate:#?}"), ty::PredicateKind::Coerce(_) => panic!("coerce predicate on function: {predicate:#?}"), + ty::PredicateKind::Ambiguous => panic!("ambiguous predicate on function: {predicate:#?}"), } } match predicates.parent { From 637139d2ff4202f9296777f8f4750bcc233b09f8 Mon Sep 17 00:00:00 2001 From: Alex Macleod Date: Mon, 21 Nov 2022 14:37:51 +0000 Subject: [PATCH 033/244] Add `clippy_utils::msrv::Msrv` to keep track of the current MSRV --- book/src/development/adding_lints.md | 8 +- clippy_dev/src/new_lint.rs | 16 ++- .../src/almost_complete_letter_range.rs | 11 +- clippy_lints/src/approx_const.rs | 8 +- clippy_lints/src/attrs.rs | 12 +- .../src/casts/cast_abs_to_unsigned.rs | 7 +- clippy_lints/src/casts/cast_lossless.rs | 16 +-- .../src/casts/cast_slice_different_sizes.rs | 8 +- .../src/casts/cast_slice_from_raw_parts.rs | 14 +-- clippy_lints/src/casts/mod.rs | 22 ++-- clippy_lints/src/casts/ptr_as_ptr.rs | 7 +- clippy_lints/src/checked_conversions.rs | 10 +- clippy_lints/src/dereference.rs | 18 +-- clippy_lints/src/format_args.rs | 10 +- clippy_lints/src/from_over_into.rs | 10 +- clippy_lints/src/if_then_some_else_none.rs | 14 +-- clippy_lints/src/index_refutable_slice.rs | 11 +- clippy_lints/src/instant_subtraction.rs | 18 ++- clippy_lints/src/lib.rs | 108 ++++++------------ clippy_lints/src/manual_bits.rs | 10 +- clippy_lints/src/manual_clamp.rs | 33 +++--- clippy_lints/src/manual_is_ascii_check.rs | 15 +-- clippy_lints/src/manual_let_else.rs | 10 +- clippy_lints/src/manual_non_exhaustive.rs | 16 +-- clippy_lints/src/manual_rem_euclid.rs | 12 +- clippy_lints/src/manual_retain.rs | 24 ++-- clippy_lints/src/manual_strip.rs | 10 +- clippy_lints/src/matches/mod.rs | 14 +-- clippy_lints/src/mem_replace.rs | 10 +- .../src/methods/cloned_instead_of_copied.rs | 10 +- clippy_lints/src/methods/err_expect.rs | 8 +- clippy_lints/src/methods/filter_map_next.rs | 8 +- .../src/methods/is_digit_ascii_radix.rs | 9 +- clippy_lints/src/methods/map_clone.rs | 16 +-- clippy_lints/src/methods/map_unwrap_or.rs | 7 +- clippy_lints/src/methods/mod.rs | 36 +++--- .../src/methods/option_as_ref_deref.rs | 8 +- clippy_lints/src/methods/str_splitn.rs | 8 +- .../src/methods/unnecessary_to_owned.rs | 9 +- clippy_lints/src/missing_const_for_fn.rs | 14 +-- clippy_lints/src/ranges.rs | 10 +- clippy_lints/src/redundant_field_names.rs | 9 +- .../src/redundant_static_lifetimes.rs | 9 +- clippy_lints/src/transmute/mod.rs | 8 +- .../src/transmute/transmute_ptr_to_ref.rs | 10 +- clippy_lints/src/unnested_or_patterns.rs | 17 ++- clippy_lints/src/use_self.rs | 14 +-- .../utils/internal_lints/msrv_attr_impl.rs | 2 +- clippy_utils/src/lib.rs | 33 +----- clippy_utils/src/msrvs.rs | 101 ++++++++++++++++ clippy_utils/src/paths.rs | 4 +- clippy_utils/src/qualify_min_const_fn.rs | 18 ++- .../ui-internal/invalid_msrv_attr_impl.fixed | 4 +- tests/ui-internal/invalid_msrv_attr_impl.rs | 4 +- tests/ui/min_rust_version_attr.rs | 23 ++++ tests/ui/min_rust_version_attr.stderr | 18 ++- 56 files changed, 466 insertions(+), 433 deletions(-) diff --git a/book/src/development/adding_lints.md b/book/src/development/adding_lints.md index 3c3f368a529b1..29d0d45dcc965 100644 --- a/book/src/development/adding_lints.md +++ b/book/src/development/adding_lints.md @@ -443,22 +443,22 @@ value is passed to the constructor in `clippy_lints/lib.rs`. ```rust pub struct ManualStrip { - msrv: Option, + msrv: Msrv, } impl ManualStrip { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } ``` The project's MSRV can then be matched against the feature MSRV in the LintPass -using the `meets_msrv` utility function. +using the `Msrv::meets` method. ``` rust -if !meets_msrv(self.msrv, msrvs::STR_STRIP_PREFIX) { +if !self.msrv.meets(msrvs::STR_STRIP_PREFIX) { return; } ``` diff --git a/clippy_dev/src/new_lint.rs b/clippy_dev/src/new_lint.rs index 9e15f1504fa91..ec7f1dd0d846c 100644 --- a/clippy_dev/src/new_lint.rs +++ b/clippy_dev/src/new_lint.rs @@ -120,7 +120,7 @@ fn add_lint(lint: &LintData<'_>, enable_msrv: bool) -> io::Result<()> { let new_lint = if enable_msrv { format!( - "store.register_{lint_pass}_pass(move |{ctor_arg}| Box::new({module_name}::{camel_name}::new(msrv)));\n ", + "store.register_{lint_pass}_pass(move |{ctor_arg}| Box::new({module_name}::{camel_name}::new(msrv())));\n ", lint_pass = lint.pass, ctor_arg = if lint.pass == "late" { "_" } else { "" }, module_name = lint.name, @@ -238,10 +238,9 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String { result.push_str(&if enable_msrv { formatdoc!( r#" - use clippy_utils::msrvs; + use clippy_utils::msrvs::{{self, Msrv}}; {pass_import} use rustc_lint::{{{context_import}, {pass_type}, LintContext}}; - use rustc_semver::RustcVersion; use rustc_session::{{declare_tool_lint, impl_lint_pass}}; "# @@ -263,12 +262,12 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String { formatdoc!( r#" pub struct {name_camel} {{ - msrv: Option, + msrv: Msrv, }} impl {name_camel} {{ #[must_use] - pub fn new(msrv: Option) -> Self {{ + pub fn new(msrv: Msrv) -> Self {{ Self {{ msrv }} }} }} @@ -357,15 +356,14 @@ fn create_lint_for_ty(lint: &LintData<'_>, enable_msrv: bool, ty: &str) -> io::R let _ = writedoc!( lint_file_contents, r#" - use clippy_utils::{{meets_msrv, msrvs}}; + use clippy_utils::msrvs::{{self, Msrv}}; use rustc_lint::{{{context_import}, LintContext}}; - use rustc_semver::RustcVersion; use super::{name_upper}; // TODO: Adjust the parameters as necessary - pub(super) fn check(cx: &{context_import}, msrv: Option) {{ - if !meets_msrv(msrv, todo!("Add a new entry in `clippy_utils/src/msrvs`")) {{ + pub(super) fn check(cx: &{context_import}, msrv: &Msrv) {{ + if !msrv.meets(todo!("Add a new entry in `clippy_utils/src/msrvs`")) {{ return; }} todo!(); diff --git a/clippy_lints/src/almost_complete_letter_range.rs b/clippy_lints/src/almost_complete_letter_range.rs index 073e4af1318e3..3cf8ba0a52119 100644 --- a/clippy_lints/src/almost_complete_letter_range.rs +++ b/clippy_lints/src/almost_complete_letter_range.rs @@ -1,11 +1,10 @@ use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::{trim_span, walk_span_to_context}; -use clippy_utils::{meets_msrv, msrvs}; use rustc_ast::ast::{Expr, ExprKind, LitKind, Pat, PatKind, RangeEnd, RangeLimits}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; use rustc_middle::lint::in_external_macro; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::Span; @@ -33,10 +32,10 @@ declare_clippy_lint! { impl_lint_pass!(AlmostCompleteLetterRange => [ALMOST_COMPLETE_LETTER_RANGE]); pub struct AlmostCompleteLetterRange { - msrv: Option, + msrv: Msrv, } impl AlmostCompleteLetterRange { - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -46,7 +45,7 @@ impl EarlyLintPass for AlmostCompleteLetterRange { let ctxt = e.span.ctxt(); let sugg = if let Some(start) = walk_span_to_context(start.span, ctxt) && let Some(end) = walk_span_to_context(end.span, ctxt) - && meets_msrv(self.msrv, msrvs::RANGE_INCLUSIVE) + && self.msrv.meets(msrvs::RANGE_INCLUSIVE) { Some((trim_span(cx.sess().source_map(), start.between(end)), "..=")) } else { @@ -60,7 +59,7 @@ impl EarlyLintPass for AlmostCompleteLetterRange { if let PatKind::Range(Some(start), Some(end), kind) = &p.kind && matches!(kind.node, RangeEnd::Excluded) { - let sugg = if meets_msrv(self.msrv, msrvs::RANGE_INCLUSIVE) { + let sugg = if self.msrv.meets(msrvs::RANGE_INCLUSIVE) { "..=" } else { "..." diff --git a/clippy_lints/src/approx_const.rs b/clippy_lints/src/approx_const.rs index 724490fb49592..ccf82f132f4e4 100644 --- a/clippy_lints/src/approx_const.rs +++ b/clippy_lints/src/approx_const.rs @@ -1,5 +1,5 @@ use clippy_utils::diagnostics::span_lint_and_help; -use clippy_utils::{meets_msrv, msrvs}; +use clippy_utils::msrvs::{self, Msrv}; use rustc_ast::ast::{FloatTy, LitFloatType, LitKind}; use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; @@ -63,12 +63,12 @@ const KNOWN_CONSTS: [(f64, &str, usize, Option); 19] = [ ]; pub struct ApproxConstant { - msrv: Option, + msrv: Msrv, } impl ApproxConstant { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } @@ -87,7 +87,7 @@ impl ApproxConstant { let s = s.as_str(); if s.parse::().is_ok() { for &(constant, name, min_digits, msrv) in &KNOWN_CONSTS { - if is_approx_const(constant, s, min_digits) && msrv.map_or(true, |msrv| meets_msrv(self.msrv, msrv)) { + if is_approx_const(constant, s, min_digits) && msrv.map_or(true, |msrv| self.msrv.meets(msrv)) { span_lint_and_help( cx, APPROX_CONSTANT, diff --git a/clippy_lints/src/attrs.rs b/clippy_lints/src/attrs.rs index ecf8e83375dbf..54364f9dab214 100644 --- a/clippy_lints/src/attrs.rs +++ b/clippy_lints/src/attrs.rs @@ -2,9 +2,8 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then}; use clippy_utils::macros::{is_panic, macro_backtrace}; -use clippy_utils::msrvs; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::{first_line_of_span, is_present_in_source, snippet_opt, without_block_comments}; -use clippy_utils::{extract_msrv_attr, meets_msrv}; use if_chain::if_chain; use rustc_ast::{AttrKind, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem}; use rustc_errors::Applicability; @@ -14,7 +13,6 @@ use rustc_hir::{ use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, Level, LintContext}; use rustc_middle::lint::in_external_macro; use rustc_middle::ty; -use rustc_semver::RustcVersion; use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass}; use rustc_span::source_map::Span; use rustc_span::symbol::Symbol; @@ -599,7 +597,7 @@ fn is_word(nmi: &NestedMetaItem, expected: Symbol) -> bool { } pub struct EarlyAttributes { - pub msrv: Option, + pub msrv: Msrv, } impl_lint_pass!(EarlyAttributes => [ @@ -614,7 +612,7 @@ impl EarlyLintPass for EarlyAttributes { } fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &Attribute) { - check_deprecated_cfg_attr(cx, attr, self.msrv); + check_deprecated_cfg_attr(cx, attr, &self.msrv); check_mismatched_target_os(cx, attr); } @@ -654,9 +652,9 @@ fn check_empty_line_after_outer_attr(cx: &EarlyContext<'_>, item: &rustc_ast::It } } -fn check_deprecated_cfg_attr(cx: &EarlyContext<'_>, attr: &Attribute, msrv: Option) { +fn check_deprecated_cfg_attr(cx: &EarlyContext<'_>, attr: &Attribute, msrv: &Msrv) { if_chain! { - if meets_msrv(msrv, msrvs::TOOL_ATTRIBUTES); + if msrv.meets(msrvs::TOOL_ATTRIBUTES); // check cfg_attr if attr.has_name(sym::cfg_attr); if let Some(items) = attr.meta_item_list(); diff --git a/clippy_lints/src/casts/cast_abs_to_unsigned.rs b/clippy_lints/src/casts/cast_abs_to_unsigned.rs index 3f1edabe6c504..4422629833376 100644 --- a/clippy_lints/src/casts/cast_abs_to_unsigned.rs +++ b/clippy_lints/src/casts/cast_abs_to_unsigned.rs @@ -1,11 +1,10 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::sugg::Sugg; -use clippy_utils::{meets_msrv, msrvs}; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; use rustc_lint::LateContext; use rustc_middle::ty::{self, Ty}; -use rustc_semver::RustcVersion; use super::CAST_ABS_TO_UNSIGNED; @@ -15,9 +14,9 @@ pub(super) fn check( cast_expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, - msrv: Option, + msrv: &Msrv, ) { - if meets_msrv(msrv, msrvs::UNSIGNED_ABS) + if msrv.meets(msrvs::UNSIGNED_ABS) && let ty::Int(from) = cast_from.kind() && let ty::Uint(to) = cast_to.kind() && let ExprKind::MethodCall(method_path, receiver, ..) = cast_expr.kind diff --git a/clippy_lints/src/casts/cast_lossless.rs b/clippy_lints/src/casts/cast_lossless.rs index 13c403234dad5..cf07e050ccce9 100644 --- a/clippy_lints/src/casts/cast_lossless.rs +++ b/clippy_lints/src/casts/cast_lossless.rs @@ -1,12 +1,12 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::in_constant; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_opt; use clippy_utils::ty::is_isize_or_usize; -use clippy_utils::{in_constant, meets_msrv, msrvs}; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; use rustc_lint::LateContext; use rustc_middle::ty::{self, FloatTy, Ty}; -use rustc_semver::RustcVersion; use super::{utils, CAST_LOSSLESS}; @@ -16,7 +16,7 @@ pub(super) fn check( cast_op: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, - msrv: Option, + msrv: &Msrv, ) { if !should_lint(cx, expr, cast_from, cast_to, msrv) { return; @@ -57,13 +57,7 @@ pub(super) fn check( ); } -fn should_lint( - cx: &LateContext<'_>, - expr: &Expr<'_>, - cast_from: Ty<'_>, - cast_to: Ty<'_>, - msrv: Option, -) -> bool { +fn should_lint(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, msrv: &Msrv) -> bool { // Do not suggest using From in consts/statics until it is valid to do so (see #2267). if in_constant(cx, expr.hir_id) { return false; @@ -89,7 +83,7 @@ fn should_lint( }; !is_isize_or_usize(cast_from) && from_nbits < to_nbits }, - (false, true) if matches!(cast_from.kind(), ty::Bool) && meets_msrv(msrv, msrvs::FROM_BOOL) => true, + (false, true) if matches!(cast_from.kind(), ty::Bool) && msrv.meets(msrvs::FROM_BOOL) => true, (_, _) => { matches!(cast_from.kind(), ty::Float(FloatTy::F32)) && matches!(cast_to.kind(), ty::Float(FloatTy::F64)) }, diff --git a/clippy_lints/src/casts/cast_slice_different_sizes.rs b/clippy_lints/src/casts/cast_slice_different_sizes.rs index d31d10d22b92b..e862f13e69fc7 100644 --- a/clippy_lints/src/casts/cast_slice_different_sizes.rs +++ b/clippy_lints/src/casts/cast_slice_different_sizes.rs @@ -1,16 +1,16 @@ -use clippy_utils::{diagnostics::span_lint_and_then, meets_msrv, msrvs, source}; +use clippy_utils::msrvs::{self, Msrv}; +use clippy_utils::{diagnostics::span_lint_and_then, source}; use if_chain::if_chain; use rustc_ast::Mutability; use rustc_hir::{Expr, ExprKind, Node}; use rustc_lint::LateContext; use rustc_middle::ty::{self, layout::LayoutOf, Ty, TypeAndMut}; -use rustc_semver::RustcVersion; use super::CAST_SLICE_DIFFERENT_SIZES; -pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Option) { +pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: &Msrv) { // suggestion is invalid if `ptr::slice_from_raw_parts` does not exist - if !meets_msrv(msrv, msrvs::PTR_SLICE_RAW_PARTS) { + if !msrv.meets(msrvs::PTR_SLICE_RAW_PARTS) { return; } diff --git a/clippy_lints/src/casts/cast_slice_from_raw_parts.rs b/clippy_lints/src/casts/cast_slice_from_raw_parts.rs index 284ef165998a7..627b795d6edd8 100644 --- a/clippy_lints/src/casts/cast_slice_from_raw_parts.rs +++ b/clippy_lints/src/casts/cast_slice_from_raw_parts.rs @@ -1,12 +1,12 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_with_applicability; -use clippy_utils::{match_def_path, meets_msrv, msrvs, paths}; +use clippy_utils::{match_def_path, paths}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::{def_id::DefId, Expr, ExprKind}; use rustc_lint::LateContext; use rustc_middle::ty::{self, Ty}; -use rustc_semver::RustcVersion; use super::CAST_SLICE_FROM_RAW_PARTS; @@ -25,15 +25,9 @@ fn raw_parts_kind(cx: &LateContext<'_>, did: DefId) -> Option { } } -pub(super) fn check( - cx: &LateContext<'_>, - expr: &Expr<'_>, - cast_expr: &Expr<'_>, - cast_to: Ty<'_>, - msrv: Option, -) { +pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_to: Ty<'_>, msrv: &Msrv) { if_chain! { - if meets_msrv(msrv, msrvs::PTR_SLICE_RAW_PARTS); + if msrv.meets(msrvs::PTR_SLICE_RAW_PARTS); if let ty::RawPtr(ptrty) = cast_to.kind(); if let ty::Slice(_) = ptrty.ty.kind(); if let ExprKind::Call(fun, [ptr_arg, len_arg]) = cast_expr.peel_blocks().kind; diff --git a/clippy_lints/src/casts/mod.rs b/clippy_lints/src/casts/mod.rs index 7148b5e6ebf45..c6d505c4a181f 100644 --- a/clippy_lints/src/casts/mod.rs +++ b/clippy_lints/src/casts/mod.rs @@ -21,11 +21,11 @@ mod ptr_as_ptr; mod unnecessary_cast; mod utils; -use clippy_utils::{is_hir_ty_cfg_dependant, meets_msrv, msrvs}; +use clippy_utils::is_hir_ty_cfg_dependant; +use clippy_utils::msrvs::{self, Msrv}; use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; declare_clippy_lint! { @@ -648,12 +648,12 @@ declare_clippy_lint! { } pub struct Casts { - msrv: Option, + msrv: Msrv, } impl Casts { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -686,7 +686,7 @@ impl_lint_pass!(Casts => [ impl<'tcx> LateLintPass<'tcx> for Casts { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { if !in_external_macro(cx.sess(), expr.span) { - ptr_as_ptr::check(cx, expr, self.msrv); + ptr_as_ptr::check(cx, expr, &self.msrv); } if expr.span.from_expansion() { @@ -705,7 +705,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts { if unnecessary_cast::check(cx, expr, cast_expr, cast_from, cast_to) { return; } - cast_slice_from_raw_parts::check(cx, expr, cast_expr, cast_to, self.msrv); + cast_slice_from_raw_parts::check(cx, expr, cast_expr, cast_to, &self.msrv); as_ptr_cast_mut::check(cx, expr, cast_expr, cast_to); fn_to_numeric_cast_any::check(cx, expr, cast_expr, cast_from, cast_to); fn_to_numeric_cast::check(cx, expr, cast_expr, cast_from, cast_to); @@ -717,16 +717,16 @@ impl<'tcx> LateLintPass<'tcx> for Casts { cast_possible_wrap::check(cx, expr, cast_from, cast_to); cast_precision_loss::check(cx, expr, cast_from, cast_to); cast_sign_loss::check(cx, expr, cast_expr, cast_from, cast_to); - cast_abs_to_unsigned::check(cx, expr, cast_expr, cast_from, cast_to, self.msrv); + cast_abs_to_unsigned::check(cx, expr, cast_expr, cast_from, cast_to, &self.msrv); cast_nan_to_int::check(cx, expr, cast_expr, cast_from, cast_to); } - cast_lossless::check(cx, expr, cast_expr, cast_from, cast_to, self.msrv); + cast_lossless::check(cx, expr, cast_expr, cast_from, cast_to, &self.msrv); cast_enum_constructor::check(cx, expr, cast_expr, cast_from); } as_underscore::check(cx, expr, cast_to_hir); - if meets_msrv(self.msrv, msrvs::BORROW_AS_PTR) { + if self.msrv.meets(msrvs::BORROW_AS_PTR) { borrow_as_ptr::check(cx, expr, cast_expr, cast_to_hir); } } @@ -734,8 +734,8 @@ impl<'tcx> LateLintPass<'tcx> for Casts { cast_ref_to_mut::check(cx, expr); cast_ptr_alignment::check(cx, expr); char_lit_as_u8::check(cx, expr); - ptr_as_ptr::check(cx, expr, self.msrv); - cast_slice_different_sizes::check(cx, expr, self.msrv); + ptr_as_ptr::check(cx, expr, &self.msrv); + cast_slice_different_sizes::check(cx, expr, &self.msrv); } extract_msrv_attr!(LateContext); diff --git a/clippy_lints/src/casts/ptr_as_ptr.rs b/clippy_lints/src/casts/ptr_as_ptr.rs index c2b9253ec35dc..43d75a03235e2 100644 --- a/clippy_lints/src/casts/ptr_as_ptr.rs +++ b/clippy_lints/src/casts/ptr_as_ptr.rs @@ -1,19 +1,18 @@ use std::borrow::Cow; use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::sugg::Sugg; -use clippy_utils::{meets_msrv, msrvs}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, Mutability, TyKind}; use rustc_lint::LateContext; use rustc_middle::ty::{self, TypeAndMut}; -use rustc_semver::RustcVersion; use super::PTR_AS_PTR; -pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: Option) { - if !meets_msrv(msrv, msrvs::POINTER_CAST) { +pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: &Msrv) { + if !msrv.meets(msrvs::POINTER_CAST) { return; } diff --git a/clippy_lints/src/checked_conversions.rs b/clippy_lints/src/checked_conversions.rs index 78e9921f036f3..9102a89e37726 100644 --- a/clippy_lints/src/checked_conversions.rs +++ b/clippy_lints/src/checked_conversions.rs @@ -1,14 +1,14 @@ //! lint on manually implemented checked conversions that could be transformed into `try_from` use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_with_applicability; -use clippy_utils::{in_constant, is_integer_literal, meets_msrv, msrvs, SpanlessEq}; +use clippy_utils::{in_constant, is_integer_literal, SpanlessEq}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::{BinOp, BinOpKind, Expr, ExprKind, QPath, TyKind}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; declare_clippy_lint! { @@ -37,12 +37,12 @@ declare_clippy_lint! { } pub struct CheckedConversions { - msrv: Option, + msrv: Msrv, } impl CheckedConversions { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -51,7 +51,7 @@ impl_lint_pass!(CheckedConversions => [CHECKED_CONVERSIONS]); impl<'tcx> LateLintPass<'tcx> for CheckedConversions { fn check_expr(&mut self, cx: &LateContext<'_>, item: &Expr<'_>) { - if !meets_msrv(self.msrv, msrvs::TRY_FROM) { + if !self.msrv.meets(msrvs::TRY_FROM) { return; } diff --git a/clippy_lints/src/dereference.rs b/clippy_lints/src/dereference.rs index 0eee7f23982d0..faa8467d26614 100644 --- a/clippy_lints/src/dereference.rs +++ b/clippy_lints/src/dereference.rs @@ -1,12 +1,13 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then}; use clippy_utils::mir::{enclosing_mir, expr_local, local_assignments, used_exactly_once, PossibleBorrowerMap}; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::{snippet_with_applicability, snippet_with_context}; use clippy_utils::sugg::has_enclosing_paren; use clippy_utils::ty::{expr_sig, is_copy, peel_mid_ty_refs, ty_sig, variant_of_res}; use clippy_utils::{ - fn_def_id, get_parent_expr, get_parent_expr_for_hir, is_lint_allowed, meets_msrv, msrvs, path_to_local, - walk_to_expr_usage, + fn_def_id, get_parent_expr, get_parent_expr_for_hir, is_lint_allowed, path_to_local, walk_to_expr_usage, }; + use rustc_ast::util::parser::{PREC_POSTFIX, PREC_PREFIX}; use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::graph::iterate::{CycleDetector, TriColorDepthFirstSearch}; @@ -28,7 +29,6 @@ use rustc_middle::ty::{ self, Binder, BoundVariableKind, EarlyBinder, FnSig, GenericArgKind, List, ParamTy, PredicateKind, ProjectionPredicate, Ty, TyCtxt, TypeVisitable, TypeckResults, }; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::{symbol::sym, Span, Symbol, DUMMY_SP}; use rustc_trait_selection::infer::InferCtxtExt as _; @@ -181,12 +181,12 @@ pub struct Dereferencing<'tcx> { possible_borrowers: Vec<(LocalDefId, PossibleBorrowerMap<'tcx, 'tcx>)>, // `IntoIterator` for arrays requires Rust 1.53. - msrv: Option, + msrv: Msrv, } impl<'tcx> Dereferencing<'tcx> { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv, ..Dereferencing::default() @@ -286,7 +286,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> { match (self.state.take(), kind) { (None, kind) => { let expr_ty = typeck.expr_ty(expr); - let (position, adjustments) = walk_parents(cx, &mut self.possible_borrowers, expr, self.msrv); + let (position, adjustments) = walk_parents(cx, &mut self.possible_borrowers, expr, &self.msrv); match kind { RefOp::Deref => { if let Position::FieldAccess { @@ -698,7 +698,7 @@ fn walk_parents<'tcx>( cx: &LateContext<'tcx>, possible_borrowers: &mut Vec<(LocalDefId, PossibleBorrowerMap<'tcx, 'tcx>)>, e: &'tcx Expr<'_>, - msrv: Option, + msrv: &Msrv, ) -> (Position, &'tcx [Adjustment<'tcx>]) { let mut adjustments = [].as_slice(); let mut precedence = 0i8; @@ -1082,7 +1082,7 @@ fn needless_borrow_impl_arg_position<'tcx>( param_ty: ParamTy, mut expr: &Expr<'tcx>, precedence: i8, - msrv: Option, + msrv: &Msrv, ) -> Position { let destruct_trait_def_id = cx.tcx.lang_items().destruct_trait(); let sized_trait_def_id = cx.tcx.lang_items().sized_trait(); @@ -1182,7 +1182,7 @@ fn needless_borrow_impl_arg_position<'tcx>( && let ty::Param(param_ty) = trait_predicate.self_ty().kind() && let GenericArgKind::Type(ty) = substs_with_referent_ty[param_ty.index as usize].unpack() && ty.is_array() - && !meets_msrv(msrv, msrvs::ARRAY_INTO_ITERATOR) + && !msrv.meets(msrvs::ARRAY_INTO_ITERATOR) { return false; } diff --git a/clippy_lints/src/format_args.rs b/clippy_lints/src/format_args.rs index a73453bf02746..ec45be558f14b 100644 --- a/clippy_lints/src/format_args.rs +++ b/clippy_lints/src/format_args.rs @@ -1,11 +1,12 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then}; +use clippy_utils::is_diag_trait_item; use clippy_utils::macros::FormatParamKind::{Implicit, Named, Numbered, Starred}; use clippy_utils::macros::{ is_format_macro, is_panic, root_macro_call, Count, FormatArg, FormatArgsExpn, FormatParam, FormatParamUsage, }; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_opt; use clippy_utils::ty::{implements_trait, is_type_diagnostic_item}; -use clippy_utils::{is_diag_trait_item, meets_msrv, msrvs}; use if_chain::if_chain; use itertools::Itertools; use rustc_errors::Applicability; @@ -13,7 +14,6 @@ use rustc_hir::{Expr, ExprKind, HirId, QPath}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::ty::adjustment::{Adjust, Adjustment}; use rustc_middle::ty::Ty; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::def_id::DefId; use rustc_span::edition::Edition::Edition2021; @@ -158,12 +158,12 @@ impl_lint_pass!(FormatArgs => [ ]); pub struct FormatArgs { - msrv: Option, + msrv: Msrv, } impl FormatArgs { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -188,7 +188,7 @@ impl<'tcx> LateLintPass<'tcx> for FormatArgs { check_format_in_format_args(cx, outermost_expn_data.call_site, name, arg.param.value); check_to_string_in_format_args(cx, name, arg.param.value); } - if meets_msrv(self.msrv, msrvs::FORMAT_ARGS_CAPTURE) { + if self.msrv.meets(msrvs::FORMAT_ARGS_CAPTURE) { check_uninlined_args(cx, &format_args, outermost_expn_data.call_site, macro_def_id); } } diff --git a/clippy_lints/src/from_over_into.rs b/clippy_lints/src/from_over_into.rs index 95eda4ea88275..7ff4a05fcdd60 100644 --- a/clippy_lints/src/from_over_into.rs +++ b/clippy_lints/src/from_over_into.rs @@ -1,7 +1,8 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::macros::span_is_local; +use clippy_utils::msrvs::{self, Msrv}; +use clippy_utils::path_def_id; use clippy_utils::source::snippet_opt; -use clippy_utils::{meets_msrv, msrvs, path_def_id}; use rustc_errors::Applicability; use rustc_hir::intravisit::{walk_path, Visitor}; use rustc_hir::{ @@ -10,7 +11,6 @@ use rustc_hir::{ }; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::hir::nested_filter::OnlyBodies; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::symbol::{kw, sym}; use rustc_span::{Span, Symbol}; @@ -49,12 +49,12 @@ declare_clippy_lint! { } pub struct FromOverInto { - msrv: Option, + msrv: Msrv, } impl FromOverInto { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { FromOverInto { msrv } } } @@ -63,7 +63,7 @@ impl_lint_pass!(FromOverInto => [FROM_OVER_INTO]); impl<'tcx> LateLintPass<'tcx> for FromOverInto { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) { - if !meets_msrv(self.msrv, msrvs::RE_REBALANCING_COHERENCE) || !span_is_local(item.span) { + if !self.msrv.meets(msrvs::RE_REBALANCING_COHERENCE) || !span_is_local(item.span) { return; } diff --git a/clippy_lints/src/if_then_some_else_none.rs b/clippy_lints/src/if_then_some_else_none.rs index 0d6718c168a5c..9cadaaa493e46 100644 --- a/clippy_lints/src/if_then_some_else_none.rs +++ b/clippy_lints/src/if_then_some_else_none.rs @@ -1,14 +1,12 @@ use clippy_utils::diagnostics::span_lint_and_help; use clippy_utils::eager_or_lazy::switch_to_eager_eval; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_with_macro_callsite; -use clippy_utils::{ - contains_return, higher, is_else_clause, is_res_lang_ctor, meets_msrv, msrvs, path_res, peel_blocks, -}; +use clippy_utils::{contains_return, higher, is_else_clause, is_res_lang_ctor, path_res, peel_blocks}; use rustc_hir::LangItem::{OptionNone, OptionSome}; use rustc_hir::{Expr, ExprKind, Stmt, StmtKind}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; declare_clippy_lint! { @@ -47,12 +45,12 @@ declare_clippy_lint! { } pub struct IfThenSomeElseNone { - msrv: Option, + msrv: Msrv, } impl IfThenSomeElseNone { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -61,7 +59,7 @@ impl_lint_pass!(IfThenSomeElseNone => [IF_THEN_SOME_ELSE_NONE]); impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { - if !meets_msrv(self.msrv, msrvs::BOOL_THEN) { + if !self.msrv.meets(msrvs::BOOL_THEN) { return; } @@ -94,7 +92,7 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone { } else { format!("{{ /* snippet */ {arg_snip} }}") }; - let method_name = if switch_to_eager_eval(cx, expr) && meets_msrv(self.msrv, msrvs::BOOL_THEN_SOME) { + let method_name = if switch_to_eager_eval(cx, expr) && self.msrv.meets(msrvs::BOOL_THEN_SOME) { "then_some" } else { method_body.insert_str(0, "|| "); diff --git a/clippy_lints/src/index_refutable_slice.rs b/clippy_lints/src/index_refutable_slice.rs index 0d5099bde6de0..ee5f10da5b848 100644 --- a/clippy_lints/src/index_refutable_slice.rs +++ b/clippy_lints/src/index_refutable_slice.rs @@ -1,8 +1,9 @@ use clippy_utils::consts::{constant, Constant}; use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::higher::IfLet; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::ty::is_copy; -use clippy_utils::{is_expn_of, is_lint_allowed, meets_msrv, msrvs, path_to_local}; +use clippy_utils::{is_expn_of, is_lint_allowed, path_to_local}; use if_chain::if_chain; use rustc_data_structures::fx::{FxHashSet, FxIndexMap}; use rustc_errors::Applicability; @@ -11,7 +12,6 @@ use rustc_hir::intravisit::{self, Visitor}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::hir::nested_filter; use rustc_middle::ty; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::{symbol::Ident, Span}; @@ -51,14 +51,13 @@ declare_clippy_lint! { "avoid indexing on slices which could be destructed" } -#[derive(Copy, Clone)] pub struct IndexRefutableSlice { max_suggested_slice: u64, - msrv: Option, + msrv: Msrv, } impl IndexRefutableSlice { - pub fn new(max_suggested_slice_pattern_length: u64, msrv: Option) -> Self { + pub fn new(max_suggested_slice_pattern_length: u64, msrv: Msrv) -> Self { Self { max_suggested_slice: max_suggested_slice_pattern_length, msrv, @@ -74,7 +73,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexRefutableSlice { if !expr.span.from_expansion() || is_expn_of(expr.span, "if_chain").is_some(); if let Some(IfLet {let_pat, if_then, ..}) = IfLet::hir(cx, expr); if !is_lint_allowed(cx, INDEX_REFUTABLE_SLICE, expr.hir_id); - if meets_msrv(self.msrv, msrvs::SLICE_PATTERNS); + if self.msrv.meets(msrvs::SLICE_PATTERNS); let found_slices = find_slice_values(cx, let_pat); if !found_slices.is_empty(); diff --git a/clippy_lints/src/instant_subtraction.rs b/clippy_lints/src/instant_subtraction.rs index 60754b224fc84..dd1b23e7d9d29 100644 --- a/clippy_lints/src/instant_subtraction.rs +++ b/clippy_lints/src/instant_subtraction.rs @@ -1,13 +1,11 @@ -use clippy_utils::{ - diagnostics::{self, span_lint_and_sugg}, - meets_msrv, msrvs, source, - sugg::Sugg, - ty, -}; +use clippy_utils::diagnostics::{self, span_lint_and_sugg}; +use clippy_utils::msrvs::{self, Msrv}; +use clippy_utils::source; +use clippy_utils::sugg::Sugg; +use clippy_utils::ty; use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::{source_map::Spanned, sym}; @@ -68,12 +66,12 @@ declare_clippy_lint! { } pub struct InstantSubtraction { - msrv: Option, + msrv: Msrv, } impl InstantSubtraction { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -101,7 +99,7 @@ impl LateLintPass<'_> for InstantSubtraction { } else { if_chain! { if !expr.span.from_expansion(); - if meets_msrv(self.msrv, msrvs::TRY_FROM); + if self.msrv.meets(msrvs::TRY_FROM); if is_an_instant(cx, lhs); if is_a_duration(cx, rhs); diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 3ab5031696d53..46acc9679f512 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -52,10 +52,9 @@ extern crate declare_clippy_lint; use std::io; use std::path::PathBuf; -use clippy_utils::parse_msrv; +use clippy_utils::msrvs::Msrv; use rustc_data_structures::fx::FxHashSet; use rustc_lint::{Lint, LintId}; -use rustc_semver::RustcVersion; use rustc_session::Session; #[cfg(feature = "internal")] @@ -322,48 +321,10 @@ pub use crate::utils::conf::{lookup_conf_file, Conf}; /// Used in `./src/driver.rs`. pub fn register_pre_expansion_lints(store: &mut rustc_lint::LintStore, sess: &Session, conf: &Conf) { // NOTE: Do not add any more pre-expansion passes. These should be removed eventually. + let msrv = Msrv::read(&conf.msrv, sess); + let msrv = move || msrv.clone(); - let msrv = conf.msrv.as_ref().and_then(|s| { - parse_msrv(s, None, None).or_else(|| { - sess.err(format!( - "error reading Clippy's configuration file. `{s}` is not a valid Rust version" - )); - None - }) - }); - - store.register_pre_expansion_pass(move || Box::new(attrs::EarlyAttributes { msrv })); -} - -fn read_msrv(conf: &Conf, sess: &Session) -> Option { - let cargo_msrv = std::env::var("CARGO_PKG_RUST_VERSION") - .ok() - .and_then(|v| parse_msrv(&v, None, None)); - let clippy_msrv = conf.msrv.as_ref().and_then(|s| { - parse_msrv(s, None, None).or_else(|| { - sess.err(format!( - "error reading Clippy's configuration file. `{s}` is not a valid Rust version" - )); - None - }) - }); - - if let Some(cargo_msrv) = cargo_msrv { - if let Some(clippy_msrv) = clippy_msrv { - // if both files have an msrv, let's compare them and emit a warning if they differ - if clippy_msrv != cargo_msrv { - sess.warn(format!( - "the MSRV in `clippy.toml` and `Cargo.toml` differ; using `{clippy_msrv}` from `clippy.toml`" - )); - } - - Some(clippy_msrv) - } else { - Some(cargo_msrv) - } - } else { - clippy_msrv - } + store.register_pre_expansion_pass(move || Box::new(attrs::EarlyAttributes { msrv: msrv() })); } #[doc(hidden)] @@ -595,43 +556,44 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(non_octal_unix_permissions::NonOctalUnixPermissions)); store.register_early_pass(|| Box::new(unnecessary_self_imports::UnnecessarySelfImports)); - let msrv = read_msrv(conf, sess); + let msrv = Msrv::read(&conf.msrv, sess); + let msrv = move || msrv.clone(); let avoid_breaking_exported_api = conf.avoid_breaking_exported_api; let allow_expect_in_tests = conf.allow_expect_in_tests; let allow_unwrap_in_tests = conf.allow_unwrap_in_tests; - store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(msrv))); + store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(msrv()))); store.register_late_pass(move |_| { Box::new(methods::Methods::new( avoid_breaking_exported_api, - msrv, + msrv(), allow_expect_in_tests, allow_unwrap_in_tests, )) }); - store.register_late_pass(move |_| Box::new(matches::Matches::new(msrv))); + store.register_late_pass(move |_| Box::new(matches::Matches::new(msrv()))); let matches_for_let_else = conf.matches_for_let_else; - store.register_late_pass(move |_| Box::new(manual_let_else::ManualLetElse::new(msrv, matches_for_let_else))); - store.register_early_pass(move || Box::new(manual_non_exhaustive::ManualNonExhaustiveStruct::new(msrv))); - store.register_late_pass(move |_| Box::new(manual_non_exhaustive::ManualNonExhaustiveEnum::new(msrv))); - store.register_late_pass(move |_| Box::new(manual_strip::ManualStrip::new(msrv))); - store.register_early_pass(move || Box::new(redundant_static_lifetimes::RedundantStaticLifetimes::new(msrv))); - store.register_early_pass(move || Box::new(redundant_field_names::RedundantFieldNames::new(msrv))); - store.register_late_pass(move |_| Box::new(checked_conversions::CheckedConversions::new(msrv))); - store.register_late_pass(move |_| Box::new(mem_replace::MemReplace::new(msrv))); - store.register_late_pass(move |_| Box::new(ranges::Ranges::new(msrv))); - store.register_late_pass(move |_| Box::new(from_over_into::FromOverInto::new(msrv))); - store.register_late_pass(move |_| Box::new(use_self::UseSelf::new(msrv))); - store.register_late_pass(move |_| Box::new(missing_const_for_fn::MissingConstForFn::new(msrv))); + store.register_late_pass(move |_| Box::new(manual_let_else::ManualLetElse::new(msrv(), matches_for_let_else))); + store.register_early_pass(move || Box::new(manual_non_exhaustive::ManualNonExhaustiveStruct::new(msrv()))); + store.register_late_pass(move |_| Box::new(manual_non_exhaustive::ManualNonExhaustiveEnum::new(msrv()))); + store.register_late_pass(move |_| Box::new(manual_strip::ManualStrip::new(msrv()))); + store.register_early_pass(move || Box::new(redundant_static_lifetimes::RedundantStaticLifetimes::new(msrv()))); + store.register_early_pass(move || Box::new(redundant_field_names::RedundantFieldNames::new(msrv()))); + store.register_late_pass(move |_| Box::new(checked_conversions::CheckedConversions::new(msrv()))); + store.register_late_pass(move |_| Box::new(mem_replace::MemReplace::new(msrv()))); + store.register_late_pass(move |_| Box::new(ranges::Ranges::new(msrv()))); + store.register_late_pass(move |_| Box::new(from_over_into::FromOverInto::new(msrv()))); + store.register_late_pass(move |_| Box::new(use_self::UseSelf::new(msrv()))); + store.register_late_pass(move |_| Box::new(missing_const_for_fn::MissingConstForFn::new(msrv()))); store.register_late_pass(move |_| Box::new(needless_question_mark::NeedlessQuestionMark)); - store.register_late_pass(move |_| Box::new(casts::Casts::new(msrv))); - store.register_early_pass(move || Box::new(unnested_or_patterns::UnnestedOrPatterns::new(msrv))); + store.register_late_pass(move |_| Box::new(casts::Casts::new(msrv()))); + store.register_early_pass(move || Box::new(unnested_or_patterns::UnnestedOrPatterns::new(msrv()))); store.register_late_pass(|_| Box::new(size_of_in_element_count::SizeOfInElementCount)); store.register_late_pass(|_| Box::new(same_name_method::SameNameMethod)); let max_suggested_slice_pattern_length = conf.max_suggested_slice_pattern_length; store.register_late_pass(move |_| { Box::new(index_refutable_slice::IndexRefutableSlice::new( max_suggested_slice_pattern_length, - msrv, + msrv(), )) }); store.register_late_pass(|_| Box::::default()); @@ -648,7 +610,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(borrow_deref_ref::BorrowDerefRef)); store.register_late_pass(|_| Box::new(no_effect::NoEffect)); store.register_late_pass(|_| Box::new(temporary_assignment::TemporaryAssignment)); - store.register_late_pass(move |_| Box::new(transmute::Transmute::new(msrv))); + store.register_late_pass(move |_| Box::new(transmute::Transmute::new(msrv()))); let cognitive_complexity_threshold = conf.cognitive_complexity_threshold; store.register_late_pass(move |_| { Box::new(cognitive_complexity::CognitiveComplexity::new( @@ -806,7 +768,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(move |_| Box::new(wildcard_imports::WildcardImports::new(warn_on_all_wildcard_imports))); store.register_late_pass(|_| Box::::default()); store.register_late_pass(|_| Box::new(unnamed_address::UnnamedAddress)); - store.register_late_pass(move |_| Box::new(dereference::Dereferencing::new(msrv))); + store.register_late_pass(move |_| Box::new(dereference::Dereferencing::new(msrv()))); store.register_late_pass(|_| Box::new(option_if_let_else::OptionIfLetElse)); store.register_late_pass(|_| Box::new(future_not_send::FutureNotSend)); store.register_late_pass(|_| Box::new(if_let_mutex::IfLetMutex)); @@ -840,7 +802,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::::default()); store.register_late_pass(|_| Box::new(redundant_slicing::RedundantSlicing)); store.register_late_pass(|_| Box::new(from_str_radix_10::FromStrRadix10)); - store.register_late_pass(move |_| Box::new(if_then_some_else_none::IfThenSomeElseNone::new(msrv))); + store.register_late_pass(move |_| Box::new(if_then_some_else_none::IfThenSomeElseNone::new(msrv()))); store.register_late_pass(|_| Box::new(bool_assert_comparison::BoolAssertComparison)); store.register_early_pass(move || Box::new(module_style::ModStyle)); store.register_late_pass(|_| Box::new(unused_async::UnusedAsync)); @@ -865,14 +827,14 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: )) }); store.register_late_pass(move |_| Box::new(undocumented_unsafe_blocks::UndocumentedUnsafeBlocks)); - store.register_late_pass(move |_| Box::new(format_args::FormatArgs::new(msrv))); + store.register_late_pass(move |_| Box::new(format_args::FormatArgs::new(msrv()))); store.register_late_pass(|_| Box::new(trailing_empty_array::TrailingEmptyArray)); store.register_early_pass(|| Box::new(octal_escapes::OctalEscapes)); store.register_late_pass(|_| Box::new(needless_late_init::NeedlessLateInit)); store.register_late_pass(|_| Box::new(return_self_not_must_use::ReturnSelfNotMustUse)); store.register_late_pass(|_| Box::new(init_numbered_fields::NumberedFields)); store.register_early_pass(|| Box::new(single_char_lifetime_names::SingleCharLifetimeNames)); - store.register_late_pass(move |_| Box::new(manual_bits::ManualBits::new(msrv))); + store.register_late_pass(move |_| Box::new(manual_bits::ManualBits::new(msrv()))); store.register_late_pass(|_| Box::new(default_union_representation::DefaultUnionRepresentation)); store.register_late_pass(|_| Box::::default()); let allow_dbg_in_tests = conf.allow_dbg_in_tests; @@ -896,20 +858,20 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(rc_clone_in_vec_init::RcCloneInVecInit)); store.register_early_pass(|| Box::::default()); store.register_early_pass(|| Box::new(unused_rounding::UnusedRounding)); - store.register_early_pass(move || Box::new(almost_complete_letter_range::AlmostCompleteLetterRange::new(msrv))); + store.register_early_pass(move || Box::new(almost_complete_letter_range::AlmostCompleteLetterRange::new(msrv()))); store.register_late_pass(|_| Box::new(swap_ptr_to_ref::SwapPtrToRef)); store.register_late_pass(|_| Box::new(mismatching_type_param_order::TypeParamMismatch)); store.register_late_pass(|_| Box::new(read_zero_byte_vec::ReadZeroByteVec)); store.register_late_pass(|_| Box::new(default_instead_of_iter_empty::DefaultIterEmpty)); - store.register_late_pass(move |_| Box::new(manual_rem_euclid::ManualRemEuclid::new(msrv))); - store.register_late_pass(move |_| Box::new(manual_retain::ManualRetain::new(msrv))); + store.register_late_pass(move |_| Box::new(manual_rem_euclid::ManualRemEuclid::new(msrv()))); + store.register_late_pass(move |_| Box::new(manual_retain::ManualRetain::new(msrv()))); let verbose_bit_mask_threshold = conf.verbose_bit_mask_threshold; store.register_late_pass(move |_| Box::new(operators::Operators::new(verbose_bit_mask_threshold))); store.register_late_pass(|_| Box::new(invalid_utf8_in_unchecked::InvalidUtf8InUnchecked)); store.register_late_pass(|_| Box::::default()); - store.register_late_pass(move |_| Box::new(instant_subtraction::InstantSubtraction::new(msrv))); + store.register_late_pass(move |_| Box::new(instant_subtraction::InstantSubtraction::new(msrv()))); store.register_late_pass(|_| Box::new(partialeq_to_none::PartialeqToNone)); - store.register_late_pass(move |_| Box::new(manual_clamp::ManualClamp::new(msrv))); + store.register_late_pass(move |_| Box::new(manual_clamp::ManualClamp::new(msrv()))); store.register_late_pass(|_| Box::new(manual_string_new::ManualStringNew)); store.register_late_pass(|_| Box::new(unused_peekable::UnusedPeekable)); store.register_early_pass(|| Box::new(multi_assignments::MultiAssignments)); @@ -920,7 +882,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(missing_trait_methods::MissingTraitMethods)); store.register_late_pass(|_| Box::new(from_raw_with_void_ptr::FromRawWithVoidPtr)); store.register_late_pass(|_| Box::new(suspicious_xor_used_as_pow::ConfusingXorAndPow)); - store.register_late_pass(move |_| Box::new(manual_is_ascii_check::ManualIsAsciiCheck::new(msrv))); + store.register_late_pass(move |_| Box::new(manual_is_ascii_check::ManualIsAsciiCheck::new(msrv()))); // add lints here, do not remove this comment, it's used in `new_lint` } diff --git a/clippy_lints/src/manual_bits.rs b/clippy_lints/src/manual_bits.rs index 6655c92b1da8d..462d73cf0b974 100644 --- a/clippy_lints/src/manual_bits.rs +++ b/clippy_lints/src/manual_bits.rs @@ -1,12 +1,12 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::get_parent_expr; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_with_applicability; -use clippy_utils::{get_parent_expr, meets_msrv, msrvs}; use rustc_ast::ast::LitKind; use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind, GenericArg, QPath}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty::{self, Ty}; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::sym; @@ -34,12 +34,12 @@ declare_clippy_lint! { #[derive(Clone)] pub struct ManualBits { - msrv: Option, + msrv: Msrv, } impl ManualBits { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -48,7 +48,7 @@ impl_lint_pass!(ManualBits => [MANUAL_BITS]); impl<'tcx> LateLintPass<'tcx> for ManualBits { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - if !meets_msrv(self.msrv, msrvs::MANUAL_BITS) { + if !self.msrv.meets(msrvs::MANUAL_BITS) { return; } diff --git a/clippy_lints/src/manual_clamp.rs b/clippy_lints/src/manual_clamp.rs index 02dc8755dd61c..bb6d628af3b50 100644 --- a/clippy_lints/src/manual_clamp.rs +++ b/clippy_lints/src/manual_clamp.rs @@ -1,28 +1,25 @@ +use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then}; +use clippy_utils::higher::If; +use clippy_utils::msrvs::{self, Msrv}; +use clippy_utils::sugg::Sugg; +use clippy_utils::ty::implements_trait; +use clippy_utils::visitors::is_const_evaluatable; +use clippy_utils::MaybePath; +use clippy_utils::{ + eq_expr_value, is_diag_trait_item, is_trait_method, path_res, path_to_local_id, peel_blocks, peel_blocks_with_stmt, +}; use itertools::Itertools; +use rustc_errors::Applicability; use rustc_errors::Diagnostic; use rustc_hir::{ def::Res, Arm, BinOpKind, Block, Expr, ExprKind, Guard, HirId, PatKind, PathSegment, PrimTy, QPath, StmtKind, }; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty::Ty; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::{symbol::sym, Span}; use std::ops::Deref; -use clippy_utils::{ - diagnostics::{span_lint_and_then, span_lint_hir_and_then}, - eq_expr_value, - higher::If, - is_diag_trait_item, is_trait_method, meets_msrv, msrvs, path_res, path_to_local_id, peel_blocks, - peel_blocks_with_stmt, - sugg::Sugg, - ty::implements_trait, - visitors::is_const_evaluatable, - MaybePath, -}; -use rustc_errors::Applicability; - declare_clippy_lint! { /// ### What it does /// Identifies good opportunities for a clamp function from std or core, and suggests using it. @@ -87,11 +84,11 @@ declare_clippy_lint! { impl_lint_pass!(ManualClamp => [MANUAL_CLAMP]); pub struct ManualClamp { - msrv: Option, + msrv: Msrv, } impl ManualClamp { - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -114,7 +111,7 @@ struct InputMinMax<'tcx> { impl<'tcx> LateLintPass<'tcx> for ManualClamp { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { - if !meets_msrv(self.msrv, msrvs::CLAMP) { + if !self.msrv.meets(msrvs::CLAMP) { return; } if !expr.span.from_expansion() { @@ -130,7 +127,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualClamp { } fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) { - if !meets_msrv(self.msrv, msrvs::CLAMP) { + if !self.msrv.meets(msrvs::CLAMP) { return; } for suggestion in is_two_if_pattern(cx, block) { diff --git a/clippy_lints/src/manual_is_ascii_check.rs b/clippy_lints/src/manual_is_ascii_check.rs index bb8c142f8e468..5ab049d8d133f 100644 --- a/clippy_lints/src/manual_is_ascii_check.rs +++ b/clippy_lints/src/manual_is_ascii_check.rs @@ -1,15 +1,12 @@ +use clippy_utils::msrvs::{self, Msrv}; +use clippy_utils::{diagnostics::span_lint_and_sugg, in_constant, macros::root_macro_call, source::snippet}; use rustc_ast::LitKind::{Byte, Char}; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, PatKind, RangeEnd}; use rustc_lint::{LateContext, LateLintPass}; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::{def_id::DefId, sym}; -use clippy_utils::{ - diagnostics::span_lint_and_sugg, in_constant, macros::root_macro_call, meets_msrv, msrvs, source::snippet, -}; - declare_clippy_lint! { /// ### What it does /// Suggests to use dedicated built-in methods, @@ -45,12 +42,12 @@ declare_clippy_lint! { impl_lint_pass!(ManualIsAsciiCheck => [MANUAL_IS_ASCII_CHECK]); pub struct ManualIsAsciiCheck { - msrv: Option, + msrv: Msrv, } impl ManualIsAsciiCheck { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -70,11 +67,11 @@ enum CharRange { impl<'tcx> LateLintPass<'tcx> for ManualIsAsciiCheck { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - if !meets_msrv(self.msrv, msrvs::IS_ASCII_DIGIT) { + if !self.msrv.meets(msrvs::IS_ASCII_DIGIT) { return; } - if in_constant(cx, expr.hir_id) && !meets_msrv(self.msrv, msrvs::IS_ASCII_DIGIT_CONST) { + if in_constant(cx, expr.hir_id) && !self.msrv.meets(msrvs::IS_ASCII_DIGIT_CONST) { return; } diff --git a/clippy_lints/src/manual_let_else.rs b/clippy_lints/src/manual_let_else.rs index 1846596fa4c8e..20f06830952cf 100644 --- a/clippy_lints/src/manual_let_else.rs +++ b/clippy_lints/src/manual_let_else.rs @@ -1,16 +1,16 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::higher::IfLetOrMatch; +use clippy_utils::msrvs::{self, Msrv}; +use clippy_utils::peel_blocks; use clippy_utils::source::snippet_opt; use clippy_utils::ty::is_type_diagnostic_item; use clippy_utils::visitors::{for_each_expr, Descend}; -use clippy_utils::{meets_msrv, msrvs, peel_blocks}; use if_chain::if_chain; use rustc_data_structures::fx::FxHashSet; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, MatchSource, Pat, PatKind, QPath, Stmt, StmtKind}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::symbol::sym; use rustc_span::Span; @@ -50,13 +50,13 @@ declare_clippy_lint! { } pub struct ManualLetElse { - msrv: Option, + msrv: Msrv, matches_behaviour: MatchLintBehaviour, } impl ManualLetElse { #[must_use] - pub fn new(msrv: Option, matches_behaviour: MatchLintBehaviour) -> Self { + pub fn new(msrv: Msrv, matches_behaviour: MatchLintBehaviour) -> Self { Self { msrv, matches_behaviour, @@ -69,7 +69,7 @@ impl_lint_pass!(ManualLetElse => [MANUAL_LET_ELSE]); impl<'tcx> LateLintPass<'tcx> for ManualLetElse { fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &'tcx Stmt<'tcx>) { let if_let_or_match = if_chain! { - if meets_msrv(self.msrv, msrvs::LET_ELSE); + if self.msrv.meets(msrvs::LET_ELSE); if !in_external_macro(cx.sess(), stmt.span); if let StmtKind::Local(local) = stmt.kind; if let Some(init) = local.init; diff --git a/clippy_lints/src/manual_non_exhaustive.rs b/clippy_lints/src/manual_non_exhaustive.rs index 6a42275322b43..d32fa1121f4e7 100644 --- a/clippy_lints/src/manual_non_exhaustive.rs +++ b/clippy_lints/src/manual_non_exhaustive.rs @@ -1,6 +1,7 @@ use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then}; +use clippy_utils::is_doc_hidden; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_opt; -use clippy_utils::{is_doc_hidden, meets_msrv, msrvs}; use rustc_ast::ast::{self, VisibilityKind}; use rustc_data_structures::fx::FxHashSet; use rustc_errors::Applicability; @@ -8,7 +9,6 @@ use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res}; use rustc_hir::{self as hir, Expr, ExprKind, QPath}; use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext}; use rustc_middle::ty::DefIdTree; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::def_id::{DefId, LocalDefId}; use rustc_span::{sym, Span}; @@ -63,12 +63,12 @@ declare_clippy_lint! { #[expect(clippy::module_name_repetitions)] pub struct ManualNonExhaustiveStruct { - msrv: Option, + msrv: Msrv, } impl ManualNonExhaustiveStruct { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -77,14 +77,14 @@ impl_lint_pass!(ManualNonExhaustiveStruct => [MANUAL_NON_EXHAUSTIVE]); #[expect(clippy::module_name_repetitions)] pub struct ManualNonExhaustiveEnum { - msrv: Option, + msrv: Msrv, constructed_enum_variants: FxHashSet<(DefId, DefId)>, potential_enums: Vec<(LocalDefId, LocalDefId, Span, Span)>, } impl ManualNonExhaustiveEnum { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv, constructed_enum_variants: FxHashSet::default(), @@ -97,7 +97,7 @@ impl_lint_pass!(ManualNonExhaustiveEnum => [MANUAL_NON_EXHAUSTIVE]); impl EarlyLintPass for ManualNonExhaustiveStruct { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) { - if !meets_msrv(self.msrv, msrvs::NON_EXHAUSTIVE) { + if !self.msrv.meets(msrvs::NON_EXHAUSTIVE) { return; } @@ -149,7 +149,7 @@ impl EarlyLintPass for ManualNonExhaustiveStruct { impl<'tcx> LateLintPass<'tcx> for ManualNonExhaustiveEnum { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) { - if !meets_msrv(self.msrv, msrvs::NON_EXHAUSTIVE) { + if !self.msrv.meets(msrvs::NON_EXHAUSTIVE) { return; } diff --git a/clippy_lints/src/manual_rem_euclid.rs b/clippy_lints/src/manual_rem_euclid.rs index 6f25a2ed8e434..8d447c37150b8 100644 --- a/clippy_lints/src/manual_rem_euclid.rs +++ b/clippy_lints/src/manual_rem_euclid.rs @@ -1,12 +1,12 @@ use clippy_utils::consts::{constant_full_int, FullInt}; use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_with_applicability; -use clippy_utils::{in_constant, meets_msrv, msrvs, path_to_local}; +use clippy_utils::{in_constant, path_to_local}; use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind, Node, TyKind}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; declare_clippy_lint! { @@ -34,12 +34,12 @@ declare_clippy_lint! { } pub struct ManualRemEuclid { - msrv: Option, + msrv: Msrv, } impl ManualRemEuclid { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -48,11 +48,11 @@ impl_lint_pass!(ManualRemEuclid => [MANUAL_REM_EUCLID]); impl<'tcx> LateLintPass<'tcx> for ManualRemEuclid { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - if !meets_msrv(self.msrv, msrvs::REM_EUCLID) { + if !self.msrv.meets(msrvs::REM_EUCLID) { return; } - if in_constant(cx, expr.hir_id) && !meets_msrv(self.msrv, msrvs::REM_EUCLID_CONST) { + if in_constant(cx, expr.hir_id) && !self.msrv.meets(msrvs::REM_EUCLID_CONST) { return; } diff --git a/clippy_lints/src/manual_retain.rs b/clippy_lints/src/manual_retain.rs index 3181bc86d1793..4907f02b5a3ef 100644 --- a/clippy_lints/src/manual_retain.rs +++ b/clippy_lints/src/manual_retain.rs @@ -1,8 +1,8 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet; use clippy_utils::ty::is_type_diagnostic_item; use clippy_utils::{get_parent_expr, match_def_path, paths, SpanlessEq}; -use clippy_utils::{meets_msrv, msrvs}; use rustc_errors::Applicability; use rustc_hir as hir; use rustc_hir::def_id::DefId; @@ -50,12 +50,12 @@ declare_clippy_lint! { } pub struct ManualRetain { - msrv: Option, + msrv: Msrv, } impl ManualRetain { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -71,9 +71,9 @@ impl<'tcx> LateLintPass<'tcx> for ManualRetain { && let hir::ExprKind::MethodCall(_, target_expr, [], _) = &collect_expr.kind && let Some(collect_def_id) = cx.typeck_results().type_dependent_def_id(collect_expr.hir_id) && match_def_path(cx, collect_def_id, &paths::CORE_ITER_COLLECT) { - check_into_iter(cx, parent_expr, left_expr, target_expr, self.msrv); - check_iter(cx, parent_expr, left_expr, target_expr, self.msrv); - check_to_owned(cx, parent_expr, left_expr, target_expr, self.msrv); + check_into_iter(cx, parent_expr, left_expr, target_expr, &self.msrv); + check_iter(cx, parent_expr, left_expr, target_expr, &self.msrv); + check_to_owned(cx, parent_expr, left_expr, target_expr, &self.msrv); } } @@ -85,7 +85,7 @@ fn check_into_iter( parent_expr: &hir::Expr<'_>, left_expr: &hir::Expr<'_>, target_expr: &hir::Expr<'_>, - msrv: Option, + msrv: &Msrv, ) { if let hir::ExprKind::MethodCall(_, into_iter_expr, [_], _) = &target_expr.kind && let Some(filter_def_id) = cx.typeck_results().type_dependent_def_id(target_expr.hir_id) @@ -104,7 +104,7 @@ fn check_iter( parent_expr: &hir::Expr<'_>, left_expr: &hir::Expr<'_>, target_expr: &hir::Expr<'_>, - msrv: Option, + msrv: &Msrv, ) { if let hir::ExprKind::MethodCall(_, filter_expr, [], _) = &target_expr.kind && let Some(copied_def_id) = cx.typeck_results().type_dependent_def_id(target_expr.hir_id) @@ -127,9 +127,9 @@ fn check_to_owned( parent_expr: &hir::Expr<'_>, left_expr: &hir::Expr<'_>, target_expr: &hir::Expr<'_>, - msrv: Option, + msrv: &Msrv, ) { - if meets_msrv(msrv, msrvs::STRING_RETAIN) + if msrv.meets(msrvs::STRING_RETAIN) && let hir::ExprKind::MethodCall(_, filter_expr, [], _) = &target_expr.kind && let Some(to_owned_def_id) = cx.typeck_results().type_dependent_def_id(target_expr.hir_id) && match_def_path(cx, to_owned_def_id, &paths::TO_OWNED_METHOD) @@ -215,10 +215,10 @@ fn match_acceptable_def_path(cx: &LateContext<'_>, collect_def_id: DefId) -> boo .any(|&method| match_def_path(cx, collect_def_id, method)) } -fn match_acceptable_type(cx: &LateContext<'_>, expr: &hir::Expr<'_>, msrv: Option) -> bool { +fn match_acceptable_type(cx: &LateContext<'_>, expr: &hir::Expr<'_>, msrv: &Msrv) -> bool { let expr_ty = cx.typeck_results().expr_ty(expr).peel_refs(); ACCEPTABLE_TYPES.iter().any(|(ty, acceptable_msrv)| { is_type_diagnostic_item(cx, expr_ty, *ty) - && acceptable_msrv.map_or(true, |acceptable_msrv| meets_msrv(msrv, acceptable_msrv)) + && acceptable_msrv.map_or(true, |acceptable_msrv| msrv.meets(acceptable_msrv)) }) } diff --git a/clippy_lints/src/manual_strip.rs b/clippy_lints/src/manual_strip.rs index 0976940afac35..de166b9765f41 100644 --- a/clippy_lints/src/manual_strip.rs +++ b/clippy_lints/src/manual_strip.rs @@ -1,8 +1,9 @@ use clippy_utils::consts::{constant, Constant}; use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then}; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet; use clippy_utils::usage::mutated_variables; -use clippy_utils::{eq_expr_value, higher, match_def_path, meets_msrv, msrvs, paths}; +use clippy_utils::{eq_expr_value, higher, match_def_path, paths}; use if_chain::if_chain; use rustc_ast::ast::LitKind; use rustc_hir::def::Res; @@ -11,7 +12,6 @@ use rustc_hir::BinOpKind; use rustc_hir::{BorrowKind, Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::source_map::Spanned; use rustc_span::Span; @@ -48,12 +48,12 @@ declare_clippy_lint! { } pub struct ManualStrip { - msrv: Option, + msrv: Msrv, } impl ManualStrip { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -68,7 +68,7 @@ enum StripKind { impl<'tcx> LateLintPass<'tcx> for ManualStrip { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - if !meets_msrv(self.msrv, msrvs::STR_STRIP_PREFIX) { + if !self.msrv.meets(msrvs::STR_STRIP_PREFIX) { return; } diff --git a/clippy_lints/src/matches/mod.rs b/clippy_lints/src/matches/mod.rs index 7d8171ead89e1..7b15a307fecf4 100644 --- a/clippy_lints/src/matches/mod.rs +++ b/clippy_lints/src/matches/mod.rs @@ -23,13 +23,13 @@ mod single_match; mod try_err; mod wild_in_or_pats; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::{snippet_opt, walk_span_to_context}; -use clippy_utils::{higher, in_constant, is_span_match, meets_msrv, msrvs}; +use clippy_utils::{higher, in_constant, is_span_match}; use rustc_hir::{Arm, Expr, ExprKind, Local, MatchSource, Pat}; use rustc_lexer::{tokenize, TokenKind}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::{Span, SpanData, SyntaxContext}; @@ -930,13 +930,13 @@ declare_clippy_lint! { #[derive(Default)] pub struct Matches { - msrv: Option, + msrv: Msrv, infallible_destructuring_match_linted: bool, } impl Matches { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv, ..Matches::default() @@ -1000,9 +1000,7 @@ impl<'tcx> LateLintPass<'tcx> for Matches { if !from_expansion && !contains_cfg_arm(cx, expr, ex, arms) { if source == MatchSource::Normal { - if !(meets_msrv(self.msrv, msrvs::MATCHES_MACRO) - && match_like_matches::check_match(cx, expr, ex, arms)) - { + if !(self.msrv.meets(msrvs::MATCHES_MACRO) && match_like_matches::check_match(cx, expr, ex, arms)) { match_same_arms::check(cx, arms); } @@ -1034,7 +1032,7 @@ impl<'tcx> LateLintPass<'tcx> for Matches { collapsible_match::check_if_let(cx, if_let.let_pat, if_let.if_then, if_let.if_else); if !from_expansion { if let Some(else_expr) = if_let.if_else { - if meets_msrv(self.msrv, msrvs::MATCHES_MACRO) { + if self.msrv.meets(msrvs::MATCHES_MACRO) { match_like_matches::check_if_let( cx, expr, diff --git a/clippy_lints/src/mem_replace.rs b/clippy_lints/src/mem_replace.rs index 0c4d9f100f7a9..35024ec1224f0 100644 --- a/clippy_lints/src/mem_replace.rs +++ b/clippy_lints/src/mem_replace.rs @@ -1,14 +1,14 @@ use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg, span_lint_and_then}; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::{snippet, snippet_with_applicability}; use clippy_utils::ty::is_non_aggregate_primitive_type; -use clippy_utils::{is_default_equivalent, is_res_lang_ctor, meets_msrv, msrvs, path_res}; +use clippy_utils::{is_default_equivalent, is_res_lang_ctor, path_res}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::LangItem::OptionNone; use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, QPath}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::lint::in_external_macro; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::source_map::Span; use rustc_span::symbol::sym; @@ -227,12 +227,12 @@ fn check_replace_with_default(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr< } pub struct MemReplace { - msrv: Option, + msrv: Msrv, } impl MemReplace { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -248,7 +248,7 @@ impl<'tcx> LateLintPass<'tcx> for MemReplace { then { check_replace_option_with_none(cx, src, dest, expr.span); check_replace_with_uninit(cx, src, dest, expr.span); - if meets_msrv(self.msrv, msrvs::MEM_TAKE) { + if self.msrv.meets(msrvs::MEM_TAKE) { check_replace_with_default(cx, src, dest, expr.span); } } diff --git a/clippy_lints/src/methods/cloned_instead_of_copied.rs b/clippy_lints/src/methods/cloned_instead_of_copied.rs index e9aeab2d5b62e..4e6ec61f6a83d 100644 --- a/clippy_lints/src/methods/cloned_instead_of_copied.rs +++ b/clippy_lints/src/methods/cloned_instead_of_copied.rs @@ -1,25 +1,25 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::is_trait_method; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::ty::{get_iterator_item_ty, is_copy}; -use clippy_utils::{is_trait_method, meets_msrv, msrvs}; use rustc_errors::Applicability; use rustc_hir::Expr; use rustc_lint::LateContext; use rustc_middle::ty; -use rustc_semver::RustcVersion; use rustc_span::{sym, Span}; use super::CLONED_INSTEAD_OF_COPIED; -pub fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, span: Span, msrv: Option) { +pub fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, span: Span, msrv: &Msrv) { let recv_ty = cx.typeck_results().expr_ty_adjusted(recv); let inner_ty = match recv_ty.kind() { // `Option` -> `T` ty::Adt(adt, subst) - if cx.tcx.is_diagnostic_item(sym::Option, adt.did()) && meets_msrv(msrv, msrvs::OPTION_COPIED) => + if cx.tcx.is_diagnostic_item(sym::Option, adt.did()) && msrv.meets(msrvs::OPTION_COPIED) => { subst.type_at(0) }, - _ if is_trait_method(cx, expr, sym::Iterator) && meets_msrv(msrv, msrvs::ITERATOR_COPIED) => { + _ if is_trait_method(cx, expr, sym::Iterator) && msrv.meets(msrvs::ITERATOR_COPIED) => { match get_iterator_item_ty(cx, recv_ty) { // ::Item Some(ty) => ty, diff --git a/clippy_lints/src/methods/err_expect.rs b/clippy_lints/src/methods/err_expect.rs index 720d9a68c85ee..ae03da0d3f9ce 100644 --- a/clippy_lints/src/methods/err_expect.rs +++ b/clippy_lints/src/methods/err_expect.rs @@ -1,27 +1,27 @@ use super::ERR_EXPECT; use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::ty::has_debug_impl; -use clippy_utils::{meets_msrv, msrvs, ty::is_type_diagnostic_item}; +use clippy_utils::ty::is_type_diagnostic_item; use rustc_errors::Applicability; use rustc_lint::LateContext; use rustc_middle::ty; use rustc_middle::ty::Ty; -use rustc_semver::RustcVersion; use rustc_span::{sym, Span}; pub(super) fn check( cx: &LateContext<'_>, _expr: &rustc_hir::Expr<'_>, recv: &rustc_hir::Expr<'_>, - msrv: Option, expect_span: Span, err_span: Span, + msrv: &Msrv, ) { if_chain! { if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result); // Test the version to make sure the lint can be showed (expect_err has been // introduced in rust 1.17.0 : https://github.com/rust-lang/rust/pull/38982) - if meets_msrv(msrv, msrvs::EXPECT_ERR); + if msrv.meets(msrvs::EXPECT_ERR); // Grabs the `Result` type let result_type = cx.typeck_results().expr_ty(recv); diff --git a/clippy_lints/src/methods/filter_map_next.rs b/clippy_lints/src/methods/filter_map_next.rs index ddf8a1f09b87d..175e04f8ac061 100644 --- a/clippy_lints/src/methods/filter_map_next.rs +++ b/clippy_lints/src/methods/filter_map_next.rs @@ -1,10 +1,10 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg}; +use clippy_utils::is_trait_method; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet; -use clippy_utils::{is_trait_method, meets_msrv, msrvs}; use rustc_errors::Applicability; use rustc_hir as hir; use rustc_lint::LateContext; -use rustc_semver::RustcVersion; use rustc_span::sym; use super::FILTER_MAP_NEXT; @@ -14,10 +14,10 @@ pub(super) fn check<'tcx>( expr: &'tcx hir::Expr<'_>, recv: &'tcx hir::Expr<'_>, arg: &'tcx hir::Expr<'_>, - msrv: Option, + msrv: &Msrv, ) { if is_trait_method(cx, expr, sym::Iterator) { - if !meets_msrv(msrv, msrvs::ITERATOR_FIND_MAP) { + if !msrv.meets(msrvs::ITERATOR_FIND_MAP) { return; } diff --git a/clippy_lints/src/methods/is_digit_ascii_radix.rs b/clippy_lints/src/methods/is_digit_ascii_radix.rs index 304024e80666f..301aff5ae6ac0 100644 --- a/clippy_lints/src/methods/is_digit_ascii_radix.rs +++ b/clippy_lints/src/methods/is_digit_ascii_radix.rs @@ -1,23 +1,22 @@ //! Lint for `c.is_digit(10)` use super::IS_DIGIT_ASCII_RADIX; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::{ - consts::constant_full_int, consts::FullInt, diagnostics::span_lint_and_sugg, meets_msrv, msrvs, - source::snippet_with_applicability, + consts::constant_full_int, consts::FullInt, diagnostics::span_lint_and_sugg, source::snippet_with_applicability, }; use rustc_errors::Applicability; use rustc_hir::Expr; use rustc_lint::LateContext; -use rustc_semver::RustcVersion; pub(super) fn check<'tcx>( cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, self_arg: &'tcx Expr<'_>, radix: &'tcx Expr<'_>, - msrv: Option, + msrv: &Msrv, ) { - if !meets_msrv(msrv, msrvs::IS_ASCII_DIGIT) { + if !msrv.meets(msrvs::IS_ASCII_DIGIT) { return; } diff --git a/clippy_lints/src/methods/map_clone.rs b/clippy_lints/src/methods/map_clone.rs index 6bc783c6d505a..52cc1e0464bf3 100644 --- a/clippy_lints/src/methods/map_clone.rs +++ b/clippy_lints/src/methods/map_clone.rs @@ -1,7 +1,8 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_with_applicability; use clippy_utils::ty::{is_copy, is_type_diagnostic_item}; -use clippy_utils::{is_diag_trait_item, meets_msrv, msrvs, peel_blocks}; +use clippy_utils::{is_diag_trait_item, peel_blocks}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir as hir; @@ -9,19 +10,12 @@ use rustc_lint::LateContext; use rustc_middle::mir::Mutability; use rustc_middle::ty; use rustc_middle::ty::adjustment::Adjust; -use rustc_semver::RustcVersion; use rustc_span::symbol::Ident; use rustc_span::{sym, Span}; use super::MAP_CLONE; -pub(super) fn check( - cx: &LateContext<'_>, - e: &hir::Expr<'_>, - recv: &hir::Expr<'_>, - arg: &hir::Expr<'_>, - msrv: Option, -) { +pub(super) fn check(cx: &LateContext<'_>, e: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>, msrv: &Msrv) { if_chain! { if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id); if cx.tcx.impl_of_method(method_id) @@ -97,10 +91,10 @@ fn lint_needless_cloning(cx: &LateContext<'_>, root: Span, receiver: Span) { ); } -fn lint_explicit_closure(cx: &LateContext<'_>, replace: Span, root: Span, is_copy: bool, msrv: Option) { +fn lint_explicit_closure(cx: &LateContext<'_>, replace: Span, root: Span, is_copy: bool, msrv: &Msrv) { let mut applicability = Applicability::MachineApplicable; - let (message, sugg_method) = if is_copy && meets_msrv(msrv, msrvs::ITERATOR_COPIED) { + let (message, sugg_method) = if is_copy && msrv.meets(msrvs::ITERATOR_COPIED) { ("you are using an explicit closure for copying elements", "copied") } else { ("you are using an explicit closure for cloning elements", "cloned") diff --git a/clippy_lints/src/methods/map_unwrap_or.rs b/clippy_lints/src/methods/map_unwrap_or.rs index 74fdead216b0a..3122f72ee9155 100644 --- a/clippy_lints/src/methods/map_unwrap_or.rs +++ b/clippy_lints/src/methods/map_unwrap_or.rs @@ -1,12 +1,11 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg}; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet; use clippy_utils::ty::is_type_diagnostic_item; use clippy_utils::usage::mutated_variables; -use clippy_utils::{meets_msrv, msrvs}; use rustc_errors::Applicability; use rustc_hir as hir; use rustc_lint::LateContext; -use rustc_semver::RustcVersion; use rustc_span::symbol::sym; use super::MAP_UNWRAP_OR; @@ -19,13 +18,13 @@ pub(super) fn check<'tcx>( recv: &'tcx hir::Expr<'_>, map_arg: &'tcx hir::Expr<'_>, unwrap_arg: &'tcx hir::Expr<'_>, - msrv: Option, + msrv: &Msrv, ) -> bool { // lint if the caller of `map()` is an `Option` let is_option = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Option); let is_result = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result); - if is_result && !meets_msrv(msrv, msrvs::RESULT_MAP_OR_ELSE) { + if is_result && !msrv.meets(msrvs::RESULT_MAP_OR_ELSE) { return false; } diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index acb3b20a61b5b..c5451195727cf 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -104,8 +104,9 @@ mod zst_offset; use bind_instead_of_map::BindInsteadOfMap; use clippy_utils::consts::{constant, Constant}; use clippy_utils::diagnostics::{span_lint, span_lint_and_help}; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::ty::{contains_ty_adt_constructor_opaque, implements_trait, is_copy, is_type_diagnostic_item}; -use clippy_utils::{contains_return, is_bool, is_trait_method, iter_input_pats, meets_msrv, msrvs, return_ty}; +use clippy_utils::{contains_return, is_bool, is_trait_method, iter_input_pats, return_ty}; use if_chain::if_chain; use rustc_hir as hir; use rustc_hir::{Expr, ExprKind, TraitItem, TraitItemKind}; @@ -113,7 +114,6 @@ use rustc_hir_analysis::hir_ty_to_ty; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; use rustc_middle::ty::{self, TraitRef, Ty}; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::{sym, Span}; @@ -3163,7 +3163,7 @@ declare_clippy_lint! { pub struct Methods { avoid_breaking_exported_api: bool, - msrv: Option, + msrv: Msrv, allow_expect_in_tests: bool, allow_unwrap_in_tests: bool, } @@ -3172,7 +3172,7 @@ impl Methods { #[must_use] pub fn new( avoid_breaking_exported_api: bool, - msrv: Option, + msrv: Msrv, allow_expect_in_tests: bool, allow_unwrap_in_tests: bool, ) -> Self { @@ -3325,7 +3325,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods { single_char_add_str::check(cx, expr, receiver, args); into_iter_on_ref::check(cx, expr, method_span, method_call.ident.name, receiver); single_char_pattern::check(cx, expr, method_call.ident.name, receiver, args); - unnecessary_to_owned::check(cx, expr, method_call.ident.name, receiver, args, self.msrv); + unnecessary_to_owned::check(cx, expr, method_call.ident.name, receiver, args, &self.msrv); }, hir::ExprKind::Binary(op, lhs, rhs) if op.node == hir::BinOpKind::Eq || op.node == hir::BinOpKind::Ne => { let mut info = BinaryExprInfo { @@ -3501,7 +3501,7 @@ impl Methods { ("as_mut", []) => useless_asref::check(cx, expr, "as_mut", recv), ("as_ref", []) => useless_asref::check(cx, expr, "as_ref", recv), ("assume_init", []) => uninit_assumed_init::check(cx, expr, recv), - ("cloned", []) => cloned_instead_of_copied::check(cx, expr, recv, span, self.msrv), + ("cloned", []) => cloned_instead_of_copied::check(cx, expr, recv, span, &self.msrv), ("collect", []) if is_trait_method(cx, expr, sym::Iterator) => { needless_collect::check(cx, span, expr, recv, call_span); match method_call(recv) { @@ -3512,7 +3512,7 @@ impl Methods { map_collect_result_unit::check(cx, expr, m_recv, m_arg); }, Some(("take", take_self_arg, [take_arg], _, _)) => { - if meets_msrv(self.msrv, msrvs::STR_REPEAT) { + if self.msrv.meets(msrvs::STR_REPEAT) { manual_str_repeat::check(cx, expr, recv, take_self_arg, take_arg); } }, @@ -3539,7 +3539,7 @@ impl Methods { }, ("expect", [_]) => match method_call(recv) { Some(("ok", recv, [], _, _)) => ok_expect::check(cx, expr, recv), - Some(("err", recv, [], err_span, _)) => err_expect::check(cx, expr, recv, self.msrv, span, err_span), + Some(("err", recv, [], err_span, _)) => err_expect::check(cx, expr, recv, span, err_span, &self.msrv), _ => expect_used::check(cx, expr, recv, false, self.allow_expect_in_tests), }, ("expect_err", [_]) => expect_used::check(cx, expr, recv, true, self.allow_expect_in_tests), @@ -3578,7 +3578,7 @@ impl Methods { unit_hash::check(cx, expr, recv, arg); }, ("is_file", []) => filetype_is_file::check(cx, expr, recv), - ("is_digit", [radix]) => is_digit_ascii_radix::check(cx, expr, recv, radix, self.msrv), + ("is_digit", [radix]) => is_digit_ascii_radix::check(cx, expr, recv, radix, &self.msrv), ("is_none", []) => check_is_some_is_none(cx, expr, recv, false), ("is_some", []) => check_is_some_is_none(cx, expr, recv, true), ("iter" | "iter_mut" | "into_iter", []) => { @@ -3601,7 +3601,7 @@ impl Methods { }, (name @ ("map" | "map_err"), [m_arg]) => { if name == "map" { - map_clone::check(cx, expr, recv, m_arg, self.msrv); + map_clone::check(cx, expr, recv, m_arg, &self.msrv); if let Some((map_name @ ("iter" | "into_iter"), recv2, _, _, _)) = method_call(recv) { iter_kv_map::check(cx, map_name, expr, recv2, m_arg); } @@ -3610,8 +3610,8 @@ impl Methods { } if let Some((name, recv2, args, span2,_)) = method_call(recv) { match (name, args) { - ("as_mut", []) => option_as_ref_deref::check(cx, expr, recv2, m_arg, true, self.msrv), - ("as_ref", []) => option_as_ref_deref::check(cx, expr, recv2, m_arg, false, self.msrv), + ("as_mut", []) => option_as_ref_deref::check(cx, expr, recv2, m_arg, true, &self.msrv), + ("as_ref", []) => option_as_ref_deref::check(cx, expr, recv2, m_arg, false, &self.msrv), ("filter", [f_arg]) => { filter_map::check(cx, expr, recv2, f_arg, span2, recv, m_arg, span, false); }, @@ -3632,7 +3632,7 @@ impl Methods { match (name2, args2) { ("cloned", []) => iter_overeager_cloned::check(cx, expr, recv, recv2, false, false), ("filter", [arg]) => filter_next::check(cx, expr, recv2, arg), - ("filter_map", [arg]) => filter_map_next::check(cx, expr, recv2, arg, self.msrv), + ("filter_map", [arg]) => filter_map_next::check(cx, expr, recv2, arg, &self.msrv), ("iter", []) => iter_next_slice::check(cx, expr, recv2), ("skip", [arg]) => iter_skip_next::check(cx, expr, recv2, arg), ("skip_while", [_]) => skip_while_next::check(cx, expr), @@ -3680,10 +3680,10 @@ impl Methods { vec_resize_to_zero::check(cx, expr, count_arg, default_arg, span); }, ("seek", [arg]) => { - if meets_msrv(self.msrv, msrvs::SEEK_FROM_CURRENT) { + if self.msrv.meets(msrvs::SEEK_FROM_CURRENT) { seek_from_current::check(cx, expr, recv, arg); } - if meets_msrv(self.msrv, msrvs::SEEK_REWIND) { + if self.msrv.meets(msrvs::SEEK_REWIND) { seek_to_start_instead_of_rewind::check(cx, expr, recv, arg, span); } }, @@ -3699,7 +3699,7 @@ impl Methods { ("splitn" | "rsplitn", [count_arg, pat_arg]) => { if let Some((Constant::Int(count), _)) = constant(cx, cx.typeck_results(), count_arg) { suspicious_splitn::check(cx, name, expr, recv, count); - str_splitn::check(cx, name, expr, recv, pat_arg, count, self.msrv); + str_splitn::check(cx, name, expr, recv, pat_arg, count, &self.msrv); } }, ("splitn_mut" | "rsplitn_mut", [count_arg, _]) => { @@ -3717,7 +3717,7 @@ impl Methods { }, ("take", []) => needless_option_take::check(cx, expr, recv), ("then", [arg]) => { - if !meets_msrv(self.msrv, msrvs::BOOL_THEN_SOME) { + if !self.msrv.meets(msrvs::BOOL_THEN_SOME) { return; } unnecessary_lazy_eval::check(cx, expr, recv, arg, "then_some"); @@ -3760,7 +3760,7 @@ impl Methods { }, ("unwrap_or_else", [u_arg]) => match method_call(recv) { Some(("map", recv, [map_arg], _, _)) - if map_unwrap_or::check(cx, expr, recv, map_arg, u_arg, self.msrv) => {}, + if map_unwrap_or::check(cx, expr, recv, map_arg, u_arg, &self.msrv) => {}, _ => { unwrap_or_else_default::check(cx, expr, recv, u_arg); unnecessary_lazy_eval::check(cx, expr, recv, u_arg, "unwrap_or"); diff --git a/clippy_lints/src/methods/option_as_ref_deref.rs b/clippy_lints/src/methods/option_as_ref_deref.rs index e6eb64bcbde64..3e33f9193374e 100644 --- a/clippy_lints/src/methods/option_as_ref_deref.rs +++ b/clippy_lints/src/methods/option_as_ref_deref.rs @@ -1,13 +1,13 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet; use clippy_utils::ty::is_type_diagnostic_item; -use clippy_utils::{match_def_path, meets_msrv, msrvs, path_to_local_id, paths, peel_blocks}; +use clippy_utils::{match_def_path, path_to_local_id, paths, peel_blocks}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir as hir; use rustc_lint::LateContext; use rustc_middle::ty; -use rustc_semver::RustcVersion; use rustc_span::sym; use super::OPTION_AS_REF_DEREF; @@ -19,9 +19,9 @@ pub(super) fn check( as_ref_recv: &hir::Expr<'_>, map_arg: &hir::Expr<'_>, is_mut: bool, - msrv: Option, + msrv: &Msrv, ) { - if !meets_msrv(msrv, msrvs::OPTION_AS_DEREF) { + if !msrv.meets(msrvs::OPTION_AS_DEREF) { return; } diff --git a/clippy_lints/src/methods/str_splitn.rs b/clippy_lints/src/methods/str_splitn.rs index 1acac59144cee..3c01ce1fecd3a 100644 --- a/clippy_lints/src/methods/str_splitn.rs +++ b/clippy_lints/src/methods/str_splitn.rs @@ -1,9 +1,10 @@ use clippy_utils::consts::{constant, Constant}; use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then}; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_with_context; use clippy_utils::usage::local_used_after_expr; use clippy_utils::visitors::{for_each_expr_with_closures, Descend}; -use clippy_utils::{is_diag_item_method, match_def_path, meets_msrv, msrvs, path_to_local_id, paths}; +use clippy_utils::{is_diag_item_method, match_def_path, path_to_local_id, paths}; use core::ops::ControlFlow; use if_chain::if_chain; use rustc_errors::Applicability; @@ -12,7 +13,6 @@ use rustc_hir::{ }; use rustc_lint::LateContext; use rustc_middle::ty; -use rustc_semver::RustcVersion; use rustc_span::{sym, Span, Symbol, SyntaxContext}; use super::{MANUAL_SPLIT_ONCE, NEEDLESS_SPLITN}; @@ -24,7 +24,7 @@ pub(super) fn check( self_arg: &Expr<'_>, pat_arg: &Expr<'_>, count: u128, - msrv: Option, + msrv: &Msrv, ) { if count < 2 || !cx.typeck_results().expr_ty_adjusted(self_arg).peel_refs().is_str() { return; @@ -34,7 +34,7 @@ pub(super) fn check( IterUsageKind::Nth(n) => count > n + 1, IterUsageKind::NextTuple => count > 2, }; - let manual = count == 2 && meets_msrv(msrv, msrvs::STR_SPLIT_ONCE); + let manual = count == 2 && msrv.meets(msrvs::STR_SPLIT_ONCE); match parse_iter_usage(cx, expr.span.ctxt(), cx.tcx.hir().parent_iter(expr.hir_id)) { Some(usage) if needless(usage.kind) => lint_needless(cx, method_name, expr, self_arg, pat_arg), diff --git a/clippy_lints/src/methods/unnecessary_to_owned.rs b/clippy_lints/src/methods/unnecessary_to_owned.rs index 4b4f2f47b1d92..21d1df4d0c212 100644 --- a/clippy_lints/src/methods/unnecessary_to_owned.rs +++ b/clippy_lints/src/methods/unnecessary_to_owned.rs @@ -1,11 +1,11 @@ use super::implicit_clone::is_clone_like; use super::unnecessary_iter_cloned::{self, is_into_iter}; use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_opt; use clippy_utils::ty::{get_associated_type, get_iterator_item_ty, implements_trait, is_copy, peel_mid_ty_refs}; use clippy_utils::visitors::find_all_ret_expressions; use clippy_utils::{fn_def_id, get_parent_expr, is_diag_item_method, is_diag_trait_item, return_ty}; -use clippy_utils::{meets_msrv, msrvs}; use rustc_errors::Applicability; use rustc_hir::{def_id::DefId, BorrowKind, Expr, ExprKind, ItemKind, LangItem, Node}; use rustc_hir_analysis::check::{FnCtxt, Inherited}; @@ -16,7 +16,6 @@ use rustc_middle::ty::adjustment::{Adjust, Adjustment, OverloadedDeref}; use rustc_middle::ty::subst::{GenericArg, GenericArgKind, SubstsRef}; use rustc_middle::ty::EarlyBinder; use rustc_middle::ty::{self, ParamTy, PredicateKind, ProjectionPredicate, TraitPredicate, Ty}; -use rustc_semver::RustcVersion; use rustc_span::{sym, Symbol}; use rustc_trait_selection::traits::{query::evaluate_obligation::InferCtxtExt as _, Obligation, ObligationCause}; use std::cmp::max; @@ -29,7 +28,7 @@ pub fn check<'tcx>( method_name: Symbol, receiver: &'tcx Expr<'_>, args: &'tcx [Expr<'_>], - msrv: Option, + msrv: &Msrv, ) { if_chain! { if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id); @@ -200,7 +199,7 @@ fn check_into_iter_call_arg( expr: &Expr<'_>, method_name: Symbol, receiver: &Expr<'_>, - msrv: Option, + msrv: &Msrv, ) -> bool { if_chain! { if let Some(parent) = get_parent_expr(cx, expr); @@ -215,7 +214,7 @@ fn check_into_iter_call_arg( if unnecessary_iter_cloned::check_for_loop_iter(cx, parent, method_name, receiver, true) { return true; } - let cloned_or_copied = if is_copy(cx, item_ty) && meets_msrv(msrv, msrvs::ITERATOR_COPIED) { + let cloned_or_copied = if is_copy(cx, item_ty) && msrv.meets(msrvs::ITERATOR_COPIED) { "copied" } else { "cloned" diff --git a/clippy_lints/src/missing_const_for_fn.rs b/clippy_lints/src/missing_const_for_fn.rs index 71cc0d0a81cd2..5bc04bc17fb4f 100644 --- a/clippy_lints/src/missing_const_for_fn.rs +++ b/clippy_lints/src/missing_const_for_fn.rs @@ -1,9 +1,8 @@ use clippy_utils::diagnostics::span_lint; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::qualify_min_const_fn::is_min_const_fn; use clippy_utils::ty::has_drop; -use clippy_utils::{ - fn_has_unsatisfiable_preds, is_entrypoint_fn, is_from_proc_macro, meets_msrv, msrvs, trait_ref_of_method, -}; +use clippy_utils::{fn_has_unsatisfiable_preds, is_entrypoint_fn, is_from_proc_macro, trait_ref_of_method}; use rustc_hir as hir; use rustc_hir::def_id::CRATE_DEF_ID; use rustc_hir::intravisit::FnKind; @@ -11,7 +10,6 @@ use rustc_hir::{Body, Constness, FnDecl, GenericParamKind, HirId}; use rustc_hir_analysis::hir_ty_to_ty; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::lint::in_external_macro; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::Span; @@ -75,12 +73,12 @@ declare_clippy_lint! { impl_lint_pass!(MissingConstForFn => [MISSING_CONST_FOR_FN]); pub struct MissingConstForFn { - msrv: Option, + msrv: Msrv, } impl MissingConstForFn { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -95,7 +93,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn { span: Span, hir_id: HirId, ) { - if !meets_msrv(self.msrv, msrvs::CONST_IF_MATCH) { + if !self.msrv.meets(msrvs::CONST_IF_MATCH) { return; } @@ -152,7 +150,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn { let mir = cx.tcx.optimized_mir(def_id); - if let Err((span, err)) = is_min_const_fn(cx.tcx, mir, self.msrv) { + if let Err((span, err)) = is_min_const_fn(cx.tcx, mir, &self.msrv) { if cx.tcx.is_const_fn_raw(def_id.to_def_id()) { cx.tcx.sess.span_err(span, err.as_ref()); } diff --git a/clippy_lints/src/ranges.rs b/clippy_lints/src/ranges.rs index c6fbb5e805ab2..0a1b9d173cf94 100644 --- a/clippy_lints/src/ranges.rs +++ b/clippy_lints/src/ranges.rs @@ -1,16 +1,16 @@ use clippy_utils::consts::{constant, Constant}; use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then}; use clippy_utils::higher; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::{snippet, snippet_opt, snippet_with_applicability}; use clippy_utils::sugg::Sugg; -use clippy_utils::{get_parent_expr, in_constant, is_integer_const, meets_msrv, msrvs, path_to_local}; +use clippy_utils::{get_parent_expr, in_constant, is_integer_const, path_to_local}; use if_chain::if_chain; use rustc_ast::ast::RangeLimits; use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind, HirId}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::source_map::{Span, Spanned}; use std::cmp::Ordering; @@ -161,12 +161,12 @@ declare_clippy_lint! { } pub struct Ranges { - msrv: Option, + msrv: Msrv, } impl Ranges { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -181,7 +181,7 @@ impl_lint_pass!(Ranges => [ impl<'tcx> LateLintPass<'tcx> for Ranges { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { if let ExprKind::Binary(ref op, l, r) = expr.kind { - if meets_msrv(self.msrv, msrvs::RANGE_CONTAINS) { + if self.msrv.meets(msrvs::RANGE_CONTAINS) { check_possible_range_contains(cx, op.node, l, r, expr, expr.span); } } diff --git a/clippy_lints/src/redundant_field_names.rs b/clippy_lints/src/redundant_field_names.rs index 40b03068f6c77..61bff4a0e38d8 100644 --- a/clippy_lints/src/redundant_field_names.rs +++ b/clippy_lints/src/redundant_field_names.rs @@ -1,10 +1,9 @@ use clippy_utils::diagnostics::span_lint_and_sugg; -use clippy_utils::{meets_msrv, msrvs}; +use clippy_utils::msrvs::{self, Msrv}; use rustc_ast::ast::{Expr, ExprKind}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; use rustc_middle::lint::in_external_macro; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; declare_clippy_lint! { @@ -37,12 +36,12 @@ declare_clippy_lint! { } pub struct RedundantFieldNames { - msrv: Option, + msrv: Msrv, } impl RedundantFieldNames { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -51,7 +50,7 @@ impl_lint_pass!(RedundantFieldNames => [REDUNDANT_FIELD_NAMES]); impl EarlyLintPass for RedundantFieldNames { fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { - if !meets_msrv(self.msrv, msrvs::FIELD_INIT_SHORTHAND) { + if !self.msrv.meets(msrvs::FIELD_INIT_SHORTHAND) { return; } diff --git a/clippy_lints/src/redundant_static_lifetimes.rs b/clippy_lints/src/redundant_static_lifetimes.rs index 60ba62c4a4332..3aa2490bc44e0 100644 --- a/clippy_lints/src/redundant_static_lifetimes.rs +++ b/clippy_lints/src/redundant_static_lifetimes.rs @@ -1,10 +1,9 @@ use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet; -use clippy_utils::{meets_msrv, msrvs}; use rustc_ast::ast::{Item, ItemKind, Ty, TyKind}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass}; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; declare_clippy_lint! { @@ -34,12 +33,12 @@ declare_clippy_lint! { } pub struct RedundantStaticLifetimes { - msrv: Option, + msrv: Msrv, } impl RedundantStaticLifetimes { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -96,7 +95,7 @@ impl RedundantStaticLifetimes { impl EarlyLintPass for RedundantStaticLifetimes { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { - if !meets_msrv(self.msrv, msrvs::STATIC_IN_CONST) { + if !self.msrv.meets(msrvs::STATIC_IN_CONST) { return; } diff --git a/clippy_lints/src/transmute/mod.rs b/clippy_lints/src/transmute/mod.rs index 424a6e9264e4b..83e651aba8e89 100644 --- a/clippy_lints/src/transmute/mod.rs +++ b/clippy_lints/src/transmute/mod.rs @@ -16,10 +16,10 @@ mod utils; mod wrong_transmute; use clippy_utils::in_constant; +use clippy_utils::msrvs::Msrv; use if_chain::if_chain; use rustc_hir::{Expr, ExprKind, QPath}; use rustc_lint::{LateContext, LateLintPass}; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::symbol::sym; @@ -410,7 +410,7 @@ declare_clippy_lint! { } pub struct Transmute { - msrv: Option, + msrv: Msrv, } impl_lint_pass!(Transmute => [ CROSSPOINTER_TRANSMUTE, @@ -431,7 +431,7 @@ impl_lint_pass!(Transmute => [ ]); impl Transmute { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -461,7 +461,7 @@ impl<'tcx> LateLintPass<'tcx> for Transmute { let linted = wrong_transmute::check(cx, e, from_ty, to_ty) | crosspointer_transmute::check(cx, e, from_ty, to_ty) | transmuting_null::check(cx, e, arg, to_ty) - | transmute_ptr_to_ref::check(cx, e, from_ty, to_ty, arg, path, self.msrv) + | transmute_ptr_to_ref::check(cx, e, from_ty, to_ty, arg, path, &self.msrv) | transmute_int_to_char::check(cx, e, from_ty, to_ty, arg, const_context) | transmute_ref_to_ref::check(cx, e, from_ty, to_ty, arg, const_context) | transmute_ptr_to_ptr::check(cx, e, from_ty, to_ty, arg) diff --git a/clippy_lints/src/transmute/transmute_ptr_to_ref.rs b/clippy_lints/src/transmute/transmute_ptr_to_ref.rs index 12d0b866e1c9b..3dde4eee67179 100644 --- a/clippy_lints/src/transmute/transmute_ptr_to_ref.rs +++ b/clippy_lints/src/transmute/transmute_ptr_to_ref.rs @@ -1,12 +1,12 @@ use super::TRANSMUTE_PTR_TO_REF; use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_with_applicability; -use clippy_utils::{meets_msrv, msrvs, sugg}; +use clippy_utils::sugg; use rustc_errors::Applicability; use rustc_hir::{self as hir, Expr, GenericArg, Mutability, Path, TyKind}; use rustc_lint::LateContext; use rustc_middle::ty::{self, Ty, TypeVisitable}; -use rustc_semver::RustcVersion; /// Checks for `transmute_ptr_to_ref` lint. /// Returns `true` if it's triggered, otherwise returns `false`. @@ -17,7 +17,7 @@ pub(super) fn check<'tcx>( to_ty: Ty<'tcx>, arg: &'tcx Expr<'_>, path: &'tcx Path<'_>, - msrv: Option, + msrv: &Msrv, ) -> bool { match (&from_ty.kind(), &to_ty.kind()) { (ty::RawPtr(from_ptr_ty), ty::Ref(_, to_ref_ty, mutbl)) => { @@ -37,7 +37,7 @@ pub(super) fn check<'tcx>( let sugg = if let Some(ty) = get_explicit_type(path) { let ty_snip = snippet_with_applicability(cx, ty.span, "..", &mut app); - if meets_msrv(msrv, msrvs::POINTER_CAST) { + if msrv.meets(msrvs::POINTER_CAST) { format!("{deref}{}.cast::<{ty_snip}>()", arg.maybe_par()) } else if from_ptr_ty.has_erased_regions() { sugg::make_unop(deref, arg.as_ty(format!("{cast} () as {cast} {ty_snip}"))).to_string() @@ -46,7 +46,7 @@ pub(super) fn check<'tcx>( } } else if from_ptr_ty.ty == *to_ref_ty { if from_ptr_ty.has_erased_regions() { - if meets_msrv(msrv, msrvs::POINTER_CAST) { + if msrv.meets(msrvs::POINTER_CAST) { format!("{deref}{}.cast::<{to_ref_ty}>()", arg.maybe_par()) } else { sugg::make_unop(deref, arg.as_ty(format!("{cast} () as {cast} {to_ref_ty}"))) diff --git a/clippy_lints/src/unnested_or_patterns.rs b/clippy_lints/src/unnested_or_patterns.rs index b305dae76084c..2c2730267db64 100644 --- a/clippy_lints/src/unnested_or_patterns.rs +++ b/clippy_lints/src/unnested_or_patterns.rs @@ -2,14 +2,14 @@ use clippy_utils::ast_utils::{eq_field_pat, eq_id, eq_maybe_qself, eq_pat, eq_path}; use clippy_utils::diagnostics::span_lint_and_then; -use clippy_utils::{meets_msrv, msrvs, over}; +use clippy_utils::msrvs::{self, Msrv}; +use clippy_utils::over; use rustc_ast::mut_visit::*; use rustc_ast::ptr::P; use rustc_ast::{self as ast, Mutability, Pat, PatKind, PatKind::*, DUMMY_NODE_ID}; use rustc_ast_pretty::pprust; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass}; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::DUMMY_SP; @@ -45,14 +45,13 @@ declare_clippy_lint! { "unnested or-patterns, e.g., `Foo(Bar) | Foo(Baz) instead of `Foo(Bar | Baz)`" } -#[derive(Clone, Copy)] pub struct UnnestedOrPatterns { - msrv: Option, + msrv: Msrv, } impl UnnestedOrPatterns { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -61,13 +60,13 @@ impl_lint_pass!(UnnestedOrPatterns => [UNNESTED_OR_PATTERNS]); impl EarlyLintPass for UnnestedOrPatterns { fn check_arm(&mut self, cx: &EarlyContext<'_>, a: &ast::Arm) { - if meets_msrv(self.msrv, msrvs::OR_PATTERNS) { + if self.msrv.meets(msrvs::OR_PATTERNS) { lint_unnested_or_patterns(cx, &a.pat); } } fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) { - if meets_msrv(self.msrv, msrvs::OR_PATTERNS) { + if self.msrv.meets(msrvs::OR_PATTERNS) { if let ast::ExprKind::Let(pat, _, _) = &e.kind { lint_unnested_or_patterns(cx, pat); } @@ -75,13 +74,13 @@ impl EarlyLintPass for UnnestedOrPatterns { } fn check_param(&mut self, cx: &EarlyContext<'_>, p: &ast::Param) { - if meets_msrv(self.msrv, msrvs::OR_PATTERNS) { + if self.msrv.meets(msrvs::OR_PATTERNS) { lint_unnested_or_patterns(cx, &p.pat); } } fn check_local(&mut self, cx: &EarlyContext<'_>, l: &ast::Local) { - if meets_msrv(self.msrv, msrvs::OR_PATTERNS) { + if self.msrv.meets(msrvs::OR_PATTERNS) { lint_unnested_or_patterns(cx, &l.pat); } } diff --git a/clippy_lints/src/use_self.rs b/clippy_lints/src/use_self.rs index 464ffdc0a2ad9..06975443d8fa1 100644 --- a/clippy_lints/src/use_self.rs +++ b/clippy_lints/src/use_self.rs @@ -1,6 +1,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::is_from_proc_macro; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::ty::same_type_and_consts; -use clippy_utils::{is_from_proc_macro, meets_msrv, msrvs}; use if_chain::if_chain; use rustc_data_structures::fx::FxHashSet; use rustc_errors::Applicability; @@ -14,7 +15,6 @@ use rustc_hir::{ }; use rustc_hir_analysis::hir_ty_to_ty; use rustc_lint::{LateContext, LateLintPass}; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::Span; @@ -57,13 +57,13 @@ declare_clippy_lint! { #[derive(Default)] pub struct UseSelf { - msrv: Option, + msrv: Msrv, stack: Vec, } impl UseSelf { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv, ..Self::default() @@ -199,7 +199,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf { fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>) { if_chain! { if !hir_ty.span.from_expansion(); - if meets_msrv(self.msrv, msrvs::TYPE_ALIAS_ENUM_VARIANTS); + if self.msrv.meets(msrvs::TYPE_ALIAS_ENUM_VARIANTS); if let Some(&StackItem::Check { impl_id, in_body, @@ -228,7 +228,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf { fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { if_chain! { if !expr.span.from_expansion(); - if meets_msrv(self.msrv, msrvs::TYPE_ALIAS_ENUM_VARIANTS); + if self.msrv.meets(msrvs::TYPE_ALIAS_ENUM_VARIANTS); if let Some(&StackItem::Check { impl_id, .. }) = self.stack.last(); if cx.typeck_results().expr_ty(expr) == cx.tcx.type_of(impl_id); then {} else { return; } @@ -248,7 +248,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf { fn check_pat(&mut self, cx: &LateContext<'_>, pat: &Pat<'_>) { if_chain! { if !pat.span.from_expansion(); - if meets_msrv(self.msrv, msrvs::TYPE_ALIAS_ENUM_VARIANTS); + if self.msrv.meets(msrvs::TYPE_ALIAS_ENUM_VARIANTS); if let Some(&StackItem::Check { impl_id, .. }) = self.stack.last(); // get the path from the pattern if let PatKind::Path(QPath::Resolved(_, path)) diff --git a/clippy_lints/src/utils/internal_lints/msrv_attr_impl.rs b/clippy_lints/src/utils/internal_lints/msrv_attr_impl.rs index 1e994e3f2b171..9876a8a765ccb 100644 --- a/clippy_lints/src/utils/internal_lints/msrv_attr_impl.rs +++ b/clippy_lints/src/utils/internal_lints/msrv_attr_impl.rs @@ -41,7 +41,7 @@ impl LateLintPass<'_> for MsrvAttrImpl { .type_of(f.did) .walk() .filter(|t| matches!(t.unpack(), GenericArgKind::Type(_))) - .any(|t| match_type(cx, t.expect_ty(), &paths::RUSTC_VERSION)) + .any(|t| match_type(cx, t.expect_ty(), &paths::MSRV)) }); if !items.iter().any(|item| item.ident.name == sym!(enter_lint_attrs)); then { diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index b0063758dcef0..86ee172cb28ad 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -105,8 +105,6 @@ use rustc_middle::ty::{ layout::IntegerExt, BorrowKind, ClosureKind, DefIdTree, Ty, TyCtxt, TypeAndMut, TypeVisitable, UpvarCapture, }; use rustc_middle::ty::{FloatTy, IntTy, UintTy}; -use rustc_semver::RustcVersion; -use rustc_session::Session; use rustc_span::hygiene::{ExpnKind, MacroKind}; use rustc_span::source_map::SourceMap; use rustc_span::sym; @@ -118,36 +116,17 @@ use crate::consts::{constant, Constant}; use crate::ty::{can_partially_move_ty, expr_sig, is_copy, is_recursively_primitive_type, ty_is_fn_once_param}; use crate::visitors::for_each_expr; -pub fn parse_msrv(msrv: &str, sess: Option<&Session>, span: Option) -> Option { - if let Ok(version) = RustcVersion::parse(msrv) { - return Some(version); - } else if let Some(sess) = sess { - if let Some(span) = span { - sess.span_err(span, format!("`{msrv}` is not a valid Rust version")); - } - } - None -} - -pub fn meets_msrv(msrv: Option, lint_msrv: RustcVersion) -> bool { - msrv.map_or(true, |msrv| msrv.meets(lint_msrv)) -} - #[macro_export] macro_rules! extract_msrv_attr { ($context:ident) => { fn enter_lint_attrs(&mut self, cx: &rustc_lint::$context<'_>, attrs: &[rustc_ast::ast::Attribute]) { let sess = rustc_lint::LintContext::sess(cx); - match $crate::get_unique_inner_attr(sess, attrs, "msrv") { - Some(msrv_attr) => { - if let Some(msrv) = msrv_attr.value_str() { - self.msrv = $crate::parse_msrv(&msrv.to_string(), Some(sess), Some(msrv_attr.span)); - } else { - sess.span_err(msrv_attr.span, "bad clippy attribute"); - } - }, - _ => (), - } + self.msrv.enter_lint_attrs(sess, attrs); + } + + fn exit_lint_attrs(&mut self, cx: &rustc_lint::$context<'_>, attrs: &[rustc_ast::ast::Attribute]) { + let sess = rustc_lint::LintContext::sess(cx); + self.msrv.exit_lint_attrs(sess, attrs); } }; } diff --git a/clippy_utils/src/msrvs.rs b/clippy_utils/src/msrvs.rs index 79b19e6fb3eb0..2c9f75736e529 100644 --- a/clippy_utils/src/msrvs.rs +++ b/clippy_utils/src/msrvs.rs @@ -1,4 +1,11 @@ +use std::sync::OnceLock; + +use rustc_ast::Attribute; use rustc_semver::RustcVersion; +use rustc_session::Session; +use rustc_span::Span; + +use crate::attrs::get_unique_inner_attr; macro_rules! msrv_aliases { ($($major:literal,$minor:literal,$patch:literal { @@ -40,3 +47,97 @@ msrv_aliases! { 1,16,0 { STR_REPEAT } 1,55,0 { SEEK_REWIND } } + +fn parse_msrv(msrv: &str, sess: Option<&Session>, span: Option) -> Option { + if let Ok(version) = RustcVersion::parse(msrv) { + return Some(version); + } else if let Some(sess) = sess { + if let Some(span) = span { + sess.span_err(span, format!("`{msrv}` is not a valid Rust version")); + } + } + None +} + +/// Tracks the current MSRV from `clippy.toml`, `Cargo.toml` or set via `#[clippy::msrv]` +#[derive(Debug, Clone, Default)] +pub struct Msrv { + stack: Vec, +} + +impl Msrv { + fn new(initial: Option) -> Self { + Self { + stack: Vec::from_iter(initial), + } + } + + fn read_inner(conf_msrv: &Option, sess: &Session) -> Self { + let cargo_msrv = std::env::var("CARGO_PKG_RUST_VERSION") + .ok() + .and_then(|v| parse_msrv(&v, None, None)); + let clippy_msrv = conf_msrv.as_ref().and_then(|s| { + parse_msrv(s, None, None).or_else(|| { + sess.err(format!( + "error reading Clippy's configuration file. `{s}` is not a valid Rust version" + )); + None + }) + }); + + // if both files have an msrv, let's compare them and emit a warning if they differ + if let Some(cargo_msrv) = cargo_msrv + && let Some(clippy_msrv) = clippy_msrv + && clippy_msrv != cargo_msrv + { + sess.warn(format!( + "the MSRV in `clippy.toml` and `Cargo.toml` differ; using `{clippy_msrv}` from `clippy.toml`" + )); + } + + Self::new(clippy_msrv.or(cargo_msrv)) + } + + /// Set the initial MSRV from the Clippy config file or from Cargo due to the `rust-version` + /// field in `Cargo.toml` + /// + /// Returns a `&'static Msrv` as `Copy` types are more easily passed to the + /// `register_{late,early}_pass` callbacks + pub fn read(conf_msrv: &Option, sess: &Session) -> &'static Self { + static PARSED: OnceLock = OnceLock::new(); + + PARSED.get_or_init(|| Self::read_inner(conf_msrv, sess)) + } + + pub fn current(&self) -> Option { + self.stack.last().copied() + } + + pub fn meets(&self, required: RustcVersion) -> bool { + self.current().map_or(true, |version| version.meets(required)) + } + + fn parse_attr(sess: &Session, attrs: &[Attribute]) -> Option { + if let Some(msrv_attr) = get_unique_inner_attr(sess, attrs, "msrv") { + if let Some(msrv) = msrv_attr.value_str() { + return parse_msrv(&msrv.to_string(), Some(sess), Some(msrv_attr.span)); + } + + sess.span_err(msrv_attr.span, "bad clippy attribute"); + } + + None + } + + pub fn enter_lint_attrs(&mut self, sess: &Session, attrs: &[Attribute]) { + if let Some(version) = Self::parse_attr(sess, attrs) { + self.stack.push(version); + } + } + + pub fn exit_lint_attrs(&mut self, sess: &Session, attrs: &[Attribute]) { + if Self::parse_attr(sess, attrs).is_some() { + self.stack.pop(); + } + } +} diff --git a/clippy_utils/src/paths.rs b/clippy_utils/src/paths.rs index 6c09c146082ab..3cf8cb76651e0 100644 --- a/clippy_utils/src/paths.rs +++ b/clippy_utils/src/paths.rs @@ -60,6 +60,8 @@ pub const LATE_LINT_PASS: [&str; 3] = ["rustc_lint", "passes", "LateLintPass"]; #[cfg(feature = "internal")] pub const LINT: [&str; 2] = ["rustc_lint_defs", "Lint"]; pub const MEM_SWAP: [&str; 3] = ["core", "mem", "swap"]; +#[cfg(feature = "internal")] +pub const MSRV: [&str; 3] = ["clippy_utils", "msrvs", "Msrv"]; pub const OPEN_OPTIONS: [&str; 3] = ["std", "fs", "OpenOptions"]; pub const OS_STRING_AS_OS_STR: [&str; 5] = ["std", "ffi", "os_str", "OsString", "as_os_str"]; pub const OS_STR_TO_OS_STRING: [&str; 5] = ["std", "ffi", "os_str", "OsStr", "to_os_string"]; @@ -101,8 +103,6 @@ pub const REGEX_BYTES_NEW: [&str; 4] = ["regex", "re_bytes", "Regex", "new"]; pub const REGEX_BYTES_SET_NEW: [&str; 5] = ["regex", "re_set", "bytes", "RegexSet", "new"]; pub const REGEX_NEW: [&str; 4] = ["regex", "re_unicode", "Regex", "new"]; pub const REGEX_SET_NEW: [&str; 5] = ["regex", "re_set", "unicode", "RegexSet", "new"]; -#[cfg(feature = "internal")] -pub const RUSTC_VERSION: [&str; 2] = ["rustc_semver", "RustcVersion"]; pub const SERDE_DESERIALIZE: [&str; 3] = ["serde", "de", "Deserialize"]; pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"]; pub const SLICE_FROM_RAW_PARTS: [&str; 4] = ["core", "slice", "raw", "from_raw_parts"]; diff --git a/clippy_utils/src/qualify_min_const_fn.rs b/clippy_utils/src/qualify_min_const_fn.rs index 65722f142aa69..0def124561563 100644 --- a/clippy_utils/src/qualify_min_const_fn.rs +++ b/clippy_utils/src/qualify_min_const_fn.rs @@ -3,6 +3,7 @@ // of terminologies might not be relevant in the context of Clippy. Note that its behavior might // differ from the time of `rustc` even if the name stays the same. +use crate::msrvs::Msrv; use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_middle::mir::{ @@ -18,7 +19,7 @@ use std::borrow::Cow; type McfResult = Result<(), (Span, Cow<'static, str>)>; -pub fn is_min_const_fn<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, msrv: Option) -> McfResult { +pub fn is_min_const_fn<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, msrv: &Msrv) -> McfResult { let def_id = body.source.def_id(); let mut current = def_id; loop { @@ -280,7 +281,7 @@ fn check_terminator<'tcx>( tcx: TyCtxt<'tcx>, body: &Body<'tcx>, terminator: &Terminator<'tcx>, - msrv: Option, + msrv: &Msrv, ) -> McfResult { let span = terminator.source_info.span; match &terminator.kind { @@ -364,7 +365,7 @@ fn check_terminator<'tcx>( } } -fn is_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: Option) -> bool { +fn is_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: &Msrv) -> bool { tcx.is_const_fn(def_id) && tcx.lookup_const_stability(def_id).map_or(true, |const_stab| { if let rustc_attr::StabilityLevel::Stable { since, .. } = const_stab.level { @@ -383,15 +384,12 @@ fn is_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: Option) -> bo let since = rustc_span::Symbol::intern(short_version); - crate::meets_msrv( - msrv, - RustcVersion::parse(since.as_str()).unwrap_or_else(|err| { - panic!("`rustc_attr::StabilityLevel::Stable::since` is ill-formatted: `{since}`, {err:?}") - }), - ) + msrv.meets(RustcVersion::parse(since.as_str()).unwrap_or_else(|err| { + panic!("`rustc_attr::StabilityLevel::Stable::since` is ill-formatted: `{since}`, {err:?}") + })) } else { // Unstable const fn with the feature enabled. - msrv.is_none() + msrv.current().is_none() } }) } diff --git a/tests/ui-internal/invalid_msrv_attr_impl.fixed b/tests/ui-internal/invalid_msrv_attr_impl.fixed index 900a8fffd4080..08634063a5754 100644 --- a/tests/ui-internal/invalid_msrv_attr_impl.fixed +++ b/tests/ui-internal/invalid_msrv_attr_impl.fixed @@ -11,9 +11,9 @@ extern crate rustc_middle; #[macro_use] extern crate rustc_session; use clippy_utils::extract_msrv_attr; +use clippy_utils::msrvs::Msrv; use rustc_hir::Expr; use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass}; -use rustc_semver::RustcVersion; declare_lint! { pub TEST_LINT, @@ -22,7 +22,7 @@ declare_lint! { } struct Pass { - msrv: Option, + msrv: Msrv, } impl_lint_pass!(Pass => [TEST_LINT]); diff --git a/tests/ui-internal/invalid_msrv_attr_impl.rs b/tests/ui-internal/invalid_msrv_attr_impl.rs index 4bc8164db67b0..f8af77e6d395c 100644 --- a/tests/ui-internal/invalid_msrv_attr_impl.rs +++ b/tests/ui-internal/invalid_msrv_attr_impl.rs @@ -11,9 +11,9 @@ extern crate rustc_middle; #[macro_use] extern crate rustc_session; use clippy_utils::extract_msrv_attr; +use clippy_utils::msrvs::Msrv; use rustc_hir::Expr; use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass}; -use rustc_semver::RustcVersion; declare_lint! { pub TEST_LINT, @@ -22,7 +22,7 @@ declare_lint! { } struct Pass { - msrv: Option, + msrv: Msrv, } impl_lint_pass!(Pass => [TEST_LINT]); diff --git a/tests/ui/min_rust_version_attr.rs b/tests/ui/min_rust_version_attr.rs index cd148063bf065..28ab132394b82 100644 --- a/tests/ui/min_rust_version_attr.rs +++ b/tests/ui/min_rust_version_attr.rs @@ -27,3 +27,26 @@ fn no_patch_meets() { #![clippy::msrv = "1.43"] let log2_10 = 3.321928094887362; } + +// https://github.com/rust-lang/rust-clippy/issues/6920 +fn scoping() { + mod m { + #![clippy::msrv = "1.42.0"] + } + + // Should warn + let log2_10 = 3.321928094887362; + + mod a { + #![clippy::msrv = "1.42.0"] + + fn should_warn() { + #![clippy::msrv = "1.43.0"] + let log2_10 = 3.321928094887362; + } + + fn should_not_warn() { + let log2_10 = 3.321928094887362; + } + } +} diff --git a/tests/ui/min_rust_version_attr.stderr b/tests/ui/min_rust_version_attr.stderr index 68aa58748190b..6174443372f87 100644 --- a/tests/ui/min_rust_version_attr.stderr +++ b/tests/ui/min_rust_version_attr.stderr @@ -23,5 +23,21 @@ LL | let log2_10 = 3.321928094887362; | = help: consider using the constant directly -error: aborting due to 3 previous errors +error: approximate value of `f{32, 64}::consts::LOG2_10` found + --> $DIR/min_rust_version_attr.rs:38:19 + | +LL | let log2_10 = 3.321928094887362; + | ^^^^^^^^^^^^^^^^^ + | + = help: consider using the constant directly + +error: approximate value of `f{32, 64}::consts::LOG2_10` found + --> $DIR/min_rust_version_attr.rs:45:27 + | +LL | let log2_10 = 3.321928094887362; + | ^^^^^^^^^^^^^^^^^ + | + = help: consider using the constant directly + +error: aborting due to 5 previous errors From ae8f75c6a3fe019f2f04145049aba84adf4f3f94 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Tue, 25 Oct 2022 20:15:15 +0400 Subject: [PATCH 034/244] Unreserve braced enum variants in value namespace --- clippy_lints/src/matches/match_wild_enum.rs | 14 +++++++------- .../utils/internal_lints/unnecessary_def_path.rs | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/clippy_lints/src/matches/match_wild_enum.rs b/clippy_lints/src/matches/match_wild_enum.rs index 6f8d766aef7c7..7f8d124838cb8 100644 --- a/clippy_lints/src/matches/match_wild_enum.rs +++ b/clippy_lints/src/matches/match_wild_enum.rs @@ -65,14 +65,14 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) { _ => return, }; if arm.guard.is_none() { - missing_variants.retain(|e| e.ctor_def_id != Some(id)); + missing_variants.retain(|e| e.ctor_def_id() != Some(id)); } path }, PatKind::TupleStruct(path, patterns, ..) => { if let Some(id) = cx.qpath_res(path, pat.hir_id).opt_def_id() { if arm.guard.is_none() && patterns.iter().all(|p| !is_refutable(cx, p)) { - missing_variants.retain(|e| e.ctor_def_id != Some(id)); + missing_variants.retain(|e| e.ctor_def_id() != Some(id)); } } path @@ -122,11 +122,11 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) { s }, variant.name, - match variant.ctor_kind { - CtorKind::Fn if variant.fields.len() == 1 => "(_)", - CtorKind::Fn => "(..)", - CtorKind::Const => "", - CtorKind::Fictive => "{ .. }", + match variant.ctor_kind() { + Some(CtorKind::Fn) if variant.fields.len() == 1 => "(_)", + Some(CtorKind::Fn) => "(..)", + Some(CtorKind::Const) => "", + None => "{ .. }", } ) }; diff --git a/clippy_lints/src/utils/internal_lints/unnecessary_def_path.rs b/clippy_lints/src/utils/internal_lints/unnecessary_def_path.rs index cfba7fa8791de..f2276395fed0b 100644 --- a/clippy_lints/src/utils/internal_lints/unnecessary_def_path.rs +++ b/clippy_lints/src/utils/internal_lints/unnecessary_def_path.rs @@ -133,11 +133,11 @@ impl UnnecessaryDefPath { let has_ctor = match cx.tcx.def_kind(def_id) { DefKind::Struct => { let variant = cx.tcx.adt_def(def_id).non_enum_variant(); - variant.ctor_def_id.is_some() && variant.fields.iter().all(|f| f.vis.is_public()) + variant.ctor.is_some() && variant.fields.iter().all(|f| f.vis.is_public()) }, DefKind::Variant => { let variant = cx.tcx.adt_def(cx.tcx.parent(def_id)).variant_with_id(def_id); - variant.ctor_def_id.is_some() && variant.fields.iter().all(|f| f.vis.is_public()) + variant.ctor.is_some() && variant.fields.iter().all(|f| f.vis.is_public()) }, _ => false, }; From 46c5a5d234f13dcf4bb4cf4241b2addedbf0be14 Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Mon, 21 Nov 2022 20:34:47 +0100 Subject: [PATCH 035/244] Merge commit 'f4850f7292efa33759b4f7f9b7621268979e9914' into clippyup --- CHANGELOG.md | 159 ++++- CODE_OF_CONDUCT.md | 69 +- CONTRIBUTING.md | 33 + Cargo.toml | 2 +- README.md | 8 +- clippy_dev/Cargo.toml | 1 - clippy_dev/src/lint.rs | 8 - clippy_dev/src/update_lints.rs | 307 +++------ clippy_lints/Cargo.toml | 3 +- clippy_lints/src/attrs.rs | 37 +- clippy_lints/src/await_holding_invalid.rs | 3 +- clippy_lints/src/bool_to_int_with_if.rs | 27 +- clippy_lints/src/booleans.rs | 2 +- clippy_lints/src/casts/mod.rs | 2 +- clippy_lints/src/cognitive_complexity.rs | 21 +- clippy_lints/src/declared_lints.rs | 628 ++++++++++++++++++ clippy_lints/src/dereference.rs | 128 +++- clippy_lints/src/disallowed_macros.rs | 5 +- clippy_lints/src/disallowed_methods.rs | 5 +- clippy_lints/src/disallowed_types.rs | 40 +- clippy_lints/src/doc.rs | 123 ++-- clippy_lints/src/enum_variants.rs | 2 +- clippy_lints/src/equatable_if_let.rs | 27 +- clippy_lints/src/escape.rs | 8 +- clippy_lints/src/excessive_bools.rs | 129 ++-- clippy_lints/src/fallible_impl_from.rs | 2 +- clippy_lints/src/from_raw_with_void_ptr.rs | 77 +++ clippy_lints/src/functions/mod.rs | 2 +- clippy_lints/src/functions/must_use.rs | 30 +- clippy_lints/src/functions/result.rs | 68 +- clippy_lints/src/implicit_hasher.rs | 4 +- clippy_lints/src/index_refutable_slice.rs | 4 +- clippy_lints/src/indexing_slicing.rs | 6 +- clippy_lints/src/instant_subtraction.rs | 184 +++++ clippy_lints/src/int_plus_one.rs | 28 +- .../src/invalid_upcast_comparisons.rs | 2 +- clippy_lints/src/large_enum_variant.rs | 60 +- clippy_lints/src/len_zero.rs | 3 +- clippy_lints/src/let_underscore.rs | 157 ++--- clippy_lints/src/lib.register_all.rs | 368 ---------- clippy_lints/src/lib.register_cargo.rs | 11 - clippy_lints/src/lib.register_complexity.rs | 111 ---- clippy_lints/src/lib.register_correctness.rs | 78 --- clippy_lints/src/lib.register_internal.rs | 22 - clippy_lints/src/lib.register_lints.rs | 620 ----------------- clippy_lints/src/lib.register_nursery.rs | 39 -- clippy_lints/src/lib.register_pedantic.rs | 104 --- clippy_lints/src/lib.register_perf.rs | 34 - clippy_lints/src/lib.register_restriction.rs | 90 --- clippy_lints/src/lib.register_style.rs | 131 ---- clippy_lints/src/lib.register_suspicious.rs | 38 -- clippy_lints/src/lib.rs | 267 ++++---- clippy_lints/src/lifetimes.rs | 131 ++-- clippy_lints/src/loops/mod.rs | 26 - clippy_lints/src/loops/mut_range_bound.rs | 12 +- clippy_lints/src/loops/never_loop.rs | 114 ++-- clippy_lints/src/manual_instant_elapsed.rs | 69 -- clippy_lints/src/manual_is_ascii_check.rs | 158 +++++ clippy_lints/src/manual_let_else.rs | 297 +++++++++ clippy_lints/src/map_unit_fn.rs | 2 +- .../matches/infallible_destructuring_match.rs | 7 +- clippy_lints/src/matches/manual_filter.rs | 2 +- .../matches/significant_drop_in_scrutinee.rs | 8 +- clippy_lints/src/matches/single_match.rs | 2 +- .../src/methods/chars_cmp_with_unwrap.rs | 4 +- clippy_lints/src/methods/chars_last_cmp.rs | 2 +- .../src/methods/chars_last_cmp_with_unwrap.rs | 2 +- clippy_lints/src/methods/chars_next_cmp.rs | 2 +- .../src/methods/chars_next_cmp_with_unwrap.rs | 2 +- .../src/methods/collapsible_str_replace.rs | 6 +- clippy_lints/src/methods/expect_used.rs | 10 +- clippy_lints/src/methods/filter_map.rs | 8 +- .../src/methods/inefficient_to_string.rs | 4 +- clippy_lints/src/methods/iter_nth_zero.rs | 2 +- .../iter_on_single_or_empty_collections.rs | 2 +- .../methods/manual_saturating_arithmetic.rs | 2 +- clippy_lints/src/methods/manual_str_repeat.rs | 2 - clippy_lints/src/methods/map_clone.rs | 4 +- .../src/methods/map_collect_result_unit.rs | 15 +- clippy_lints/src/methods/map_err_ignore.rs | 2 +- clippy_lints/src/methods/mod.rs | 281 +++++--- .../{loops => methods}/needless_collect.rs | 223 ++++--- .../src/methods/option_as_ref_deref.rs | 4 +- clippy_lints/src/methods/or_fun_call.rs | 57 +- clippy_lints/src/methods/seek_from_current.rs | 48 ++ .../seek_to_start_instead_of_rewind.rs | 45 ++ .../src/methods/string_extend_chars.rs | 6 +- clippy_lints/src/methods/suspicious_map.rs | 2 +- clippy_lints/src/methods/unnecessary_join.rs | 2 +- clippy_lints/src/methods/unwrap_used.rs | 10 +- .../src/missing_enforced_import_rename.rs | 2 +- .../src/mixed_read_write_in_expression.rs | 4 +- clippy_lints/src/mut_key.rs | 155 +++-- clippy_lints/src/mut_mut.rs | 16 +- clippy_lints/src/needless_borrowed_ref.rs | 108 +-- clippy_lints/src/needless_continue.rs | 8 +- clippy_lints/src/needless_pass_by_value.rs | 8 +- clippy_lints/src/octal_escapes.rs | 4 +- .../src/operators/arithmetic_side_effects.rs | 110 +-- clippy_lints/src/operators/op_ref.rs | 2 +- clippy_lints/src/option_if_let_else.rs | 13 +- clippy_lints/src/partialeq_to_none.rs | 2 +- clippy_lints/src/pattern_type_mismatch.rs | 4 +- clippy_lints/src/ptr_offset_with_cast.rs | 8 +- clippy_lints/src/question_mark.rs | 1 + clippy_lints/src/redundant_closure_call.rs | 4 +- clippy_lints/src/redundant_pub_crate.rs | 3 +- clippy_lints/src/renamed_lints.rs | 5 +- .../src/single_component_path_imports.rs | 219 +++--- .../src/slow_vector_initialization.rs | 2 +- .../src/suspicious_xor_used_as_pow.rs | 53 ++ clippy_lints/src/swap.rs | 8 +- clippy_lints/src/trailing_empty_array.rs | 8 +- .../src/transmute/transmute_undefined_repr.rs | 164 ++--- clippy_lints/src/transmute/utils.rs | 11 +- clippy_lints/src/types/box_collection.rs | 19 +- clippy_lints/src/types/mod.rs | 4 +- .../src/types/redundant_allocation.rs | 24 +- clippy_lints/src/types/vec_box.rs | 2 +- .../src/undocumented_unsafe_blocks.rs | 40 +- clippy_lints/src/unsafe_removed_from_name.rs | 2 +- clippy_lints/src/unused_peekable.rs | 2 +- clippy_lints/src/unused_rounding.rs | 11 +- clippy_lints/src/unused_unit.rs | 8 +- clippy_lints/src/use_self.rs | 39 +- clippy_lints/src/utils/conf.rs | 31 +- .../interning_defined_symbol.rs | 4 +- .../src/utils/internal_lints/invalid_paths.rs | 4 +- .../internal_lints/lint_without_lint_pass.rs | 2 +- .../internal_lints/unnecessary_def_path.rs | 45 +- clippy_lints/src/write.rs | 17 +- clippy_utils/Cargo.toml | 2 +- clippy_utils/src/ast_utils.rs | 20 +- clippy_utils/src/consts.rs | 32 +- clippy_utils/src/diagnostics.rs | 12 +- clippy_utils/src/hir_utils.rs | 33 +- clippy_utils/src/lib.rs | 315 +++++---- clippy_utils/src/msrvs.rs | 6 +- clippy_utils/src/paths.rs | 3 + clippy_utils/src/qualify_min_const_fn.rs | 6 +- clippy_utils/src/source.rs | 22 +- clippy_utils/src/sugg.rs | 2 +- clippy_utils/src/ty.rs | 224 ++++++- clippy_utils/src/usage.rs | 7 +- declare_clippy_lint/Cargo.toml | 13 + declare_clippy_lint/src/lib.rs | 173 +++++ lintcheck/src/main.rs | 47 +- rust-toolchain | 4 +- src/docs.rs | 606 ----------------- src/docs/absurd_extreme_comparisons.txt | 25 - src/docs/alloc_instead_of_core.txt | 18 - src/docs/allow_attributes_without_reason.txt | 22 - src/docs/almost_complete_letter_range.txt | 15 - src/docs/almost_swapped.txt | 15 - src/docs/approx_constant.txt | 24 - src/docs/arithmetic_side_effects.txt | 33 - src/docs/as_conversions.txt | 32 - src/docs/as_ptr_cast_mut.txt | 19 - src/docs/as_underscore.txt | 21 - src/docs/assertions_on_constants.txt | 14 - src/docs/assertions_on_result_states.txt | 14 - src/docs/assign_op_pattern.txt | 28 - src/docs/async_yields_async.txt | 28 - src/docs/await_holding_invalid_type.txt | 29 - src/docs/await_holding_lock.txt | 51 -- src/docs/await_holding_refcell_ref.txt | 47 -- src/docs/bad_bit_mask.txt | 30 - src/docs/bind_instead_of_map.txt | 22 - src/docs/blanket_clippy_restriction_lints.txt | 16 - src/docs/blocks_in_if_conditions.txt | 21 - src/docs/bool_assert_comparison.txt | 16 - src/docs/bool_comparison.txt | 18 - src/docs/bool_to_int_with_if.txt | 26 - src/docs/borrow_as_ptr.txt | 26 - src/docs/borrow_deref_ref.txt | 27 - src/docs/borrow_interior_mutable_const.txt | 40 -- src/docs/borrowed_box.txt | 19 - src/docs/box_collection.txt | 23 - src/docs/box_default.txt | 17 - src/docs/boxed_local.txt | 18 - src/docs/branches_sharing_code.txt | 32 - src/docs/builtin_type_shadow.txt | 15 - src/docs/bytes_count_to_len.txt | 18 - src/docs/bytes_nth.txt | 16 - src/docs/cargo_common_metadata.txt | 33 - ...e_sensitive_file_extension_comparisons.txt | 21 - src/docs/cast_abs_to_unsigned.txt | 16 - src/docs/cast_enum_constructor.txt | 11 - src/docs/cast_enum_truncation.txt | 12 - src/docs/cast_lossless.txt | 26 - src/docs/cast_nan_to_int.txt | 15 - src/docs/cast_possible_truncation.txt | 16 - src/docs/cast_possible_wrap.txt | 17 - src/docs/cast_precision_loss.txt | 19 - src/docs/cast_ptr_alignment.txt | 21 - src/docs/cast_ref_to_mut.txt | 28 - src/docs/cast_sign_loss.txt | 15 - src/docs/cast_slice_different_sizes.txt | 38 -- src/docs/cast_slice_from_raw_parts.txt | 20 - src/docs/char_lit_as_u8.txt | 21 - src/docs/chars_last_cmp.txt | 17 - src/docs/chars_next_cmp.txt | 19 - src/docs/checked_conversions.txt | 15 - src/docs/clone_double_ref.txt | 16 - src/docs/clone_on_copy.txt | 11 - src/docs/clone_on_ref_ptr.txt | 21 - src/docs/cloned_instead_of_copied.txt | 16 - src/docs/cmp_nan.txt | 16 - src/docs/cmp_null.txt | 23 - src/docs/cmp_owned.txt | 18 - src/docs/cognitive_complexity.txt | 13 - src/docs/collapsible_else_if.txt | 29 - src/docs/collapsible_if.txt | 23 - src/docs/collapsible_match.txt | 31 - src/docs/collapsible_str_replace.txt | 19 - src/docs/comparison_chain.txt | 36 - src/docs/comparison_to_empty.txt | 31 - src/docs/copy_iterator.txt | 20 - src/docs/crate_in_macro_def.txt | 35 - src/docs/create_dir.txt | 15 - src/docs/crosspointer_transmute.txt | 12 - src/docs/dbg_macro.txt | 16 - src/docs/debug_assert_with_mut_call.txt | 18 - src/docs/decimal_literal_representation.txt | 13 - src/docs/declare_interior_mutable_const.txt | 46 -- src/docs/default_instead_of_iter_empty.txt | 15 - src/docs/default_numeric_fallback.txt | 28 - src/docs/default_trait_access.txt | 16 - src/docs/default_union_representation.txt | 36 - src/docs/deprecated_cfg_attr.txt | 24 - src/docs/deprecated_semver.txt | 13 - src/docs/deref_addrof.txt | 22 - src/docs/deref_by_slicing.txt | 17 - src/docs/derivable_impls.txt | 35 - src/docs/derive_hash_xor_eq.txt | 23 - src/docs/derive_ord_xor_partial_ord.txt | 44 -- src/docs/derive_partial_eq_without_eq.txt | 25 - src/docs/disallowed_macros.txt | 36 - src/docs/disallowed_methods.txt | 41 -- src/docs/disallowed_names.txt | 12 - src/docs/disallowed_script_idents.txt | 32 - src/docs/disallowed_types.txt | 33 - src/docs/diverging_sub_expression.txt | 19 - src/docs/doc_link_with_quotes.txt | 16 - src/docs/doc_markdown.txt | 35 - src/docs/double_comparisons.txt | 17 - src/docs/double_must_use.txt | 17 - src/docs/double_neg.txt | 12 - src/docs/double_parens.txt | 24 - src/docs/drop_copy.txt | 15 - src/docs/drop_non_drop.txt | 13 - src/docs/drop_ref.txt | 17 - src/docs/duplicate_mod.txt | 31 - src/docs/duplicate_underscore_argument.txt | 16 - src/docs/duration_subsec.txt | 19 - src/docs/else_if_without_else.txt | 27 - src/docs/empty_drop.txt | 20 - src/docs/empty_enum.txt | 27 - src/docs/empty_line_after_outer_attr.txt | 35 - src/docs/empty_loop.txt | 27 - src/docs/empty_structs_with_brackets.txt | 14 - src/docs/enum_clike_unportable_variant.txt | 16 - src/docs/enum_glob_use.txt | 24 - src/docs/enum_variant_names.txt | 30 - src/docs/eq_op.txt | 22 - src/docs/equatable_if_let.txt | 23 - src/docs/erasing_op.txt | 15 - src/docs/err_expect.txt | 16 - src/docs/excessive_precision.txt | 18 - src/docs/exhaustive_enums.txt | 23 - src/docs/exhaustive_structs.txt | 23 - src/docs/exit.txt | 12 - src/docs/expect_fun_call.txt | 24 - src/docs/expect_used.txt | 26 - src/docs/expl_impl_clone_on_copy.txt | 20 - src/docs/explicit_auto_deref.txt | 16 - src/docs/explicit_counter_loop.txt | 21 - src/docs/explicit_deref_methods.txt | 24 - src/docs/explicit_into_iter_loop.txt | 20 - src/docs/explicit_iter_loop.txt | 25 - src/docs/explicit_write.txt | 18 - src/docs/extend_with_drain.txt | 21 - src/docs/extra_unused_lifetimes.txt | 23 - src/docs/fallible_impl_from.txt | 32 - src/docs/field_reassign_with_default.txt | 23 - src/docs/filetype_is_file.txt | 29 - src/docs/filter_map_identity.txt | 14 - src/docs/filter_map_next.txt | 16 - src/docs/filter_next.txt | 16 - src/docs/flat_map_identity.txt | 14 - src/docs/flat_map_option.txt | 16 - src/docs/float_arithmetic.txt | 11 - src/docs/float_cmp.txt | 28 - src/docs/float_cmp_const.txt | 26 - src/docs/float_equality_without_abs.txt | 26 - src/docs/fn_address_comparisons.txt | 17 - src/docs/fn_params_excessive_bools.txt | 31 - src/docs/fn_to_numeric_cast.txt | 21 - src/docs/fn_to_numeric_cast_any.txt | 35 - .../fn_to_numeric_cast_with_truncation.txt | 26 - src/docs/for_kv_map.txt | 22 - src/docs/forget_copy.txt | 21 - src/docs/forget_non_drop.txt | 13 - src/docs/forget_ref.txt | 15 - src/docs/format_in_format_args.txt | 16 - src/docs/format_push_string.txt | 26 - src/docs/from_iter_instead_of_collect.txt | 24 - src/docs/from_over_into.txt | 26 - src/docs/from_str_radix_10.txt | 25 - src/docs/future_not_send.txt | 29 - src/docs/get_first.txt | 19 - src/docs/get_last_with_len.txt | 26 - src/docs/get_unwrap.txt | 30 - src/docs/identity_op.txt | 11 - src/docs/if_let_mutex.txt | 25 - src/docs/if_not_else.txt | 25 - src/docs/if_same_then_else.txt | 15 - src/docs/if_then_some_else_none.txt | 26 - src/docs/ifs_same_cond.txt | 25 - src/docs/implicit_clone.txt | 19 - src/docs/implicit_hasher.txt | 26 - src/docs/implicit_return.txt | 22 - src/docs/implicit_saturating_add.txt | 20 - src/docs/implicit_saturating_sub.txt | 21 - src/docs/imprecise_flops.txt | 23 - src/docs/inconsistent_digit_grouping.txt | 17 - src/docs/inconsistent_struct_constructor.txt | 40 -- src/docs/index_refutable_slice.txt | 29 - src/docs/indexing_slicing.txt | 33 - src/docs/ineffective_bit_mask.txt | 25 - src/docs/inefficient_to_string.txt | 17 - src/docs/infallible_destructuring_match.txt | 29 - src/docs/infinite_iter.txt | 13 - src/docs/inherent_to_string.txt | 29 - .../inherent_to_string_shadow_display.txt | 37 -- src/docs/init_numbered_fields.txt | 24 - src/docs/inline_always.txt | 23 - src/docs/inline_asm_x86_att_syntax.txt | 16 - src/docs/inline_asm_x86_intel_syntax.txt | 16 - src/docs/inline_fn_without_body.txt | 14 - src/docs/inspect_for_each.txt | 23 - src/docs/int_plus_one.txt | 15 - src/docs/integer_arithmetic.txt | 18 - src/docs/integer_division.txt | 19 - src/docs/into_iter_on_ref.txt | 18 - src/docs/invalid_null_ptr_usage.txt | 16 - src/docs/invalid_regex.txt | 12 - src/docs/invalid_upcast_comparisons.txt | 18 - src/docs/invalid_utf8_in_unchecked.txt | 12 - src/docs/invisible_characters.txt | 10 - src/docs/is_digit_ascii_radix.txt | 20 - src/docs/items_after_statements.txt | 37 -- src/docs/iter_cloned_collect.txt | 17 - src/docs/iter_count.txt | 22 - src/docs/iter_kv_map.txt | 22 - src/docs/iter_next_loop.txt | 17 - src/docs/iter_next_slice.txt | 16 - src/docs/iter_not_returning_iterator.txt | 26 - src/docs/iter_nth.txt | 20 - src/docs/iter_nth_zero.txt | 17 - src/docs/iter_on_empty_collections.txt | 25 - src/docs/iter_on_single_items.txt | 24 - src/docs/iter_overeager_cloned.txt | 22 - src/docs/iter_skip_next.txt | 18 - src/docs/iter_with_drain.txt | 16 - src/docs/iterator_step_by_zero.txt | 13 - src/docs/just_underscores_and_digits.txt | 14 - src/docs/large_const_arrays.txt | 17 - src/docs/large_digit_groups.txt | 12 - src/docs/large_enum_variant.txt | 41 -- src/docs/large_include_file.txt | 21 - src/docs/large_stack_arrays.txt | 10 - src/docs/large_types_passed_by_value.txt | 24 - src/docs/len_without_is_empty.txt | 19 - src/docs/len_zero.txt | 28 - src/docs/let_and_return.txt | 21 - src/docs/let_underscore_drop.txt | 29 - src/docs/let_underscore_lock.txt | 20 - src/docs/let_underscore_must_use.txt | 17 - src/docs/let_unit_value.txt | 13 - src/docs/linkedlist.txt | 32 - src/docs/lossy_float_literal.txt | 18 - src/docs/macro_use_imports.txt | 12 - src/docs/main_recursion.txt | 13 - src/docs/manual_assert.txt | 18 - src/docs/manual_async_fn.txt | 16 - src/docs/manual_bits.txt | 15 - src/docs/manual_clamp.txt | 46 -- src/docs/manual_filter.txt | 21 - src/docs/manual_filter_map.txt | 19 - src/docs/manual_find.txt | 24 - src/docs/manual_find_map.txt | 19 - src/docs/manual_flatten.txt | 25 - src/docs/manual_instant_elapsed.txt | 21 - src/docs/manual_map.txt | 17 - src/docs/manual_memcpy.txt | 18 - src/docs/manual_non_exhaustive.txt | 41 -- src/docs/manual_ok_or.txt | 19 - src/docs/manual_range_contains.txt | 19 - src/docs/manual_rem_euclid.txt | 17 - src/docs/manual_retain.txt | 15 - src/docs/manual_saturating_arithmetic.txt | 18 - src/docs/manual_split_once.txt | 29 - src/docs/manual_str_repeat.txt | 15 - src/docs/manual_string_new.txt | 20 - src/docs/manual_strip.txt | 24 - src/docs/manual_swap.txt | 22 - src/docs/manual_unwrap_or.txt | 20 - src/docs/many_single_char_names.txt | 12 - src/docs/map_clone.txt | 22 - src/docs/map_collect_result_unit.txt | 14 - src/docs/map_entry.txt | 28 - src/docs/map_err_ignore.txt | 93 --- src/docs/map_flatten.txt | 21 - src/docs/map_identity.txt | 16 - src/docs/map_unwrap_or.txt | 22 - src/docs/match_as_ref.txt | 23 - src/docs/match_bool.txt | 24 - src/docs/match_like_matches_macro.txt | 32 - src/docs/match_on_vec_items.txt | 29 - src/docs/match_overlapping_arm.txt | 16 - src/docs/match_ref_pats.txt | 26 - src/docs/match_result_ok.txt | 27 - src/docs/match_same_arms.txt | 38 -- src/docs/match_single_binding.txt | 23 - src/docs/match_str_case_mismatch.txt | 22 - src/docs/match_wild_err_arm.txt | 16 - .../match_wildcard_for_single_variants.txt | 27 - src/docs/maybe_infinite_iter.txt | 16 - src/docs/mem_forget.txt | 12 - src/docs/mem_replace_option_with_none.txt | 21 - src/docs/mem_replace_with_default.txt | 18 - src/docs/mem_replace_with_uninit.txt | 24 - src/docs/min_max.txt | 18 - src/docs/mismatched_target_os.txt | 24 - src/docs/mismatching_type_param_order.txt | 33 - src/docs/misrefactored_assign_op.txt | 20 - src/docs/missing_const_for_fn.txt | 40 -- src/docs/missing_docs_in_private_items.txt | 9 - src/docs/missing_enforced_import_renames.txt | 22 - src/docs/missing_errors_doc.txt | 21 - src/docs/missing_inline_in_public_items.txt | 45 -- src/docs/missing_panics_doc.txt | 24 - src/docs/missing_safety_doc.txt | 26 - src/docs/missing_spin_loop.txt | 27 - src/docs/missing_trait_methods.txt | 40 -- src/docs/mistyped_literal_suffixes.txt | 15 - src/docs/mixed_case_hex_literals.txt | 16 - src/docs/mixed_read_write_in_expression.txt | 32 - src/docs/mod_module_files.txt | 22 - src/docs/module_inception.txt | 24 - src/docs/module_name_repetitions.txt | 20 - src/docs/modulo_arithmetic.txt | 15 - src/docs/modulo_one.txt | 16 - src/docs/multi_assignments.txt | 17 - src/docs/multiple_crate_versions.txt | 19 - src/docs/multiple_inherent_impl.txt | 26 - src/docs/must_use_candidate.txt | 23 - src/docs/must_use_unit.txt | 13 - src/docs/mut_from_ref.txt | 26 - src/docs/mut_mut.txt | 12 - src/docs/mut_mutex_lock.txt | 29 - src/docs/mut_range_bound.txt | 29 - src/docs/mutable_key_type.txt | 61 -- src/docs/mutex_atomic.txt | 22 - src/docs/mutex_integer.txt | 22 - src/docs/naive_bytecount.txt | 22 - src/docs/needless_arbitrary_self_type.txt | 44 -- src/docs/needless_bitwise_bool.txt | 22 - src/docs/needless_bool.txt | 26 - src/docs/needless_borrow.txt | 21 - src/docs/needless_borrowed_reference.txt | 22 - src/docs/needless_collect.txt | 14 - src/docs/needless_continue.txt | 61 -- src/docs/needless_doctest_main.txt | 22 - src/docs/needless_for_each.txt | 24 - src/docs/needless_late_init.txt | 42 -- src/docs/needless_lifetimes.txt | 29 - src/docs/needless_match.txt | 36 - src/docs/needless_option_as_deref.txt | 18 - src/docs/needless_option_take.txt | 17 - .../needless_parens_on_range_literals.txt | 23 - src/docs/needless_pass_by_value.txt | 27 - src/docs/needless_question_mark.txt | 43 -- src/docs/needless_range_loop.txt | 23 - src/docs/needless_return.txt | 19 - src/docs/needless_splitn.txt | 16 - src/docs/needless_update.txt | 30 - src/docs/neg_cmp_op_on_partial_ord.txt | 26 - src/docs/neg_multiply.txt | 18 - src/docs/negative_feature_names.txt | 22 - src/docs/never_loop.txt | 15 - src/docs/new_ret_no_self.txt | 47 -- src/docs/new_without_default.txt | 32 - src/docs/no_effect.txt | 12 - src/docs/no_effect_replace.txt | 11 - src/docs/no_effect_underscore_binding.txt | 16 - src/docs/non_ascii_literal.txt | 19 - src/docs/non_octal_unix_permissions.txt | 23 - src/docs/non_send_fields_in_send_ty.txt | 36 - src/docs/nonminimal_bool.txt | 23 - src/docs/nonsensical_open_options.txt | 14 - src/docs/nonstandard_macro_braces.txt | 15 - src/docs/not_unsafe_ptr_arg_deref.txt | 30 - src/docs/obfuscated_if_else.txt | 21 - src/docs/octal_escapes.txt | 33 - src/docs/ok_expect.txt | 19 - src/docs/only_used_in_recursion.txt | 58 -- src/docs/op_ref.txt | 17 - src/docs/option_as_ref_deref.txt | 15 - src/docs/option_env_unwrap.txt | 19 - src/docs/option_filter_map.txt | 15 - src/docs/option_if_let_else.txt | 46 -- src/docs/option_map_or_none.txt | 19 - src/docs/option_map_unit_fn.txt | 27 - src/docs/option_option.txt | 32 - src/docs/or_fun_call.txt | 27 - src/docs/or_then_unwrap.txt | 22 - src/docs/out_of_bounds_indexing.txt | 22 - src/docs/overflow_check_conditional.txt | 11 - src/docs/overly_complex_bool_expr.txt | 20 - src/docs/panic.txt | 10 - src/docs/panic_in_result_fn.txt | 22 - src/docs/panicking_unwrap.txt | 18 - src/docs/partial_pub_fields.txt | 27 - src/docs/partialeq_ne_impl.txt | 18 - src/docs/partialeq_to_none.txt | 24 - src/docs/path_buf_push_overwrite.txt | 25 - src/docs/pattern_type_mismatch.txt | 64 -- src/docs/possible_missing_comma.txt | 14 - src/docs/precedence.txt | 17 - src/docs/print_in_format_impl.txt | 34 - src/docs/print_literal.txt | 16 - src/docs/print_stderr.txt | 15 - src/docs/print_stdout.txt | 15 - src/docs/print_with_newline.txt | 16 - src/docs/println_empty_string.txt | 16 - src/docs/ptr_arg.txt | 29 - src/docs/ptr_as_ptr.txt | 22 - src/docs/ptr_eq.txt | 22 - src/docs/ptr_offset_with_cast.txt | 30 - src/docs/pub_use.txt | 28 - src/docs/question_mark.txt | 18 - src/docs/range_minus_one.txt | 27 - src/docs/range_plus_one.txt | 36 - src/docs/range_zip_with_len.txt | 16 - src/docs/rc_buffer.txt | 27 - src/docs/rc_clone_in_vec_init.txt | 29 - src/docs/rc_mutex.txt | 26 - src/docs/read_zero_byte_vec.txt | 30 - src/docs/recursive_format_impl.txt | 32 - src/docs/redundant_allocation.txt | 17 - src/docs/redundant_clone.txt | 23 - src/docs/redundant_closure.txt | 25 - src/docs/redundant_closure_call.txt | 17 - .../redundant_closure_for_method_calls.txt | 15 - src/docs/redundant_else.txt | 30 - src/docs/redundant_feature_names.txt | 23 - src/docs/redundant_field_names.txt | 22 - src/docs/redundant_pattern.txt | 22 - src/docs/redundant_pattern_matching.txt | 45 -- src/docs/redundant_pub_crate.txt | 21 - src/docs/redundant_slicing.txt | 24 - src/docs/redundant_static_lifetimes.txt | 19 - src/docs/ref_binding_to_reference.txt | 21 - src/docs/ref_option_ref.txt | 19 - src/docs/repeat_once.txt | 25 - src/docs/rest_pat_in_fully_bound_structs.txt | 24 - src/docs/result_large_err.txt | 36 - src/docs/result_map_or_into_option.txt | 16 - src/docs/result_map_unit_fn.txt | 26 - src/docs/result_unit_err.txt | 40 -- src/docs/return_self_not_must_use.txt | 46 -- src/docs/reversed_empty_ranges.txt | 26 - src/docs/same_functions_in_if_condition.txt | 41 -- src/docs/same_item_push.txt | 29 - src/docs/same_name_method.txt | 23 - src/docs/search_is_some.txt | 24 - src/docs/self_assignment.txt | 32 - src/docs/self_named_constructors.txt | 26 - src/docs/self_named_module_files.txt | 22 - src/docs/semicolon_if_nothing_returned.txt | 20 - src/docs/separated_literal_suffix.txt | 17 - src/docs/serde_api_misuse.txt | 10 - src/docs/shadow_reuse.txt | 20 - src/docs/shadow_same.txt | 18 - src/docs/shadow_unrelated.txt | 22 - src/docs/short_circuit_statement.txt | 14 - src/docs/should_implement_trait.txt | 22 - src/docs/significant_drop_in_scrutinee.txt | 43 -- src/docs/similar_names.txt | 16 - src/docs/single_char_add_str.txt | 18 - src/docs/single_char_lifetime_names.txt | 28 - src/docs/single_char_pattern.txt | 20 - src/docs/single_component_path_imports.txt | 21 - src/docs/single_element_loop.txt | 21 - src/docs/single_match.txt | 21 - src/docs/single_match_else.txt | 29 - src/docs/size_of_in_element_count.txt | 16 - src/docs/skip_while_next.txt | 16 - src/docs/slow_vector_initialization.txt | 24 - src/docs/stable_sort_primitive.txt | 31 - src/docs/std_instead_of_alloc.txt | 18 - src/docs/std_instead_of_core.txt | 18 - src/docs/str_to_string.txt | 18 - src/docs/string_add.txt | 27 - src/docs/string_add_assign.txt | 17 - src/docs/string_extend_chars.txt | 23 - src/docs/string_from_utf8_as_bytes.txt | 15 - src/docs/string_lit_as_bytes.txt | 39 -- src/docs/string_slice.txt | 17 - src/docs/string_to_string.txt | 19 - src/docs/strlen_on_c_strings.txt | 20 - src/docs/struct_excessive_bools.txt | 29 - src/docs/suboptimal_flops.txt | 50 -- src/docs/suspicious_arithmetic_impl.txt | 17 - src/docs/suspicious_assignment_formatting.txt | 12 - src/docs/suspicious_else_formatting.txt | 30 - src/docs/suspicious_map.txt | 13 - src/docs/suspicious_op_assign_impl.txt | 15 - src/docs/suspicious_operation_groupings.txt | 41 -- src/docs/suspicious_splitn.txt | 22 - src/docs/suspicious_to_owned.txt | 39 -- src/docs/suspicious_unary_op_formatting.txt | 18 - src/docs/swap_ptr_to_ref.txt | 23 - src/docs/tabs_in_doc_comments.txt | 44 -- src/docs/temporary_assignment.txt | 12 - src/docs/to_digit_is_some.txt | 15 - src/docs/to_string_in_format_args.txt | 17 - src/docs/todo.txt | 10 - src/docs/too_many_arguments.txt | 14 - src/docs/too_many_lines.txt | 17 - src/docs/toplevel_ref_arg.txt | 28 - src/docs/trailing_empty_array.txt | 22 - src/docs/trait_duplication_in_bounds.txt | 37 -- src/docs/transmute_bytes_to_str.txt | 27 - src/docs/transmute_float_to_int.txt | 16 - src/docs/transmute_int_to_bool.txt | 16 - src/docs/transmute_int_to_char.txt | 27 - src/docs/transmute_int_to_float.txt | 16 - src/docs/transmute_num_to_bytes.txt | 16 - src/docs/transmute_ptr_to_ptr.txt | 21 - src/docs/transmute_ptr_to_ref.txt | 21 - src/docs/transmute_undefined_repr.txt | 22 - .../transmutes_expressible_as_ptr_casts.txt | 16 - src/docs/transmuting_null.txt | 15 - src/docs/trim_split_whitespace.txt | 14 - src/docs/trivial_regex.txt | 18 - src/docs/trivially_copy_pass_by_ref.txt | 43 -- src/docs/try_err.txt | 28 - src/docs/type_complexity.txt | 14 - src/docs/type_repetition_in_bounds.txt | 16 - src/docs/undocumented_unsafe_blocks.txt | 43 -- src/docs/undropped_manually_drops.txt | 22 - src/docs/unicode_not_nfc.txt | 12 - src/docs/unimplemented.txt | 10 - src/docs/uninit_assumed_init.txt | 28 - src/docs/uninit_vec.txt | 41 -- src/docs/uninlined_format_args.txt | 36 - src/docs/unit_arg.txt | 14 - src/docs/unit_cmp.txt | 33 - src/docs/unit_hash.txt | 20 - src/docs/unit_return_expecting_ord.txt | 20 - src/docs/unnecessary_cast.txt | 19 - src/docs/unnecessary_filter_map.txt | 23 - src/docs/unnecessary_find_map.txt | 23 - src/docs/unnecessary_fold.txt | 17 - src/docs/unnecessary_join.txt | 25 - src/docs/unnecessary_lazy_evaluations.txt | 32 - src/docs/unnecessary_mut_passed.txt | 17 - src/docs/unnecessary_operation.txt | 12 - src/docs/unnecessary_owned_empty_strings.txt | 16 - src/docs/unnecessary_self_imports.txt | 19 - src/docs/unnecessary_sort_by.txt | 21 - src/docs/unnecessary_to_owned.txt | 24 - src/docs/unnecessary_unwrap.txt | 20 - src/docs/unnecessary_wraps.txt | 36 - src/docs/unneeded_field_pattern.txt | 26 - src/docs/unneeded_wildcard_pattern.txt | 28 - src/docs/unnested_or_patterns.txt | 22 - src/docs/unreachable.txt | 10 - src/docs/unreadable_literal.txt | 16 - src/docs/unsafe_derive_deserialize.txt | 27 - src/docs/unsafe_removed_from_name.txt | 15 - src/docs/unseparated_literal_suffix.txt | 18 - src/docs/unsound_collection_transmute.txt | 25 - src/docs/unused_async.txt | 23 - src/docs/unused_format_specs.txt | 24 - src/docs/unused_io_amount.txt | 31 - src/docs/unused_peekable.txt | 26 - src/docs/unused_rounding.txt | 17 - src/docs/unused_self.txt | 23 - src/docs/unused_unit.txt | 18 - src/docs/unusual_byte_groupings.txt | 12 - src/docs/unwrap_in_result.txt | 39 -- src/docs/unwrap_or_else_default.txt | 18 - src/docs/unwrap_used.txt | 37 -- src/docs/upper_case_acronyms.txt | 25 - src/docs/use_debug.txt | 12 - src/docs/use_self.txt | 31 - src/docs/used_underscore_binding.txt | 19 - src/docs/useless_asref.txt | 17 - src/docs/useless_attribute.txt | 36 - src/docs/useless_conversion.txt | 17 - src/docs/useless_format.txt | 22 - src/docs/useless_let_if_seq.txt | 39 -- src/docs/useless_transmute.txt | 12 - src/docs/useless_vec.txt | 18 - src/docs/vec_box.txt | 26 - src/docs/vec_init_then_push.txt | 23 - src/docs/vec_resize_to_zero.txt | 15 - src/docs/verbose_bit_mask.txt | 15 - src/docs/verbose_file_reads.txt | 17 - src/docs/vtable_address_comparisons.txt | 17 - src/docs/while_immutable_condition.txt | 20 - src/docs/while_let_loop.txt | 25 - src/docs/while_let_on_iterator.txt | 20 - src/docs/wildcard_dependencies.txt | 13 - src/docs/wildcard_enum_match_arm.txt | 25 - src/docs/wildcard_imports.txt | 45 -- src/docs/wildcard_in_or_patterns.txt | 22 - src/docs/write_literal.txt | 17 - src/docs/write_with_newline.txt | 18 - src/docs/writeln_empty_string.txt | 16 - src/docs/wrong_self_convention.txt | 39 -- src/docs/wrong_transmute.txt | 15 - src/docs/zero_divided_by_zero.txt | 15 - src/docs/zero_prefixed_literal.txt | 32 - src/docs/zero_ptr.txt | 16 - src/docs/zero_sized_map_values.txt | 24 - src/docs/zst_offset.txt | 11 - src/driver.rs | 55 +- src/main.rs | 4 +- .../fail_both_diff/src/main.stderr | 8 +- .../fail_both_same/src/main.stderr | 8 +- .../fail_cargo/src/main.stderr | 8 +- .../fail_clippy/src/main.stderr | 8 +- .../fail_file_attr/src/main.stderr | 8 +- tests/ui-internal/custom_ice_message.rs | 1 + tests/ui-internal/custom_ice_message.stderr | 2 +- ...unnecessary_def_path_hardcoded_path.stderr | 18 +- .../arithmetic_side_effects_allowed.rs | 11 +- .../await_holding_invalid_type.stderr | 4 +- tests/ui-toml/expect_used/expect_used.rs | 23 +- tests/ui-toml/expect_used/expect_used.stderr | 4 +- tests/ui-toml/mut_key/clippy.toml | 1 + tests/ui-toml/mut_key/mut_key.rs | 53 ++ tests/ui-toml/print_macro/clippy.toml | 1 + tests/ui-toml/print_macro/print_macro.rs | 20 + tests/ui-toml/print_macro/print_macro.stderr | 18 + .../toml_disallowed_methods/clippy.toml | 6 + .../conf_disallowed_methods.rs | 30 + .../conf_disallowed_methods.stderr | 50 +- .../toml_unknown_key/conf_unknown_key.stderr | 3 + tests/ui-toml/unwrap_used/unwrap_used.rs | 21 +- tests/ui-toml/unwrap_used/unwrap_used.stderr | 32 +- tests/ui-toml/vec_box_sized/test.rs | 5 +- tests/ui-toml/vec_box_sized/test.stderr | 6 +- tests/ui/arithmetic_side_effects.rs | 8 +- tests/ui/arithmetic_side_effects.stderr | 110 +-- tests/ui/auxiliary/doc_unsafe_macros.rs | 8 + tests/ui/blanket_clippy_restriction_lints.rs | 2 + .../blanket_clippy_restriction_lints.stderr | 27 +- tests/ui/bool_to_int_with_if.fixed | 22 + tests/ui/bool_to_int_with_if.rs | 22 + tests/ui/bool_to_int_with_if.stderr | 18 +- tests/ui/cognitive_complexity.rs | 16 + tests/ui/cognitive_complexity.stderr | 18 +- tests/ui/crashes/ice-2774.stderr | 2 +- tests/ui/crashes/ice-9746.rs | 15 + .../needless_lifetimes_impl_trait.stderr | 2 +- .../no_std_main_recursion.rs | 1 - tests/ui/doc_errors.stderr | 36 +- tests/ui/doc_unnecessary_unsafe.rs | 148 +++++ tests/ui/doc_unnecessary_unsafe.stderr | 51 ++ tests/ui/doc_unsafe.stderr | 34 +- tests/ui/eq_op.rs | 1 + tests/ui/eq_op.stderr | 56 +- tests/ui/equatable_if_let.fixed | 11 +- tests/ui/equatable_if_let.rs | 7 + tests/ui/equatable_if_let.stderr | 42 +- tests/ui/expect.stderr | 6 +- tests/ui/explicit_auto_deref.fixed | 11 + tests/ui/explicit_auto_deref.rs | 11 + tests/ui/fn_params_excessive_bools.rs | 8 + tests/ui/fn_params_excessive_bools.stderr | 22 +- tests/ui/from_raw_with_void_ptr.rs | 34 + tests/ui/from_raw_with_void_ptr.stderr | 63 ++ tests/ui/get_unwrap.stderr | 26 +- tests/ui/infallible_destructuring_match.fixed | 12 + tests/ui/infallible_destructuring_match.rs | 14 + .../ui/infallible_destructuring_match.stderr | 16 +- tests/ui/issue_4266.stderr | 4 +- tests/ui/let_underscore_drop.rs | 28 - tests/ui/let_underscore_drop.stderr | 27 - tests/ui/let_underscore_future.rs | 20 + tests/ui/let_underscore_future.stderr | 27 + tests/ui/let_underscore_lock.rs | 31 +- tests/ui/let_underscore_lock.stderr | 66 +- tests/ui/let_underscore_must_use.stderr | 24 +- tests/ui/manual_flatten.rs | 2 +- tests/ui/manual_instant_elapsed.fixed | 1 + tests/ui/manual_instant_elapsed.rs | 1 + tests/ui/manual_instant_elapsed.stderr | 4 +- tests/ui/manual_is_ascii_check.fixed | 45 ++ tests/ui/manual_is_ascii_check.rs | 45 ++ tests/ui/manual_is_ascii_check.stderr | 70 ++ tests/ui/manual_let_else.rs | 237 +++++++ tests/ui/manual_let_else.stderr | 263 ++++++++ tests/ui/manual_let_else_match.rs | 121 ++++ tests/ui/manual_let_else_match.stderr | 58 ++ tests/ui/manual_ok_or.fixed | 1 + tests/ui/manual_ok_or.rs | 1 + tests/ui/manual_ok_or.stderr | 8 +- tests/ui/map_flatten_fixable.fixed | 1 - tests/ui/map_flatten_fixable.rs | 1 - tests/ui/map_flatten_fixable.stderr | 18 +- tests/ui/match_expr_like_matches_macro.fixed | 7 +- tests/ui/match_expr_like_matches_macro.rs | 7 +- tests/ui/match_expr_like_matches_macro.stderr | 28 +- tests/ui/missing_panics_doc.stderr | 49 +- tests/ui/mut_from_ref.rs | 2 +- tests/ui/mut_mut.rs | 17 + tests/ui/mut_range_bound.rs | 2 +- tests/ui/mut_range_bound.stderr | 2 +- tests/ui/needless_borrow.fixed | 125 ++++ tests/ui/needless_borrow.rs | 125 ++++ tests/ui/needless_borrow.stderr | 8 +- tests/ui/needless_borrowed_ref.fixed | 67 +- tests/ui/needless_borrowed_ref.rs | 67 +- tests/ui/needless_borrowed_ref.stderr | 113 +++- tests/ui/needless_collect.fixed | 29 + tests/ui/needless_collect.rs | 29 + tests/ui/needless_collect.stderr | 26 +- tests/ui/needless_collect_indirect.rs | 1 + tests/ui/needless_collect_indirect.stderr | 32 +- tests/ui/needless_lifetimes.rs | 96 ++- tests/ui/needless_lifetimes.stderr | 246 +++++-- tests/ui/never_loop.rs | 21 + tests/ui/never_loop.stderr | 14 +- tests/ui/new_ret_no_self.rs | 50 ++ tests/ui/new_ret_no_self.stderr | 18 +- tests/ui/option_if_let_else.fixed | 9 + tests/ui/option_if_let_else.rs | 9 + tests/ui/or_fun_call.fixed | 16 + tests/ui/or_fun_call.rs | 16 + tests/ui/or_fun_call.stderr | 14 +- tests/ui/question_mark.fixed | 3 + tests/ui/question_mark.rs | 3 + tests/ui/question_mark.stderr | 4 +- tests/ui/rename.fixed | 8 +- tests/ui/rename.rs | 8 +- tests/ui/rename.stderr | 106 +-- tests/ui/result_large_err.rs | 12 + tests/ui/result_large_err.stderr | 30 +- tests/ui/seek_from_current.fixed | 26 + tests/ui/seek_from_current.rs | 26 + tests/ui/seek_from_current.stderr | 10 + .../ui/seek_to_start_instead_of_rewind.fixed | 137 ++++ tests/ui/seek_to_start_instead_of_rewind.rs | 137 ++++ .../ui/seek_to_start_instead_of_rewind.stderr | 22 + tests/ui/single_component_path_imports.stderr | 12 +- ...component_path_imports_nested_first.stderr | 15 +- tests/ui/string_extend.fixed | 3 + tests/ui/string_extend.rs | 3 + tests/ui/string_extend.stderr | 8 +- tests/ui/suspicious_xor_used_as_pow.rs | 34 + tests/ui/suspicious_xor_used_as_pow.stderr | 51 ++ tests/ui/swap.fixed | 9 + tests/ui/swap.rs | 9 + tests/ui/transmute.rs | 2 +- tests/ui/trivially_copy_pass_by_ref.rs | 1 + tests/ui/trivially_copy_pass_by_ref.stderr | 36 +- tests/ui/unchecked_duration_subtraction.fixed | 17 + tests/ui/unchecked_duration_subtraction.rs | 17 + .../ui/unchecked_duration_subtraction.stderr | 28 + tests/ui/undocumented_unsafe_blocks.rs | 19 + tests/ui/undocumented_unsafe_blocks.stderr | 26 +- tests/ui/unnecessary_join.stderr | 4 +- tests/ui/unused_rounding.fixed | 5 + tests/ui/unused_rounding.rs | 5 + tests/ui/unused_rounding.stderr | 8 +- tests/ui/unused_unit.fixed | 7 + tests/ui/unused_unit.rs | 7 + tests/ui/unused_unit.stderr | 40 +- tests/ui/unwrap.stderr | 6 +- tests/ui/unwrap_expect_used.stderr | 12 +- tests/ui/unwrap_or.rs | 2 +- tests/ui/use_self_trait.fixed | 41 +- tests/ui/use_self_trait.rs | 39 +- tests/ui/use_self_trait.stderr | 14 +- tests/ui/useless_attribute.fixed | 21 +- tests/ui/useless_attribute.rs | 21 +- tests/ui/useless_attribute.stderr | 6 +- tests/versioncheck.rs | 15 +- 895 files changed, 8241 insertions(+), 18373 deletions(-) create mode 100644 clippy_lints/src/declared_lints.rs create mode 100644 clippy_lints/src/from_raw_with_void_ptr.rs create mode 100644 clippy_lints/src/instant_subtraction.rs delete mode 100644 clippy_lints/src/lib.register_all.rs delete mode 100644 clippy_lints/src/lib.register_cargo.rs delete mode 100644 clippy_lints/src/lib.register_complexity.rs delete mode 100644 clippy_lints/src/lib.register_correctness.rs delete mode 100644 clippy_lints/src/lib.register_internal.rs delete mode 100644 clippy_lints/src/lib.register_lints.rs delete mode 100644 clippy_lints/src/lib.register_nursery.rs delete mode 100644 clippy_lints/src/lib.register_pedantic.rs delete mode 100644 clippy_lints/src/lib.register_perf.rs delete mode 100644 clippy_lints/src/lib.register_restriction.rs delete mode 100644 clippy_lints/src/lib.register_style.rs delete mode 100644 clippy_lints/src/lib.register_suspicious.rs delete mode 100644 clippy_lints/src/manual_instant_elapsed.rs create mode 100644 clippy_lints/src/manual_is_ascii_check.rs create mode 100644 clippy_lints/src/manual_let_else.rs rename clippy_lints/src/{loops => methods}/needless_collect.rs (62%) create mode 100644 clippy_lints/src/methods/seek_from_current.rs create mode 100644 clippy_lints/src/methods/seek_to_start_instead_of_rewind.rs create mode 100644 clippy_lints/src/suspicious_xor_used_as_pow.rs create mode 100644 declare_clippy_lint/Cargo.toml create mode 100644 declare_clippy_lint/src/lib.rs delete mode 100644 src/docs.rs delete mode 100644 src/docs/absurd_extreme_comparisons.txt delete mode 100644 src/docs/alloc_instead_of_core.txt delete mode 100644 src/docs/allow_attributes_without_reason.txt delete mode 100644 src/docs/almost_complete_letter_range.txt delete mode 100644 src/docs/almost_swapped.txt delete mode 100644 src/docs/approx_constant.txt delete mode 100644 src/docs/arithmetic_side_effects.txt delete mode 100644 src/docs/as_conversions.txt delete mode 100644 src/docs/as_ptr_cast_mut.txt delete mode 100644 src/docs/as_underscore.txt delete mode 100644 src/docs/assertions_on_constants.txt delete mode 100644 src/docs/assertions_on_result_states.txt delete mode 100644 src/docs/assign_op_pattern.txt delete mode 100644 src/docs/async_yields_async.txt delete mode 100644 src/docs/await_holding_invalid_type.txt delete mode 100644 src/docs/await_holding_lock.txt delete mode 100644 src/docs/await_holding_refcell_ref.txt delete mode 100644 src/docs/bad_bit_mask.txt delete mode 100644 src/docs/bind_instead_of_map.txt delete mode 100644 src/docs/blanket_clippy_restriction_lints.txt delete mode 100644 src/docs/blocks_in_if_conditions.txt delete mode 100644 src/docs/bool_assert_comparison.txt delete mode 100644 src/docs/bool_comparison.txt delete mode 100644 src/docs/bool_to_int_with_if.txt delete mode 100644 src/docs/borrow_as_ptr.txt delete mode 100644 src/docs/borrow_deref_ref.txt delete mode 100644 src/docs/borrow_interior_mutable_const.txt delete mode 100644 src/docs/borrowed_box.txt delete mode 100644 src/docs/box_collection.txt delete mode 100644 src/docs/box_default.txt delete mode 100644 src/docs/boxed_local.txt delete mode 100644 src/docs/branches_sharing_code.txt delete mode 100644 src/docs/builtin_type_shadow.txt delete mode 100644 src/docs/bytes_count_to_len.txt delete mode 100644 src/docs/bytes_nth.txt delete mode 100644 src/docs/cargo_common_metadata.txt delete mode 100644 src/docs/case_sensitive_file_extension_comparisons.txt delete mode 100644 src/docs/cast_abs_to_unsigned.txt delete mode 100644 src/docs/cast_enum_constructor.txt delete mode 100644 src/docs/cast_enum_truncation.txt delete mode 100644 src/docs/cast_lossless.txt delete mode 100644 src/docs/cast_nan_to_int.txt delete mode 100644 src/docs/cast_possible_truncation.txt delete mode 100644 src/docs/cast_possible_wrap.txt delete mode 100644 src/docs/cast_precision_loss.txt delete mode 100644 src/docs/cast_ptr_alignment.txt delete mode 100644 src/docs/cast_ref_to_mut.txt delete mode 100644 src/docs/cast_sign_loss.txt delete mode 100644 src/docs/cast_slice_different_sizes.txt delete mode 100644 src/docs/cast_slice_from_raw_parts.txt delete mode 100644 src/docs/char_lit_as_u8.txt delete mode 100644 src/docs/chars_last_cmp.txt delete mode 100644 src/docs/chars_next_cmp.txt delete mode 100644 src/docs/checked_conversions.txt delete mode 100644 src/docs/clone_double_ref.txt delete mode 100644 src/docs/clone_on_copy.txt delete mode 100644 src/docs/clone_on_ref_ptr.txt delete mode 100644 src/docs/cloned_instead_of_copied.txt delete mode 100644 src/docs/cmp_nan.txt delete mode 100644 src/docs/cmp_null.txt delete mode 100644 src/docs/cmp_owned.txt delete mode 100644 src/docs/cognitive_complexity.txt delete mode 100644 src/docs/collapsible_else_if.txt delete mode 100644 src/docs/collapsible_if.txt delete mode 100644 src/docs/collapsible_match.txt delete mode 100644 src/docs/collapsible_str_replace.txt delete mode 100644 src/docs/comparison_chain.txt delete mode 100644 src/docs/comparison_to_empty.txt delete mode 100644 src/docs/copy_iterator.txt delete mode 100644 src/docs/crate_in_macro_def.txt delete mode 100644 src/docs/create_dir.txt delete mode 100644 src/docs/crosspointer_transmute.txt delete mode 100644 src/docs/dbg_macro.txt delete mode 100644 src/docs/debug_assert_with_mut_call.txt delete mode 100644 src/docs/decimal_literal_representation.txt delete mode 100644 src/docs/declare_interior_mutable_const.txt delete mode 100644 src/docs/default_instead_of_iter_empty.txt delete mode 100644 src/docs/default_numeric_fallback.txt delete mode 100644 src/docs/default_trait_access.txt delete mode 100644 src/docs/default_union_representation.txt delete mode 100644 src/docs/deprecated_cfg_attr.txt delete mode 100644 src/docs/deprecated_semver.txt delete mode 100644 src/docs/deref_addrof.txt delete mode 100644 src/docs/deref_by_slicing.txt delete mode 100644 src/docs/derivable_impls.txt delete mode 100644 src/docs/derive_hash_xor_eq.txt delete mode 100644 src/docs/derive_ord_xor_partial_ord.txt delete mode 100644 src/docs/derive_partial_eq_without_eq.txt delete mode 100644 src/docs/disallowed_macros.txt delete mode 100644 src/docs/disallowed_methods.txt delete mode 100644 src/docs/disallowed_names.txt delete mode 100644 src/docs/disallowed_script_idents.txt delete mode 100644 src/docs/disallowed_types.txt delete mode 100644 src/docs/diverging_sub_expression.txt delete mode 100644 src/docs/doc_link_with_quotes.txt delete mode 100644 src/docs/doc_markdown.txt delete mode 100644 src/docs/double_comparisons.txt delete mode 100644 src/docs/double_must_use.txt delete mode 100644 src/docs/double_neg.txt delete mode 100644 src/docs/double_parens.txt delete mode 100644 src/docs/drop_copy.txt delete mode 100644 src/docs/drop_non_drop.txt delete mode 100644 src/docs/drop_ref.txt delete mode 100644 src/docs/duplicate_mod.txt delete mode 100644 src/docs/duplicate_underscore_argument.txt delete mode 100644 src/docs/duration_subsec.txt delete mode 100644 src/docs/else_if_without_else.txt delete mode 100644 src/docs/empty_drop.txt delete mode 100644 src/docs/empty_enum.txt delete mode 100644 src/docs/empty_line_after_outer_attr.txt delete mode 100644 src/docs/empty_loop.txt delete mode 100644 src/docs/empty_structs_with_brackets.txt delete mode 100644 src/docs/enum_clike_unportable_variant.txt delete mode 100644 src/docs/enum_glob_use.txt delete mode 100644 src/docs/enum_variant_names.txt delete mode 100644 src/docs/eq_op.txt delete mode 100644 src/docs/equatable_if_let.txt delete mode 100644 src/docs/erasing_op.txt delete mode 100644 src/docs/err_expect.txt delete mode 100644 src/docs/excessive_precision.txt delete mode 100644 src/docs/exhaustive_enums.txt delete mode 100644 src/docs/exhaustive_structs.txt delete mode 100644 src/docs/exit.txt delete mode 100644 src/docs/expect_fun_call.txt delete mode 100644 src/docs/expect_used.txt delete mode 100644 src/docs/expl_impl_clone_on_copy.txt delete mode 100644 src/docs/explicit_auto_deref.txt delete mode 100644 src/docs/explicit_counter_loop.txt delete mode 100644 src/docs/explicit_deref_methods.txt delete mode 100644 src/docs/explicit_into_iter_loop.txt delete mode 100644 src/docs/explicit_iter_loop.txt delete mode 100644 src/docs/explicit_write.txt delete mode 100644 src/docs/extend_with_drain.txt delete mode 100644 src/docs/extra_unused_lifetimes.txt delete mode 100644 src/docs/fallible_impl_from.txt delete mode 100644 src/docs/field_reassign_with_default.txt delete mode 100644 src/docs/filetype_is_file.txt delete mode 100644 src/docs/filter_map_identity.txt delete mode 100644 src/docs/filter_map_next.txt delete mode 100644 src/docs/filter_next.txt delete mode 100644 src/docs/flat_map_identity.txt delete mode 100644 src/docs/flat_map_option.txt delete mode 100644 src/docs/float_arithmetic.txt delete mode 100644 src/docs/float_cmp.txt delete mode 100644 src/docs/float_cmp_const.txt delete mode 100644 src/docs/float_equality_without_abs.txt delete mode 100644 src/docs/fn_address_comparisons.txt delete mode 100644 src/docs/fn_params_excessive_bools.txt delete mode 100644 src/docs/fn_to_numeric_cast.txt delete mode 100644 src/docs/fn_to_numeric_cast_any.txt delete mode 100644 src/docs/fn_to_numeric_cast_with_truncation.txt delete mode 100644 src/docs/for_kv_map.txt delete mode 100644 src/docs/forget_copy.txt delete mode 100644 src/docs/forget_non_drop.txt delete mode 100644 src/docs/forget_ref.txt delete mode 100644 src/docs/format_in_format_args.txt delete mode 100644 src/docs/format_push_string.txt delete mode 100644 src/docs/from_iter_instead_of_collect.txt delete mode 100644 src/docs/from_over_into.txt delete mode 100644 src/docs/from_str_radix_10.txt delete mode 100644 src/docs/future_not_send.txt delete mode 100644 src/docs/get_first.txt delete mode 100644 src/docs/get_last_with_len.txt delete mode 100644 src/docs/get_unwrap.txt delete mode 100644 src/docs/identity_op.txt delete mode 100644 src/docs/if_let_mutex.txt delete mode 100644 src/docs/if_not_else.txt delete mode 100644 src/docs/if_same_then_else.txt delete mode 100644 src/docs/if_then_some_else_none.txt delete mode 100644 src/docs/ifs_same_cond.txt delete mode 100644 src/docs/implicit_clone.txt delete mode 100644 src/docs/implicit_hasher.txt delete mode 100644 src/docs/implicit_return.txt delete mode 100644 src/docs/implicit_saturating_add.txt delete mode 100644 src/docs/implicit_saturating_sub.txt delete mode 100644 src/docs/imprecise_flops.txt delete mode 100644 src/docs/inconsistent_digit_grouping.txt delete mode 100644 src/docs/inconsistent_struct_constructor.txt delete mode 100644 src/docs/index_refutable_slice.txt delete mode 100644 src/docs/indexing_slicing.txt delete mode 100644 src/docs/ineffective_bit_mask.txt delete mode 100644 src/docs/inefficient_to_string.txt delete mode 100644 src/docs/infallible_destructuring_match.txt delete mode 100644 src/docs/infinite_iter.txt delete mode 100644 src/docs/inherent_to_string.txt delete mode 100644 src/docs/inherent_to_string_shadow_display.txt delete mode 100644 src/docs/init_numbered_fields.txt delete mode 100644 src/docs/inline_always.txt delete mode 100644 src/docs/inline_asm_x86_att_syntax.txt delete mode 100644 src/docs/inline_asm_x86_intel_syntax.txt delete mode 100644 src/docs/inline_fn_without_body.txt delete mode 100644 src/docs/inspect_for_each.txt delete mode 100644 src/docs/int_plus_one.txt delete mode 100644 src/docs/integer_arithmetic.txt delete mode 100644 src/docs/integer_division.txt delete mode 100644 src/docs/into_iter_on_ref.txt delete mode 100644 src/docs/invalid_null_ptr_usage.txt delete mode 100644 src/docs/invalid_regex.txt delete mode 100644 src/docs/invalid_upcast_comparisons.txt delete mode 100644 src/docs/invalid_utf8_in_unchecked.txt delete mode 100644 src/docs/invisible_characters.txt delete mode 100644 src/docs/is_digit_ascii_radix.txt delete mode 100644 src/docs/items_after_statements.txt delete mode 100644 src/docs/iter_cloned_collect.txt delete mode 100644 src/docs/iter_count.txt delete mode 100644 src/docs/iter_kv_map.txt delete mode 100644 src/docs/iter_next_loop.txt delete mode 100644 src/docs/iter_next_slice.txt delete mode 100644 src/docs/iter_not_returning_iterator.txt delete mode 100644 src/docs/iter_nth.txt delete mode 100644 src/docs/iter_nth_zero.txt delete mode 100644 src/docs/iter_on_empty_collections.txt delete mode 100644 src/docs/iter_on_single_items.txt delete mode 100644 src/docs/iter_overeager_cloned.txt delete mode 100644 src/docs/iter_skip_next.txt delete mode 100644 src/docs/iter_with_drain.txt delete mode 100644 src/docs/iterator_step_by_zero.txt delete mode 100644 src/docs/just_underscores_and_digits.txt delete mode 100644 src/docs/large_const_arrays.txt delete mode 100644 src/docs/large_digit_groups.txt delete mode 100644 src/docs/large_enum_variant.txt delete mode 100644 src/docs/large_include_file.txt delete mode 100644 src/docs/large_stack_arrays.txt delete mode 100644 src/docs/large_types_passed_by_value.txt delete mode 100644 src/docs/len_without_is_empty.txt delete mode 100644 src/docs/len_zero.txt delete mode 100644 src/docs/let_and_return.txt delete mode 100644 src/docs/let_underscore_drop.txt delete mode 100644 src/docs/let_underscore_lock.txt delete mode 100644 src/docs/let_underscore_must_use.txt delete mode 100644 src/docs/let_unit_value.txt delete mode 100644 src/docs/linkedlist.txt delete mode 100644 src/docs/lossy_float_literal.txt delete mode 100644 src/docs/macro_use_imports.txt delete mode 100644 src/docs/main_recursion.txt delete mode 100644 src/docs/manual_assert.txt delete mode 100644 src/docs/manual_async_fn.txt delete mode 100644 src/docs/manual_bits.txt delete mode 100644 src/docs/manual_clamp.txt delete mode 100644 src/docs/manual_filter.txt delete mode 100644 src/docs/manual_filter_map.txt delete mode 100644 src/docs/manual_find.txt delete mode 100644 src/docs/manual_find_map.txt delete mode 100644 src/docs/manual_flatten.txt delete mode 100644 src/docs/manual_instant_elapsed.txt delete mode 100644 src/docs/manual_map.txt delete mode 100644 src/docs/manual_memcpy.txt delete mode 100644 src/docs/manual_non_exhaustive.txt delete mode 100644 src/docs/manual_ok_or.txt delete mode 100644 src/docs/manual_range_contains.txt delete mode 100644 src/docs/manual_rem_euclid.txt delete mode 100644 src/docs/manual_retain.txt delete mode 100644 src/docs/manual_saturating_arithmetic.txt delete mode 100644 src/docs/manual_split_once.txt delete mode 100644 src/docs/manual_str_repeat.txt delete mode 100644 src/docs/manual_string_new.txt delete mode 100644 src/docs/manual_strip.txt delete mode 100644 src/docs/manual_swap.txt delete mode 100644 src/docs/manual_unwrap_or.txt delete mode 100644 src/docs/many_single_char_names.txt delete mode 100644 src/docs/map_clone.txt delete mode 100644 src/docs/map_collect_result_unit.txt delete mode 100644 src/docs/map_entry.txt delete mode 100644 src/docs/map_err_ignore.txt delete mode 100644 src/docs/map_flatten.txt delete mode 100644 src/docs/map_identity.txt delete mode 100644 src/docs/map_unwrap_or.txt delete mode 100644 src/docs/match_as_ref.txt delete mode 100644 src/docs/match_bool.txt delete mode 100644 src/docs/match_like_matches_macro.txt delete mode 100644 src/docs/match_on_vec_items.txt delete mode 100644 src/docs/match_overlapping_arm.txt delete mode 100644 src/docs/match_ref_pats.txt delete mode 100644 src/docs/match_result_ok.txt delete mode 100644 src/docs/match_same_arms.txt delete mode 100644 src/docs/match_single_binding.txt delete mode 100644 src/docs/match_str_case_mismatch.txt delete mode 100644 src/docs/match_wild_err_arm.txt delete mode 100644 src/docs/match_wildcard_for_single_variants.txt delete mode 100644 src/docs/maybe_infinite_iter.txt delete mode 100644 src/docs/mem_forget.txt delete mode 100644 src/docs/mem_replace_option_with_none.txt delete mode 100644 src/docs/mem_replace_with_default.txt delete mode 100644 src/docs/mem_replace_with_uninit.txt delete mode 100644 src/docs/min_max.txt delete mode 100644 src/docs/mismatched_target_os.txt delete mode 100644 src/docs/mismatching_type_param_order.txt delete mode 100644 src/docs/misrefactored_assign_op.txt delete mode 100644 src/docs/missing_const_for_fn.txt delete mode 100644 src/docs/missing_docs_in_private_items.txt delete mode 100644 src/docs/missing_enforced_import_renames.txt delete mode 100644 src/docs/missing_errors_doc.txt delete mode 100644 src/docs/missing_inline_in_public_items.txt delete mode 100644 src/docs/missing_panics_doc.txt delete mode 100644 src/docs/missing_safety_doc.txt delete mode 100644 src/docs/missing_spin_loop.txt delete mode 100644 src/docs/missing_trait_methods.txt delete mode 100644 src/docs/mistyped_literal_suffixes.txt delete mode 100644 src/docs/mixed_case_hex_literals.txt delete mode 100644 src/docs/mixed_read_write_in_expression.txt delete mode 100644 src/docs/mod_module_files.txt delete mode 100644 src/docs/module_inception.txt delete mode 100644 src/docs/module_name_repetitions.txt delete mode 100644 src/docs/modulo_arithmetic.txt delete mode 100644 src/docs/modulo_one.txt delete mode 100644 src/docs/multi_assignments.txt delete mode 100644 src/docs/multiple_crate_versions.txt delete mode 100644 src/docs/multiple_inherent_impl.txt delete mode 100644 src/docs/must_use_candidate.txt delete mode 100644 src/docs/must_use_unit.txt delete mode 100644 src/docs/mut_from_ref.txt delete mode 100644 src/docs/mut_mut.txt delete mode 100644 src/docs/mut_mutex_lock.txt delete mode 100644 src/docs/mut_range_bound.txt delete mode 100644 src/docs/mutable_key_type.txt delete mode 100644 src/docs/mutex_atomic.txt delete mode 100644 src/docs/mutex_integer.txt delete mode 100644 src/docs/naive_bytecount.txt delete mode 100644 src/docs/needless_arbitrary_self_type.txt delete mode 100644 src/docs/needless_bitwise_bool.txt delete mode 100644 src/docs/needless_bool.txt delete mode 100644 src/docs/needless_borrow.txt delete mode 100644 src/docs/needless_borrowed_reference.txt delete mode 100644 src/docs/needless_collect.txt delete mode 100644 src/docs/needless_continue.txt delete mode 100644 src/docs/needless_doctest_main.txt delete mode 100644 src/docs/needless_for_each.txt delete mode 100644 src/docs/needless_late_init.txt delete mode 100644 src/docs/needless_lifetimes.txt delete mode 100644 src/docs/needless_match.txt delete mode 100644 src/docs/needless_option_as_deref.txt delete mode 100644 src/docs/needless_option_take.txt delete mode 100644 src/docs/needless_parens_on_range_literals.txt delete mode 100644 src/docs/needless_pass_by_value.txt delete mode 100644 src/docs/needless_question_mark.txt delete mode 100644 src/docs/needless_range_loop.txt delete mode 100644 src/docs/needless_return.txt delete mode 100644 src/docs/needless_splitn.txt delete mode 100644 src/docs/needless_update.txt delete mode 100644 src/docs/neg_cmp_op_on_partial_ord.txt delete mode 100644 src/docs/neg_multiply.txt delete mode 100644 src/docs/negative_feature_names.txt delete mode 100644 src/docs/never_loop.txt delete mode 100644 src/docs/new_ret_no_self.txt delete mode 100644 src/docs/new_without_default.txt delete mode 100644 src/docs/no_effect.txt delete mode 100644 src/docs/no_effect_replace.txt delete mode 100644 src/docs/no_effect_underscore_binding.txt delete mode 100644 src/docs/non_ascii_literal.txt delete mode 100644 src/docs/non_octal_unix_permissions.txt delete mode 100644 src/docs/non_send_fields_in_send_ty.txt delete mode 100644 src/docs/nonminimal_bool.txt delete mode 100644 src/docs/nonsensical_open_options.txt delete mode 100644 src/docs/nonstandard_macro_braces.txt delete mode 100644 src/docs/not_unsafe_ptr_arg_deref.txt delete mode 100644 src/docs/obfuscated_if_else.txt delete mode 100644 src/docs/octal_escapes.txt delete mode 100644 src/docs/ok_expect.txt delete mode 100644 src/docs/only_used_in_recursion.txt delete mode 100644 src/docs/op_ref.txt delete mode 100644 src/docs/option_as_ref_deref.txt delete mode 100644 src/docs/option_env_unwrap.txt delete mode 100644 src/docs/option_filter_map.txt delete mode 100644 src/docs/option_if_let_else.txt delete mode 100644 src/docs/option_map_or_none.txt delete mode 100644 src/docs/option_map_unit_fn.txt delete mode 100644 src/docs/option_option.txt delete mode 100644 src/docs/or_fun_call.txt delete mode 100644 src/docs/or_then_unwrap.txt delete mode 100644 src/docs/out_of_bounds_indexing.txt delete mode 100644 src/docs/overflow_check_conditional.txt delete mode 100644 src/docs/overly_complex_bool_expr.txt delete mode 100644 src/docs/panic.txt delete mode 100644 src/docs/panic_in_result_fn.txt delete mode 100644 src/docs/panicking_unwrap.txt delete mode 100644 src/docs/partial_pub_fields.txt delete mode 100644 src/docs/partialeq_ne_impl.txt delete mode 100644 src/docs/partialeq_to_none.txt delete mode 100644 src/docs/path_buf_push_overwrite.txt delete mode 100644 src/docs/pattern_type_mismatch.txt delete mode 100644 src/docs/possible_missing_comma.txt delete mode 100644 src/docs/precedence.txt delete mode 100644 src/docs/print_in_format_impl.txt delete mode 100644 src/docs/print_literal.txt delete mode 100644 src/docs/print_stderr.txt delete mode 100644 src/docs/print_stdout.txt delete mode 100644 src/docs/print_with_newline.txt delete mode 100644 src/docs/println_empty_string.txt delete mode 100644 src/docs/ptr_arg.txt delete mode 100644 src/docs/ptr_as_ptr.txt delete mode 100644 src/docs/ptr_eq.txt delete mode 100644 src/docs/ptr_offset_with_cast.txt delete mode 100644 src/docs/pub_use.txt delete mode 100644 src/docs/question_mark.txt delete mode 100644 src/docs/range_minus_one.txt delete mode 100644 src/docs/range_plus_one.txt delete mode 100644 src/docs/range_zip_with_len.txt delete mode 100644 src/docs/rc_buffer.txt delete mode 100644 src/docs/rc_clone_in_vec_init.txt delete mode 100644 src/docs/rc_mutex.txt delete mode 100644 src/docs/read_zero_byte_vec.txt delete mode 100644 src/docs/recursive_format_impl.txt delete mode 100644 src/docs/redundant_allocation.txt delete mode 100644 src/docs/redundant_clone.txt delete mode 100644 src/docs/redundant_closure.txt delete mode 100644 src/docs/redundant_closure_call.txt delete mode 100644 src/docs/redundant_closure_for_method_calls.txt delete mode 100644 src/docs/redundant_else.txt delete mode 100644 src/docs/redundant_feature_names.txt delete mode 100644 src/docs/redundant_field_names.txt delete mode 100644 src/docs/redundant_pattern.txt delete mode 100644 src/docs/redundant_pattern_matching.txt delete mode 100644 src/docs/redundant_pub_crate.txt delete mode 100644 src/docs/redundant_slicing.txt delete mode 100644 src/docs/redundant_static_lifetimes.txt delete mode 100644 src/docs/ref_binding_to_reference.txt delete mode 100644 src/docs/ref_option_ref.txt delete mode 100644 src/docs/repeat_once.txt delete mode 100644 src/docs/rest_pat_in_fully_bound_structs.txt delete mode 100644 src/docs/result_large_err.txt delete mode 100644 src/docs/result_map_or_into_option.txt delete mode 100644 src/docs/result_map_unit_fn.txt delete mode 100644 src/docs/result_unit_err.txt delete mode 100644 src/docs/return_self_not_must_use.txt delete mode 100644 src/docs/reversed_empty_ranges.txt delete mode 100644 src/docs/same_functions_in_if_condition.txt delete mode 100644 src/docs/same_item_push.txt delete mode 100644 src/docs/same_name_method.txt delete mode 100644 src/docs/search_is_some.txt delete mode 100644 src/docs/self_assignment.txt delete mode 100644 src/docs/self_named_constructors.txt delete mode 100644 src/docs/self_named_module_files.txt delete mode 100644 src/docs/semicolon_if_nothing_returned.txt delete mode 100644 src/docs/separated_literal_suffix.txt delete mode 100644 src/docs/serde_api_misuse.txt delete mode 100644 src/docs/shadow_reuse.txt delete mode 100644 src/docs/shadow_same.txt delete mode 100644 src/docs/shadow_unrelated.txt delete mode 100644 src/docs/short_circuit_statement.txt delete mode 100644 src/docs/should_implement_trait.txt delete mode 100644 src/docs/significant_drop_in_scrutinee.txt delete mode 100644 src/docs/similar_names.txt delete mode 100644 src/docs/single_char_add_str.txt delete mode 100644 src/docs/single_char_lifetime_names.txt delete mode 100644 src/docs/single_char_pattern.txt delete mode 100644 src/docs/single_component_path_imports.txt delete mode 100644 src/docs/single_element_loop.txt delete mode 100644 src/docs/single_match.txt delete mode 100644 src/docs/single_match_else.txt delete mode 100644 src/docs/size_of_in_element_count.txt delete mode 100644 src/docs/skip_while_next.txt delete mode 100644 src/docs/slow_vector_initialization.txt delete mode 100644 src/docs/stable_sort_primitive.txt delete mode 100644 src/docs/std_instead_of_alloc.txt delete mode 100644 src/docs/std_instead_of_core.txt delete mode 100644 src/docs/str_to_string.txt delete mode 100644 src/docs/string_add.txt delete mode 100644 src/docs/string_add_assign.txt delete mode 100644 src/docs/string_extend_chars.txt delete mode 100644 src/docs/string_from_utf8_as_bytes.txt delete mode 100644 src/docs/string_lit_as_bytes.txt delete mode 100644 src/docs/string_slice.txt delete mode 100644 src/docs/string_to_string.txt delete mode 100644 src/docs/strlen_on_c_strings.txt delete mode 100644 src/docs/struct_excessive_bools.txt delete mode 100644 src/docs/suboptimal_flops.txt delete mode 100644 src/docs/suspicious_arithmetic_impl.txt delete mode 100644 src/docs/suspicious_assignment_formatting.txt delete mode 100644 src/docs/suspicious_else_formatting.txt delete mode 100644 src/docs/suspicious_map.txt delete mode 100644 src/docs/suspicious_op_assign_impl.txt delete mode 100644 src/docs/suspicious_operation_groupings.txt delete mode 100644 src/docs/suspicious_splitn.txt delete mode 100644 src/docs/suspicious_to_owned.txt delete mode 100644 src/docs/suspicious_unary_op_formatting.txt delete mode 100644 src/docs/swap_ptr_to_ref.txt delete mode 100644 src/docs/tabs_in_doc_comments.txt delete mode 100644 src/docs/temporary_assignment.txt delete mode 100644 src/docs/to_digit_is_some.txt delete mode 100644 src/docs/to_string_in_format_args.txt delete mode 100644 src/docs/todo.txt delete mode 100644 src/docs/too_many_arguments.txt delete mode 100644 src/docs/too_many_lines.txt delete mode 100644 src/docs/toplevel_ref_arg.txt delete mode 100644 src/docs/trailing_empty_array.txt delete mode 100644 src/docs/trait_duplication_in_bounds.txt delete mode 100644 src/docs/transmute_bytes_to_str.txt delete mode 100644 src/docs/transmute_float_to_int.txt delete mode 100644 src/docs/transmute_int_to_bool.txt delete mode 100644 src/docs/transmute_int_to_char.txt delete mode 100644 src/docs/transmute_int_to_float.txt delete mode 100644 src/docs/transmute_num_to_bytes.txt delete mode 100644 src/docs/transmute_ptr_to_ptr.txt delete mode 100644 src/docs/transmute_ptr_to_ref.txt delete mode 100644 src/docs/transmute_undefined_repr.txt delete mode 100644 src/docs/transmutes_expressible_as_ptr_casts.txt delete mode 100644 src/docs/transmuting_null.txt delete mode 100644 src/docs/trim_split_whitespace.txt delete mode 100644 src/docs/trivial_regex.txt delete mode 100644 src/docs/trivially_copy_pass_by_ref.txt delete mode 100644 src/docs/try_err.txt delete mode 100644 src/docs/type_complexity.txt delete mode 100644 src/docs/type_repetition_in_bounds.txt delete mode 100644 src/docs/undocumented_unsafe_blocks.txt delete mode 100644 src/docs/undropped_manually_drops.txt delete mode 100644 src/docs/unicode_not_nfc.txt delete mode 100644 src/docs/unimplemented.txt delete mode 100644 src/docs/uninit_assumed_init.txt delete mode 100644 src/docs/uninit_vec.txt delete mode 100644 src/docs/uninlined_format_args.txt delete mode 100644 src/docs/unit_arg.txt delete mode 100644 src/docs/unit_cmp.txt delete mode 100644 src/docs/unit_hash.txt delete mode 100644 src/docs/unit_return_expecting_ord.txt delete mode 100644 src/docs/unnecessary_cast.txt delete mode 100644 src/docs/unnecessary_filter_map.txt delete mode 100644 src/docs/unnecessary_find_map.txt delete mode 100644 src/docs/unnecessary_fold.txt delete mode 100644 src/docs/unnecessary_join.txt delete mode 100644 src/docs/unnecessary_lazy_evaluations.txt delete mode 100644 src/docs/unnecessary_mut_passed.txt delete mode 100644 src/docs/unnecessary_operation.txt delete mode 100644 src/docs/unnecessary_owned_empty_strings.txt delete mode 100644 src/docs/unnecessary_self_imports.txt delete mode 100644 src/docs/unnecessary_sort_by.txt delete mode 100644 src/docs/unnecessary_to_owned.txt delete mode 100644 src/docs/unnecessary_unwrap.txt delete mode 100644 src/docs/unnecessary_wraps.txt delete mode 100644 src/docs/unneeded_field_pattern.txt delete mode 100644 src/docs/unneeded_wildcard_pattern.txt delete mode 100644 src/docs/unnested_or_patterns.txt delete mode 100644 src/docs/unreachable.txt delete mode 100644 src/docs/unreadable_literal.txt delete mode 100644 src/docs/unsafe_derive_deserialize.txt delete mode 100644 src/docs/unsafe_removed_from_name.txt delete mode 100644 src/docs/unseparated_literal_suffix.txt delete mode 100644 src/docs/unsound_collection_transmute.txt delete mode 100644 src/docs/unused_async.txt delete mode 100644 src/docs/unused_format_specs.txt delete mode 100644 src/docs/unused_io_amount.txt delete mode 100644 src/docs/unused_peekable.txt delete mode 100644 src/docs/unused_rounding.txt delete mode 100644 src/docs/unused_self.txt delete mode 100644 src/docs/unused_unit.txt delete mode 100644 src/docs/unusual_byte_groupings.txt delete mode 100644 src/docs/unwrap_in_result.txt delete mode 100644 src/docs/unwrap_or_else_default.txt delete mode 100644 src/docs/unwrap_used.txt delete mode 100644 src/docs/upper_case_acronyms.txt delete mode 100644 src/docs/use_debug.txt delete mode 100644 src/docs/use_self.txt delete mode 100644 src/docs/used_underscore_binding.txt delete mode 100644 src/docs/useless_asref.txt delete mode 100644 src/docs/useless_attribute.txt delete mode 100644 src/docs/useless_conversion.txt delete mode 100644 src/docs/useless_format.txt delete mode 100644 src/docs/useless_let_if_seq.txt delete mode 100644 src/docs/useless_transmute.txt delete mode 100644 src/docs/useless_vec.txt delete mode 100644 src/docs/vec_box.txt delete mode 100644 src/docs/vec_init_then_push.txt delete mode 100644 src/docs/vec_resize_to_zero.txt delete mode 100644 src/docs/verbose_bit_mask.txt delete mode 100644 src/docs/verbose_file_reads.txt delete mode 100644 src/docs/vtable_address_comparisons.txt delete mode 100644 src/docs/while_immutable_condition.txt delete mode 100644 src/docs/while_let_loop.txt delete mode 100644 src/docs/while_let_on_iterator.txt delete mode 100644 src/docs/wildcard_dependencies.txt delete mode 100644 src/docs/wildcard_enum_match_arm.txt delete mode 100644 src/docs/wildcard_imports.txt delete mode 100644 src/docs/wildcard_in_or_patterns.txt delete mode 100644 src/docs/write_literal.txt delete mode 100644 src/docs/write_with_newline.txt delete mode 100644 src/docs/writeln_empty_string.txt delete mode 100644 src/docs/wrong_self_convention.txt delete mode 100644 src/docs/wrong_transmute.txt delete mode 100644 src/docs/zero_divided_by_zero.txt delete mode 100644 src/docs/zero_prefixed_literal.txt delete mode 100644 src/docs/zero_ptr.txt delete mode 100644 src/docs/zero_sized_map_values.txt delete mode 100644 src/docs/zst_offset.txt create mode 100644 tests/ui-toml/mut_key/clippy.toml create mode 100644 tests/ui-toml/mut_key/mut_key.rs create mode 100644 tests/ui-toml/print_macro/clippy.toml create mode 100644 tests/ui-toml/print_macro/print_macro.rs create mode 100644 tests/ui-toml/print_macro/print_macro.stderr create mode 100644 tests/ui/crashes/ice-9746.rs create mode 100644 tests/ui/doc_unnecessary_unsafe.rs create mode 100644 tests/ui/doc_unnecessary_unsafe.stderr create mode 100644 tests/ui/from_raw_with_void_ptr.rs create mode 100644 tests/ui/from_raw_with_void_ptr.stderr delete mode 100644 tests/ui/let_underscore_drop.rs delete mode 100644 tests/ui/let_underscore_drop.stderr create mode 100644 tests/ui/let_underscore_future.rs create mode 100644 tests/ui/let_underscore_future.stderr create mode 100644 tests/ui/manual_is_ascii_check.fixed create mode 100644 tests/ui/manual_is_ascii_check.rs create mode 100644 tests/ui/manual_is_ascii_check.stderr create mode 100644 tests/ui/manual_let_else.rs create mode 100644 tests/ui/manual_let_else.stderr create mode 100644 tests/ui/manual_let_else_match.rs create mode 100644 tests/ui/manual_let_else_match.stderr create mode 100644 tests/ui/seek_from_current.fixed create mode 100644 tests/ui/seek_from_current.rs create mode 100644 tests/ui/seek_from_current.stderr create mode 100644 tests/ui/seek_to_start_instead_of_rewind.fixed create mode 100644 tests/ui/seek_to_start_instead_of_rewind.rs create mode 100644 tests/ui/seek_to_start_instead_of_rewind.stderr create mode 100644 tests/ui/suspicious_xor_used_as_pow.rs create mode 100644 tests/ui/suspicious_xor_used_as_pow.stderr create mode 100644 tests/ui/unchecked_duration_subtraction.fixed create mode 100644 tests/ui/unchecked_duration_subtraction.rs create mode 100644 tests/ui/unchecked_duration_subtraction.stderr diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d7bda27e4fcc..6f1f73c1fd2fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,13 +4,157 @@ All notable changes to this project will be documented in this file. See [Changelog Update](book/src/development/infrastructure/changelog_update.md) if you want to update this document. -## Unreleased / In Rust Nightly +## Unreleased / Beta / In Rust Nightly -[3c7e7dbc...master](https://github.com/rust-lang/rust-clippy/compare/3c7e7dbc...master) +[b52fb523...master](https://github.com/rust-lang/rust-clippy/compare/b52fb523...master) + +## Rust 1.65 + +Current stable, released 2022-11-03 + +[3c7e7dbc...b52fb523](https://github.com/rust-lang/rust-clippy/compare/3c7e7dbc...b52fb523) + +### Important Changes + +* Clippy now has an `--explain ` command to show the lint description in the console + [#8952](https://github.com/rust-lang/rust-clippy/pull/8952) + +### New Lints + +* [`unused_peekable`] + [#9258](https://github.com/rust-lang/rust-clippy/pull/9258) +* [`collapsible_str_replace`] + [#9269](https://github.com/rust-lang/rust-clippy/pull/9269) +* [`manual_string_new`] + [#9295](https://github.com/rust-lang/rust-clippy/pull/9295) +* [`iter_on_empty_collections`] + [#9187](https://github.com/rust-lang/rust-clippy/pull/9187) +* [`iter_on_single_items`] + [#9187](https://github.com/rust-lang/rust-clippy/pull/9187) +* [`bool_to_int_with_if`] + [#9412](https://github.com/rust-lang/rust-clippy/pull/9412) +* [`multi_assignments`] + [#9379](https://github.com/rust-lang/rust-clippy/pull/9379) +* [`result_large_err`] + [#9373](https://github.com/rust-lang/rust-clippy/pull/9373) +* [`partialeq_to_none`] + [#9288](https://github.com/rust-lang/rust-clippy/pull/9288) +* [`suspicious_to_owned`] + [#8984](https://github.com/rust-lang/rust-clippy/pull/8984) +* [`cast_slice_from_raw_parts`] + [#9247](https://github.com/rust-lang/rust-clippy/pull/9247) +* [`manual_instant_elapsed`] + [#9264](https://github.com/rust-lang/rust-clippy/pull/9264) + +### Moves and Deprecations + +* Moved [`significant_drop_in_scrutinee`] to `nursery` (now allow-by-default) + [#9302](https://github.com/rust-lang/rust-clippy/pull/9302) +* Rename `logic_bug` to [`overly_complex_bool_expr`] + [#9306](https://github.com/rust-lang/rust-clippy/pull/9306) +* Rename `arithmetic` to [`arithmetic_side_effects`] + [#9443](https://github.com/rust-lang/rust-clippy/pull/9443) +* Moved [`only_used_in_recursion`] to complexity (now warn-by-default) + [#8804](https://github.com/rust-lang/rust-clippy/pull/8804) +* Moved [`assertions_on_result_states`] to restriction (now allow-by-default) + [#9273](https://github.com/rust-lang/rust-clippy/pull/9273) +* Renamed `blacklisted_name` to [`disallowed_names`] + [#8974](https://github.com/rust-lang/rust-clippy/pull/8974) + +### Enhancements + +* [`option_if_let_else`]: Now also checks for match expressions + [#8696](https://github.com/rust-lang/rust-clippy/pull/8696) +* [`explicit_auto_deref`]: Now lints on implicit returns in closures + [#9126](https://github.com/rust-lang/rust-clippy/pull/9126) +* [`needless_borrow`]: Now considers trait implementations + [#9136](https://github.com/rust-lang/rust-clippy/pull/9136) +* [`suboptimal_flops`], [`imprecise_flops`]: Now lint on constant expressions + [#9404](https://github.com/rust-lang/rust-clippy/pull/9404) +* [`if_let_mutex`]: Now detects mutex behind references and warns about deadlocks + [#9318](https://github.com/rust-lang/rust-clippy/pull/9318) + +### False Positive Fixes + +* [`unit_arg`] [`default_trait_access`] [`missing_docs_in_private_items`]: No longer + trigger in code generated from proc-macros + [#8694](https://github.com/rust-lang/rust-clippy/pull/8694) +* [`unwrap_used`]: Now lints uses of `unwrap_err` + [#9338](https://github.com/rust-lang/rust-clippy/pull/9338) +* [`expect_used`]: Now lints uses of `expect_err` + [#9338](https://github.com/rust-lang/rust-clippy/pull/9338) +* [`transmute_undefined_repr`]: Now longer lints if the first field is compatible + with the other type + [#9287](https://github.com/rust-lang/rust-clippy/pull/9287) +* [`unnecessary_to_owned`]: No longer lints, if type change cased errors in + the caller function + [#9424](https://github.com/rust-lang/rust-clippy/pull/9424) +* [`match_like_matches_macro`]: No longer lints, if there are comments inside the + match expression + [#9276](https://github.com/rust-lang/rust-clippy/pull/9276) +* [`partialeq_to_none`]: No longer trigger in code generated from macros + [#9389](https://github.com/rust-lang/rust-clippy/pull/9389) +* [`arithmetic_side_effects`]: No longer lints expressions that only use literals + [#9365](https://github.com/rust-lang/rust-clippy/pull/9365) +* [`explicit_auto_deref`]: Now ignores references on block expressions when the type + is `Sized`, on `dyn Trait` returns and when the suggestion is non-trivial + [#9126](https://github.com/rust-lang/rust-clippy/pull/9126) +* [`trait_duplication_in_bounds`]: Now better tracks bounds to avoid false positives + [#9167](https://github.com/rust-lang/rust-clippy/pull/9167) +* [`format_in_format_args`]: Now suggests cases where the result is formatted again + [#9349](https://github.com/rust-lang/rust-clippy/pull/9349) +* [`only_used_in_recursion`]: No longer lints on function without recursions and + takes external functions into account + [#8804](https://github.com/rust-lang/rust-clippy/pull/8804) +* [`missing_const_for_fn`]: No longer lints in proc-macros + [#9308](https://github.com/rust-lang/rust-clippy/pull/9308) +* [`non_ascii_literal`]: Allow non-ascii comments in tests and make sure `#[allow]` + attributes work in tests + [#9327](https://github.com/rust-lang/rust-clippy/pull/9327) +* [`question_mark`]: No longer lint `if let`s with subpatterns + [#9348](https://github.com/rust-lang/rust-clippy/pull/9348) +* [`needless_collect`]: No longer lints in loops + [#8992](https://github.com/rust-lang/rust-clippy/pull/8992) +* [`mut_mutex_lock`]: No longer lints if the mutex is behind an immutable reference + [#9418](https://github.com/rust-lang/rust-clippy/pull/9418) +* [`needless_return`]: Now ignores returns with arguments + [#9381](https://github.com/rust-lang/rust-clippy/pull/9381) +* [`range_plus_one`], [`range_minus_one`]: Now ignores code with macros + [#9446](https://github.com/rust-lang/rust-clippy/pull/9446) +* [`assertions_on_result_states`]: No longer lints on the unit type + [#9273](https://github.com/rust-lang/rust-clippy/pull/9273) + +### Suggestion Fixes/Improvements + +* [`unwrap_or_else_default`]: Now suggests `unwrap_or_default()` for empty strings + [#9421](https://github.com/rust-lang/rust-clippy/pull/9421) +* [`if_then_some_else_none`]: Now also suggests `bool::then_some` + [#9289](https://github.com/rust-lang/rust-clippy/pull/9289) +* [`redundant_closure_call`]: The suggestion now works for async closures + [#9053](https://github.com/rust-lang/rust-clippy/pull/9053) +* [`suboptimal_flops`]: Now suggests parenthesis when they are required + [#9394](https://github.com/rust-lang/rust-clippy/pull/9394) +* [`case_sensitive_file_extension_comparisons`]: Now suggests `map_or(..)` instead of `map(..).unwrap_or` + [#9341](https://github.com/rust-lang/rust-clippy/pull/9341) +* Deprecated configuration values can now be updated automatically + [#9252](https://github.com/rust-lang/rust-clippy/pull/9252) +* [`or_fun_call`]: Now suggest `Entry::or_default` for `Entry::or_insert(Default::default())` + [#9342](https://github.com/rust-lang/rust-clippy/pull/9342) +* [`unwrap_used`]: Only suggests `expect` if [`expect_used`] is allowed + [#9223](https://github.com/rust-lang/rust-clippy/pull/9223) + +### ICE Fixes + +* Fix ICE in [`useless_format`] for literals + [#9406](https://github.com/rust-lang/rust-clippy/pull/9406) +* Fix infinite loop in [`vec_init_then_push`] + [#9441](https://github.com/rust-lang/rust-clippy/pull/9441) +* Fix ICE when reading literals with weird proc-macro spans + [#9303](https://github.com/rust-lang/rust-clippy/pull/9303) ## Rust 1.64 -Current stable, released 2022-09-22 +Released 2022-09-22 [d7b5cbf0...3c7e7dbc](https://github.com/rust-lang/rust-clippy/compare/d7b5cbf0...3c7e7dbc) @@ -3903,6 +4047,7 @@ Released 2018-09-13 [`format_push_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#format_push_string [`from_iter_instead_of_collect`]: https://rust-lang.github.io/rust-clippy/master/index.html#from_iter_instead_of_collect [`from_over_into`]: https://rust-lang.github.io/rust-clippy/master/index.html#from_over_into +[`from_raw_with_void_ptr`]: https://rust-lang.github.io/rust-clippy/master/index.html#from_raw_with_void_ptr [`from_str_radix_10`]: https://rust-lang.github.io/rust-clippy/master/index.html#from_str_radix_10 [`future_not_send`]: https://rust-lang.github.io/rust-clippy/master/index.html#future_not_send [`get_first`]: https://rust-lang.github.io/rust-clippy/master/index.html#get_first @@ -3978,6 +4123,7 @@ Released 2018-09-13 [`len_zero`]: https://rust-lang.github.io/rust-clippy/master/index.html#len_zero [`let_and_return`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return [`let_underscore_drop`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_underscore_drop +[`let_underscore_future`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_underscore_future [`let_underscore_lock`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_underscore_lock [`let_underscore_must_use`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_underscore_must_use [`let_unit_value`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value @@ -3996,6 +4142,8 @@ Released 2018-09-13 [`manual_find_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_find_map [`manual_flatten`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_flatten [`manual_instant_elapsed`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_instant_elapsed +[`manual_is_ascii_check`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_is_ascii_check +[`manual_let_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_let_else [`manual_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_map [`manual_memcpy`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_memcpy [`manual_non_exhaustive`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_non_exhaustive @@ -4198,6 +4346,8 @@ Released 2018-09-13 [`same_item_push`]: https://rust-lang.github.io/rust-clippy/master/index.html#same_item_push [`same_name_method`]: https://rust-lang.github.io/rust-clippy/master/index.html#same_name_method [`search_is_some`]: https://rust-lang.github.io/rust-clippy/master/index.html#search_is_some +[`seek_from_current`]: https://rust-lang.github.io/rust-clippy/master/index.html#seek_from_current +[`seek_to_start_instead_of_rewind`]: https://rust-lang.github.io/rust-clippy/master/index.html#seek_to_start_instead_of_rewind [`self_assignment`]: https://rust-lang.github.io/rust-clippy/master/index.html#self_assignment [`self_named_constructors`]: https://rust-lang.github.io/rust-clippy/master/index.html#self_named_constructors [`self_named_module_files`]: https://rust-lang.github.io/rust-clippy/master/index.html#self_named_module_files @@ -4247,6 +4397,7 @@ Released 2018-09-13 [`suspicious_splitn`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_splitn [`suspicious_to_owned`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_to_owned [`suspicious_unary_op_formatting`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_unary_op_formatting +[`suspicious_xor_used_as_pow`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_xor_used_as_pow [`swap_ptr_to_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#swap_ptr_to_ref [`tabs_in_doc_comments`]: https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments [`temporary_assignment`]: https://rust-lang.github.io/rust-clippy/master/index.html#temporary_assignment @@ -4277,6 +4428,7 @@ Released 2018-09-13 [`try_err`]: https://rust-lang.github.io/rust-clippy/master/index.html#try_err [`type_complexity`]: https://rust-lang.github.io/rust-clippy/master/index.html#type_complexity [`type_repetition_in_bounds`]: https://rust-lang.github.io/rust-clippy/master/index.html#type_repetition_in_bounds +[`unchecked_duration_subtraction`]: https://rust-lang.github.io/rust-clippy/master/index.html#unchecked_duration_subtraction [`undocumented_unsafe_blocks`]: https://rust-lang.github.io/rust-clippy/master/index.html#undocumented_unsafe_blocks [`undropped_manually_drops`]: https://rust-lang.github.io/rust-clippy/master/index.html#undropped_manually_drops [`unicode_not_nfc`]: https://rust-lang.github.io/rust-clippy/master/index.html#unicode_not_nfc @@ -4298,6 +4450,7 @@ Released 2018-09-13 [`unnecessary_mut_passed`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_mut_passed [`unnecessary_operation`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation [`unnecessary_owned_empty_strings`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_owned_empty_strings +[`unnecessary_safety_doc`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_safety_doc [`unnecessary_self_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_self_imports [`unnecessary_sort_by`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_sort_by [`unnecessary_to_owned`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_to_owned diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index dec13e44a17f8..e3708bc485399 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -1,70 +1,3 @@ # The Rust Code of Conduct -A version of this document [can be found online](https://www.rust-lang.org/conduct.html). - -## Conduct - -**Contact**: [rust-mods@rust-lang.org](mailto:rust-mods@rust-lang.org) - -* We are committed to providing a friendly, safe and welcoming environment for all, regardless of level of experience, - gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, - religion, nationality, or other similar characteristic. -* On IRC, please avoid using overtly sexual nicknames or other nicknames that might detract from a friendly, safe and - welcoming environment for all. -* Please be kind and courteous. There's no need to be mean or rude. -* Respect that people have differences of opinion and that every design or implementation choice carries a trade-off and - numerous costs. There is seldom a right answer. -* Please keep unstructured critique to a minimum. If you have solid ideas you want to experiment with, make a fork and - see how it works. -* We will exclude you from interaction if you insult, demean or harass anyone. That is not welcome behavior. We - interpret the term "harassment" as including the definition in the Citizen - Code of Conduct; if you have any lack of clarity about what might be included in that concept, please read their - definition. In particular, we don't tolerate behavior that excludes people in socially marginalized groups. -* Private harassment is also unacceptable. No matter who you are, if you feel you have been or are being harassed or - made uncomfortable by a community member, please contact one of the channel ops or any of the [Rust moderation - team][mod_team] immediately. Whether you're a regular contributor or a newcomer, we care about making this community a - safe place for you and we've got your back. -* Likewise any spamming, trolling, flaming, baiting or other attention-stealing behavior is not welcome. - -## Moderation - - -These are the policies for upholding our community's standards of conduct. If you feel that a thread needs moderation, -please contact the [Rust moderation team][mod_team]. - -1. Remarks that violate the Rust standards of conduct, including hateful, hurtful, oppressive, or exclusionary remarks, - are not allowed. (Cursing is allowed, but never targeting another user, and never in a hateful manner.) -2. Remarks that moderators find inappropriate, whether listed in the code of conduct or not, are also not allowed. -3. Moderators will first respond to such remarks with a warning. -4. If the warning is unheeded, the user will be "kicked," i.e., kicked out of the communication channel to cool off. -5. If the user comes back and continues to make trouble, they will be banned, i.e., indefinitely excluded. -6. Moderators may choose at their discretion to un-ban the user if it was a first offense and they offer the offended - party a genuine apology. -7. If a moderator bans someone and you think it was unjustified, please take it up with that moderator, or with a - different moderator, **in private**. Complaints about bans in-channel are not allowed. -8. Moderators are held to a higher standard than other community members. If a moderator creates an inappropriate - situation, they should expect less leeway than others. - -In the Rust community we strive to go the extra step to look out for each other. Don't just aim to be technically -unimpeachable, try to be your best self. In particular, avoid flirting with offensive or sensitive issues, particularly -if they're off-topic; this all too often leads to unnecessary fights, hurt feelings, and damaged trust; worse, it can -drive people away from the community entirely. - -And if someone takes issue with something you said or did, resist the urge to be defensive. Just stop doing what it was -they complained about and apologize. Even if you feel you were misinterpreted or unfairly accused, chances are good -there was something you could've communicated better — remember that it's your responsibility to make your fellow -Rustaceans comfortable. Everyone wants to get along and we are all here first and foremost because we want to talk about -cool technology. You will find that people will be eager to assume good intent and forgive as long as you earn their -trust. - -The enforcement policies listed above apply to all official Rust venues; including official IRC channels (#rust, -#rust-internals, #rust-tools, #rust-libs, #rustc, #rust-beginners, #rust-docs, #rust-community, #rust-lang, and #cargo); -GitHub repositories under rust-lang, rust-lang-nursery, and rust-lang-deprecated; and all forums under rust-lang.org -(users.rust-lang.org, internals.rust-lang.org). For other projects adopting the Rust Code of Conduct, please contact the -maintainers of those projects for enforcement. If you wish to use this code of conduct for your own project, consider -explicitly mentioning your moderation policy or making a copy with your own moderation policy so as to avoid confusion. - -*Adapted from the [Node.js Policy on Trolling](http://blog.izs.me/post/30036893703/policy-on-trolling) as well as the -[Contributor Covenant v1.3.0](https://www.contributor-covenant.org/version/1/3/0/).* - -[mod_team]: https://www.rust-lang.org/team.html#Moderation-team +The Code of Conduct for this repository [can be found online](https://www.rust-lang.org/conduct.html). diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 85f94a74ad91d..3158080d2b309 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -23,6 +23,7 @@ All contributors are expected to follow the [Rust Code of Conduct]. - [Issue and PR triage](#issue-and-pr-triage) - [Bors and Homu](#bors-and-homu) - [Contributions](#contributions) + - [License](#license) [Zulip]: https://rust-lang.zulipchat.com/#narrow/stream/clippy [Rust Code of Conduct]: https://www.rust-lang.org/policies/code-of-conduct @@ -245,6 +246,38 @@ Contributions to Clippy should be made in the form of GitHub pull requests. Each be reviewed by a core contributor (someone with permission to land patches) and either landed in the main tree or given feedback for changes that would be required. +All PRs should include a `changelog` entry with a short comment explaining the change. The rule of thumb is basically, +"what do you believe is important from an outsider's perspective?" Often, PRs are only related to a single property of a +lint, and then it's good to mention that one. Otherwise, it's better to include too much detail than too little. + +Clippy's [changelog] is created from these comments. Every release, someone gets all commits from bors with a +`changelog: XYZ` entry and combines them into the changelog. This is a manual process. + +Examples: +- New lint + ``` + changelog: new lint: [`missing_trait_methods`] + ``` +- False positive fix + ``` + changelog: Fix [`unused_peekable`] false positive when peeked in a closure or called as `f(&mut peekable)` + ``` +- Purely internal change + ``` + changelog: none + ``` + +Note this it is fine for a PR to include multiple `changelog` entries, e.g.: +``` +changelog: Something 1 +changelog: Something 2 +changelog: Something 3 +``` + +[changelog]: CHANGELOG.md + +## License + All code in this repository is under the [Apache-2.0] or the [MIT] license. diff --git a/Cargo.toml b/Cargo.toml index 60200a88b8582..fe425a2fb991f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "clippy" -version = "0.1.66" +version = "0.1.67" description = "A bunch of helpful lints to avoid common pitfalls in Rust" repository = "https://github.com/rust-lang/rust-clippy" readme = "README.md" diff --git a/README.md b/README.md index a8a6b86d2a15b..f74de7de42b8b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Clippy -[![Clippy Test](https://github.com/rust-lang/rust-clippy/workflows/Clippy%20Test/badge.svg?branch=auto&event=push)](https://github.com/rust-lang/rust-clippy/actions?query=workflow%3A%22Clippy+Test%22+event%3Apush+branch%3Aauto) +[![Clippy Test](https://github.com/rust-lang/rust-clippy/workflows/Clippy%20Test%20(bors)/badge.svg?branch=auto&event=push)](https://github.com/rust-lang/rust-clippy/actions?query=workflow%3A%22Clippy+Test+(bors)%22+event%3Apush+branch%3Aauto) [![License: MIT OR Apache-2.0](https://img.shields.io/crates/l/clippy.svg)](#license) A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code. @@ -204,12 +204,6 @@ lints can be configured and the meaning of the variables. > > `clippy.toml` or `.clippy.toml` cannot be used to allow/deny lints. -> **Note** -> -> Configuration changes will not apply for code that has already been compiled and cached under `./target/`; -> for example, adding a new string to `doc-valid-idents` may still result in Clippy flagging that string. To be sure -> that any configuration changes are applied, you may want to run `cargo clean` and re-compile your crate from scratch. - To deactivate the “for further information visit *lint-link*” message you can define the `CLIPPY_DISABLE_DOCS_LINKS` environment variable. diff --git a/clippy_dev/Cargo.toml b/clippy_dev/Cargo.toml index 2ac3b4fe2ed4f..510c7e852af6e 100644 --- a/clippy_dev/Cargo.toml +++ b/clippy_dev/Cargo.toml @@ -10,7 +10,6 @@ indoc = "1.0" itertools = "0.10.1" opener = "0.5" shell-escape = "0.1" -tempfile = "3.2" walkdir = "2.3" [features] diff --git a/clippy_dev/src/lint.rs b/clippy_dev/src/lint.rs index 71005449b4ddf..aafd0f71a59bc 100644 --- a/clippy_dev/src/lint.rs +++ b/clippy_dev/src/lint.rs @@ -36,20 +36,12 @@ pub fn run<'a>(path: &str, args: impl Iterator) { } else { exit_if_err(Command::new("cargo").arg("build").status()); - // Run in a tempdir as changes to clippy do not retrigger linting - let target = tempfile::Builder::new() - .prefix("clippy") - .tempdir() - .expect("failed to create tempdir"); - let status = Command::new(cargo_clippy_path()) .arg("clippy") .args(args) .current_dir(path) - .env("CARGO_TARGET_DIR", target.as_ref()) .status(); - target.close().expect("failed to remove tempdir"); exit_if_err(status); } } diff --git a/clippy_dev/src/update_lints.rs b/clippy_dev/src/update_lints.rs index e690bc369cd43..837618c9294b7 100644 --- a/clippy_dev/src/update_lints.rs +++ b/clippy_dev/src/update_lints.rs @@ -3,7 +3,7 @@ use aho_corasick::AhoCorasickBuilder; use indoc::writedoc; use itertools::Itertools; use rustc_lexer::{tokenize, unescape, LiteralKind, TokenKind}; -use std::collections::{BTreeSet, HashMap, HashSet}; +use std::collections::{HashMap, HashSet}; use std::ffi::OsStr; use std::fmt::Write; use std::fs::{self, OpenOptions}; @@ -36,6 +36,60 @@ pub enum UpdateMode { pub fn update(update_mode: UpdateMode) { let (lints, deprecated_lints, renamed_lints) = gather_all(); generate_lint_files(update_mode, &lints, &deprecated_lints, &renamed_lints); + remove_old_files(update_mode); +} + +/// Remove files no longer needed after +/// that may be reintroduced unintentionally +/// +/// FIXME: This is a temporary measure that should be removed when there are no more PRs that +/// include the stray files +fn remove_old_files(update_mode: UpdateMode) { + let mut failed = false; + let mut remove_file = |path: &Path| match update_mode { + UpdateMode::Check => { + if path.exists() { + failed = true; + println!("unexpected file: {}", path.display()); + } + }, + UpdateMode::Change => { + if fs::remove_file(path).is_ok() { + println!("removed file: {}", path.display()); + } + }, + }; + + let files = [ + "clippy_lints/src/lib.register_all.rs", + "clippy_lints/src/lib.register_cargo.rs", + "clippy_lints/src/lib.register_complexity.rs", + "clippy_lints/src/lib.register_correctness.rs", + "clippy_lints/src/lib.register_internal.rs", + "clippy_lints/src/lib.register_lints.rs", + "clippy_lints/src/lib.register_nursery.rs", + "clippy_lints/src/lib.register_pedantic.rs", + "clippy_lints/src/lib.register_perf.rs", + "clippy_lints/src/lib.register_restriction.rs", + "clippy_lints/src/lib.register_style.rs", + "clippy_lints/src/lib.register_suspicious.rs", + "src/docs.rs", + ]; + + for file in files { + remove_file(Path::new(file)); + } + + if let Ok(docs_dir) = fs::read_dir("src/docs") { + for doc_file in docs_dir { + let path = doc_file.unwrap().path(); + remove_file(&path); + } + } + + if failed { + exit_with_failure(); + } } fn generate_lint_files( @@ -104,9 +158,9 @@ fn generate_lint_files( ); process_file( - "clippy_lints/src/lib.register_lints.rs", + "clippy_lints/src/declared_lints.rs", update_mode, - &gen_register_lint_list(internal_lints.iter(), usable_lints.iter()), + &gen_declared_lints(internal_lints.iter(), usable_lints.iter()), ); process_file( "clippy_lints/src/lib.deprecated.rs", @@ -114,26 +168,6 @@ fn generate_lint_files( &gen_deprecated(deprecated_lints), ); - let all_group_lints = usable_lints.iter().filter(|l| { - matches!( - &*l.group, - "correctness" | "suspicious" | "style" | "complexity" | "perf" - ) - }); - let content = gen_lint_group_list("all", all_group_lints); - process_file("clippy_lints/src/lib.register_all.rs", update_mode, &content); - - update_docs(update_mode, &usable_lints); - - for (lint_group, lints) in Lint::by_lint_group(usable_lints.into_iter().chain(internal_lints)) { - let content = gen_lint_group_list(&lint_group, lints.iter()); - process_file( - format!("clippy_lints/src/lib.register_{lint_group}.rs"), - update_mode, - &content, - ); - } - let content = gen_deprecated_lints_test(deprecated_lints); process_file("tests/ui/deprecated.rs", update_mode, &content); @@ -141,62 +175,6 @@ fn generate_lint_files( process_file("tests/ui/rename.rs", update_mode, &content); } -fn update_docs(update_mode: UpdateMode, usable_lints: &[Lint]) { - replace_region_in_file(update_mode, Path::new("src/docs.rs"), "docs! {\n", "\n}\n", |res| { - for name in usable_lints.iter().map(|lint| lint.name.clone()).sorted() { - writeln!(res, r#" "{name}","#).unwrap(); - } - }); - - if update_mode == UpdateMode::Check { - let mut extra = BTreeSet::new(); - let mut lint_names = usable_lints - .iter() - .map(|lint| lint.name.clone()) - .collect::>(); - for file in std::fs::read_dir("src/docs").unwrap() { - let filename = file.unwrap().file_name().into_string().unwrap(); - if let Some(name) = filename.strip_suffix(".txt") { - if !lint_names.remove(name) { - extra.insert(name.to_string()); - } - } - } - - let failed = print_lint_names("extra lint docs:", &extra) | print_lint_names("missing lint docs:", &lint_names); - - if failed { - exit_with_failure(); - } - } else { - if std::fs::remove_dir_all("src/docs").is_err() { - eprintln!("could not remove src/docs directory"); - } - if std::fs::create_dir("src/docs").is_err() { - eprintln!("could not recreate src/docs directory"); - } - } - for lint in usable_lints { - process_file( - Path::new("src/docs").join(lint.name.clone() + ".txt"), - update_mode, - &lint.documentation, - ); - } -} - -fn print_lint_names(header: &str, lints: &BTreeSet) -> bool { - if lints.is_empty() { - return false; - } - println!("{header}"); - for lint in lints.iter().sorted() { - println!(" {lint}"); - } - println!(); - true -} - pub fn print_lints() { let (lint_list, _, _) = gather_all(); let usable_lints = Lint::usable_lints(&lint_list); @@ -641,26 +619,17 @@ struct Lint { desc: String, module: String, declaration_range: Range, - documentation: String, } impl Lint { #[must_use] - fn new( - name: &str, - group: &str, - desc: &str, - module: &str, - declaration_range: Range, - documentation: String, - ) -> Self { + fn new(name: &str, group: &str, desc: &str, module: &str, declaration_range: Range) -> Self { Self { name: name.to_lowercase(), group: group.into(), desc: remove_line_splices(desc), module: module.into(), declaration_range, - documentation, } } @@ -716,25 +685,6 @@ impl RenamedLint { } } -/// Generates the code for registering a group -fn gen_lint_group_list<'a>(group_name: &str, lints: impl Iterator) -> String { - let mut details: Vec<_> = lints.map(|l| (&l.module, l.name.to_uppercase())).collect(); - details.sort_unstable(); - - let mut output = GENERATED_FILE_COMMENT.to_string(); - - let _ = writeln!( - output, - "store.register_group(true, \"clippy::{group_name}\", Some(\"clippy_{group_name}\"), vec![", - ); - for (module, name) in details { - let _ = writeln!(output, " LintId::of({module}::{name}),"); - } - output.push_str("])\n"); - - output -} - /// Generates the `register_removed` code #[must_use] fn gen_deprecated(lints: &[DeprecatedLint]) -> String { @@ -759,7 +709,7 @@ fn gen_deprecated(lints: &[DeprecatedLint]) -> String { /// Generates the code for registering lints #[must_use] -fn gen_register_lint_list<'a>( +fn gen_declared_lints<'a>( internal_lints: impl Iterator, usable_lints: impl Iterator, ) -> String { @@ -770,15 +720,15 @@ fn gen_register_lint_list<'a>( details.sort_unstable(); let mut output = GENERATED_FILE_COMMENT.to_string(); - output.push_str("store.register_lints(&[\n"); + output.push_str("pub(crate) static LINTS: &[&crate::LintInfo] = &[\n"); for (is_public, module_name, lint_name) in details { if !is_public { output.push_str(" #[cfg(feature = \"internal\")]\n"); } - let _ = writeln!(output, " {module_name}::{lint_name},"); + let _ = writeln!(output, " crate::{module_name}::{lint_name}_INFO,"); } - output.push_str("])\n"); + output.push_str("];\n"); output } @@ -910,35 +860,26 @@ fn parse_contents(contents: &str, module: &str, lints: &mut Vec) { }| token_kind == &TokenKind::Ident && *content == "declare_clippy_lint", ) { let start = range.start; - let mut docs = String::with_capacity(128); - let mut iter = iter.by_ref().filter(|t| !matches!(t.token_kind, TokenKind::Whitespace)); + let mut iter = iter + .by_ref() + .filter(|t| !matches!(t.token_kind, TokenKind::Whitespace | TokenKind::LineComment { .. })); // matches `!{` match_tokens!(iter, Bang OpenBrace); - let mut in_code = false; - while let Some(t) = iter.next() { - match t.token_kind { - TokenKind::LineComment { .. } => { - if let Some(line) = t.content.strip_prefix("/// ").or_else(|| t.content.strip_prefix("///")) { - if line.starts_with("```") { - docs += "```\n"; - in_code = !in_code; - } else if !(in_code && line.starts_with("# ")) { - docs += line; - docs.push('\n'); - } - } - }, - TokenKind::Pound => { - match_tokens!(iter, OpenBracket Ident Colon Colon Ident Eq Literal{..} CloseBracket Ident); - break; - }, - TokenKind::Ident => { - break; - }, - _ => {}, - } + match iter.next() { + // #[clippy::version = "version"] pub + Some(LintDeclSearchResult { + token_kind: TokenKind::Pound, + .. + }) => { + match_tokens!(iter, OpenBracket Ident Colon Colon Ident Eq Literal{..} CloseBracket Ident); + }, + // pub + Some(LintDeclSearchResult { + token_kind: TokenKind::Ident, + .. + }) => (), + _ => continue, } - docs.pop(); // remove final newline let (name, group, desc) = match_tokens!( iter, @@ -956,7 +897,7 @@ fn parse_contents(contents: &str, module: &str, lints: &mut Vec) { .. }) = iter.next() { - lints.push(Lint::new(name, group, desc, module, start..range.end, docs)); + lints.push(Lint::new(name, group, desc, module, start..range.end)); } } } @@ -1186,7 +1127,6 @@ mod tests { "\"really long text\"", "module_name", Range::default(), - String::new(), ), Lint::new( "doc_markdown", @@ -1194,7 +1134,6 @@ mod tests { "\"single line\"", "module_name", Range::default(), - String::new(), ), ]; assert_eq!(expected, result); @@ -1234,7 +1173,6 @@ mod tests { "\"abc\"", "module_name", Range::default(), - String::new(), ), Lint::new( "should_assert_eq2", @@ -1242,7 +1180,6 @@ mod tests { "\"abc\"", "module_name", Range::default(), - String::new(), ), Lint::new( "should_assert_eq2", @@ -1250,7 +1187,6 @@ mod tests { "\"abc\"", "module_name", Range::default(), - String::new(), ), ]; let expected = vec![Lint::new( @@ -1259,7 +1195,6 @@ mod tests { "\"abc\"", "module_name", Range::default(), - String::new(), )]; assert_eq!(expected, Lint::usable_lints(&lints)); } @@ -1267,51 +1202,22 @@ mod tests { #[test] fn test_by_lint_group() { let lints = vec![ - Lint::new( - "should_assert_eq", - "group1", - "\"abc\"", - "module_name", - Range::default(), - String::new(), - ), + Lint::new("should_assert_eq", "group1", "\"abc\"", "module_name", Range::default()), Lint::new( "should_assert_eq2", "group2", "\"abc\"", "module_name", Range::default(), - String::new(), - ), - Lint::new( - "incorrect_match", - "group1", - "\"abc\"", - "module_name", - Range::default(), - String::new(), ), + Lint::new("incorrect_match", "group1", "\"abc\"", "module_name", Range::default()), ]; let mut expected: HashMap> = HashMap::new(); expected.insert( "group1".to_string(), vec![ - Lint::new( - "should_assert_eq", - "group1", - "\"abc\"", - "module_name", - Range::default(), - String::new(), - ), - Lint::new( - "incorrect_match", - "group1", - "\"abc\"", - "module_name", - Range::default(), - String::new(), - ), + Lint::new("should_assert_eq", "group1", "\"abc\"", "module_name", Range::default()), + Lint::new("incorrect_match", "group1", "\"abc\"", "module_name", Range::default()), ], ); expected.insert( @@ -1322,7 +1228,6 @@ mod tests { "\"abc\"", "module_name", Range::default(), - String::new(), )], ); assert_eq!(expected, Lint::by_lint_group(lints.into_iter())); @@ -1357,48 +1262,4 @@ mod tests { assert_eq!(expected, gen_deprecated(&lints)); } - - #[test] - fn test_gen_lint_group_list() { - let lints = vec![ - Lint::new( - "abc", - "group1", - "\"abc\"", - "module_name", - Range::default(), - String::new(), - ), - Lint::new( - "should_assert_eq", - "group1", - "\"abc\"", - "module_name", - Range::default(), - String::new(), - ), - Lint::new( - "internal", - "internal_style", - "\"abc\"", - "module_name", - Range::default(), - String::new(), - ), - ]; - let expected = GENERATED_FILE_COMMENT.to_string() - + &[ - "store.register_group(true, \"clippy::group1\", Some(\"clippy_group1\"), vec![", - " LintId::of(module_name::ABC),", - " LintId::of(module_name::INTERNAL),", - " LintId::of(module_name::SHOULD_ASSERT_EQ),", - "])", - ] - .join("\n") - + "\n"; - - let result = gen_lint_group_list("group1", lints.iter()); - - assert_eq!(expected, result); - } } diff --git a/clippy_lints/Cargo.toml b/clippy_lints/Cargo.toml index 6fbd6401ef3eb..aedff24c12c60 100644 --- a/clippy_lints/Cargo.toml +++ b/clippy_lints/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "clippy_lints" -version = "0.1.66" +version = "0.1.67" description = "A bunch of helpful lints to avoid common pitfalls in Rust" repository = "https://github.com/rust-lang/rust-clippy" readme = "README.md" @@ -11,6 +11,7 @@ edition = "2021" [dependencies] cargo_metadata = "0.14" clippy_utils = { path = "../clippy_utils" } +declare_clippy_lint = { path = "../declare_clippy_lint" } if_chain = "1.0" itertools = "0.10.1" pulldown-cmark = { version = "0.9", default-features = false } diff --git a/clippy_lints/src/attrs.rs b/clippy_lints/src/attrs.rs index 0bd1f8b784e8f..ecf8e83375dbf 100644 --- a/clippy_lints/src/attrs.rs +++ b/clippy_lints/src/attrs.rs @@ -11,14 +11,14 @@ use rustc_errors::Applicability; use rustc_hir::{ Block, Expr, ExprKind, ImplItem, ImplItemKind, Item, ItemKind, StmtKind, TraitFn, TraitItem, TraitItemKind, }; -use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext}; +use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, Level, LintContext}; use rustc_middle::lint::in_external_macro; use rustc_middle::ty; use rustc_semver::RustcVersion; use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass}; use rustc_span::source_map::Span; -use rustc_span::sym; use rustc_span::symbol::Symbol; +use rustc_span::{sym, DUMMY_SP}; use semver::Version; static UNIX_SYSTEMS: &[&str] = &[ @@ -303,6 +303,26 @@ declare_lint_pass!(Attributes => [ ]); impl<'tcx> LateLintPass<'tcx> for Attributes { + fn check_crate(&mut self, cx: &LateContext<'tcx>) { + for (name, level) in &cx.sess().opts.lint_opts { + if name == "clippy::restriction" && *level > Level::Allow { + span_lint_and_then( + cx, + BLANKET_CLIPPY_RESTRICTION_LINTS, + DUMMY_SP, + "`clippy::restriction` is not meant to be enabled as a group", + |diag| { + diag.note(format!( + "because of the command line `--{} clippy::restriction`", + level.as_str() + )); + diag.help("enable the restriction lints you need individually"); + }, + ); + } + } + } + fn check_attribute(&mut self, cx: &LateContext<'tcx>, attr: &'tcx Attribute) { if let Some(items) = &attr.meta_item_list() { if let Some(ident) = attr.ident() { @@ -358,7 +378,9 @@ impl<'tcx> LateLintPass<'tcx> for Attributes { | "enum_glob_use" | "redundant_pub_crate" | "macro_use_imports" - | "unsafe_removed_from_name", + | "unsafe_removed_from_name" + | "module_name_repetitions" + | "single_component_path_imports" ) }) { @@ -441,9 +463,9 @@ fn check_clippy_lint_names(cx: &LateContext<'_>, name: Symbol, items: &[NestedMe cx, BLANKET_CLIPPY_RESTRICTION_LINTS, lint.span(), - "restriction lints are not meant to be all enabled", + "`clippy::restriction` is not meant to be enabled as a group", None, - "try enabling only the lints you really need", + "enable the restriction lints you need individually", ); } } @@ -464,6 +486,11 @@ fn check_lint_reason(cx: &LateContext<'_>, name: Symbol, items: &[NestedMetaItem return; } + // Check if the attribute is in an external macro and therefore out of the developer's control + if in_external_macro(cx.sess(), attr.span) { + return; + } + span_lint_and_help( cx, ALLOW_ATTRIBUTES_WITHOUT_REASON, diff --git a/clippy_lints/src/await_holding_invalid.rs b/clippy_lints/src/await_holding_invalid.rs index 34717811866d8..d40a385435afb 100644 --- a/clippy_lints/src/await_holding_invalid.rs +++ b/clippy_lints/src/await_holding_invalid.rs @@ -1,7 +1,6 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::{match_def_path, paths}; use rustc_data_structures::fx::FxHashMap; -use rustc_hir::def::{Namespace, Res}; use rustc_hir::def_id::DefId; use rustc_hir::{AsyncGeneratorKind, Body, BodyId, GeneratorKind}; use rustc_lint::{LateContext, LateLintPass}; @@ -189,7 +188,7 @@ impl LateLintPass<'_> for AwaitHolding { fn check_crate(&mut self, cx: &LateContext<'_>) { for conf in &self.conf_invalid_types { let segs: Vec<_> = conf.path().split("::").collect(); - if let Res::Def(_, id) = clippy_utils::def_path_res(cx, &segs, Some(Namespace::TypeNS)) { + for id in clippy_utils::def_path_def_ids(cx, &segs) { self.def_ids.insert(id, conf.clone()); } } diff --git a/clippy_lints/src/bool_to_int_with_if.rs b/clippy_lints/src/bool_to_int_with_if.rs index 001d74c260545..bdb3a01160272 100644 --- a/clippy_lints/src/bool_to_int_with_if.rs +++ b/clippy_lints/src/bool_to_int_with_if.rs @@ -1,9 +1,10 @@ +use clippy_utils::higher::If; use rustc_ast::LitKind; use rustc_hir::{Block, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; -use clippy_utils::{diagnostics::span_lint_and_then, is_else_clause, is_integer_literal, sugg::Sugg}; +use clippy_utils::{diagnostics::span_lint_and_then, in_constant, is_else_clause, is_integer_literal, sugg::Sugg}; use rustc_errors::Applicability; declare_clippy_lint! { @@ -12,7 +13,7 @@ declare_clippy_lint! { /// this lint suggests using a `from()` function or an `as` coercion. /// /// ### Why is this bad? - /// Coercion or `from()` is idiomatic way to convert bool to a number. + /// Coercion or `from()` is another way to convert bool to a number. /// Both methods are guaranteed to return 1 for true, and 0 for false. /// /// See https://doc.rust-lang.org/std/primitive.bool.html#impl-From%3Cbool%3E @@ -38,23 +39,23 @@ declare_clippy_lint! { /// ``` #[clippy::version = "1.65.0"] pub BOOL_TO_INT_WITH_IF, - style, + pedantic, "using if to convert bool to int" } declare_lint_pass!(BoolToIntWithIf => [BOOL_TO_INT_WITH_IF]); impl<'tcx> LateLintPass<'tcx> for BoolToIntWithIf { - fn check_expr(&mut self, ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) { - if !expr.span.from_expansion() { - check_if_else(ctx, expr); + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) { + if !expr.span.from_expansion() && !in_constant(cx, expr.hir_id) { + check_if_else(cx, expr); } } } -fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) { - if let ExprKind::If(check, then, Some(else_)) = expr.kind +fn check_if_else<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) { + if let Some(If { cond, then, r#else: Some(r#else) }) = If::hir(expr) && let Some(then_lit) = int_literal(then) - && let Some(else_lit) = int_literal(else_) + && let Some(else_lit) = int_literal(r#else) { let inverted = if is_integer_literal(then_lit, 1) && is_integer_literal(else_lit, 0) { false @@ -66,17 +67,17 @@ fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx }; let mut applicability = Applicability::MachineApplicable; let snippet = { - let mut sugg = Sugg::hir_with_applicability(ctx, check, "..", &mut applicability); + let mut sugg = Sugg::hir_with_applicability(cx, cond, "..", &mut applicability); if inverted { sugg = !sugg; } sugg }; - let ty = ctx.typeck_results().expr_ty(then_lit); // then and else must be of same type + let ty = cx.typeck_results().expr_ty(then_lit); // then and else must be of same type let suggestion = { - let wrap_in_curly = is_else_clause(ctx.tcx, expr); + let wrap_in_curly = is_else_clause(cx.tcx, expr); let mut s = Sugg::NonParen(format!("{ty}::from({snippet})").into()); if wrap_in_curly { s = s.blockify(); @@ -87,7 +88,7 @@ fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx let into_snippet = snippet.clone().maybe_par(); let as_snippet = snippet.as_ty(ty); - span_lint_and_then(ctx, + span_lint_and_then(cx, BOOL_TO_INT_WITH_IF, expr.span, "boolean to int conversion using if", diff --git a/clippy_lints/src/booleans.rs b/clippy_lints/src/booleans.rs index 08164c0b654e2..939bdbcdc7cd7 100644 --- a/clippy_lints/src/booleans.rs +++ b/clippy_lints/src/booleans.rs @@ -481,7 +481,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> { } } -fn implements_ord<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) -> bool { +fn implements_ord(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { let ty = cx.typeck_results().expr_ty(expr); cx.tcx .get_diagnostic_item(sym::Ord) diff --git a/clippy_lints/src/casts/mod.rs b/clippy_lints/src/casts/mod.rs index b72c4c772f1ce..7148b5e6ebf45 100644 --- a/clippy_lints/src/casts/mod.rs +++ b/clippy_lints/src/casts/mod.rs @@ -593,7 +593,7 @@ declare_clippy_lint! { /// let _: *mut [u8] = std::ptr::slice_from_raw_parts_mut(ptr, len); /// ``` /// [safety requirements]: https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html#safety - #[clippy::version = "1.64.0"] + #[clippy::version = "1.65.0"] pub CAST_SLICE_FROM_RAW_PARTS, suspicious, "casting a slice created from a pointer and length to a slice pointer" diff --git a/clippy_lints/src/cognitive_complexity.rs b/clippy_lints/src/cognitive_complexity.rs index 77af3b53d6333..1c3a89a97824c 100644 --- a/clippy_lints/src/cognitive_complexity.rs +++ b/clippy_lints/src/cognitive_complexity.rs @@ -4,11 +4,11 @@ use clippy_utils::diagnostics::span_lint_and_help; use clippy_utils::source::snippet_opt; use clippy_utils::ty::is_type_diagnostic_item; use clippy_utils::visitors::for_each_expr; -use clippy_utils::LimitStack; +use clippy_utils::{get_async_fn_body, is_async_fn, LimitStack}; use core::ops::ControlFlow; use rustc_ast::ast::Attribute; use rustc_hir::intravisit::FnKind; -use rustc_hir::{Body, ExprKind, FnDecl, HirId}; +use rustc_hir::{Body, Expr, ExprKind, FnDecl, HirId}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::source_map::Span; @@ -56,15 +56,13 @@ impl CognitiveComplexity { cx: &LateContext<'tcx>, kind: FnKind<'tcx>, decl: &'tcx FnDecl<'_>, - body: &'tcx Body<'_>, + expr: &'tcx Expr<'_>, body_span: Span, ) { if body_span.from_expansion() { return; } - let expr = body.value; - let mut cc = 1u64; let mut returns = 0u64; let _: Option = for_each_expr(expr, |e| { @@ -146,7 +144,18 @@ impl<'tcx> LateLintPass<'tcx> for CognitiveComplexity { ) { let def_id = cx.tcx.hir().local_def_id(hir_id); if !cx.tcx.has_attr(def_id.to_def_id(), sym::test) { - self.check(cx, kind, decl, body, span); + let expr = if is_async_fn(kind) { + match get_async_fn_body(cx.tcx, body) { + Some(b) => b, + None => { + return; + }, + } + } else { + body.value + }; + + self.check(cx, kind, decl, expr, span); } } diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs new file mode 100644 index 0000000000000..0d3fc43a6443b --- /dev/null +++ b/clippy_lints/src/declared_lints.rs @@ -0,0 +1,628 @@ +// This file was generated by `cargo dev update_lints`. +// Use that command to update this file and do not edit by hand. +// Manual edits will be overwritten. + +pub(crate) static LINTS: &[&crate::LintInfo] = &[ + #[cfg(feature = "internal")] + crate::utils::internal_lints::clippy_lints_internal::CLIPPY_LINTS_INTERNAL_INFO, + #[cfg(feature = "internal")] + crate::utils::internal_lints::collapsible_calls::COLLAPSIBLE_SPAN_LINT_CALLS_INFO, + #[cfg(feature = "internal")] + crate::utils::internal_lints::compiler_lint_functions::COMPILER_LINT_FUNCTIONS_INFO, + #[cfg(feature = "internal")] + crate::utils::internal_lints::if_chain_style::IF_CHAIN_STYLE_INFO, + #[cfg(feature = "internal")] + crate::utils::internal_lints::interning_defined_symbol::INTERNING_DEFINED_SYMBOL_INFO, + #[cfg(feature = "internal")] + crate::utils::internal_lints::interning_defined_symbol::UNNECESSARY_SYMBOL_STR_INFO, + #[cfg(feature = "internal")] + crate::utils::internal_lints::invalid_paths::INVALID_PATHS_INFO, + #[cfg(feature = "internal")] + crate::utils::internal_lints::lint_without_lint_pass::DEFAULT_DEPRECATION_REASON_INFO, + #[cfg(feature = "internal")] + crate::utils::internal_lints::lint_without_lint_pass::DEFAULT_LINT_INFO, + #[cfg(feature = "internal")] + crate::utils::internal_lints::lint_without_lint_pass::INVALID_CLIPPY_VERSION_ATTRIBUTE_INFO, + #[cfg(feature = "internal")] + crate::utils::internal_lints::lint_without_lint_pass::LINT_WITHOUT_LINT_PASS_INFO, + #[cfg(feature = "internal")] + crate::utils::internal_lints::lint_without_lint_pass::MISSING_CLIPPY_VERSION_ATTRIBUTE_INFO, + #[cfg(feature = "internal")] + crate::utils::internal_lints::msrv_attr_impl::MISSING_MSRV_ATTR_IMPL_INFO, + #[cfg(feature = "internal")] + crate::utils::internal_lints::outer_expn_data_pass::OUTER_EXPN_EXPN_DATA_INFO, + #[cfg(feature = "internal")] + crate::utils::internal_lints::produce_ice::PRODUCE_ICE_INFO, + #[cfg(feature = "internal")] + crate::utils::internal_lints::unnecessary_def_path::UNNECESSARY_DEF_PATH_INFO, + crate::almost_complete_letter_range::ALMOST_COMPLETE_LETTER_RANGE_INFO, + crate::approx_const::APPROX_CONSTANT_INFO, + crate::as_conversions::AS_CONVERSIONS_INFO, + crate::asm_syntax::INLINE_ASM_X86_ATT_SYNTAX_INFO, + crate::asm_syntax::INLINE_ASM_X86_INTEL_SYNTAX_INFO, + crate::assertions_on_constants::ASSERTIONS_ON_CONSTANTS_INFO, + crate::assertions_on_result_states::ASSERTIONS_ON_RESULT_STATES_INFO, + crate::async_yields_async::ASYNC_YIELDS_ASYNC_INFO, + crate::attrs::ALLOW_ATTRIBUTES_WITHOUT_REASON_INFO, + crate::attrs::BLANKET_CLIPPY_RESTRICTION_LINTS_INFO, + crate::attrs::DEPRECATED_CFG_ATTR_INFO, + crate::attrs::DEPRECATED_SEMVER_INFO, + crate::attrs::EMPTY_LINE_AFTER_OUTER_ATTR_INFO, + crate::attrs::INLINE_ALWAYS_INFO, + crate::attrs::MISMATCHED_TARGET_OS_INFO, + crate::attrs::USELESS_ATTRIBUTE_INFO, + crate::await_holding_invalid::AWAIT_HOLDING_INVALID_TYPE_INFO, + crate::await_holding_invalid::AWAIT_HOLDING_LOCK_INFO, + crate::await_holding_invalid::AWAIT_HOLDING_REFCELL_REF_INFO, + crate::blocks_in_if_conditions::BLOCKS_IN_IF_CONDITIONS_INFO, + crate::bool_assert_comparison::BOOL_ASSERT_COMPARISON_INFO, + crate::bool_to_int_with_if::BOOL_TO_INT_WITH_IF_INFO, + crate::booleans::NONMINIMAL_BOOL_INFO, + crate::booleans::OVERLY_COMPLEX_BOOL_EXPR_INFO, + crate::borrow_deref_ref::BORROW_DEREF_REF_INFO, + crate::box_default::BOX_DEFAULT_INFO, + crate::cargo::CARGO_COMMON_METADATA_INFO, + crate::cargo::MULTIPLE_CRATE_VERSIONS_INFO, + crate::cargo::NEGATIVE_FEATURE_NAMES_INFO, + crate::cargo::REDUNDANT_FEATURE_NAMES_INFO, + crate::cargo::WILDCARD_DEPENDENCIES_INFO, + crate::casts::AS_PTR_CAST_MUT_INFO, + crate::casts::AS_UNDERSCORE_INFO, + crate::casts::BORROW_AS_PTR_INFO, + crate::casts::CAST_ABS_TO_UNSIGNED_INFO, + crate::casts::CAST_ENUM_CONSTRUCTOR_INFO, + crate::casts::CAST_ENUM_TRUNCATION_INFO, + crate::casts::CAST_LOSSLESS_INFO, + crate::casts::CAST_NAN_TO_INT_INFO, + crate::casts::CAST_POSSIBLE_TRUNCATION_INFO, + crate::casts::CAST_POSSIBLE_WRAP_INFO, + crate::casts::CAST_PRECISION_LOSS_INFO, + crate::casts::CAST_PTR_ALIGNMENT_INFO, + crate::casts::CAST_REF_TO_MUT_INFO, + crate::casts::CAST_SIGN_LOSS_INFO, + crate::casts::CAST_SLICE_DIFFERENT_SIZES_INFO, + crate::casts::CAST_SLICE_FROM_RAW_PARTS_INFO, + crate::casts::CHAR_LIT_AS_U8_INFO, + crate::casts::FN_TO_NUMERIC_CAST_INFO, + crate::casts::FN_TO_NUMERIC_CAST_ANY_INFO, + crate::casts::FN_TO_NUMERIC_CAST_WITH_TRUNCATION_INFO, + crate::casts::PTR_AS_PTR_INFO, + crate::casts::UNNECESSARY_CAST_INFO, + crate::checked_conversions::CHECKED_CONVERSIONS_INFO, + crate::cognitive_complexity::COGNITIVE_COMPLEXITY_INFO, + crate::collapsible_if::COLLAPSIBLE_ELSE_IF_INFO, + crate::collapsible_if::COLLAPSIBLE_IF_INFO, + crate::comparison_chain::COMPARISON_CHAIN_INFO, + crate::copies::BRANCHES_SHARING_CODE_INFO, + crate::copies::IFS_SAME_COND_INFO, + crate::copies::IF_SAME_THEN_ELSE_INFO, + crate::copies::SAME_FUNCTIONS_IN_IF_CONDITION_INFO, + crate::copy_iterator::COPY_ITERATOR_INFO, + crate::crate_in_macro_def::CRATE_IN_MACRO_DEF_INFO, + crate::create_dir::CREATE_DIR_INFO, + crate::dbg_macro::DBG_MACRO_INFO, + crate::default::DEFAULT_TRAIT_ACCESS_INFO, + crate::default::FIELD_REASSIGN_WITH_DEFAULT_INFO, + crate::default_instead_of_iter_empty::DEFAULT_INSTEAD_OF_ITER_EMPTY_INFO, + crate::default_numeric_fallback::DEFAULT_NUMERIC_FALLBACK_INFO, + crate::default_union_representation::DEFAULT_UNION_REPRESENTATION_INFO, + crate::dereference::EXPLICIT_AUTO_DEREF_INFO, + crate::dereference::EXPLICIT_DEREF_METHODS_INFO, + crate::dereference::NEEDLESS_BORROW_INFO, + crate::dereference::REF_BINDING_TO_REFERENCE_INFO, + crate::derivable_impls::DERIVABLE_IMPLS_INFO, + crate::derive::DERIVE_HASH_XOR_EQ_INFO, + crate::derive::DERIVE_ORD_XOR_PARTIAL_ORD_INFO, + crate::derive::DERIVE_PARTIAL_EQ_WITHOUT_EQ_INFO, + crate::derive::EXPL_IMPL_CLONE_ON_COPY_INFO, + crate::derive::UNSAFE_DERIVE_DESERIALIZE_INFO, + crate::disallowed_macros::DISALLOWED_MACROS_INFO, + crate::disallowed_methods::DISALLOWED_METHODS_INFO, + crate::disallowed_names::DISALLOWED_NAMES_INFO, + crate::disallowed_script_idents::DISALLOWED_SCRIPT_IDENTS_INFO, + crate::disallowed_types::DISALLOWED_TYPES_INFO, + crate::doc::DOC_LINK_WITH_QUOTES_INFO, + crate::doc::DOC_MARKDOWN_INFO, + crate::doc::MISSING_ERRORS_DOC_INFO, + crate::doc::MISSING_PANICS_DOC_INFO, + crate::doc::MISSING_SAFETY_DOC_INFO, + crate::doc::NEEDLESS_DOCTEST_MAIN_INFO, + crate::doc::UNNECESSARY_SAFETY_DOC_INFO, + crate::double_parens::DOUBLE_PARENS_INFO, + crate::drop_forget_ref::DROP_COPY_INFO, + crate::drop_forget_ref::DROP_NON_DROP_INFO, + crate::drop_forget_ref::DROP_REF_INFO, + crate::drop_forget_ref::FORGET_COPY_INFO, + crate::drop_forget_ref::FORGET_NON_DROP_INFO, + crate::drop_forget_ref::FORGET_REF_INFO, + crate::drop_forget_ref::UNDROPPED_MANUALLY_DROPS_INFO, + crate::duplicate_mod::DUPLICATE_MOD_INFO, + crate::else_if_without_else::ELSE_IF_WITHOUT_ELSE_INFO, + crate::empty_drop::EMPTY_DROP_INFO, + crate::empty_enum::EMPTY_ENUM_INFO, + crate::empty_structs_with_brackets::EMPTY_STRUCTS_WITH_BRACKETS_INFO, + crate::entry::MAP_ENTRY_INFO, + crate::enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT_INFO, + crate::enum_variants::ENUM_VARIANT_NAMES_INFO, + crate::enum_variants::MODULE_INCEPTION_INFO, + crate::enum_variants::MODULE_NAME_REPETITIONS_INFO, + crate::equatable_if_let::EQUATABLE_IF_LET_INFO, + crate::escape::BOXED_LOCAL_INFO, + crate::eta_reduction::REDUNDANT_CLOSURE_INFO, + crate::eta_reduction::REDUNDANT_CLOSURE_FOR_METHOD_CALLS_INFO, + crate::excessive_bools::FN_PARAMS_EXCESSIVE_BOOLS_INFO, + crate::excessive_bools::STRUCT_EXCESSIVE_BOOLS_INFO, + crate::exhaustive_items::EXHAUSTIVE_ENUMS_INFO, + crate::exhaustive_items::EXHAUSTIVE_STRUCTS_INFO, + crate::exit::EXIT_INFO, + crate::explicit_write::EXPLICIT_WRITE_INFO, + crate::fallible_impl_from::FALLIBLE_IMPL_FROM_INFO, + crate::float_literal::EXCESSIVE_PRECISION_INFO, + crate::float_literal::LOSSY_FLOAT_LITERAL_INFO, + crate::floating_point_arithmetic::IMPRECISE_FLOPS_INFO, + crate::floating_point_arithmetic::SUBOPTIMAL_FLOPS_INFO, + crate::format::USELESS_FORMAT_INFO, + crate::format_args::FORMAT_IN_FORMAT_ARGS_INFO, + crate::format_args::TO_STRING_IN_FORMAT_ARGS_INFO, + crate::format_args::UNINLINED_FORMAT_ARGS_INFO, + crate::format_args::UNUSED_FORMAT_SPECS_INFO, + crate::format_impl::PRINT_IN_FORMAT_IMPL_INFO, + crate::format_impl::RECURSIVE_FORMAT_IMPL_INFO, + crate::format_push_string::FORMAT_PUSH_STRING_INFO, + crate::formatting::POSSIBLE_MISSING_COMMA_INFO, + crate::formatting::SUSPICIOUS_ASSIGNMENT_FORMATTING_INFO, + crate::formatting::SUSPICIOUS_ELSE_FORMATTING_INFO, + crate::formatting::SUSPICIOUS_UNARY_OP_FORMATTING_INFO, + crate::from_over_into::FROM_OVER_INTO_INFO, + crate::from_raw_with_void_ptr::FROM_RAW_WITH_VOID_PTR_INFO, + crate::from_str_radix_10::FROM_STR_RADIX_10_INFO, + crate::functions::DOUBLE_MUST_USE_INFO, + crate::functions::MUST_USE_CANDIDATE_INFO, + crate::functions::MUST_USE_UNIT_INFO, + crate::functions::NOT_UNSAFE_PTR_ARG_DEREF_INFO, + crate::functions::RESULT_LARGE_ERR_INFO, + crate::functions::RESULT_UNIT_ERR_INFO, + crate::functions::TOO_MANY_ARGUMENTS_INFO, + crate::functions::TOO_MANY_LINES_INFO, + crate::future_not_send::FUTURE_NOT_SEND_INFO, + crate::if_let_mutex::IF_LET_MUTEX_INFO, + crate::if_not_else::IF_NOT_ELSE_INFO, + crate::if_then_some_else_none::IF_THEN_SOME_ELSE_NONE_INFO, + crate::implicit_hasher::IMPLICIT_HASHER_INFO, + crate::implicit_return::IMPLICIT_RETURN_INFO, + crate::implicit_saturating_add::IMPLICIT_SATURATING_ADD_INFO, + crate::implicit_saturating_sub::IMPLICIT_SATURATING_SUB_INFO, + crate::inconsistent_struct_constructor::INCONSISTENT_STRUCT_CONSTRUCTOR_INFO, + crate::index_refutable_slice::INDEX_REFUTABLE_SLICE_INFO, + crate::indexing_slicing::INDEXING_SLICING_INFO, + crate::indexing_slicing::OUT_OF_BOUNDS_INDEXING_INFO, + crate::infinite_iter::INFINITE_ITER_INFO, + crate::infinite_iter::MAYBE_INFINITE_ITER_INFO, + crate::inherent_impl::MULTIPLE_INHERENT_IMPL_INFO, + crate::inherent_to_string::INHERENT_TO_STRING_INFO, + crate::inherent_to_string::INHERENT_TO_STRING_SHADOW_DISPLAY_INFO, + crate::init_numbered_fields::INIT_NUMBERED_FIELDS_INFO, + crate::inline_fn_without_body::INLINE_FN_WITHOUT_BODY_INFO, + crate::instant_subtraction::MANUAL_INSTANT_ELAPSED_INFO, + crate::instant_subtraction::UNCHECKED_DURATION_SUBTRACTION_INFO, + crate::int_plus_one::INT_PLUS_ONE_INFO, + crate::invalid_upcast_comparisons::INVALID_UPCAST_COMPARISONS_INFO, + crate::invalid_utf8_in_unchecked::INVALID_UTF8_IN_UNCHECKED_INFO, + crate::items_after_statements::ITEMS_AFTER_STATEMENTS_INFO, + crate::iter_not_returning_iterator::ITER_NOT_RETURNING_ITERATOR_INFO, + crate::large_const_arrays::LARGE_CONST_ARRAYS_INFO, + crate::large_enum_variant::LARGE_ENUM_VARIANT_INFO, + crate::large_include_file::LARGE_INCLUDE_FILE_INFO, + crate::large_stack_arrays::LARGE_STACK_ARRAYS_INFO, + crate::len_zero::COMPARISON_TO_EMPTY_INFO, + crate::len_zero::LEN_WITHOUT_IS_EMPTY_INFO, + crate::len_zero::LEN_ZERO_INFO, + crate::let_if_seq::USELESS_LET_IF_SEQ_INFO, + crate::let_underscore::LET_UNDERSCORE_FUTURE_INFO, + crate::let_underscore::LET_UNDERSCORE_LOCK_INFO, + crate::let_underscore::LET_UNDERSCORE_MUST_USE_INFO, + crate::lifetimes::EXTRA_UNUSED_LIFETIMES_INFO, + crate::lifetimes::NEEDLESS_LIFETIMES_INFO, + crate::literal_representation::DECIMAL_LITERAL_REPRESENTATION_INFO, + crate::literal_representation::INCONSISTENT_DIGIT_GROUPING_INFO, + crate::literal_representation::LARGE_DIGIT_GROUPS_INFO, + crate::literal_representation::MISTYPED_LITERAL_SUFFIXES_INFO, + crate::literal_representation::UNREADABLE_LITERAL_INFO, + crate::literal_representation::UNUSUAL_BYTE_GROUPINGS_INFO, + crate::loops::EMPTY_LOOP_INFO, + crate::loops::EXPLICIT_COUNTER_LOOP_INFO, + crate::loops::EXPLICIT_INTO_ITER_LOOP_INFO, + crate::loops::EXPLICIT_ITER_LOOP_INFO, + crate::loops::FOR_KV_MAP_INFO, + crate::loops::ITER_NEXT_LOOP_INFO, + crate::loops::MANUAL_FIND_INFO, + crate::loops::MANUAL_FLATTEN_INFO, + crate::loops::MANUAL_MEMCPY_INFO, + crate::loops::MISSING_SPIN_LOOP_INFO, + crate::loops::MUT_RANGE_BOUND_INFO, + crate::loops::NEEDLESS_RANGE_LOOP_INFO, + crate::loops::NEVER_LOOP_INFO, + crate::loops::SAME_ITEM_PUSH_INFO, + crate::loops::SINGLE_ELEMENT_LOOP_INFO, + crate::loops::WHILE_IMMUTABLE_CONDITION_INFO, + crate::loops::WHILE_LET_LOOP_INFO, + crate::loops::WHILE_LET_ON_ITERATOR_INFO, + crate::macro_use::MACRO_USE_IMPORTS_INFO, + crate::main_recursion::MAIN_RECURSION_INFO, + crate::manual_assert::MANUAL_ASSERT_INFO, + crate::manual_async_fn::MANUAL_ASYNC_FN_INFO, + crate::manual_bits::MANUAL_BITS_INFO, + crate::manual_clamp::MANUAL_CLAMP_INFO, + crate::manual_is_ascii_check::MANUAL_IS_ASCII_CHECK_INFO, + crate::manual_let_else::MANUAL_LET_ELSE_INFO, + crate::manual_non_exhaustive::MANUAL_NON_EXHAUSTIVE_INFO, + crate::manual_rem_euclid::MANUAL_REM_EUCLID_INFO, + crate::manual_retain::MANUAL_RETAIN_INFO, + crate::manual_string_new::MANUAL_STRING_NEW_INFO, + crate::manual_strip::MANUAL_STRIP_INFO, + crate::map_unit_fn::OPTION_MAP_UNIT_FN_INFO, + crate::map_unit_fn::RESULT_MAP_UNIT_FN_INFO, + crate::match_result_ok::MATCH_RESULT_OK_INFO, + crate::matches::COLLAPSIBLE_MATCH_INFO, + crate::matches::INFALLIBLE_DESTRUCTURING_MATCH_INFO, + crate::matches::MANUAL_FILTER_INFO, + crate::matches::MANUAL_MAP_INFO, + crate::matches::MANUAL_UNWRAP_OR_INFO, + crate::matches::MATCH_AS_REF_INFO, + crate::matches::MATCH_BOOL_INFO, + crate::matches::MATCH_LIKE_MATCHES_MACRO_INFO, + crate::matches::MATCH_ON_VEC_ITEMS_INFO, + crate::matches::MATCH_OVERLAPPING_ARM_INFO, + crate::matches::MATCH_REF_PATS_INFO, + crate::matches::MATCH_SAME_ARMS_INFO, + crate::matches::MATCH_SINGLE_BINDING_INFO, + crate::matches::MATCH_STR_CASE_MISMATCH_INFO, + crate::matches::MATCH_WILDCARD_FOR_SINGLE_VARIANTS_INFO, + crate::matches::MATCH_WILD_ERR_ARM_INFO, + crate::matches::NEEDLESS_MATCH_INFO, + crate::matches::REDUNDANT_PATTERN_MATCHING_INFO, + crate::matches::REST_PAT_IN_FULLY_BOUND_STRUCTS_INFO, + crate::matches::SIGNIFICANT_DROP_IN_SCRUTINEE_INFO, + crate::matches::SINGLE_MATCH_INFO, + crate::matches::SINGLE_MATCH_ELSE_INFO, + crate::matches::TRY_ERR_INFO, + crate::matches::WILDCARD_ENUM_MATCH_ARM_INFO, + crate::matches::WILDCARD_IN_OR_PATTERNS_INFO, + crate::mem_forget::MEM_FORGET_INFO, + crate::mem_replace::MEM_REPLACE_OPTION_WITH_NONE_INFO, + crate::mem_replace::MEM_REPLACE_WITH_DEFAULT_INFO, + crate::mem_replace::MEM_REPLACE_WITH_UNINIT_INFO, + crate::methods::BIND_INSTEAD_OF_MAP_INFO, + crate::methods::BYTES_COUNT_TO_LEN_INFO, + crate::methods::BYTES_NTH_INFO, + crate::methods::CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS_INFO, + crate::methods::CHARS_LAST_CMP_INFO, + crate::methods::CHARS_NEXT_CMP_INFO, + crate::methods::CLONED_INSTEAD_OF_COPIED_INFO, + crate::methods::CLONE_DOUBLE_REF_INFO, + crate::methods::CLONE_ON_COPY_INFO, + crate::methods::CLONE_ON_REF_PTR_INFO, + crate::methods::COLLAPSIBLE_STR_REPLACE_INFO, + crate::methods::ERR_EXPECT_INFO, + crate::methods::EXPECT_FUN_CALL_INFO, + crate::methods::EXPECT_USED_INFO, + crate::methods::EXTEND_WITH_DRAIN_INFO, + crate::methods::FILETYPE_IS_FILE_INFO, + crate::methods::FILTER_MAP_IDENTITY_INFO, + crate::methods::FILTER_MAP_NEXT_INFO, + crate::methods::FILTER_NEXT_INFO, + crate::methods::FLAT_MAP_IDENTITY_INFO, + crate::methods::FLAT_MAP_OPTION_INFO, + crate::methods::FROM_ITER_INSTEAD_OF_COLLECT_INFO, + crate::methods::GET_FIRST_INFO, + crate::methods::GET_LAST_WITH_LEN_INFO, + crate::methods::GET_UNWRAP_INFO, + crate::methods::IMPLICIT_CLONE_INFO, + crate::methods::INEFFICIENT_TO_STRING_INFO, + crate::methods::INSPECT_FOR_EACH_INFO, + crate::methods::INTO_ITER_ON_REF_INFO, + crate::methods::IS_DIGIT_ASCII_RADIX_INFO, + crate::methods::ITERATOR_STEP_BY_ZERO_INFO, + crate::methods::ITER_CLONED_COLLECT_INFO, + crate::methods::ITER_COUNT_INFO, + crate::methods::ITER_KV_MAP_INFO, + crate::methods::ITER_NEXT_SLICE_INFO, + crate::methods::ITER_NTH_INFO, + crate::methods::ITER_NTH_ZERO_INFO, + crate::methods::ITER_ON_EMPTY_COLLECTIONS_INFO, + crate::methods::ITER_ON_SINGLE_ITEMS_INFO, + crate::methods::ITER_OVEREAGER_CLONED_INFO, + crate::methods::ITER_SKIP_NEXT_INFO, + crate::methods::ITER_WITH_DRAIN_INFO, + crate::methods::MANUAL_FILTER_MAP_INFO, + crate::methods::MANUAL_FIND_MAP_INFO, + crate::methods::MANUAL_OK_OR_INFO, + crate::methods::MANUAL_SATURATING_ARITHMETIC_INFO, + crate::methods::MANUAL_SPLIT_ONCE_INFO, + crate::methods::MANUAL_STR_REPEAT_INFO, + crate::methods::MAP_CLONE_INFO, + crate::methods::MAP_COLLECT_RESULT_UNIT_INFO, + crate::methods::MAP_ERR_IGNORE_INFO, + crate::methods::MAP_FLATTEN_INFO, + crate::methods::MAP_IDENTITY_INFO, + crate::methods::MAP_UNWRAP_OR_INFO, + crate::methods::MUT_MUTEX_LOCK_INFO, + crate::methods::NAIVE_BYTECOUNT_INFO, + crate::methods::NEEDLESS_COLLECT_INFO, + crate::methods::NEEDLESS_OPTION_AS_DEREF_INFO, + crate::methods::NEEDLESS_OPTION_TAKE_INFO, + crate::methods::NEEDLESS_SPLITN_INFO, + crate::methods::NEW_RET_NO_SELF_INFO, + crate::methods::NONSENSICAL_OPEN_OPTIONS_INFO, + crate::methods::NO_EFFECT_REPLACE_INFO, + crate::methods::OBFUSCATED_IF_ELSE_INFO, + crate::methods::OK_EXPECT_INFO, + crate::methods::OPTION_AS_REF_DEREF_INFO, + crate::methods::OPTION_FILTER_MAP_INFO, + crate::methods::OPTION_MAP_OR_NONE_INFO, + crate::methods::OR_FUN_CALL_INFO, + crate::methods::OR_THEN_UNWRAP_INFO, + crate::methods::PATH_BUF_PUSH_OVERWRITE_INFO, + crate::methods::RANGE_ZIP_WITH_LEN_INFO, + crate::methods::REPEAT_ONCE_INFO, + crate::methods::RESULT_MAP_OR_INTO_OPTION_INFO, + crate::methods::SEARCH_IS_SOME_INFO, + crate::methods::SEEK_FROM_CURRENT_INFO, + crate::methods::SEEK_TO_START_INSTEAD_OF_REWIND_INFO, + crate::methods::SHOULD_IMPLEMENT_TRAIT_INFO, + crate::methods::SINGLE_CHAR_ADD_STR_INFO, + crate::methods::SINGLE_CHAR_PATTERN_INFO, + crate::methods::SKIP_WHILE_NEXT_INFO, + crate::methods::STABLE_SORT_PRIMITIVE_INFO, + crate::methods::STRING_EXTEND_CHARS_INFO, + crate::methods::SUSPICIOUS_MAP_INFO, + crate::methods::SUSPICIOUS_SPLITN_INFO, + crate::methods::SUSPICIOUS_TO_OWNED_INFO, + crate::methods::UNINIT_ASSUMED_INIT_INFO, + crate::methods::UNIT_HASH_INFO, + crate::methods::UNNECESSARY_FILTER_MAP_INFO, + crate::methods::UNNECESSARY_FIND_MAP_INFO, + crate::methods::UNNECESSARY_FOLD_INFO, + crate::methods::UNNECESSARY_JOIN_INFO, + crate::methods::UNNECESSARY_LAZY_EVALUATIONS_INFO, + crate::methods::UNNECESSARY_SORT_BY_INFO, + crate::methods::UNNECESSARY_TO_OWNED_INFO, + crate::methods::UNWRAP_OR_ELSE_DEFAULT_INFO, + crate::methods::UNWRAP_USED_INFO, + crate::methods::USELESS_ASREF_INFO, + crate::methods::VEC_RESIZE_TO_ZERO_INFO, + crate::methods::VERBOSE_FILE_READS_INFO, + crate::methods::WRONG_SELF_CONVENTION_INFO, + crate::methods::ZST_OFFSET_INFO, + crate::minmax::MIN_MAX_INFO, + crate::misc::SHORT_CIRCUIT_STATEMENT_INFO, + crate::misc::TOPLEVEL_REF_ARG_INFO, + crate::misc::USED_UNDERSCORE_BINDING_INFO, + crate::misc::ZERO_PTR_INFO, + crate::misc_early::BUILTIN_TYPE_SHADOW_INFO, + crate::misc_early::DOUBLE_NEG_INFO, + crate::misc_early::DUPLICATE_UNDERSCORE_ARGUMENT_INFO, + crate::misc_early::MIXED_CASE_HEX_LITERALS_INFO, + crate::misc_early::REDUNDANT_PATTERN_INFO, + crate::misc_early::SEPARATED_LITERAL_SUFFIX_INFO, + crate::misc_early::UNNEEDED_FIELD_PATTERN_INFO, + crate::misc_early::UNNEEDED_WILDCARD_PATTERN_INFO, + crate::misc_early::UNSEPARATED_LITERAL_SUFFIX_INFO, + crate::misc_early::ZERO_PREFIXED_LITERAL_INFO, + crate::mismatching_type_param_order::MISMATCHING_TYPE_PARAM_ORDER_INFO, + crate::missing_const_for_fn::MISSING_CONST_FOR_FN_INFO, + crate::missing_doc::MISSING_DOCS_IN_PRIVATE_ITEMS_INFO, + crate::missing_enforced_import_rename::MISSING_ENFORCED_IMPORT_RENAMES_INFO, + crate::missing_inline::MISSING_INLINE_IN_PUBLIC_ITEMS_INFO, + crate::missing_trait_methods::MISSING_TRAIT_METHODS_INFO, + crate::mixed_read_write_in_expression::DIVERGING_SUB_EXPRESSION_INFO, + crate::mixed_read_write_in_expression::MIXED_READ_WRITE_IN_EXPRESSION_INFO, + crate::module_style::MOD_MODULE_FILES_INFO, + crate::module_style::SELF_NAMED_MODULE_FILES_INFO, + crate::multi_assignments::MULTI_ASSIGNMENTS_INFO, + crate::mut_key::MUTABLE_KEY_TYPE_INFO, + crate::mut_mut::MUT_MUT_INFO, + crate::mut_reference::UNNECESSARY_MUT_PASSED_INFO, + crate::mutable_debug_assertion::DEBUG_ASSERT_WITH_MUT_CALL_INFO, + crate::mutex_atomic::MUTEX_ATOMIC_INFO, + crate::mutex_atomic::MUTEX_INTEGER_INFO, + crate::needless_arbitrary_self_type::NEEDLESS_ARBITRARY_SELF_TYPE_INFO, + crate::needless_bool::BOOL_COMPARISON_INFO, + crate::needless_bool::NEEDLESS_BOOL_INFO, + crate::needless_borrowed_ref::NEEDLESS_BORROWED_REFERENCE_INFO, + crate::needless_continue::NEEDLESS_CONTINUE_INFO, + crate::needless_for_each::NEEDLESS_FOR_EACH_INFO, + crate::needless_late_init::NEEDLESS_LATE_INIT_INFO, + crate::needless_parens_on_range_literals::NEEDLESS_PARENS_ON_RANGE_LITERALS_INFO, + crate::needless_pass_by_value::NEEDLESS_PASS_BY_VALUE_INFO, + crate::needless_question_mark::NEEDLESS_QUESTION_MARK_INFO, + crate::needless_update::NEEDLESS_UPDATE_INFO, + crate::neg_cmp_op_on_partial_ord::NEG_CMP_OP_ON_PARTIAL_ORD_INFO, + crate::neg_multiply::NEG_MULTIPLY_INFO, + crate::new_without_default::NEW_WITHOUT_DEFAULT_INFO, + crate::no_effect::NO_EFFECT_INFO, + crate::no_effect::NO_EFFECT_UNDERSCORE_BINDING_INFO, + crate::no_effect::UNNECESSARY_OPERATION_INFO, + crate::non_copy_const::BORROW_INTERIOR_MUTABLE_CONST_INFO, + crate::non_copy_const::DECLARE_INTERIOR_MUTABLE_CONST_INFO, + crate::non_expressive_names::JUST_UNDERSCORES_AND_DIGITS_INFO, + crate::non_expressive_names::MANY_SINGLE_CHAR_NAMES_INFO, + crate::non_expressive_names::SIMILAR_NAMES_INFO, + crate::non_octal_unix_permissions::NON_OCTAL_UNIX_PERMISSIONS_INFO, + crate::non_send_fields_in_send_ty::NON_SEND_FIELDS_IN_SEND_TY_INFO, + crate::nonstandard_macro_braces::NONSTANDARD_MACRO_BRACES_INFO, + crate::octal_escapes::OCTAL_ESCAPES_INFO, + crate::only_used_in_recursion::ONLY_USED_IN_RECURSION_INFO, + crate::operators::ABSURD_EXTREME_COMPARISONS_INFO, + crate::operators::ARITHMETIC_SIDE_EFFECTS_INFO, + crate::operators::ASSIGN_OP_PATTERN_INFO, + crate::operators::BAD_BIT_MASK_INFO, + crate::operators::CMP_NAN_INFO, + crate::operators::CMP_OWNED_INFO, + crate::operators::DOUBLE_COMPARISONS_INFO, + crate::operators::DURATION_SUBSEC_INFO, + crate::operators::EQ_OP_INFO, + crate::operators::ERASING_OP_INFO, + crate::operators::FLOAT_ARITHMETIC_INFO, + crate::operators::FLOAT_CMP_INFO, + crate::operators::FLOAT_CMP_CONST_INFO, + crate::operators::FLOAT_EQUALITY_WITHOUT_ABS_INFO, + crate::operators::IDENTITY_OP_INFO, + crate::operators::INEFFECTIVE_BIT_MASK_INFO, + crate::operators::INTEGER_ARITHMETIC_INFO, + crate::operators::INTEGER_DIVISION_INFO, + crate::operators::MISREFACTORED_ASSIGN_OP_INFO, + crate::operators::MODULO_ARITHMETIC_INFO, + crate::operators::MODULO_ONE_INFO, + crate::operators::NEEDLESS_BITWISE_BOOL_INFO, + crate::operators::OP_REF_INFO, + crate::operators::PTR_EQ_INFO, + crate::operators::SELF_ASSIGNMENT_INFO, + crate::operators::VERBOSE_BIT_MASK_INFO, + crate::option_env_unwrap::OPTION_ENV_UNWRAP_INFO, + crate::option_if_let_else::OPTION_IF_LET_ELSE_INFO, + crate::overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL_INFO, + crate::panic_in_result_fn::PANIC_IN_RESULT_FN_INFO, + crate::panic_unimplemented::PANIC_INFO, + crate::panic_unimplemented::TODO_INFO, + crate::panic_unimplemented::UNIMPLEMENTED_INFO, + crate::panic_unimplemented::UNREACHABLE_INFO, + crate::partial_pub_fields::PARTIAL_PUB_FIELDS_INFO, + crate::partialeq_ne_impl::PARTIALEQ_NE_IMPL_INFO, + crate::partialeq_to_none::PARTIALEQ_TO_NONE_INFO, + crate::pass_by_ref_or_value::LARGE_TYPES_PASSED_BY_VALUE_INFO, + crate::pass_by_ref_or_value::TRIVIALLY_COPY_PASS_BY_REF_INFO, + crate::pattern_type_mismatch::PATTERN_TYPE_MISMATCH_INFO, + crate::precedence::PRECEDENCE_INFO, + crate::ptr::CMP_NULL_INFO, + crate::ptr::INVALID_NULL_PTR_USAGE_INFO, + crate::ptr::MUT_FROM_REF_INFO, + crate::ptr::PTR_ARG_INFO, + crate::ptr_offset_with_cast::PTR_OFFSET_WITH_CAST_INFO, + crate::pub_use::PUB_USE_INFO, + crate::question_mark::QUESTION_MARK_INFO, + crate::ranges::MANUAL_RANGE_CONTAINS_INFO, + crate::ranges::RANGE_MINUS_ONE_INFO, + crate::ranges::RANGE_PLUS_ONE_INFO, + crate::ranges::REVERSED_EMPTY_RANGES_INFO, + crate::rc_clone_in_vec_init::RC_CLONE_IN_VEC_INIT_INFO, + crate::read_zero_byte_vec::READ_ZERO_BYTE_VEC_INFO, + crate::redundant_clone::REDUNDANT_CLONE_INFO, + crate::redundant_closure_call::REDUNDANT_CLOSURE_CALL_INFO, + crate::redundant_else::REDUNDANT_ELSE_INFO, + crate::redundant_field_names::REDUNDANT_FIELD_NAMES_INFO, + crate::redundant_pub_crate::REDUNDANT_PUB_CRATE_INFO, + crate::redundant_slicing::DEREF_BY_SLICING_INFO, + crate::redundant_slicing::REDUNDANT_SLICING_INFO, + crate::redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES_INFO, + crate::ref_option_ref::REF_OPTION_REF_INFO, + crate::reference::DEREF_ADDROF_INFO, + crate::regex::INVALID_REGEX_INFO, + crate::regex::TRIVIAL_REGEX_INFO, + crate::return_self_not_must_use::RETURN_SELF_NOT_MUST_USE_INFO, + crate::returns::LET_AND_RETURN_INFO, + crate::returns::NEEDLESS_RETURN_INFO, + crate::same_name_method::SAME_NAME_METHOD_INFO, + crate::self_named_constructors::SELF_NAMED_CONSTRUCTORS_INFO, + crate::semicolon_if_nothing_returned::SEMICOLON_IF_NOTHING_RETURNED_INFO, + crate::serde_api::SERDE_API_MISUSE_INFO, + crate::shadow::SHADOW_REUSE_INFO, + crate::shadow::SHADOW_SAME_INFO, + crate::shadow::SHADOW_UNRELATED_INFO, + crate::single_char_lifetime_names::SINGLE_CHAR_LIFETIME_NAMES_INFO, + crate::single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS_INFO, + crate::size_of_in_element_count::SIZE_OF_IN_ELEMENT_COUNT_INFO, + crate::slow_vector_initialization::SLOW_VECTOR_INITIALIZATION_INFO, + crate::std_instead_of_core::ALLOC_INSTEAD_OF_CORE_INFO, + crate::std_instead_of_core::STD_INSTEAD_OF_ALLOC_INFO, + crate::std_instead_of_core::STD_INSTEAD_OF_CORE_INFO, + crate::strings::STRING_ADD_INFO, + crate::strings::STRING_ADD_ASSIGN_INFO, + crate::strings::STRING_FROM_UTF8_AS_BYTES_INFO, + crate::strings::STRING_LIT_AS_BYTES_INFO, + crate::strings::STRING_SLICE_INFO, + crate::strings::STRING_TO_STRING_INFO, + crate::strings::STR_TO_STRING_INFO, + crate::strings::TRIM_SPLIT_WHITESPACE_INFO, + crate::strlen_on_c_strings::STRLEN_ON_C_STRINGS_INFO, + crate::suspicious_operation_groupings::SUSPICIOUS_OPERATION_GROUPINGS_INFO, + crate::suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL_INFO, + crate::suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL_INFO, + crate::suspicious_xor_used_as_pow::SUSPICIOUS_XOR_USED_AS_POW_INFO, + crate::swap::ALMOST_SWAPPED_INFO, + crate::swap::MANUAL_SWAP_INFO, + crate::swap_ptr_to_ref::SWAP_PTR_TO_REF_INFO, + crate::tabs_in_doc_comments::TABS_IN_DOC_COMMENTS_INFO, + crate::temporary_assignment::TEMPORARY_ASSIGNMENT_INFO, + crate::to_digit_is_some::TO_DIGIT_IS_SOME_INFO, + crate::trailing_empty_array::TRAILING_EMPTY_ARRAY_INFO, + crate::trait_bounds::TRAIT_DUPLICATION_IN_BOUNDS_INFO, + crate::trait_bounds::TYPE_REPETITION_IN_BOUNDS_INFO, + crate::transmute::CROSSPOINTER_TRANSMUTE_INFO, + crate::transmute::TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS_INFO, + crate::transmute::TRANSMUTE_BYTES_TO_STR_INFO, + crate::transmute::TRANSMUTE_FLOAT_TO_INT_INFO, + crate::transmute::TRANSMUTE_INT_TO_BOOL_INFO, + crate::transmute::TRANSMUTE_INT_TO_CHAR_INFO, + crate::transmute::TRANSMUTE_INT_TO_FLOAT_INFO, + crate::transmute::TRANSMUTE_NUM_TO_BYTES_INFO, + crate::transmute::TRANSMUTE_PTR_TO_PTR_INFO, + crate::transmute::TRANSMUTE_PTR_TO_REF_INFO, + crate::transmute::TRANSMUTE_UNDEFINED_REPR_INFO, + crate::transmute::TRANSMUTING_NULL_INFO, + crate::transmute::UNSOUND_COLLECTION_TRANSMUTE_INFO, + crate::transmute::USELESS_TRANSMUTE_INFO, + crate::transmute::WRONG_TRANSMUTE_INFO, + crate::types::BORROWED_BOX_INFO, + crate::types::BOX_COLLECTION_INFO, + crate::types::LINKEDLIST_INFO, + crate::types::OPTION_OPTION_INFO, + crate::types::RC_BUFFER_INFO, + crate::types::RC_MUTEX_INFO, + crate::types::REDUNDANT_ALLOCATION_INFO, + crate::types::TYPE_COMPLEXITY_INFO, + crate::types::VEC_BOX_INFO, + crate::undocumented_unsafe_blocks::UNDOCUMENTED_UNSAFE_BLOCKS_INFO, + crate::unicode::INVISIBLE_CHARACTERS_INFO, + crate::unicode::NON_ASCII_LITERAL_INFO, + crate::unicode::UNICODE_NOT_NFC_INFO, + crate::uninit_vec::UNINIT_VEC_INFO, + crate::unit_return_expecting_ord::UNIT_RETURN_EXPECTING_ORD_INFO, + crate::unit_types::LET_UNIT_VALUE_INFO, + crate::unit_types::UNIT_ARG_INFO, + crate::unit_types::UNIT_CMP_INFO, + crate::unnamed_address::FN_ADDRESS_COMPARISONS_INFO, + crate::unnamed_address::VTABLE_ADDRESS_COMPARISONS_INFO, + crate::unnecessary_owned_empty_strings::UNNECESSARY_OWNED_EMPTY_STRINGS_INFO, + crate::unnecessary_self_imports::UNNECESSARY_SELF_IMPORTS_INFO, + crate::unnecessary_wraps::UNNECESSARY_WRAPS_INFO, + crate::unnested_or_patterns::UNNESTED_OR_PATTERNS_INFO, + crate::unsafe_removed_from_name::UNSAFE_REMOVED_FROM_NAME_INFO, + crate::unused_async::UNUSED_ASYNC_INFO, + crate::unused_io_amount::UNUSED_IO_AMOUNT_INFO, + crate::unused_peekable::UNUSED_PEEKABLE_INFO, + crate::unused_rounding::UNUSED_ROUNDING_INFO, + crate::unused_self::UNUSED_SELF_INFO, + crate::unused_unit::UNUSED_UNIT_INFO, + crate::unwrap::PANICKING_UNWRAP_INFO, + crate::unwrap::UNNECESSARY_UNWRAP_INFO, + crate::unwrap_in_result::UNWRAP_IN_RESULT_INFO, + crate::upper_case_acronyms::UPPER_CASE_ACRONYMS_INFO, + crate::use_self::USE_SELF_INFO, + crate::useless_conversion::USELESS_CONVERSION_INFO, + crate::vec::USELESS_VEC_INFO, + crate::vec_init_then_push::VEC_INIT_THEN_PUSH_INFO, + crate::wildcard_imports::ENUM_GLOB_USE_INFO, + crate::wildcard_imports::WILDCARD_IMPORTS_INFO, + crate::write::PRINTLN_EMPTY_STRING_INFO, + crate::write::PRINT_LITERAL_INFO, + crate::write::PRINT_STDERR_INFO, + crate::write::PRINT_STDOUT_INFO, + crate::write::PRINT_WITH_NEWLINE_INFO, + crate::write::USE_DEBUG_INFO, + crate::write::WRITELN_EMPTY_STRING_INFO, + crate::write::WRITE_LITERAL_INFO, + crate::write::WRITE_WITH_NEWLINE_INFO, + crate::zero_div_zero::ZERO_DIVIDED_BY_ZERO_INFO, + crate::zero_sized_map_values::ZERO_SIZED_MAP_VALUES_INFO, +]; diff --git a/clippy_lints/src/dereference.rs b/clippy_lints/src/dereference.rs index 218dbeaddcade..9da64ffc13e16 100644 --- a/clippy_lints/src/dereference.rs +++ b/clippy_lints/src/dereference.rs @@ -9,6 +9,7 @@ use clippy_utils::{ }; use rustc_ast::util::parser::{PREC_POSTFIX, PREC_PREFIX}; use rustc_data_structures::fx::FxIndexMap; +use rustc_data_structures::graph::iterate::{CycleDetector, TriColorDepthFirstSearch}; use rustc_errors::Applicability; use rustc_hir::intravisit::{walk_ty, Visitor}; use rustc_hir::{ @@ -274,9 +275,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> { } let typeck = cx.typeck_results(); - let (kind, sub_expr) = if let Some(x) = try_parse_ref_op(cx.tcx, typeck, expr) { - x - } else { + let Some((kind, sub_expr)) = try_parse_ref_op(cx.tcx, typeck, expr) else { // The whole chain of reference operations has been seen if let Some((state, data)) = self.state.take() { report(cx, expr, state, data); @@ -806,30 +805,39 @@ fn walk_parents<'tcx>( .position(|arg| arg.hir_id == child_id) .zip(expr_sig(cx, func)) .and_then(|(i, sig)| { - sig.input_with_hir(i).map(|(hir_ty, ty)| match hir_ty { - // Type inference for closures can depend on how they're called. Only go by the explicit - // types here. - Some(hir_ty) => binding_ty_auto_deref_stability(cx, hir_ty, precedence, ty.bound_vars()), - None => { - if let ty::Param(param_ty) = ty.skip_binder().kind() { - needless_borrow_impl_arg_position( - cx, - possible_borrowers, - parent, - i, - *param_ty, - e, - precedence, - msrv, - ) - } else { - ty_auto_deref_stability(cx, cx.tcx.erase_late_bound_regions(ty), precedence) - .position_for_arg() - } - }, + sig.input_with_hir(i).map(|(hir_ty, ty)| { + match hir_ty { + // Type inference for closures can depend on how they're called. Only go by the explicit + // types here. + Some(hir_ty) => { + binding_ty_auto_deref_stability(cx, hir_ty, precedence, ty.bound_vars()) + }, + None => { + // `e.hir_id == child_id` for https://github.com/rust-lang/rust-clippy/issues/9739 + // `!call_is_qualified(func)` for https://github.com/rust-lang/rust-clippy/issues/9782 + if e.hir_id == child_id + && !call_is_qualified(func) + && let ty::Param(param_ty) = ty.skip_binder().kind() + { + needless_borrow_impl_arg_position( + cx, + possible_borrowers, + parent, + i, + *param_ty, + e, + precedence, + msrv, + ) + } else { + ty_auto_deref_stability(cx, cx.tcx.erase_late_bound_regions(ty), precedence) + .position_for_arg() + } + }, + } }) }), - ExprKind::MethodCall(_, receiver, args, _) => { + ExprKind::MethodCall(method, receiver, args, _) => { let id = cx.typeck_results().type_dependent_def_id(parent.hir_id).unwrap(); if receiver.hir_id == child_id { // Check for calls to trait methods where the trait is implemented on a reference. @@ -867,7 +875,9 @@ fn walk_parents<'tcx>( } args.iter().position(|arg| arg.hir_id == child_id).map(|i| { let ty = cx.tcx.fn_sig(id).skip_binder().inputs()[i + 1]; - if let ty::Param(param_ty) = ty.kind() { + // `e.hir_id == child_id` for https://github.com/rust-lang/rust-clippy/issues/9739 + // `method.args.is_none()` for https://github.com/rust-lang/rust-clippy/issues/9782 + if e.hir_id == child_id && method.args.is_none() && let ty::Param(param_ty) = ty.kind() { needless_borrow_impl_arg_position( cx, possible_borrowers, @@ -1045,13 +1055,25 @@ fn ty_contains_infer(ty: &hir::Ty<'_>) -> bool { v.0 } +fn call_is_qualified(expr: &Expr<'_>) -> bool { + if let ExprKind::Path(path) = &expr.kind { + match path { + QPath::Resolved(_, path) => path.segments.last().map_or(false, |segment| segment.args.is_some()), + QPath::TypeRelative(_, segment) => segment.args.is_some(), + QPath::LangItem(..) => false, + } + } else { + false + } +} + // Checks whether: // * child is an expression of the form `&e` in an argument position requiring an `impl Trait` // * `e`'s type implements `Trait` and is copyable // If the conditions are met, returns `Some(Position::ImplArg(..))`; otherwise, returns `None`. // The "is copyable" condition is to avoid the case where removing the `&` means `e` would have to // be moved, but it cannot be. -#[expect(clippy::too_many_arguments)] +#[expect(clippy::too_many_arguments, clippy::too_many_lines)] fn needless_borrow_impl_arg_position<'tcx>( cx: &LateContext<'tcx>, possible_borrowers: &mut Vec<(LocalDefId, PossibleBorrowerMap<'tcx, 'tcx>)>, @@ -1113,6 +1135,16 @@ fn needless_borrow_impl_arg_position<'tcx>( return Position::Other(precedence); } + // See: + // - https://github.com/rust-lang/rust-clippy/pull/9674#issuecomment-1289294201 + // - https://github.com/rust-lang/rust-clippy/pull/9674#issuecomment-1292225232 + if projection_predicates + .iter() + .any(|projection_predicate| is_mixed_projection_predicate(cx, callee_def_id, projection_predicate)) + { + return Position::Other(precedence); + } + // `substs_with_referent_ty` can be constructed outside of `check_referent` because the same // elements are modified each time `check_referent` is called. let mut substs_with_referent_ty = substs_with_expr_ty.to_vec(); @@ -1192,6 +1224,37 @@ fn has_ref_mut_self_method(cx: &LateContext<'_>, trait_def_id: DefId) -> bool { }) } +fn is_mixed_projection_predicate<'tcx>( + cx: &LateContext<'tcx>, + callee_def_id: DefId, + projection_predicate: &ProjectionPredicate<'tcx>, +) -> bool { + let generics = cx.tcx.generics_of(callee_def_id); + // The predicate requires the projected type to equal a type parameter from the parent context. + if let Some(term_ty) = projection_predicate.term.ty() + && let ty::Param(term_param_ty) = term_ty.kind() + && (term_param_ty.index as usize) < generics.parent_count + { + // The inner-most self type is a type parameter from the current function. + let mut projection_ty = projection_predicate.projection_ty; + loop { + match projection_ty.self_ty().kind() { + ty::Projection(inner_projection_ty) => { + projection_ty = *inner_projection_ty; + } + ty::Param(param_ty) => { + return (param_ty.index as usize) >= generics.parent_count; + } + _ => { + return false; + } + } + } + } else { + false + } +} + fn referent_used_exactly_once<'tcx>( cx: &LateContext<'tcx>, possible_borrowers: &mut Vec<(LocalDefId, PossibleBorrowerMap<'tcx, 'tcx>)>, @@ -1203,6 +1266,8 @@ fn referent_used_exactly_once<'tcx>( && let Some(statement) = mir.basic_blocks[location.block].statements.get(location.statement_index) && let StatementKind::Assign(box (_, Rvalue::Ref(_, _, place))) = statement.kind && !place.has_deref() + // Ensure not in a loop (https://github.com/rust-lang/rust-clippy/issues/9710) + && TriColorDepthFirstSearch::new(&mir.basic_blocks).run_from(location.block, &mut CycleDetector).is_none() { let body_owner_local_def_id = cx.tcx.hir().enclosing_body_owner(reference.hir_id); if possible_borrowers @@ -1320,6 +1385,7 @@ fn ty_auto_deref_stability<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, precedenc continue; }, ty::Param(_) => TyPosition::new_deref_stable_for_result(precedence, ty), + ty::Projection(_) if ty.has_non_region_param() => TyPosition::new_deref_stable_for_result(precedence, ty), ty::Infer(_) | ty::Error(_) | ty::Bound(..) | ty::Opaque(..) | ty::Placeholder(_) | ty::Dynamic(..) => { Position::ReborrowStable(precedence).into() }, @@ -1346,11 +1412,9 @@ fn ty_auto_deref_stability<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, precedenc | ty::Closure(..) | ty::Never | ty::Tuple(_) - | ty::Projection(_) => Position::DerefStable( - precedence, - ty.is_sized(cx.tcx, cx.param_env.without_caller_bounds()), - ) - .into(), + | ty::Projection(_) => { + Position::DerefStable(precedence, ty.is_sized(cx.tcx, cx.param_env.without_caller_bounds())).into() + }, }; } } diff --git a/clippy_lints/src/disallowed_macros.rs b/clippy_lints/src/disallowed_macros.rs index 5ab7144e29098..68122b4cef577 100644 --- a/clippy_lints/src/disallowed_macros.rs +++ b/clippy_lints/src/disallowed_macros.rs @@ -1,7 +1,6 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::macros::macro_backtrace; use rustc_data_structures::fx::FxHashSet; -use rustc_hir::def::{Namespace, Res}; use rustc_hir::def_id::DefIdMap; use rustc_hir::{Expr, ForeignItem, HirId, ImplItem, Item, Pat, Path, Stmt, TraitItem, Ty}; use rustc_lint::{LateContext, LateLintPass}; @@ -89,7 +88,7 @@ impl DisallowedMacros { &format!("use of a disallowed macro `{}`", conf.path()), |diag| { if let Some(reason) = conf.reason() { - diag.note(&format!("{reason} (from clippy.toml)")); + diag.note(reason); } }, ); @@ -104,7 +103,7 @@ impl LateLintPass<'_> for DisallowedMacros { fn check_crate(&mut self, cx: &LateContext<'_>) { for (index, conf) in self.conf_disallowed.iter().enumerate() { let segs: Vec<_> = conf.path().split("::").collect(); - if let Res::Def(_, id) = clippy_utils::def_path_res(cx, &segs, Some(Namespace::MacroNS)) { + for id in clippy_utils::def_path_def_ids(cx, &segs) { self.disallowed.insert(id, index); } } diff --git a/clippy_lints/src/disallowed_methods.rs b/clippy_lints/src/disallowed_methods.rs index 6ac85606d9c7c..ca8671c8f1aa0 100644 --- a/clippy_lints/src/disallowed_methods.rs +++ b/clippy_lints/src/disallowed_methods.rs @@ -1,7 +1,6 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::{fn_def_id, get_parent_expr, path_def_id}; -use rustc_hir::def::{Namespace, Res}; use rustc_hir::def_id::DefIdMap; use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; @@ -79,7 +78,7 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedMethods { fn check_crate(&mut self, cx: &LateContext<'_>) { for (index, conf) in self.conf_disallowed.iter().enumerate() { let segs: Vec<_> = conf.path().split("::").collect(); - if let Res::Def(_, id) = clippy_utils::def_path_res(cx, &segs, Some(Namespace::ValueNS)) { + for id in clippy_utils::def_path_def_ids(cx, &segs) { self.disallowed.insert(id, index); } } @@ -104,7 +103,7 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedMethods { let msg = format!("use of a disallowed method `{}`", conf.path()); span_lint_and_then(cx, DISALLOWED_METHODS, expr.span, &msg, |diag| { if let Some(reason) = conf.reason() { - diag.note(&format!("{reason} (from clippy.toml)")); + diag.note(reason); } }); } diff --git a/clippy_lints/src/disallowed_types.rs b/clippy_lints/src/disallowed_types.rs index c7131fc164d3e..aee3d8c4f0852 100644 --- a/clippy_lints/src/disallowed_types.rs +++ b/clippy_lints/src/disallowed_types.rs @@ -1,7 +1,7 @@ use clippy_utils::diagnostics::span_lint_and_then; use rustc_data_structures::fx::FxHashMap; -use rustc_hir::def::{Namespace, Res}; +use rustc_hir::def::Res; use rustc_hir::def_id::DefId; use rustc_hir::{Item, ItemKind, PolyTraitRef, PrimTy, Ty, TyKind, UseKind}; use rustc_lint::{LateContext, LateLintPass}; @@ -53,8 +53,8 @@ declare_clippy_lint! { #[derive(Clone, Debug)] pub struct DisallowedTypes { conf_disallowed: Vec, - def_ids: FxHashMap>, - prim_tys: FxHashMap>, + def_ids: FxHashMap, + prim_tys: FxHashMap, } impl DisallowedTypes { @@ -69,13 +69,13 @@ impl DisallowedTypes { fn check_res_emit(&self, cx: &LateContext<'_>, res: &Res, span: Span) { match res { Res::Def(_, did) => { - if let Some(reason) = self.def_ids.get(did) { - emit(cx, &cx.tcx.def_path_str(*did), span, reason.as_deref()); + if let Some(&index) = self.def_ids.get(did) { + emit(cx, &cx.tcx.def_path_str(*did), span, &self.conf_disallowed[index]); } }, Res::PrimTy(prim) => { - if let Some(reason) = self.prim_tys.get(prim) { - emit(cx, prim.name_str(), span, reason.as_deref()); + if let Some(&index) = self.prim_tys.get(prim) { + emit(cx, prim.name_str(), span, &self.conf_disallowed[index]); } }, _ => {}, @@ -87,17 +87,19 @@ impl_lint_pass!(DisallowedTypes => [DISALLOWED_TYPES]); impl<'tcx> LateLintPass<'tcx> for DisallowedTypes { fn check_crate(&mut self, cx: &LateContext<'_>) { - for conf in &self.conf_disallowed { + for (index, conf) in self.conf_disallowed.iter().enumerate() { let segs: Vec<_> = conf.path().split("::").collect(); - let reason = conf.reason().map(|reason| format!("{reason} (from clippy.toml)")); - match clippy_utils::def_path_res(cx, &segs, Some(Namespace::TypeNS)) { - Res::Def(_, id) => { - self.def_ids.insert(id, reason); - }, - Res::PrimTy(ty) => { - self.prim_tys.insert(ty, reason); - }, - _ => {}, + + for res in clippy_utils::def_path_res(cx, &segs) { + match res { + Res::Def(_, id) => { + self.def_ids.insert(id, index); + }, + Res::PrimTy(ty) => { + self.prim_tys.insert(ty, index); + }, + _ => {}, + } } } } @@ -119,14 +121,14 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedTypes { } } -fn emit(cx: &LateContext<'_>, name: &str, span: Span, reason: Option<&str>) { +fn emit(cx: &LateContext<'_>, name: &str, span: Span, conf: &conf::DisallowedPath) { span_lint_and_then( cx, DISALLOWED_TYPES, span, &format!("`{name}` is not allowed according to config"), |diag| { - if let Some(reason) = reason { + if let Some(reason) = conf.reason() { diag.note(reason); } }, diff --git a/clippy_lints/src/doc.rs b/clippy_lints/src/doc.rs index daaab79fef9ae..4557e43288542 100644 --- a/clippy_lints/src/doc.rs +++ b/clippy_lints/src/doc.rs @@ -11,7 +11,7 @@ use rustc_ast::token::CommentKind; use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::sync::Lrc; use rustc_errors::emitter::EmitterWriter; -use rustc_errors::{Applicability, Handler, MultiSpan, SuggestionStyle}; +use rustc_errors::{Applicability, Handler, SuggestionStyle}; use rustc_hir as hir; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::{AnonConst, Expr}; @@ -221,6 +221,42 @@ declare_clippy_lint! { "possible typo for an intra-doc link" } +declare_clippy_lint! { + /// ### What it does + /// Checks for the doc comments of publicly visible + /// safe functions and traits and warns if there is a `# Safety` section. + /// + /// ### Why is this bad? + /// Safe functions and traits are safe to implement and therefore do not + /// need to describe safety preconditions that users are required to uphold. + /// + /// ### Examples + /// ```rust + ///# type Universe = (); + /// /// # Safety + /// /// + /// /// This function should not be called before the horsemen are ready. + /// pub fn start_apocalypse_but_safely(u: &mut Universe) { + /// unimplemented!(); + /// } + /// ``` + /// + /// The function is safe, so there shouldn't be any preconditions + /// that have to be explained for safety reasons. + /// + /// ```rust + ///# type Universe = (); + /// /// This function should really be documented + /// pub fn start_apocalypse(u: &mut Universe) { + /// unimplemented!(); + /// } + /// ``` + #[clippy::version = "1.66.0"] + pub UNNECESSARY_SAFETY_DOC, + style, + "`pub fn` or `pub trait` with `# Safety` docs" +} + #[expect(clippy::module_name_repetitions)] #[derive(Clone)] pub struct DocMarkdown { @@ -243,7 +279,8 @@ impl_lint_pass!(DocMarkdown => [ MISSING_SAFETY_DOC, MISSING_ERRORS_DOC, MISSING_PANICS_DOC, - NEEDLESS_DOCTEST_MAIN + NEEDLESS_DOCTEST_MAIN, + UNNECESSARY_SAFETY_DOC, ]); impl<'tcx> LateLintPass<'tcx> for DocMarkdown { @@ -254,7 +291,7 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) { let attrs = cx.tcx.hir().attrs(item.hir_id()); - let headers = check_attrs(cx, &self.valid_idents, attrs); + let Some(headers) = check_attrs(cx, &self.valid_idents, attrs) else { return }; match item.kind { hir::ItemKind::Fn(ref sig, _, body_id) => { if !(is_entrypoint_fn(cx, item.owner_id.to_def_id()) || in_external_macro(cx.tcx.sess, item.span)) { @@ -265,29 +302,26 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown { panic_span: None, }; fpu.visit_expr(body.value); - lint_for_missing_headers( - cx, - item.owner_id.def_id, - item.span, - sig, - headers, - Some(body_id), - fpu.panic_span, - ); + lint_for_missing_headers(cx, item.owner_id.def_id, sig, headers, Some(body_id), fpu.panic_span); } }, hir::ItemKind::Impl(impl_) => { self.in_trait_impl = impl_.of_trait.is_some(); }, - hir::ItemKind::Trait(_, unsafety, ..) => { - if !headers.safety && unsafety == hir::Unsafety::Unsafe { - span_lint( - cx, - MISSING_SAFETY_DOC, - item.span, - "docs for unsafe trait missing `# Safety` section", - ); - } + hir::ItemKind::Trait(_, unsafety, ..) => match (headers.safety, unsafety) { + (false, hir::Unsafety::Unsafe) => span_lint( + cx, + MISSING_SAFETY_DOC, + cx.tcx.def_span(item.owner_id), + "docs for unsafe trait missing `# Safety` section", + ), + (true, hir::Unsafety::Normal) => span_lint( + cx, + UNNECESSARY_SAFETY_DOC, + cx.tcx.def_span(item.owner_id), + "docs for safe trait have unnecessary `# Safety` section", + ), + _ => (), }, _ => (), } @@ -301,17 +335,17 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown { fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'_>) { let attrs = cx.tcx.hir().attrs(item.hir_id()); - let headers = check_attrs(cx, &self.valid_idents, attrs); + let Some(headers) = check_attrs(cx, &self.valid_idents, attrs) else { return }; if let hir::TraitItemKind::Fn(ref sig, ..) = item.kind { if !in_external_macro(cx.tcx.sess, item.span) { - lint_for_missing_headers(cx, item.owner_id.def_id, item.span, sig, headers, None, None); + lint_for_missing_headers(cx, item.owner_id.def_id, sig, headers, None, None); } } } fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'_>) { let attrs = cx.tcx.hir().attrs(item.hir_id()); - let headers = check_attrs(cx, &self.valid_idents, attrs); + let Some(headers) = check_attrs(cx, &self.valid_idents, attrs) else { return }; if self.in_trait_impl || in_external_macro(cx.tcx.sess, item.span) { return; } @@ -323,23 +357,14 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown { panic_span: None, }; fpu.visit_expr(body.value); - lint_for_missing_headers( - cx, - item.owner_id.def_id, - item.span, - sig, - headers, - Some(body_id), - fpu.panic_span, - ); + lint_for_missing_headers(cx, item.owner_id.def_id, sig, headers, Some(body_id), fpu.panic_span); } } } -fn lint_for_missing_headers<'tcx>( - cx: &LateContext<'tcx>, +fn lint_for_missing_headers( + cx: &LateContext<'_>, def_id: LocalDefId, - span: impl Into + Copy, sig: &hir::FnSig<'_>, headers: DocHeaders, body_id: Option, @@ -359,13 +384,21 @@ fn lint_for_missing_headers<'tcx>( return; } - if !headers.safety && sig.header.unsafety == hir::Unsafety::Unsafe { - span_lint( + let span = cx.tcx.def_span(def_id); + match (headers.safety, sig.header.unsafety) { + (false, hir::Unsafety::Unsafe) => span_lint( cx, MISSING_SAFETY_DOC, span, "unsafe function's docs miss `# Safety` section", - ); + ), + (true, hir::Unsafety::Normal) => span_lint( + cx, + UNNECESSARY_SAFETY_DOC, + span, + "safe function's docs have unnecessary `# Safety` section", + ), + _ => (), } if !headers.panics && panic_span.is_some() { span_lint_and_note( @@ -467,7 +500,7 @@ struct DocHeaders { panics: bool, } -fn check_attrs<'a>(cx: &LateContext<'_>, valid_idents: &FxHashSet, attrs: &'a [Attribute]) -> DocHeaders { +fn check_attrs(cx: &LateContext<'_>, valid_idents: &FxHashSet, attrs: &[Attribute]) -> Option { use pulldown_cmark::{BrokenLink, CowStr, Options}; /// We don't want the parser to choke on intra doc links. Since we don't /// actually care about rendering them, just pretend that all broken links are @@ -488,11 +521,7 @@ fn check_attrs<'a>(cx: &LateContext<'_>, valid_idents: &FxHashSet, attrs } else if attr.has_name(sym::doc) { // ignore mix of sugared and non-sugared doc // don't trigger the safety or errors check - return DocHeaders { - safety: true, - errors: true, - panics: true, - }; + return None; } } @@ -504,7 +533,7 @@ fn check_attrs<'a>(cx: &LateContext<'_>, valid_idents: &FxHashSet, attrs } if doc.is_empty() { - return DocHeaders::default(); + return Some(DocHeaders::default()); } let mut cb = fake_broken_link_callback; @@ -527,7 +556,7 @@ fn check_attrs<'a>(cx: &LateContext<'_>, valid_idents: &FxHashSet, attrs (previous, current) => Err(((previous, previous_range), (current, current_range))), } }); - check_doc(cx, valid_idents, events, &spans) + Some(check_doc(cx, valid_idents, events, &spans)) } const RUST_CODE: &[&str] = &["rust", "no_run", "should_panic", "compile_fail"]; diff --git a/clippy_lints/src/enum_variants.rs b/clippy_lints/src/enum_variants.rs index 223545fa79846..b77b5621b4c68 100644 --- a/clippy_lints/src/enum_variants.rs +++ b/clippy_lints/src/enum_variants.rs @@ -250,7 +250,7 @@ impl LateLintPass<'_> for EnumVariantNames { let item_name = item.ident.name.as_str(); let item_camel = to_camel_case(item_name); if !item.span.from_expansion() && is_present_in_source(cx, item.span) { - if let Some(&(ref mod_name, ref mod_camel)) = self.modules.last() { + if let Some((mod_name, mod_camel)) = self.modules.last() { // constants don't have surrounding modules if !mod_camel.is_empty() { if mod_name == &item.ident.name { diff --git a/clippy_lints/src/equatable_if_let.rs b/clippy_lints/src/equatable_if_let.rs index b40cb7cddaf17..c691e6c5402d4 100644 --- a/clippy_lints/src/equatable_if_let.rs +++ b/clippy_lints/src/equatable_if_let.rs @@ -1,7 +1,6 @@ use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::source::snippet_with_context; use clippy_utils::ty::implements_trait; -use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, Pat, PatKind}; use rustc_lint::{LateContext, LateLintPass, LintContext}; @@ -67,16 +66,14 @@ fn is_structural_partial_eq<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, other: T impl<'tcx> LateLintPass<'tcx> for PatternEquality { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { - if_chain! { - if !in_external_macro(cx.sess(), expr.span); - if let ExprKind::Let(let_expr) = expr.kind; - if unary_pattern(let_expr.pat); + if !in_external_macro(cx.sess(), expr.span) + && let ExprKind::Let(let_expr) = expr.kind + && unary_pattern(let_expr.pat) { let exp_ty = cx.typeck_results().expr_ty(let_expr.init); let pat_ty = cx.typeck_results().pat_ty(let_expr.pat); - if is_structural_partial_eq(cx, exp_ty, pat_ty); - then { + let mut applicability = Applicability::MachineApplicable; - let mut applicability = Applicability::MachineApplicable; + if is_structural_partial_eq(cx, exp_ty, pat_ty) { let pat_str = match let_expr.pat.kind { PatKind::Struct(..) => format!( "({})", @@ -96,6 +93,20 @@ impl<'tcx> LateLintPass<'tcx> for PatternEquality { ), applicability, ); + } else { + span_lint_and_sugg( + cx, + EQUATABLE_IF_LET, + expr.span, + "this pattern matching can be expressed using `matches!`", + "try", + format!( + "matches!({}, {})", + snippet_with_context(cx, let_expr.init.span, expr.span.ctxt(), "..", &mut applicability).0, + snippet_with_context(cx, let_expr.pat.span, expr.span.ctxt(), "..", &mut applicability).0, + ), + applicability, + ); } } } diff --git a/clippy_lints/src/escape.rs b/clippy_lints/src/escape.rs index 7f1a4c4beb1f2..1d09adec12f3f 100644 --- a/clippy_lints/src/escape.rs +++ b/clippy_lints/src/escape.rs @@ -176,13 +176,7 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> { } } - fn fake_read( - &mut self, - _: &rustc_hir_typeck::expr_use_visitor::PlaceWithHirId<'tcx>, - _: FakeReadCause, - _: HirId, - ) { - } + fn fake_read(&mut self, _: &rustc_hir_typeck::expr_use_visitor::PlaceWithHirId<'tcx>, _: FakeReadCause, _: HirId) {} } impl<'a, 'tcx> EscapeDelegate<'a, 'tcx> { diff --git a/clippy_lints/src/excessive_bools.rs b/clippy_lints/src/excessive_bools.rs index 453471c8cdda6..fc2912f696e03 100644 --- a/clippy_lints/src/excessive_bools.rs +++ b/clippy_lints/src/excessive_bools.rs @@ -1,8 +1,11 @@ use clippy_utils::diagnostics::span_lint_and_help; -use rustc_ast::ast::{AssocItemKind, Extern, Fn, FnSig, Impl, Item, ItemKind, Trait, Ty, TyKind}; -use rustc_lint::{EarlyContext, EarlyLintPass}; +use clippy_utils::{get_parent_as_impl, has_repr_attr, is_bool}; +use rustc_hir::intravisit::FnKind; +use rustc_hir::{Body, FnDecl, HirId, Item, ItemKind, TraitFn, TraitItem, TraitItemKind, Ty}; +use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_tool_lint, impl_lint_pass}; -use rustc_span::{sym, Span}; +use rustc_span::Span; +use rustc_target::spec::abi::Abi; declare_clippy_lint! { /// ### What it does @@ -83,6 +86,12 @@ pub struct ExcessiveBools { max_fn_params_bools: u64, } +#[derive(Eq, PartialEq, Debug, Copy, Clone)] +enum Kind { + Struct, + Fn, +} + impl ExcessiveBools { #[must_use] pub fn new(max_struct_bools: u64, max_fn_params_bools: u64) -> Self { @@ -92,21 +101,20 @@ impl ExcessiveBools { } } - fn check_fn_sig(&self, cx: &EarlyContext<'_>, fn_sig: &FnSig, span: Span) { - match fn_sig.header.ext { - Extern::Implicit(_) | Extern::Explicit(_, _) => return, - Extern::None => (), + fn too_many_bools<'tcx>(&self, tys: impl Iterator>, kind: Kind) -> bool { + if let Ok(bools) = tys.filter(|ty| is_bool(ty)).count().try_into() { + (if Kind::Fn == kind { + self.max_fn_params_bools + } else { + self.max_struct_bools + }) < bools + } else { + false } + } - let fn_sig_bools = fn_sig - .decl - .inputs - .iter() - .filter(|param| is_bool_ty(¶m.ty)) - .count() - .try_into() - .unwrap(); - if self.max_fn_params_bools < fn_sig_bools { + fn check_fn_sig(&self, cx: &LateContext<'_>, fn_decl: &FnDecl<'_>, span: Span) { + if !span.from_expansion() && self.too_many_bools(fn_decl.inputs.iter(), Kind::Fn) { span_lint_and_help( cx, FN_PARAMS_EXCESSIVE_BOOLS, @@ -121,56 +129,55 @@ impl ExcessiveBools { impl_lint_pass!(ExcessiveBools => [STRUCT_EXCESSIVE_BOOLS, FN_PARAMS_EXCESSIVE_BOOLS]); -fn is_bool_ty(ty: &Ty) -> bool { - if let TyKind::Path(None, path) = &ty.kind { - if let [name] = path.segments.as_slice() { - return name.ident.name == sym::bool; +impl<'tcx> LateLintPass<'tcx> for ExcessiveBools { + fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { + if item.span.from_expansion() { + return; + } + if let ItemKind::Struct(variant_data, _) = &item.kind { + if has_repr_attr(cx, item.hir_id()) { + return; + } + + if self.too_many_bools(variant_data.fields().iter().map(|field| field.ty), Kind::Struct) { + span_lint_and_help( + cx, + STRUCT_EXCESSIVE_BOOLS, + item.span, + &format!("more than {} bools in a struct", self.max_struct_bools), + None, + "consider using a state machine or refactoring bools into two-variant enums", + ); + } } } - false -} -impl EarlyLintPass for ExcessiveBools { - fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { - if item.span.from_expansion() { - return; + fn check_trait_item(&mut self, cx: &LateContext<'tcx>, trait_item: &'tcx TraitItem<'tcx>) { + // functions with a body are already checked by `check_fn` + if let TraitItemKind::Fn(fn_sig, TraitFn::Required(_)) = &trait_item.kind + && fn_sig.header.abi == Abi::Rust + { + self.check_fn_sig(cx, fn_sig.decl, fn_sig.span); } - match &item.kind { - ItemKind::Struct(variant_data, _) => { - if item.attrs.iter().any(|attr| attr.has_name(sym::repr)) { - return; - } + } - let struct_bools = variant_data - .fields() - .iter() - .filter(|field| is_bool_ty(&field.ty)) - .count() - .try_into() - .unwrap(); - if self.max_struct_bools < struct_bools { - span_lint_and_help( - cx, - STRUCT_EXCESSIVE_BOOLS, - item.span, - &format!("more than {} bools in a struct", self.max_struct_bools), - None, - "consider using a state machine or refactoring bools into two-variant enums", - ); - } - }, - ItemKind::Impl(box Impl { - of_trait: None, items, .. - }) - | ItemKind::Trait(box Trait { items, .. }) => { - for item in items { - if let AssocItemKind::Fn(box Fn { sig, .. }) = &item.kind { - self.check_fn_sig(cx, sig, item.span); - } - } - }, - ItemKind::Fn(box Fn { sig, .. }) => self.check_fn_sig(cx, sig, item.span), - _ => (), + fn check_fn( + &mut self, + cx: &LateContext<'tcx>, + fn_kind: FnKind<'tcx>, + fn_decl: &'tcx FnDecl<'tcx>, + _: &'tcx Body<'tcx>, + span: Span, + hir_id: HirId, + ) { + if let Some(fn_header) = fn_kind.header() + && fn_header.abi == Abi::Rust + && get_parent_as_impl(cx.tcx, hir_id) + .map_or(true, + |impl_item| impl_item.of_trait.is_none() + ) + { + self.check_fn_sig(cx, fn_decl, span); } } } diff --git a/clippy_lints/src/fallible_impl_from.rs b/clippy_lints/src/fallible_impl_from.rs index 0a633f242a5f1..9a1058470e18e 100644 --- a/clippy_lints/src/fallible_impl_from.rs +++ b/clippy_lints/src/fallible_impl_from.rs @@ -64,7 +64,7 @@ impl<'tcx> LateLintPass<'tcx> for FallibleImplFrom { } } -fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, impl_items: &[hir::ImplItemRef]) { +fn lint_impl_body(cx: &LateContext<'_>, impl_span: Span, impl_items: &[hir::ImplItemRef]) { use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::{Expr, ImplItemKind}; diff --git a/clippy_lints/src/from_raw_with_void_ptr.rs b/clippy_lints/src/from_raw_with_void_ptr.rs new file mode 100644 index 0000000000000..00f5ba56496ec --- /dev/null +++ b/clippy_lints/src/from_raw_with_void_ptr.rs @@ -0,0 +1,77 @@ +use clippy_utils::diagnostics::span_lint_and_help; +use clippy_utils::ty::is_c_void; +use clippy_utils::{match_def_path, path_def_id, paths}; +use rustc_hir::def_id::DefId; +use rustc_hir::{Expr, ExprKind, QPath}; +use rustc_lint::{LateContext, LateLintPass}; +use rustc_middle::ty::RawPtr; +use rustc_middle::ty::TypeAndMut; +use rustc_session::{declare_lint_pass, declare_tool_lint}; +use rustc_span::sym; + +declare_clippy_lint! { + /// ### What it does + /// Checks if we're passing a `c_void` raw pointer to `{Box,Rc,Arc,Weak}::from_raw(_)` + /// + /// ### Why is this bad? + /// When dealing with `c_void` raw pointers in FFI, it is easy to run into the pitfall of calling `from_raw` with the `c_void` pointer. + /// The type signature of `Box::from_raw` is `fn from_raw(raw: *mut T) -> Box`, so if you pass a `*mut c_void` you will get a `Box` (and similarly for `Rc`, `Arc` and `Weak`). + /// For this to be safe, `c_void` would need to have the same memory layout as the original type, which is often not the case. + /// + /// ### Example + /// ```rust + /// # use std::ffi::c_void; + /// let ptr = Box::into_raw(Box::new(42usize)) as *mut c_void; + /// let _ = unsafe { Box::from_raw(ptr) }; + /// ``` + /// Use instead: + /// ```rust + /// # use std::ffi::c_void; + /// # let ptr = Box::into_raw(Box::new(42usize)) as *mut c_void; + /// let _ = unsafe { Box::from_raw(ptr as *mut usize) }; + /// ``` + /// + #[clippy::version = "1.66.0"] + pub FROM_RAW_WITH_VOID_PTR, + suspicious, + "creating a `Box` from a void raw pointer" +} +declare_lint_pass!(FromRawWithVoidPtr => [FROM_RAW_WITH_VOID_PTR]); + +impl LateLintPass<'_> for FromRawWithVoidPtr { + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { + if let ExprKind::Call(box_from_raw, [arg]) = expr.kind + && let ExprKind::Path(QPath::TypeRelative(ty, seg)) = box_from_raw.kind + && seg.ident.name == sym!(from_raw) + && let Some(type_str) = path_def_id(cx, ty).and_then(|id| def_id_matches_type(cx, id)) + && let arg_kind = cx.typeck_results().expr_ty(arg).kind() + && let RawPtr(TypeAndMut { ty, .. }) = arg_kind + && is_c_void(cx, *ty) { + let msg = format!("creating a `{type_str}` from a void raw pointer"); + span_lint_and_help(cx, FROM_RAW_WITH_VOID_PTR, expr.span, &msg, Some(arg.span), "cast this to a pointer of the appropriate type"); + } + } +} + +/// Checks whether a `DefId` matches `Box`, `Rc`, `Arc`, or one of the `Weak` types. +/// Returns a static string slice with the name of the type, if one was found. +fn def_id_matches_type(cx: &LateContext<'_>, def_id: DefId) -> Option<&'static str> { + // Box + if Some(def_id) == cx.tcx.lang_items().owned_box() { + return Some("Box"); + } + + if let Some(symbol) = cx.tcx.get_diagnostic_name(def_id) { + if symbol == sym::Arc { + return Some("Arc"); + } else if symbol == sym::Rc { + return Some("Rc"); + } + } + + if match_def_path(cx, def_id, &paths::WEAK_RC) || match_def_path(cx, def_id, &paths::WEAK_ARC) { + Some("Weak") + } else { + None + } +} diff --git a/clippy_lints/src/functions/mod.rs b/clippy_lints/src/functions/mod.rs index 90911e0bf2595..ae0e083344638 100644 --- a/clippy_lints/src/functions/mod.rs +++ b/clippy_lints/src/functions/mod.rs @@ -254,7 +254,7 @@ declare_clippy_lint! { /// Ok(()) /// } /// ``` - #[clippy::version = "1.64.0"] + #[clippy::version = "1.65.0"] pub RESULT_LARGE_ERR, perf, "function returning `Result` with large `Err` type" diff --git a/clippy_lints/src/functions/must_use.rs b/clippy_lints/src/functions/must_use.rs index bff69f9151846..d22bede36b419 100644 --- a/clippy_lints/src/functions/must_use.rs +++ b/clippy_lints/src/functions/must_use.rs @@ -50,7 +50,9 @@ pub(super) fn check_impl_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Imp let attr = cx.tcx.get_attr(item.owner_id.to_def_id(), sym::must_use); if let Some(attr) = attr { check_needless_must_use(cx, sig.decl, item.hir_id(), item.span, fn_header_span, attr); - } else if is_public && !is_proc_macro(cx.sess(), attrs) && trait_ref_of_method(cx, item.owner_id.def_id).is_none() + } else if is_public + && !is_proc_macro(cx.sess(), attrs) + && trait_ref_of_method(cx, item.owner_id.def_id).is_none() { check_must_use_candidate( cx, @@ -175,7 +177,7 @@ fn is_mutable_pat(cx: &LateContext<'_>, pat: &hir::Pat<'_>, tys: &mut DefIdSet) return false; // ignore `_` patterns } if cx.tcx.has_typeck_results(pat.hir_id.owner.to_def_id()) { - is_mutable_ty(cx, cx.tcx.typeck(pat.hir_id.owner.def_id).pat_ty(pat), pat.span, tys) + is_mutable_ty(cx, cx.tcx.typeck(pat.hir_id.owner.def_id).pat_ty(pat), tys) } else { false } @@ -183,7 +185,7 @@ fn is_mutable_pat(cx: &LateContext<'_>, pat: &hir::Pat<'_>, tys: &mut DefIdSet) static KNOWN_WRAPPER_TYS: &[Symbol] = &[sym::Rc, sym::Arc]; -fn is_mutable_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, span: Span, tys: &mut DefIdSet) -> bool { +fn is_mutable_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, tys: &mut DefIdSet) -> bool { match *ty.kind() { // primitive types are never mutable ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Str => false, @@ -192,12 +194,12 @@ fn is_mutable_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, span: Span, tys: &m || KNOWN_WRAPPER_TYS .iter() .any(|&sym| cx.tcx.is_diagnostic_item(sym, adt.did())) - && substs.types().any(|ty| is_mutable_ty(cx, ty, span, tys)) + && substs.types().any(|ty| is_mutable_ty(cx, ty, tys)) }, - ty::Tuple(substs) => substs.iter().any(|ty| is_mutable_ty(cx, ty, span, tys)), - ty::Array(ty, _) | ty::Slice(ty) => is_mutable_ty(cx, ty, span, tys), + ty::Tuple(substs) => substs.iter().any(|ty| is_mutable_ty(cx, ty, tys)), + ty::Array(ty, _) | ty::Slice(ty) => is_mutable_ty(cx, ty, tys), ty::RawPtr(ty::TypeAndMut { ty, mutbl }) | ty::Ref(_, ty, mutbl) => { - mutbl == hir::Mutability::Mut || is_mutable_ty(cx, ty, span, tys) + mutbl == hir::Mutability::Mut || is_mutable_ty(cx, ty, tys) }, // calling something constitutes a side effect, so return true on all callables // also never calls need not be used, so return true for them, too @@ -225,12 +227,7 @@ fn mutates_static<'tcx>(cx: &LateContext<'tcx>, body: &'tcx hir::Body<'_>) -> bo let mut tys = DefIdSet::default(); for arg in args { if cx.tcx.has_typeck_results(arg.hir_id.owner.to_def_id()) - && is_mutable_ty( - cx, - cx.tcx.typeck(arg.hir_id.owner.def_id).expr_ty(arg), - arg.span, - &mut tys, - ) + && is_mutable_ty(cx, cx.tcx.typeck(arg.hir_id.owner.def_id).expr_ty(arg), &mut tys) && is_mutated_static(arg) { return ControlFlow::Break(()); @@ -243,12 +240,7 @@ fn mutates_static<'tcx>(cx: &LateContext<'tcx>, body: &'tcx hir::Body<'_>) -> bo let mut tys = DefIdSet::default(); for arg in std::iter::once(receiver).chain(args.iter()) { if cx.tcx.has_typeck_results(arg.hir_id.owner.to_def_id()) - && is_mutable_ty( - cx, - cx.tcx.typeck(arg.hir_id.owner.def_id).expr_ty(arg), - arg.span, - &mut tys, - ) + && is_mutable_ty(cx, cx.tcx.typeck(arg.hir_id.owner.def_id).expr_ty(arg), &mut tys) && is_mutated_static(arg) { return ControlFlow::Break(()); diff --git a/clippy_lints/src/functions/result.rs b/clippy_lints/src/functions/result.rs index 5c63fb2acb117..f7e30b051a694 100644 --- a/clippy_lints/src/functions/result.rs +++ b/clippy_lints/src/functions/result.rs @@ -2,12 +2,12 @@ use rustc_errors::Diagnostic; use rustc_hir as hir; use rustc_lint::{LateContext, LintContext}; use rustc_middle::lint::in_external_macro; -use rustc_middle::ty::{self, Ty}; +use rustc_middle::ty::{self, Adt, Ty}; use rustc_span::{sym, Span}; use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_then}; use clippy_utils::trait_ref_of_method; -use clippy_utils::ty::{approx_ty_size, is_type_diagnostic_item}; +use clippy_utils::ty::{approx_ty_size, is_type_diagnostic_item, AdtVariantInfo}; use super::{RESULT_LARGE_ERR, RESULT_UNIT_ERR}; @@ -84,17 +84,57 @@ fn check_result_unit_err(cx: &LateContext<'_>, err_ty: Ty<'_>, fn_header_span: S } fn check_result_large_err<'tcx>(cx: &LateContext<'tcx>, err_ty: Ty<'tcx>, hir_ty_span: Span, large_err_threshold: u64) { - let ty_size = approx_ty_size(cx, err_ty); - if ty_size >= large_err_threshold { - span_lint_and_then( - cx, - RESULT_LARGE_ERR, - hir_ty_span, - "the `Err`-variant returned from this function is very large", - |diag: &mut Diagnostic| { - diag.span_label(hir_ty_span, format!("the `Err`-variant is at least {ty_size} bytes")); - diag.help(format!("try reducing the size of `{err_ty}`, for example by boxing large elements or replacing it with `Box<{err_ty}>`")); - }, - ); + if_chain! { + if let Adt(adt, subst) = err_ty.kind(); + if let Some(local_def_id) = err_ty.ty_adt_def().expect("already checked this is adt").did().as_local(); + if let Some(hir::Node::Item(item)) = cx + .tcx + .hir() + .find_by_def_id(local_def_id); + if let hir::ItemKind::Enum(ref def, _) = item.kind; + then { + let variants_size = AdtVariantInfo::new(cx, *adt, subst); + if variants_size[0].size >= large_err_threshold { + span_lint_and_then( + cx, + RESULT_LARGE_ERR, + hir_ty_span, + "the `Err`-variant returned from this function is very large", + |diag| { + diag.span_label( + def.variants[variants_size[0].ind].span, + format!("the largest variant contains at least {} bytes", variants_size[0].size), + ); + + for variant in &variants_size[1..] { + if variant.size >= large_err_threshold { + let variant_def = &def.variants[variant.ind]; + diag.span_label( + variant_def.span, + format!("the variant `{}` contains at least {} bytes", variant_def.ident, variant.size), + ); + } + } + + diag.help(format!("try reducing the size of `{err_ty}`, for example by boxing large elements or replacing it with `Box<{err_ty}>`")); + } + ); + } + } + else { + let ty_size = approx_ty_size(cx, err_ty); + if ty_size >= large_err_threshold { + span_lint_and_then( + cx, + RESULT_LARGE_ERR, + hir_ty_span, + "the `Err`-variant returned from this function is very large", + |diag: &mut Diagnostic| { + diag.span_label(hir_ty_span, format!("the `Err`-variant is at least {ty_size} bytes")); + diag.help(format!("try reducing the size of `{err_ty}`, for example by boxing large elements or replacing it with `Box<{err_ty}>`")); + }, + ); + } + } } } diff --git a/clippy_lints/src/implicit_hasher.rs b/clippy_lints/src/implicit_hasher.rs index 94e06cf704ba2..64a4a3fa741bc 100644 --- a/clippy_lints/src/implicit_hasher.rs +++ b/clippy_lints/src/implicit_hasher.rs @@ -66,8 +66,8 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitHasher { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) { use rustc_span::BytePos; - fn suggestion<'tcx>( - cx: &LateContext<'tcx>, + fn suggestion( + cx: &LateContext<'_>, diag: &mut Diagnostic, generics_span: Span, generics_suggestion_span: Span, diff --git a/clippy_lints/src/index_refutable_slice.rs b/clippy_lints/src/index_refutable_slice.rs index c7b5badaae51b..0d5099bde6de0 100644 --- a/clippy_lints/src/index_refutable_slice.rs +++ b/clippy_lints/src/index_refutable_slice.rs @@ -207,8 +207,8 @@ impl SliceLintInformation { } } -fn filter_lintable_slices<'a, 'tcx>( - cx: &'a LateContext<'tcx>, +fn filter_lintable_slices<'tcx>( + cx: &LateContext<'tcx>, slice_lint_info: FxIndexMap, max_suggested_slice: u64, scope: &'tcx hir::Expr<'tcx>, diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs index af40a5a8187ee..4cd7dff4cfd76 100644 --- a/clippy_lints/src/indexing_slicing.rs +++ b/clippy_lints/src/indexing_slicing.rs @@ -171,11 +171,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { /// Returns a tuple of options with the start and end (exclusive) values of /// the range. If the start or end is not constant, None is returned. -fn to_const_range<'tcx>( - cx: &LateContext<'tcx>, - range: higher::Range<'_>, - array_size: u128, -) -> (Option, Option) { +fn to_const_range(cx: &LateContext<'_>, range: higher::Range<'_>, array_size: u128) -> (Option, Option) { let s = range .start .map(|expr| constant(cx, cx.typeck_results(), expr).map(|(c, _)| c)); diff --git a/clippy_lints/src/instant_subtraction.rs b/clippy_lints/src/instant_subtraction.rs new file mode 100644 index 0000000000000..60754b224fc84 --- /dev/null +++ b/clippy_lints/src/instant_subtraction.rs @@ -0,0 +1,184 @@ +use clippy_utils::{ + diagnostics::{self, span_lint_and_sugg}, + meets_msrv, msrvs, source, + sugg::Sugg, + ty, +}; +use rustc_errors::Applicability; +use rustc_hir::{BinOpKind, Expr, ExprKind}; +use rustc_lint::{LateContext, LateLintPass}; +use rustc_semver::RustcVersion; +use rustc_session::{declare_tool_lint, impl_lint_pass}; +use rustc_span::{source_map::Spanned, sym}; + +declare_clippy_lint! { + /// ### What it does + /// Lints subtraction between `Instant::now()` and another `Instant`. + /// + /// ### Why is this bad? + /// It is easy to accidentally write `prev_instant - Instant::now()`, which will always be 0ns + /// as `Instant` subtraction saturates. + /// + /// `prev_instant.elapsed()` also more clearly signals intention. + /// + /// ### Example + /// ```rust + /// use std::time::Instant; + /// let prev_instant = Instant::now(); + /// let duration = Instant::now() - prev_instant; + /// ``` + /// Use instead: + /// ```rust + /// use std::time::Instant; + /// let prev_instant = Instant::now(); + /// let duration = prev_instant.elapsed(); + /// ``` + #[clippy::version = "1.65.0"] + pub MANUAL_INSTANT_ELAPSED, + pedantic, + "subtraction between `Instant::now()` and previous `Instant`" +} + +declare_clippy_lint! { + /// ### What it does + /// Lints subtraction between an [`Instant`] and a [`Duration`]. + /// + /// ### Why is this bad? + /// Unchecked subtraction could cause underflow on certain platforms, leading to + /// unintentional panics. + /// + /// ### Example + /// ```rust + /// # use std::time::{Instant, Duration}; + /// let time_passed = Instant::now() - Duration::from_secs(5); + /// ``` + /// + /// Use instead: + /// ```rust + /// # use std::time::{Instant, Duration}; + /// let time_passed = Instant::now().checked_sub(Duration::from_secs(5)); + /// ``` + /// + /// [`Duration`]: std::time::Duration + /// [`Instant::now()`]: std::time::Instant::now; + #[clippy::version = "1.65.0"] + pub UNCHECKED_DURATION_SUBTRACTION, + suspicious, + "finds unchecked subtraction of a 'Duration' from an 'Instant'" +} + +pub struct InstantSubtraction { + msrv: Option, +} + +impl InstantSubtraction { + #[must_use] + pub fn new(msrv: Option) -> Self { + Self { msrv } + } +} + +impl_lint_pass!(InstantSubtraction => [MANUAL_INSTANT_ELAPSED, UNCHECKED_DURATION_SUBTRACTION]); + +impl LateLintPass<'_> for InstantSubtraction { + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) { + if let ExprKind::Binary( + Spanned { + node: BinOpKind::Sub, .. + }, + lhs, + rhs, + ) = expr.kind + { + if_chain! { + if is_instant_now_call(cx, lhs); + + if is_an_instant(cx, rhs); + if let Some(sugg) = Sugg::hir_opt(cx, rhs); + + then { + print_manual_instant_elapsed_sugg(cx, expr, sugg) + } else { + if_chain! { + if !expr.span.from_expansion(); + if meets_msrv(self.msrv, msrvs::TRY_FROM); + + if is_an_instant(cx, lhs); + if is_a_duration(cx, rhs); + + then { + print_unchecked_duration_subtraction_sugg(cx, lhs, rhs, expr) + } + } + } + } + } + } + + extract_msrv_attr!(LateContext); +} + +fn is_instant_now_call(cx: &LateContext<'_>, expr_block: &'_ Expr<'_>) -> bool { + if let ExprKind::Call(fn_expr, []) = expr_block.kind + && let Some(fn_id) = clippy_utils::path_def_id(cx, fn_expr) + && clippy_utils::match_def_path(cx, fn_id, &clippy_utils::paths::INSTANT_NOW) + { + true + } else { + false + } +} + +fn is_an_instant(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { + let expr_ty = cx.typeck_results().expr_ty(expr); + + match expr_ty.kind() { + rustc_middle::ty::Adt(def, _) => clippy_utils::match_def_path(cx, def.did(), &clippy_utils::paths::INSTANT), + _ => false, + } +} + +fn is_a_duration(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { + let expr_ty = cx.typeck_results().expr_ty(expr); + ty::is_type_diagnostic_item(cx, expr_ty, sym::Duration) +} + +fn print_manual_instant_elapsed_sugg(cx: &LateContext<'_>, expr: &Expr<'_>, sugg: Sugg<'_>) { + span_lint_and_sugg( + cx, + MANUAL_INSTANT_ELAPSED, + expr.span, + "manual implementation of `Instant::elapsed`", + "try", + format!("{}.elapsed()", sugg.maybe_par()), + Applicability::MachineApplicable, + ); +} + +fn print_unchecked_duration_subtraction_sugg( + cx: &LateContext<'_>, + left_expr: &Expr<'_>, + right_expr: &Expr<'_>, + expr: &Expr<'_>, +) { + let mut applicability = Applicability::MachineApplicable; + + let left_expr = + source::snippet_with_applicability(cx, left_expr.span, "std::time::Instant::now()", &mut applicability); + let right_expr = source::snippet_with_applicability( + cx, + right_expr.span, + "std::time::Duration::from_secs(1)", + &mut applicability, + ); + + diagnostics::span_lint_and_sugg( + cx, + UNCHECKED_DURATION_SUBTRACTION, + expr.span, + "unchecked subtraction of a 'Duration' from an 'Instant'", + "try", + format!("{left_expr}.checked_sub({right_expr}).unwrap()"), + applicability, + ); +} diff --git a/clippy_lints/src/int_plus_one.rs b/clippy_lints/src/int_plus_one.rs index f793abdfda34a..1b14e525d9a84 100644 --- a/clippy_lints/src/int_plus_one.rs +++ b/clippy_lints/src/int_plus_one.rs @@ -63,58 +63,54 @@ impl IntPlusOne { fn check_binop(cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option { match (binop, &lhs.kind, &rhs.kind) { // case where `x - 1 >= ...` or `-1 + x >= ...` - (BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => { + (BinOpKind::Ge, ExprKind::Binary(lhskind, lhslhs, lhsrhs), _) => { match (lhskind.node, &lhslhs.kind, &lhsrhs.kind) { // `-1 + x` - (BinOpKind::Add, &ExprKind::Lit(lit), _) if Self::check_lit(lit, -1) => { + (BinOpKind::Add, ExprKind::Lit(lit), _) if Self::check_lit(*lit, -1) => { Self::generate_recommendation(cx, binop, lhsrhs, rhs, Side::Lhs) }, // `x - 1` - (BinOpKind::Sub, _, &ExprKind::Lit(lit)) if Self::check_lit(lit, 1) => { + (BinOpKind::Sub, _, ExprKind::Lit(lit)) if Self::check_lit(*lit, 1) => { Self::generate_recommendation(cx, binop, lhslhs, rhs, Side::Lhs) }, _ => None, } }, // case where `... >= y + 1` or `... >= 1 + y` - (BinOpKind::Ge, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) - if rhskind.node == BinOpKind::Add => - { + (BinOpKind::Ge, _, ExprKind::Binary(rhskind, rhslhs, rhsrhs)) if rhskind.node == BinOpKind::Add => { match (&rhslhs.kind, &rhsrhs.kind) { // `y + 1` and `1 + y` - (&ExprKind::Lit(lit), _) if Self::check_lit(lit, 1) => { + (ExprKind::Lit(lit), _) if Self::check_lit(*lit, 1) => { Self::generate_recommendation(cx, binop, rhsrhs, lhs, Side::Rhs) }, - (_, &ExprKind::Lit(lit)) if Self::check_lit(lit, 1) => { + (_, ExprKind::Lit(lit)) if Self::check_lit(*lit, 1) => { Self::generate_recommendation(cx, binop, rhslhs, lhs, Side::Rhs) }, _ => None, } }, // case where `x + 1 <= ...` or `1 + x <= ...` - (BinOpKind::Le, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) - if lhskind.node == BinOpKind::Add => - { + (BinOpKind::Le, ExprKind::Binary(lhskind, lhslhs, lhsrhs), _) if lhskind.node == BinOpKind::Add => { match (&lhslhs.kind, &lhsrhs.kind) { // `1 + x` and `x + 1` - (&ExprKind::Lit(lit), _) if Self::check_lit(lit, 1) => { + (ExprKind::Lit(lit), _) if Self::check_lit(*lit, 1) => { Self::generate_recommendation(cx, binop, lhsrhs, rhs, Side::Lhs) }, - (_, &ExprKind::Lit(lit)) if Self::check_lit(lit, 1) => { + (_, ExprKind::Lit(lit)) if Self::check_lit(*lit, 1) => { Self::generate_recommendation(cx, binop, lhslhs, rhs, Side::Lhs) }, _ => None, } }, // case where `... >= y - 1` or `... >= -1 + y` - (BinOpKind::Le, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) => { + (BinOpKind::Le, _, ExprKind::Binary(rhskind, rhslhs, rhsrhs)) => { match (rhskind.node, &rhslhs.kind, &rhsrhs.kind) { // `-1 + y` - (BinOpKind::Add, &ExprKind::Lit(lit), _) if Self::check_lit(lit, -1) => { + (BinOpKind::Add, ExprKind::Lit(lit), _) if Self::check_lit(*lit, -1) => { Self::generate_recommendation(cx, binop, rhsrhs, lhs, Side::Rhs) }, // `y - 1` - (BinOpKind::Sub, _, &ExprKind::Lit(lit)) if Self::check_lit(lit, 1) => { + (BinOpKind::Sub, _, ExprKind::Lit(lit)) if Self::check_lit(*lit, 1) => { Self::generate_recommendation(cx, binop, rhslhs, lhs, Side::Rhs) }, _ => None, diff --git a/clippy_lints/src/invalid_upcast_comparisons.rs b/clippy_lints/src/invalid_upcast_comparisons.rs index 0ef77e03de906..6ea637412d5b1 100644 --- a/clippy_lints/src/invalid_upcast_comparisons.rs +++ b/clippy_lints/src/invalid_upcast_comparisons.rs @@ -38,7 +38,7 @@ declare_clippy_lint! { declare_lint_pass!(InvalidUpcastComparisons => [INVALID_UPCAST_COMPARISONS]); -fn numeric_cast_precast_bounds<'a>(cx: &LateContext<'_>, expr: &'a Expr<'_>) -> Option<(FullInt, FullInt)> { +fn numeric_cast_precast_bounds(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<(FullInt, FullInt)> { if let ExprKind::Cast(cast_exp, _) = expr.kind { let pre_cast_ty = cx.typeck_results().expr_ty(cast_exp); let cast_ty = cx.typeck_results().expr_ty(expr); diff --git a/clippy_lints/src/large_enum_variant.rs b/clippy_lints/src/large_enum_variant.rs index 06e957285499c..b18456ee52340 100644 --- a/clippy_lints/src/large_enum_variant.rs +++ b/clippy_lints/src/large_enum_variant.rs @@ -1,12 +1,15 @@ //! lint when there is a large size difference between variants on an enum use clippy_utils::source::snippet_with_applicability; -use clippy_utils::{diagnostics::span_lint_and_then, ty::approx_ty_size, ty::is_copy}; +use clippy_utils::{ + diagnostics::span_lint_and_then, + ty::{approx_ty_size, is_copy, AdtVariantInfo}, +}; use rustc_errors::Applicability; use rustc_hir::{Item, ItemKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::lint::in_external_macro; -use rustc_middle::ty::{Adt, AdtDef, GenericArg, List, Ty}; +use rustc_middle::ty::{Adt, Ty}; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::source_map::Span; @@ -72,49 +75,6 @@ impl LargeEnumVariant { } } -struct FieldInfo { - ind: usize, - size: u64, -} - -struct VariantInfo { - ind: usize, - size: u64, - fields_size: Vec, -} - -fn variants_size<'tcx>( - cx: &LateContext<'tcx>, - adt: AdtDef<'tcx>, - subst: &'tcx List>, -) -> Vec { - let mut variants_size = adt - .variants() - .iter() - .enumerate() - .map(|(i, variant)| { - let mut fields_size = variant - .fields - .iter() - .enumerate() - .map(|(i, f)| FieldInfo { - ind: i, - size: approx_ty_size(cx, f.ty(cx.tcx, subst)), - }) - .collect::>(); - fields_size.sort_by(|a, b| (a.size.cmp(&b.size))); - - VariantInfo { - ind: i, - size: fields_size.iter().map(|info| info.size).sum(), - fields_size, - } - }) - .collect::>(); - variants_size.sort_by(|a, b| (b.size.cmp(&a.size))); - variants_size -} - impl_lint_pass!(LargeEnumVariant => [LARGE_ENUM_VARIANT]); impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant { @@ -130,7 +90,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant { if adt.variants().len() <= 1 { return; } - let variants_size = variants_size(cx, *adt, subst); + let variants_size = AdtVariantInfo::new(cx, *adt, subst); let mut difference = variants_size[0].size - variants_size[1].size; if difference > self.maximum_size_difference_allowed { @@ -173,16 +133,16 @@ impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant { .fields_size .iter() .rev() - .map_while(|val| { + .map_while(|&(ind, size)| { if difference > self.maximum_size_difference_allowed { - difference = difference.saturating_sub(val.size); + difference = difference.saturating_sub(size); Some(( - fields[val.ind].ty.span, + fields[ind].ty.span, format!( "Box<{}>", snippet_with_applicability( cx, - fields[val.ind].ty.span, + fields[ind].ty.span, "..", &mut applicability ) diff --git a/clippy_lints/src/len_zero.rs b/clippy_lints/src/len_zero.rs index b0cba40c27a5d..4c133c06a157a 100644 --- a/clippy_lints/src/len_zero.rs +++ b/clippy_lints/src/len_zero.rs @@ -366,8 +366,7 @@ fn check_for_is_empty<'tcx>( } fn check_cmp(cx: &LateContext<'_>, span: Span, method: &Expr<'_>, lit: &Expr<'_>, op: &str, compare_to: u32) { - if let (&ExprKind::MethodCall(method_path, receiver, args, _), &ExprKind::Lit(ref lit)) = (&method.kind, &lit.kind) - { + if let (&ExprKind::MethodCall(method_path, receiver, args, _), ExprKind::Lit(lit)) = (&method.kind, &lit.kind) { // check if we are in an is_empty() method if let Some(name) = get_item_name(cx, method) { if name.as_str() == "is_empty" { diff --git a/clippy_lints/src/let_underscore.rs b/clippy_lints/src/let_underscore.rs index b7798b1c1d749..61f87b91400d7 100644 --- a/clippy_lints/src/let_underscore.rs +++ b/clippy_lints/src/let_underscore.rs @@ -1,13 +1,11 @@ use clippy_utils::diagnostics::span_lint_and_help; -use clippy_utils::ty::{is_must_use_ty, is_type_diagnostic_item, match_type}; +use clippy_utils::ty::{implements_trait, is_must_use_ty, match_type}; use clippy_utils::{is_must_use_func_call, paths}; -use if_chain::if_chain; use rustc_hir::{Local, PatKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::lint::in_external_macro; use rustc_middle::ty::subst::GenericArgKind; use rustc_session::{declare_lint_pass, declare_tool_lint}; -use rustc_span::{sym, Symbol}; declare_clippy_lint! { /// ### What it does @@ -30,13 +28,14 @@ declare_clippy_lint! { #[clippy::version = "1.42.0"] pub LET_UNDERSCORE_MUST_USE, restriction, - "non-binding let on a `#[must_use]` expression" + "non-binding `let` on a `#[must_use]` expression" } declare_clippy_lint! { /// ### What it does - /// Checks for `let _ = sync_lock`. - /// This supports `mutex` and `rwlock` in `std::sync` and `parking_lot`. + /// Checks for `let _ = sync_lock`. This supports `mutex` and `rwlock` in + /// `parking_lot`. For `std` locks see the `rustc` lint + /// [`let_underscore_lock`](https://doc.rust-lang.org/nightly/rustc/lints/listing/deny-by-default.html#let-underscore-lock) /// /// ### Why is this bad? /// This statement immediately drops the lock instead of @@ -57,50 +56,41 @@ declare_clippy_lint! { #[clippy::version = "1.43.0"] pub LET_UNDERSCORE_LOCK, correctness, - "non-binding let on a synchronization lock" + "non-binding `let` on a synchronization lock" } declare_clippy_lint! { /// ### What it does - /// Checks for `let _ = ` - /// where expr has a type that implements `Drop` + /// Checks for `let _ = ` where the resulting type of expr implements `Future` /// /// ### Why is this bad? - /// This statement immediately drops the initializer - /// expression instead of extending its lifetime to the end of the scope, which - /// is often not intended. To extend the expression's lifetime to the end of the - /// scope, use an underscore-prefixed name instead (i.e. _var). If you want to - /// explicitly drop the expression, `std::mem::drop` conveys your intention - /// better and is less error-prone. + /// Futures must be polled for work to be done. The original intention was most likely to await the future + /// and ignore the resulting value. /// /// ### Example /// ```rust - /// # struct DroppableItem; - /// { - /// let _ = DroppableItem; - /// // ^ dropped here - /// /* more code */ + /// async fn foo() -> Result<(), ()> { + /// Ok(()) /// } + /// let _ = foo(); /// ``` /// /// Use instead: /// ```rust - /// # struct DroppableItem; - /// { - /// let _droppable = DroppableItem; - /// /* more code */ - /// // dropped at end of scope + /// # async fn context() { + /// async fn foo() -> Result<(), ()> { + /// Ok(()) /// } + /// let _ = foo().await; + /// # } /// ``` - #[clippy::version = "1.50.0"] - pub LET_UNDERSCORE_DROP, - pedantic, - "non-binding let on a type that implements `Drop`" + #[clippy::version = "1.66"] + pub LET_UNDERSCORE_FUTURE, + suspicious, + "non-binding `let` on a future" } -declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE, LET_UNDERSCORE_LOCK, LET_UNDERSCORE_DROP]); - -const SYNC_GUARD_SYMS: [Symbol; 3] = [sym::MutexGuard, sym::RwLockReadGuard, sym::RwLockWriteGuard]; +declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE, LET_UNDERSCORE_LOCK, LET_UNDERSCORE_FUTURE]); const SYNC_GUARD_PATHS: [&[&str]; 3] = [ &paths::PARKING_LOT_MUTEX_GUARD, @@ -110,64 +100,53 @@ const SYNC_GUARD_PATHS: [&[&str]; 3] = [ impl<'tcx> LateLintPass<'tcx> for LetUnderscore { fn check_local(&mut self, cx: &LateContext<'_>, local: &Local<'_>) { - if in_external_macro(cx.tcx.sess, local.span) { - return; - } - - if_chain! { - if let PatKind::Wild = local.pat.kind; - if let Some(init) = local.init; - then { - let init_ty = cx.typeck_results().expr_ty(init); - let contains_sync_guard = init_ty.walk().any(|inner| match inner.unpack() { - GenericArgKind::Type(inner_ty) => { - SYNC_GUARD_SYMS - .iter() - .any(|&sym| is_type_diagnostic_item(cx, inner_ty, sym)) - || SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path)) - }, - - GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false, - }); - if contains_sync_guard { - span_lint_and_help( - cx, - LET_UNDERSCORE_LOCK, - local.span, - "non-binding let on a synchronization lock", - None, - "consider using an underscore-prefixed named \ - binding or dropping explicitly with `std::mem::drop`", - ); - } else if init_ty.needs_drop(cx.tcx, cx.param_env) { - span_lint_and_help( - cx, - LET_UNDERSCORE_DROP, - local.span, - "non-binding `let` on a type that implements `Drop`", - None, - "consider using an underscore-prefixed named \ + if !in_external_macro(cx.tcx.sess, local.span) + && let PatKind::Wild = local.pat.kind + && let Some(init) = local.init + { + let init_ty = cx.typeck_results().expr_ty(init); + let contains_sync_guard = init_ty.walk().any(|inner| match inner.unpack() { + GenericArgKind::Type(inner_ty) => SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path)), + GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false, + }); + if contains_sync_guard { + span_lint_and_help( + cx, + LET_UNDERSCORE_LOCK, + local.span, + "non-binding `let` on a synchronization lock", + None, + "consider using an underscore-prefixed named \ binding or dropping explicitly with `std::mem::drop`", - ); - } else if is_must_use_ty(cx, cx.typeck_results().expr_ty(init)) { - span_lint_and_help( - cx, - LET_UNDERSCORE_MUST_USE, - local.span, - "non-binding let on an expression with `#[must_use]` type", - None, - "consider explicitly using expression value", - ); - } else if is_must_use_func_call(cx, init) { - span_lint_and_help( - cx, - LET_UNDERSCORE_MUST_USE, - local.span, - "non-binding let on a result of a `#[must_use]` function", - None, - "consider explicitly using function result", - ); - } + ); + } else if let Some(future_trait_def_id) = cx.tcx.lang_items().future_trait() + && implements_trait(cx, cx.typeck_results().expr_ty(init), future_trait_def_id, &[]) { + span_lint_and_help( + cx, + LET_UNDERSCORE_FUTURE, + local.span, + "non-binding `let` on a future", + None, + "consider awaiting the future or dropping explicitly with `std::mem::drop`" + ); + } else if is_must_use_ty(cx, cx.typeck_results().expr_ty(init)) { + span_lint_and_help( + cx, + LET_UNDERSCORE_MUST_USE, + local.span, + "non-binding `let` on an expression with `#[must_use]` type", + None, + "consider explicitly using expression value", + ); + } else if is_must_use_func_call(cx, init) { + span_lint_and_help( + cx, + LET_UNDERSCORE_MUST_USE, + local.span, + "non-binding `let` on a result of a `#[must_use]` function", + None, + "consider explicitly using function result", + ); } } } diff --git a/clippy_lints/src/lib.register_all.rs b/clippy_lints/src/lib.register_all.rs deleted file mode 100644 index c455e1561b716..0000000000000 --- a/clippy_lints/src/lib.register_all.rs +++ /dev/null @@ -1,368 +0,0 @@ -// This file was generated by `cargo dev update_lints`. -// Use that command to update this file and do not edit by hand. -// Manual edits will be overwritten. - -store.register_group(true, "clippy::all", Some("clippy_all"), vec![ - LintId::of(almost_complete_letter_range::ALMOST_COMPLETE_LETTER_RANGE), - LintId::of(approx_const::APPROX_CONSTANT), - LintId::of(assertions_on_constants::ASSERTIONS_ON_CONSTANTS), - LintId::of(async_yields_async::ASYNC_YIELDS_ASYNC), - LintId::of(attrs::BLANKET_CLIPPY_RESTRICTION_LINTS), - LintId::of(attrs::DEPRECATED_CFG_ATTR), - LintId::of(attrs::DEPRECATED_SEMVER), - LintId::of(attrs::MISMATCHED_TARGET_OS), - LintId::of(attrs::USELESS_ATTRIBUTE), - LintId::of(await_holding_invalid::AWAIT_HOLDING_INVALID_TYPE), - LintId::of(await_holding_invalid::AWAIT_HOLDING_LOCK), - LintId::of(await_holding_invalid::AWAIT_HOLDING_REFCELL_REF), - LintId::of(blocks_in_if_conditions::BLOCKS_IN_IF_CONDITIONS), - LintId::of(bool_assert_comparison::BOOL_ASSERT_COMPARISON), - LintId::of(bool_to_int_with_if::BOOL_TO_INT_WITH_IF), - LintId::of(booleans::NONMINIMAL_BOOL), - LintId::of(booleans::OVERLY_COMPLEX_BOOL_EXPR), - LintId::of(borrow_deref_ref::BORROW_DEREF_REF), - LintId::of(box_default::BOX_DEFAULT), - LintId::of(casts::CAST_ABS_TO_UNSIGNED), - LintId::of(casts::CAST_ENUM_CONSTRUCTOR), - LintId::of(casts::CAST_ENUM_TRUNCATION), - LintId::of(casts::CAST_NAN_TO_INT), - LintId::of(casts::CAST_REF_TO_MUT), - LintId::of(casts::CAST_SLICE_DIFFERENT_SIZES), - LintId::of(casts::CAST_SLICE_FROM_RAW_PARTS), - LintId::of(casts::CHAR_LIT_AS_U8), - LintId::of(casts::FN_TO_NUMERIC_CAST), - LintId::of(casts::FN_TO_NUMERIC_CAST_WITH_TRUNCATION), - LintId::of(casts::UNNECESSARY_CAST), - LintId::of(collapsible_if::COLLAPSIBLE_ELSE_IF), - LintId::of(collapsible_if::COLLAPSIBLE_IF), - LintId::of(comparison_chain::COMPARISON_CHAIN), - LintId::of(copies::IFS_SAME_COND), - LintId::of(copies::IF_SAME_THEN_ELSE), - LintId::of(crate_in_macro_def::CRATE_IN_MACRO_DEF), - LintId::of(default::FIELD_REASSIGN_WITH_DEFAULT), - LintId::of(default_instead_of_iter_empty::DEFAULT_INSTEAD_OF_ITER_EMPTY), - LintId::of(dereference::EXPLICIT_AUTO_DEREF), - LintId::of(dereference::NEEDLESS_BORROW), - LintId::of(derivable_impls::DERIVABLE_IMPLS), - LintId::of(derive::DERIVE_HASH_XOR_EQ), - LintId::of(derive::DERIVE_ORD_XOR_PARTIAL_ORD), - LintId::of(disallowed_macros::DISALLOWED_MACROS), - LintId::of(disallowed_methods::DISALLOWED_METHODS), - LintId::of(disallowed_names::DISALLOWED_NAMES), - LintId::of(disallowed_types::DISALLOWED_TYPES), - LintId::of(doc::MISSING_SAFETY_DOC), - LintId::of(doc::NEEDLESS_DOCTEST_MAIN), - LintId::of(double_parens::DOUBLE_PARENS), - LintId::of(drop_forget_ref::DROP_COPY), - LintId::of(drop_forget_ref::DROP_NON_DROP), - LintId::of(drop_forget_ref::DROP_REF), - LintId::of(drop_forget_ref::FORGET_COPY), - LintId::of(drop_forget_ref::FORGET_NON_DROP), - LintId::of(drop_forget_ref::FORGET_REF), - LintId::of(drop_forget_ref::UNDROPPED_MANUALLY_DROPS), - LintId::of(duplicate_mod::DUPLICATE_MOD), - LintId::of(entry::MAP_ENTRY), - LintId::of(enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT), - LintId::of(enum_variants::ENUM_VARIANT_NAMES), - LintId::of(enum_variants::MODULE_INCEPTION), - LintId::of(escape::BOXED_LOCAL), - LintId::of(eta_reduction::REDUNDANT_CLOSURE), - LintId::of(explicit_write::EXPLICIT_WRITE), - LintId::of(float_literal::EXCESSIVE_PRECISION), - LintId::of(format::USELESS_FORMAT), - LintId::of(format_args::FORMAT_IN_FORMAT_ARGS), - LintId::of(format_args::TO_STRING_IN_FORMAT_ARGS), - LintId::of(format_args::UNUSED_FORMAT_SPECS), - LintId::of(format_impl::PRINT_IN_FORMAT_IMPL), - LintId::of(format_impl::RECURSIVE_FORMAT_IMPL), - LintId::of(formatting::POSSIBLE_MISSING_COMMA), - LintId::of(formatting::SUSPICIOUS_ASSIGNMENT_FORMATTING), - LintId::of(formatting::SUSPICIOUS_ELSE_FORMATTING), - LintId::of(formatting::SUSPICIOUS_UNARY_OP_FORMATTING), - LintId::of(from_over_into::FROM_OVER_INTO), - LintId::of(from_str_radix_10::FROM_STR_RADIX_10), - LintId::of(functions::DOUBLE_MUST_USE), - LintId::of(functions::MUST_USE_UNIT), - LintId::of(functions::NOT_UNSAFE_PTR_ARG_DEREF), - LintId::of(functions::RESULT_LARGE_ERR), - LintId::of(functions::RESULT_UNIT_ERR), - LintId::of(functions::TOO_MANY_ARGUMENTS), - LintId::of(if_let_mutex::IF_LET_MUTEX), - LintId::of(implicit_saturating_add::IMPLICIT_SATURATING_ADD), - LintId::of(implicit_saturating_sub::IMPLICIT_SATURATING_SUB), - LintId::of(indexing_slicing::OUT_OF_BOUNDS_INDEXING), - LintId::of(infinite_iter::INFINITE_ITER), - LintId::of(inherent_to_string::INHERENT_TO_STRING), - LintId::of(inherent_to_string::INHERENT_TO_STRING_SHADOW_DISPLAY), - LintId::of(init_numbered_fields::INIT_NUMBERED_FIELDS), - LintId::of(inline_fn_without_body::INLINE_FN_WITHOUT_BODY), - LintId::of(int_plus_one::INT_PLUS_ONE), - LintId::of(invalid_utf8_in_unchecked::INVALID_UTF8_IN_UNCHECKED), - LintId::of(large_const_arrays::LARGE_CONST_ARRAYS), - LintId::of(large_enum_variant::LARGE_ENUM_VARIANT), - LintId::of(len_zero::COMPARISON_TO_EMPTY), - LintId::of(len_zero::LEN_WITHOUT_IS_EMPTY), - LintId::of(len_zero::LEN_ZERO), - LintId::of(let_underscore::LET_UNDERSCORE_LOCK), - LintId::of(lifetimes::EXTRA_UNUSED_LIFETIMES), - LintId::of(lifetimes::NEEDLESS_LIFETIMES), - LintId::of(literal_representation::INCONSISTENT_DIGIT_GROUPING), - LintId::of(literal_representation::MISTYPED_LITERAL_SUFFIXES), - LintId::of(literal_representation::UNUSUAL_BYTE_GROUPINGS), - LintId::of(loops::EMPTY_LOOP), - LintId::of(loops::EXPLICIT_COUNTER_LOOP), - LintId::of(loops::FOR_KV_MAP), - LintId::of(loops::ITER_NEXT_LOOP), - LintId::of(loops::MANUAL_FIND), - LintId::of(loops::MANUAL_FLATTEN), - LintId::of(loops::MANUAL_MEMCPY), - LintId::of(loops::MISSING_SPIN_LOOP), - LintId::of(loops::MUT_RANGE_BOUND), - LintId::of(loops::NEEDLESS_COLLECT), - LintId::of(loops::NEEDLESS_RANGE_LOOP), - LintId::of(loops::NEVER_LOOP), - LintId::of(loops::SAME_ITEM_PUSH), - LintId::of(loops::SINGLE_ELEMENT_LOOP), - LintId::of(loops::WHILE_IMMUTABLE_CONDITION), - LintId::of(loops::WHILE_LET_LOOP), - LintId::of(loops::WHILE_LET_ON_ITERATOR), - LintId::of(main_recursion::MAIN_RECURSION), - LintId::of(manual_async_fn::MANUAL_ASYNC_FN), - LintId::of(manual_bits::MANUAL_BITS), - LintId::of(manual_clamp::MANUAL_CLAMP), - LintId::of(manual_non_exhaustive::MANUAL_NON_EXHAUSTIVE), - LintId::of(manual_rem_euclid::MANUAL_REM_EUCLID), - LintId::of(manual_retain::MANUAL_RETAIN), - LintId::of(manual_strip::MANUAL_STRIP), - LintId::of(map_unit_fn::OPTION_MAP_UNIT_FN), - LintId::of(map_unit_fn::RESULT_MAP_UNIT_FN), - LintId::of(match_result_ok::MATCH_RESULT_OK), - LintId::of(matches::COLLAPSIBLE_MATCH), - LintId::of(matches::INFALLIBLE_DESTRUCTURING_MATCH), - LintId::of(matches::MANUAL_FILTER), - LintId::of(matches::MANUAL_MAP), - LintId::of(matches::MANUAL_UNWRAP_OR), - LintId::of(matches::MATCH_AS_REF), - LintId::of(matches::MATCH_LIKE_MATCHES_MACRO), - LintId::of(matches::MATCH_OVERLAPPING_ARM), - LintId::of(matches::MATCH_REF_PATS), - LintId::of(matches::MATCH_SINGLE_BINDING), - LintId::of(matches::MATCH_STR_CASE_MISMATCH), - LintId::of(matches::NEEDLESS_MATCH), - LintId::of(matches::REDUNDANT_PATTERN_MATCHING), - LintId::of(matches::SINGLE_MATCH), - LintId::of(matches::WILDCARD_IN_OR_PATTERNS), - LintId::of(mem_replace::MEM_REPLACE_OPTION_WITH_NONE), - LintId::of(mem_replace::MEM_REPLACE_WITH_DEFAULT), - LintId::of(mem_replace::MEM_REPLACE_WITH_UNINIT), - LintId::of(methods::BIND_INSTEAD_OF_MAP), - LintId::of(methods::BYTES_COUNT_TO_LEN), - LintId::of(methods::BYTES_NTH), - LintId::of(methods::CHARS_LAST_CMP), - LintId::of(methods::CHARS_NEXT_CMP), - LintId::of(methods::CLONE_DOUBLE_REF), - LintId::of(methods::CLONE_ON_COPY), - LintId::of(methods::COLLAPSIBLE_STR_REPLACE), - LintId::of(methods::ERR_EXPECT), - LintId::of(methods::EXPECT_FUN_CALL), - LintId::of(methods::EXTEND_WITH_DRAIN), - LintId::of(methods::FILTER_MAP_IDENTITY), - LintId::of(methods::FILTER_NEXT), - LintId::of(methods::FLAT_MAP_IDENTITY), - LintId::of(methods::GET_FIRST), - LintId::of(methods::GET_LAST_WITH_LEN), - LintId::of(methods::INSPECT_FOR_EACH), - LintId::of(methods::INTO_ITER_ON_REF), - LintId::of(methods::IS_DIGIT_ASCII_RADIX), - LintId::of(methods::ITERATOR_STEP_BY_ZERO), - LintId::of(methods::ITER_CLONED_COLLECT), - LintId::of(methods::ITER_COUNT), - LintId::of(methods::ITER_KV_MAP), - LintId::of(methods::ITER_NEXT_SLICE), - LintId::of(methods::ITER_NTH), - LintId::of(methods::ITER_NTH_ZERO), - LintId::of(methods::ITER_OVEREAGER_CLONED), - LintId::of(methods::ITER_SKIP_NEXT), - LintId::of(methods::MANUAL_FILTER_MAP), - LintId::of(methods::MANUAL_FIND_MAP), - LintId::of(methods::MANUAL_SATURATING_ARITHMETIC), - LintId::of(methods::MANUAL_SPLIT_ONCE), - LintId::of(methods::MANUAL_STR_REPEAT), - LintId::of(methods::MAP_CLONE), - LintId::of(methods::MAP_COLLECT_RESULT_UNIT), - LintId::of(methods::MAP_FLATTEN), - LintId::of(methods::MAP_IDENTITY), - LintId::of(methods::MUT_MUTEX_LOCK), - LintId::of(methods::NEEDLESS_OPTION_AS_DEREF), - LintId::of(methods::NEEDLESS_OPTION_TAKE), - LintId::of(methods::NEEDLESS_SPLITN), - LintId::of(methods::NEW_RET_NO_SELF), - LintId::of(methods::NONSENSICAL_OPEN_OPTIONS), - LintId::of(methods::NO_EFFECT_REPLACE), - LintId::of(methods::OBFUSCATED_IF_ELSE), - LintId::of(methods::OK_EXPECT), - LintId::of(methods::OPTION_AS_REF_DEREF), - LintId::of(methods::OPTION_FILTER_MAP), - LintId::of(methods::OPTION_MAP_OR_NONE), - LintId::of(methods::OR_FUN_CALL), - LintId::of(methods::OR_THEN_UNWRAP), - LintId::of(methods::RANGE_ZIP_WITH_LEN), - LintId::of(methods::REPEAT_ONCE), - LintId::of(methods::RESULT_MAP_OR_INTO_OPTION), - LintId::of(methods::SEARCH_IS_SOME), - LintId::of(methods::SHOULD_IMPLEMENT_TRAIT), - LintId::of(methods::SINGLE_CHAR_ADD_STR), - LintId::of(methods::SINGLE_CHAR_PATTERN), - LintId::of(methods::SKIP_WHILE_NEXT), - LintId::of(methods::STRING_EXTEND_CHARS), - LintId::of(methods::SUSPICIOUS_MAP), - LintId::of(methods::SUSPICIOUS_SPLITN), - LintId::of(methods::SUSPICIOUS_TO_OWNED), - LintId::of(methods::UNINIT_ASSUMED_INIT), - LintId::of(methods::UNIT_HASH), - LintId::of(methods::UNNECESSARY_FILTER_MAP), - LintId::of(methods::UNNECESSARY_FIND_MAP), - LintId::of(methods::UNNECESSARY_FOLD), - LintId::of(methods::UNNECESSARY_LAZY_EVALUATIONS), - LintId::of(methods::UNNECESSARY_SORT_BY), - LintId::of(methods::UNNECESSARY_TO_OWNED), - LintId::of(methods::UNWRAP_OR_ELSE_DEFAULT), - LintId::of(methods::USELESS_ASREF), - LintId::of(methods::VEC_RESIZE_TO_ZERO), - LintId::of(methods::WRONG_SELF_CONVENTION), - LintId::of(methods::ZST_OFFSET), - LintId::of(minmax::MIN_MAX), - LintId::of(misc::SHORT_CIRCUIT_STATEMENT), - LintId::of(misc::TOPLEVEL_REF_ARG), - LintId::of(misc::ZERO_PTR), - LintId::of(misc_early::BUILTIN_TYPE_SHADOW), - LintId::of(misc_early::DOUBLE_NEG), - LintId::of(misc_early::DUPLICATE_UNDERSCORE_ARGUMENT), - LintId::of(misc_early::MIXED_CASE_HEX_LITERALS), - LintId::of(misc_early::REDUNDANT_PATTERN), - LintId::of(misc_early::UNNEEDED_WILDCARD_PATTERN), - LintId::of(misc_early::ZERO_PREFIXED_LITERAL), - LintId::of(mixed_read_write_in_expression::DIVERGING_SUB_EXPRESSION), - LintId::of(multi_assignments::MULTI_ASSIGNMENTS), - LintId::of(mut_key::MUTABLE_KEY_TYPE), - LintId::of(mut_reference::UNNECESSARY_MUT_PASSED), - LintId::of(needless_arbitrary_self_type::NEEDLESS_ARBITRARY_SELF_TYPE), - LintId::of(needless_bool::BOOL_COMPARISON), - LintId::of(needless_bool::NEEDLESS_BOOL), - LintId::of(needless_borrowed_ref::NEEDLESS_BORROWED_REFERENCE), - LintId::of(needless_late_init::NEEDLESS_LATE_INIT), - LintId::of(needless_parens_on_range_literals::NEEDLESS_PARENS_ON_RANGE_LITERALS), - LintId::of(needless_question_mark::NEEDLESS_QUESTION_MARK), - LintId::of(needless_update::NEEDLESS_UPDATE), - LintId::of(neg_cmp_op_on_partial_ord::NEG_CMP_OP_ON_PARTIAL_ORD), - LintId::of(neg_multiply::NEG_MULTIPLY), - LintId::of(new_without_default::NEW_WITHOUT_DEFAULT), - LintId::of(no_effect::NO_EFFECT), - LintId::of(no_effect::UNNECESSARY_OPERATION), - LintId::of(non_copy_const::BORROW_INTERIOR_MUTABLE_CONST), - LintId::of(non_copy_const::DECLARE_INTERIOR_MUTABLE_CONST), - LintId::of(non_expressive_names::JUST_UNDERSCORES_AND_DIGITS), - LintId::of(non_octal_unix_permissions::NON_OCTAL_UNIX_PERMISSIONS), - LintId::of(octal_escapes::OCTAL_ESCAPES), - LintId::of(only_used_in_recursion::ONLY_USED_IN_RECURSION), - LintId::of(operators::ABSURD_EXTREME_COMPARISONS), - LintId::of(operators::ASSIGN_OP_PATTERN), - LintId::of(operators::BAD_BIT_MASK), - LintId::of(operators::CMP_NAN), - LintId::of(operators::CMP_OWNED), - LintId::of(operators::DOUBLE_COMPARISONS), - LintId::of(operators::DURATION_SUBSEC), - LintId::of(operators::EQ_OP), - LintId::of(operators::ERASING_OP), - LintId::of(operators::FLOAT_EQUALITY_WITHOUT_ABS), - LintId::of(operators::IDENTITY_OP), - LintId::of(operators::INEFFECTIVE_BIT_MASK), - LintId::of(operators::MISREFACTORED_ASSIGN_OP), - LintId::of(operators::MODULO_ONE), - LintId::of(operators::OP_REF), - LintId::of(operators::PTR_EQ), - LintId::of(operators::SELF_ASSIGNMENT), - LintId::of(option_env_unwrap::OPTION_ENV_UNWRAP), - LintId::of(overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL), - LintId::of(partialeq_ne_impl::PARTIALEQ_NE_IMPL), - LintId::of(partialeq_to_none::PARTIALEQ_TO_NONE), - LintId::of(precedence::PRECEDENCE), - LintId::of(ptr::CMP_NULL), - LintId::of(ptr::INVALID_NULL_PTR_USAGE), - LintId::of(ptr::MUT_FROM_REF), - LintId::of(ptr::PTR_ARG), - LintId::of(ptr_offset_with_cast::PTR_OFFSET_WITH_CAST), - LintId::of(question_mark::QUESTION_MARK), - LintId::of(ranges::MANUAL_RANGE_CONTAINS), - LintId::of(ranges::REVERSED_EMPTY_RANGES), - LintId::of(rc_clone_in_vec_init::RC_CLONE_IN_VEC_INIT), - LintId::of(read_zero_byte_vec::READ_ZERO_BYTE_VEC), - LintId::of(redundant_clone::REDUNDANT_CLONE), - LintId::of(redundant_closure_call::REDUNDANT_CLOSURE_CALL), - LintId::of(redundant_field_names::REDUNDANT_FIELD_NAMES), - LintId::of(redundant_slicing::REDUNDANT_SLICING), - LintId::of(redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES), - LintId::of(reference::DEREF_ADDROF), - LintId::of(regex::INVALID_REGEX), - LintId::of(returns::LET_AND_RETURN), - LintId::of(returns::NEEDLESS_RETURN), - LintId::of(self_named_constructors::SELF_NAMED_CONSTRUCTORS), - LintId::of(serde_api::SERDE_API_MISUSE), - LintId::of(single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS), - LintId::of(size_of_in_element_count::SIZE_OF_IN_ELEMENT_COUNT), - LintId::of(slow_vector_initialization::SLOW_VECTOR_INITIALIZATION), - LintId::of(strings::STRING_FROM_UTF8_AS_BYTES), - LintId::of(strings::TRIM_SPLIT_WHITESPACE), - LintId::of(strlen_on_c_strings::STRLEN_ON_C_STRINGS), - LintId::of(suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL), - LintId::of(suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL), - LintId::of(swap::ALMOST_SWAPPED), - LintId::of(swap::MANUAL_SWAP), - LintId::of(swap_ptr_to_ref::SWAP_PTR_TO_REF), - LintId::of(tabs_in_doc_comments::TABS_IN_DOC_COMMENTS), - LintId::of(temporary_assignment::TEMPORARY_ASSIGNMENT), - LintId::of(to_digit_is_some::TO_DIGIT_IS_SOME), - LintId::of(transmute::CROSSPOINTER_TRANSMUTE), - LintId::of(transmute::TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS), - LintId::of(transmute::TRANSMUTE_BYTES_TO_STR), - LintId::of(transmute::TRANSMUTE_FLOAT_TO_INT), - LintId::of(transmute::TRANSMUTE_INT_TO_BOOL), - LintId::of(transmute::TRANSMUTE_INT_TO_CHAR), - LintId::of(transmute::TRANSMUTE_INT_TO_FLOAT), - LintId::of(transmute::TRANSMUTE_NUM_TO_BYTES), - LintId::of(transmute::TRANSMUTE_PTR_TO_REF), - LintId::of(transmute::TRANSMUTING_NULL), - LintId::of(transmute::UNSOUND_COLLECTION_TRANSMUTE), - LintId::of(transmute::USELESS_TRANSMUTE), - LintId::of(transmute::WRONG_TRANSMUTE), - LintId::of(types::BORROWED_BOX), - LintId::of(types::BOX_COLLECTION), - LintId::of(types::REDUNDANT_ALLOCATION), - LintId::of(types::TYPE_COMPLEXITY), - LintId::of(types::VEC_BOX), - LintId::of(unicode::INVISIBLE_CHARACTERS), - LintId::of(uninit_vec::UNINIT_VEC), - LintId::of(unit_return_expecting_ord::UNIT_RETURN_EXPECTING_ORD), - LintId::of(unit_types::LET_UNIT_VALUE), - LintId::of(unit_types::UNIT_ARG), - LintId::of(unit_types::UNIT_CMP), - LintId::of(unnamed_address::FN_ADDRESS_COMPARISONS), - LintId::of(unnamed_address::VTABLE_ADDRESS_COMPARISONS), - LintId::of(unnecessary_owned_empty_strings::UNNECESSARY_OWNED_EMPTY_STRINGS), - LintId::of(unsafe_removed_from_name::UNSAFE_REMOVED_FROM_NAME), - LintId::of(unused_io_amount::UNUSED_IO_AMOUNT), - LintId::of(unused_unit::UNUSED_UNIT), - LintId::of(unwrap::PANICKING_UNWRAP), - LintId::of(unwrap::UNNECESSARY_UNWRAP), - LintId::of(upper_case_acronyms::UPPER_CASE_ACRONYMS), - LintId::of(useless_conversion::USELESS_CONVERSION), - LintId::of(vec::USELESS_VEC), - LintId::of(vec_init_then_push::VEC_INIT_THEN_PUSH), - LintId::of(write::PRINTLN_EMPTY_STRING), - LintId::of(write::PRINT_LITERAL), - LintId::of(write::PRINT_WITH_NEWLINE), - LintId::of(write::WRITELN_EMPTY_STRING), - LintId::of(write::WRITE_LITERAL), - LintId::of(write::WRITE_WITH_NEWLINE), - LintId::of(zero_div_zero::ZERO_DIVIDED_BY_ZERO), -]) diff --git a/clippy_lints/src/lib.register_cargo.rs b/clippy_lints/src/lib.register_cargo.rs deleted file mode 100644 index c890523fe5aeb..0000000000000 --- a/clippy_lints/src/lib.register_cargo.rs +++ /dev/null @@ -1,11 +0,0 @@ -// This file was generated by `cargo dev update_lints`. -// Use that command to update this file and do not edit by hand. -// Manual edits will be overwritten. - -store.register_group(true, "clippy::cargo", Some("clippy_cargo"), vec![ - LintId::of(cargo::CARGO_COMMON_METADATA), - LintId::of(cargo::MULTIPLE_CRATE_VERSIONS), - LintId::of(cargo::NEGATIVE_FEATURE_NAMES), - LintId::of(cargo::REDUNDANT_FEATURE_NAMES), - LintId::of(cargo::WILDCARD_DEPENDENCIES), -]) diff --git a/clippy_lints/src/lib.register_complexity.rs b/clippy_lints/src/lib.register_complexity.rs deleted file mode 100644 index 8be9dc4baf193..0000000000000 --- a/clippy_lints/src/lib.register_complexity.rs +++ /dev/null @@ -1,111 +0,0 @@ -// This file was generated by `cargo dev update_lints`. -// Use that command to update this file and do not edit by hand. -// Manual edits will be overwritten. - -store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec![ - LintId::of(attrs::DEPRECATED_CFG_ATTR), - LintId::of(booleans::NONMINIMAL_BOOL), - LintId::of(borrow_deref_ref::BORROW_DEREF_REF), - LintId::of(casts::CHAR_LIT_AS_U8), - LintId::of(casts::UNNECESSARY_CAST), - LintId::of(dereference::EXPLICIT_AUTO_DEREF), - LintId::of(derivable_impls::DERIVABLE_IMPLS), - LintId::of(double_parens::DOUBLE_PARENS), - LintId::of(explicit_write::EXPLICIT_WRITE), - LintId::of(format::USELESS_FORMAT), - LintId::of(format_args::UNUSED_FORMAT_SPECS), - LintId::of(functions::TOO_MANY_ARGUMENTS), - LintId::of(int_plus_one::INT_PLUS_ONE), - LintId::of(lifetimes::EXTRA_UNUSED_LIFETIMES), - LintId::of(lifetimes::NEEDLESS_LIFETIMES), - LintId::of(loops::EXPLICIT_COUNTER_LOOP), - LintId::of(loops::MANUAL_FIND), - LintId::of(loops::MANUAL_FLATTEN), - LintId::of(loops::SINGLE_ELEMENT_LOOP), - LintId::of(loops::WHILE_LET_LOOP), - LintId::of(manual_clamp::MANUAL_CLAMP), - LintId::of(manual_rem_euclid::MANUAL_REM_EUCLID), - LintId::of(manual_strip::MANUAL_STRIP), - LintId::of(map_unit_fn::OPTION_MAP_UNIT_FN), - LintId::of(map_unit_fn::RESULT_MAP_UNIT_FN), - LintId::of(matches::MANUAL_FILTER), - LintId::of(matches::MANUAL_UNWRAP_OR), - LintId::of(matches::MATCH_AS_REF), - LintId::of(matches::MATCH_SINGLE_BINDING), - LintId::of(matches::NEEDLESS_MATCH), - LintId::of(matches::WILDCARD_IN_OR_PATTERNS), - LintId::of(methods::BIND_INSTEAD_OF_MAP), - LintId::of(methods::BYTES_COUNT_TO_LEN), - LintId::of(methods::CLONE_ON_COPY), - LintId::of(methods::FILTER_MAP_IDENTITY), - LintId::of(methods::FILTER_NEXT), - LintId::of(methods::FLAT_MAP_IDENTITY), - LintId::of(methods::GET_LAST_WITH_LEN), - LintId::of(methods::INSPECT_FOR_EACH), - LintId::of(methods::ITER_COUNT), - LintId::of(methods::ITER_KV_MAP), - LintId::of(methods::MANUAL_FILTER_MAP), - LintId::of(methods::MANUAL_FIND_MAP), - LintId::of(methods::MANUAL_SPLIT_ONCE), - LintId::of(methods::MAP_FLATTEN), - LintId::of(methods::MAP_IDENTITY), - LintId::of(methods::NEEDLESS_OPTION_AS_DEREF), - LintId::of(methods::NEEDLESS_OPTION_TAKE), - LintId::of(methods::NEEDLESS_SPLITN), - LintId::of(methods::OPTION_AS_REF_DEREF), - LintId::of(methods::OPTION_FILTER_MAP), - LintId::of(methods::OR_THEN_UNWRAP), - LintId::of(methods::RANGE_ZIP_WITH_LEN), - LintId::of(methods::REPEAT_ONCE), - LintId::of(methods::SEARCH_IS_SOME), - LintId::of(methods::SKIP_WHILE_NEXT), - LintId::of(methods::UNNECESSARY_FILTER_MAP), - LintId::of(methods::UNNECESSARY_FIND_MAP), - LintId::of(methods::UNNECESSARY_SORT_BY), - LintId::of(methods::USELESS_ASREF), - LintId::of(misc::SHORT_CIRCUIT_STATEMENT), - LintId::of(misc_early::UNNEEDED_WILDCARD_PATTERN), - LintId::of(misc_early::ZERO_PREFIXED_LITERAL), - LintId::of(mixed_read_write_in_expression::DIVERGING_SUB_EXPRESSION), - LintId::of(needless_arbitrary_self_type::NEEDLESS_ARBITRARY_SELF_TYPE), - LintId::of(needless_bool::BOOL_COMPARISON), - LintId::of(needless_bool::NEEDLESS_BOOL), - LintId::of(needless_borrowed_ref::NEEDLESS_BORROWED_REFERENCE), - LintId::of(needless_question_mark::NEEDLESS_QUESTION_MARK), - LintId::of(needless_update::NEEDLESS_UPDATE), - LintId::of(neg_cmp_op_on_partial_ord::NEG_CMP_OP_ON_PARTIAL_ORD), - LintId::of(no_effect::NO_EFFECT), - LintId::of(no_effect::UNNECESSARY_OPERATION), - LintId::of(only_used_in_recursion::ONLY_USED_IN_RECURSION), - LintId::of(operators::DOUBLE_COMPARISONS), - LintId::of(operators::DURATION_SUBSEC), - LintId::of(operators::IDENTITY_OP), - LintId::of(overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL), - LintId::of(partialeq_ne_impl::PARTIALEQ_NE_IMPL), - LintId::of(precedence::PRECEDENCE), - LintId::of(ptr_offset_with_cast::PTR_OFFSET_WITH_CAST), - LintId::of(redundant_closure_call::REDUNDANT_CLOSURE_CALL), - LintId::of(redundant_slicing::REDUNDANT_SLICING), - LintId::of(reference::DEREF_ADDROF), - LintId::of(strings::STRING_FROM_UTF8_AS_BYTES), - LintId::of(strlen_on_c_strings::STRLEN_ON_C_STRINGS), - LintId::of(swap::MANUAL_SWAP), - LintId::of(temporary_assignment::TEMPORARY_ASSIGNMENT), - LintId::of(transmute::CROSSPOINTER_TRANSMUTE), - LintId::of(transmute::TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS), - LintId::of(transmute::TRANSMUTE_BYTES_TO_STR), - LintId::of(transmute::TRANSMUTE_FLOAT_TO_INT), - LintId::of(transmute::TRANSMUTE_INT_TO_BOOL), - LintId::of(transmute::TRANSMUTE_INT_TO_CHAR), - LintId::of(transmute::TRANSMUTE_INT_TO_FLOAT), - LintId::of(transmute::TRANSMUTE_NUM_TO_BYTES), - LintId::of(transmute::TRANSMUTE_PTR_TO_REF), - LintId::of(transmute::USELESS_TRANSMUTE), - LintId::of(types::BORROWED_BOX), - LintId::of(types::TYPE_COMPLEXITY), - LintId::of(types::VEC_BOX), - LintId::of(unit_types::UNIT_ARG), - LintId::of(unwrap::UNNECESSARY_UNWRAP), - LintId::of(useless_conversion::USELESS_CONVERSION), - LintId::of(zero_div_zero::ZERO_DIVIDED_BY_ZERO), -]) diff --git a/clippy_lints/src/lib.register_correctness.rs b/clippy_lints/src/lib.register_correctness.rs deleted file mode 100644 index bb94037ec2e79..0000000000000 --- a/clippy_lints/src/lib.register_correctness.rs +++ /dev/null @@ -1,78 +0,0 @@ -// This file was generated by `cargo dev update_lints`. -// Use that command to update this file and do not edit by hand. -// Manual edits will be overwritten. - -store.register_group(true, "clippy::correctness", Some("clippy_correctness"), vec![ - LintId::of(approx_const::APPROX_CONSTANT), - LintId::of(async_yields_async::ASYNC_YIELDS_ASYNC), - LintId::of(attrs::DEPRECATED_SEMVER), - LintId::of(attrs::MISMATCHED_TARGET_OS), - LintId::of(attrs::USELESS_ATTRIBUTE), - LintId::of(booleans::OVERLY_COMPLEX_BOOL_EXPR), - LintId::of(casts::CAST_REF_TO_MUT), - LintId::of(casts::CAST_SLICE_DIFFERENT_SIZES), - LintId::of(copies::IFS_SAME_COND), - LintId::of(copies::IF_SAME_THEN_ELSE), - LintId::of(derive::DERIVE_HASH_XOR_EQ), - LintId::of(derive::DERIVE_ORD_XOR_PARTIAL_ORD), - LintId::of(drop_forget_ref::DROP_COPY), - LintId::of(drop_forget_ref::DROP_REF), - LintId::of(drop_forget_ref::FORGET_COPY), - LintId::of(drop_forget_ref::FORGET_REF), - LintId::of(drop_forget_ref::UNDROPPED_MANUALLY_DROPS), - LintId::of(enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT), - LintId::of(format_impl::RECURSIVE_FORMAT_IMPL), - LintId::of(formatting::POSSIBLE_MISSING_COMMA), - LintId::of(functions::NOT_UNSAFE_PTR_ARG_DEREF), - LintId::of(if_let_mutex::IF_LET_MUTEX), - LintId::of(indexing_slicing::OUT_OF_BOUNDS_INDEXING), - LintId::of(infinite_iter::INFINITE_ITER), - LintId::of(inherent_to_string::INHERENT_TO_STRING_SHADOW_DISPLAY), - LintId::of(inline_fn_without_body::INLINE_FN_WITHOUT_BODY), - LintId::of(invalid_utf8_in_unchecked::INVALID_UTF8_IN_UNCHECKED), - LintId::of(let_underscore::LET_UNDERSCORE_LOCK), - LintId::of(literal_representation::MISTYPED_LITERAL_SUFFIXES), - LintId::of(loops::ITER_NEXT_LOOP), - LintId::of(loops::NEVER_LOOP), - LintId::of(loops::WHILE_IMMUTABLE_CONDITION), - LintId::of(matches::MATCH_STR_CASE_MISMATCH), - LintId::of(mem_replace::MEM_REPLACE_WITH_UNINIT), - LintId::of(methods::CLONE_DOUBLE_REF), - LintId::of(methods::ITERATOR_STEP_BY_ZERO), - LintId::of(methods::NONSENSICAL_OPEN_OPTIONS), - LintId::of(methods::SUSPICIOUS_SPLITN), - LintId::of(methods::UNINIT_ASSUMED_INIT), - LintId::of(methods::UNIT_HASH), - LintId::of(methods::VEC_RESIZE_TO_ZERO), - LintId::of(methods::ZST_OFFSET), - LintId::of(minmax::MIN_MAX), - LintId::of(non_octal_unix_permissions::NON_OCTAL_UNIX_PERMISSIONS), - LintId::of(operators::ABSURD_EXTREME_COMPARISONS), - LintId::of(operators::BAD_BIT_MASK), - LintId::of(operators::CMP_NAN), - LintId::of(operators::EQ_OP), - LintId::of(operators::ERASING_OP), - LintId::of(operators::INEFFECTIVE_BIT_MASK), - LintId::of(operators::MODULO_ONE), - LintId::of(operators::SELF_ASSIGNMENT), - LintId::of(option_env_unwrap::OPTION_ENV_UNWRAP), - LintId::of(ptr::INVALID_NULL_PTR_USAGE), - LintId::of(ptr::MUT_FROM_REF), - LintId::of(ranges::REVERSED_EMPTY_RANGES), - LintId::of(read_zero_byte_vec::READ_ZERO_BYTE_VEC), - LintId::of(regex::INVALID_REGEX), - LintId::of(serde_api::SERDE_API_MISUSE), - LintId::of(size_of_in_element_count::SIZE_OF_IN_ELEMENT_COUNT), - LintId::of(swap::ALMOST_SWAPPED), - LintId::of(transmute::TRANSMUTING_NULL), - LintId::of(transmute::UNSOUND_COLLECTION_TRANSMUTE), - LintId::of(transmute::WRONG_TRANSMUTE), - LintId::of(unicode::INVISIBLE_CHARACTERS), - LintId::of(uninit_vec::UNINIT_VEC), - LintId::of(unit_return_expecting_ord::UNIT_RETURN_EXPECTING_ORD), - LintId::of(unit_types::UNIT_CMP), - LintId::of(unnamed_address::FN_ADDRESS_COMPARISONS), - LintId::of(unnamed_address::VTABLE_ADDRESS_COMPARISONS), - LintId::of(unused_io_amount::UNUSED_IO_AMOUNT), - LintId::of(unwrap::PANICKING_UNWRAP), -]) diff --git a/clippy_lints/src/lib.register_internal.rs b/clippy_lints/src/lib.register_internal.rs deleted file mode 100644 index 40c94c6e8d33d..0000000000000 --- a/clippy_lints/src/lib.register_internal.rs +++ /dev/null @@ -1,22 +0,0 @@ -// This file was generated by `cargo dev update_lints`. -// Use that command to update this file and do not edit by hand. -// Manual edits will be overwritten. - -store.register_group(true, "clippy::internal", Some("clippy_internal"), vec![ - LintId::of(utils::internal_lints::clippy_lints_internal::CLIPPY_LINTS_INTERNAL), - LintId::of(utils::internal_lints::collapsible_calls::COLLAPSIBLE_SPAN_LINT_CALLS), - LintId::of(utils::internal_lints::compiler_lint_functions::COMPILER_LINT_FUNCTIONS), - LintId::of(utils::internal_lints::if_chain_style::IF_CHAIN_STYLE), - LintId::of(utils::internal_lints::interning_defined_symbol::INTERNING_DEFINED_SYMBOL), - LintId::of(utils::internal_lints::interning_defined_symbol::UNNECESSARY_SYMBOL_STR), - LintId::of(utils::internal_lints::invalid_paths::INVALID_PATHS), - LintId::of(utils::internal_lints::lint_without_lint_pass::DEFAULT_DEPRECATION_REASON), - LintId::of(utils::internal_lints::lint_without_lint_pass::DEFAULT_LINT), - LintId::of(utils::internal_lints::lint_without_lint_pass::INVALID_CLIPPY_VERSION_ATTRIBUTE), - LintId::of(utils::internal_lints::lint_without_lint_pass::LINT_WITHOUT_LINT_PASS), - LintId::of(utils::internal_lints::lint_without_lint_pass::MISSING_CLIPPY_VERSION_ATTRIBUTE), - LintId::of(utils::internal_lints::msrv_attr_impl::MISSING_MSRV_ATTR_IMPL), - LintId::of(utils::internal_lints::outer_expn_data_pass::OUTER_EXPN_EXPN_DATA), - LintId::of(utils::internal_lints::produce_ice::PRODUCE_ICE), - LintId::of(utils::internal_lints::unnecessary_def_path::UNNECESSARY_DEF_PATH), -]) diff --git a/clippy_lints/src/lib.register_lints.rs b/clippy_lints/src/lib.register_lints.rs deleted file mode 100644 index 800e3a8767133..0000000000000 --- a/clippy_lints/src/lib.register_lints.rs +++ /dev/null @@ -1,620 +0,0 @@ -// This file was generated by `cargo dev update_lints`. -// Use that command to update this file and do not edit by hand. -// Manual edits will be overwritten. - -store.register_lints(&[ - #[cfg(feature = "internal")] - utils::internal_lints::clippy_lints_internal::CLIPPY_LINTS_INTERNAL, - #[cfg(feature = "internal")] - utils::internal_lints::collapsible_calls::COLLAPSIBLE_SPAN_LINT_CALLS, - #[cfg(feature = "internal")] - utils::internal_lints::compiler_lint_functions::COMPILER_LINT_FUNCTIONS, - #[cfg(feature = "internal")] - utils::internal_lints::if_chain_style::IF_CHAIN_STYLE, - #[cfg(feature = "internal")] - utils::internal_lints::interning_defined_symbol::INTERNING_DEFINED_SYMBOL, - #[cfg(feature = "internal")] - utils::internal_lints::interning_defined_symbol::UNNECESSARY_SYMBOL_STR, - #[cfg(feature = "internal")] - utils::internal_lints::invalid_paths::INVALID_PATHS, - #[cfg(feature = "internal")] - utils::internal_lints::lint_without_lint_pass::DEFAULT_DEPRECATION_REASON, - #[cfg(feature = "internal")] - utils::internal_lints::lint_without_lint_pass::DEFAULT_LINT, - #[cfg(feature = "internal")] - utils::internal_lints::lint_without_lint_pass::INVALID_CLIPPY_VERSION_ATTRIBUTE, - #[cfg(feature = "internal")] - utils::internal_lints::lint_without_lint_pass::LINT_WITHOUT_LINT_PASS, - #[cfg(feature = "internal")] - utils::internal_lints::lint_without_lint_pass::MISSING_CLIPPY_VERSION_ATTRIBUTE, - #[cfg(feature = "internal")] - utils::internal_lints::msrv_attr_impl::MISSING_MSRV_ATTR_IMPL, - #[cfg(feature = "internal")] - utils::internal_lints::outer_expn_data_pass::OUTER_EXPN_EXPN_DATA, - #[cfg(feature = "internal")] - utils::internal_lints::produce_ice::PRODUCE_ICE, - #[cfg(feature = "internal")] - utils::internal_lints::unnecessary_def_path::UNNECESSARY_DEF_PATH, - almost_complete_letter_range::ALMOST_COMPLETE_LETTER_RANGE, - approx_const::APPROX_CONSTANT, - as_conversions::AS_CONVERSIONS, - asm_syntax::INLINE_ASM_X86_ATT_SYNTAX, - asm_syntax::INLINE_ASM_X86_INTEL_SYNTAX, - assertions_on_constants::ASSERTIONS_ON_CONSTANTS, - assertions_on_result_states::ASSERTIONS_ON_RESULT_STATES, - async_yields_async::ASYNC_YIELDS_ASYNC, - attrs::ALLOW_ATTRIBUTES_WITHOUT_REASON, - attrs::BLANKET_CLIPPY_RESTRICTION_LINTS, - attrs::DEPRECATED_CFG_ATTR, - attrs::DEPRECATED_SEMVER, - attrs::EMPTY_LINE_AFTER_OUTER_ATTR, - attrs::INLINE_ALWAYS, - attrs::MISMATCHED_TARGET_OS, - attrs::USELESS_ATTRIBUTE, - await_holding_invalid::AWAIT_HOLDING_INVALID_TYPE, - await_holding_invalid::AWAIT_HOLDING_LOCK, - await_holding_invalid::AWAIT_HOLDING_REFCELL_REF, - blocks_in_if_conditions::BLOCKS_IN_IF_CONDITIONS, - bool_assert_comparison::BOOL_ASSERT_COMPARISON, - bool_to_int_with_if::BOOL_TO_INT_WITH_IF, - booleans::NONMINIMAL_BOOL, - booleans::OVERLY_COMPLEX_BOOL_EXPR, - borrow_deref_ref::BORROW_DEREF_REF, - box_default::BOX_DEFAULT, - cargo::CARGO_COMMON_METADATA, - cargo::MULTIPLE_CRATE_VERSIONS, - cargo::NEGATIVE_FEATURE_NAMES, - cargo::REDUNDANT_FEATURE_NAMES, - cargo::WILDCARD_DEPENDENCIES, - casts::AS_PTR_CAST_MUT, - casts::AS_UNDERSCORE, - casts::BORROW_AS_PTR, - casts::CAST_ABS_TO_UNSIGNED, - casts::CAST_ENUM_CONSTRUCTOR, - casts::CAST_ENUM_TRUNCATION, - casts::CAST_LOSSLESS, - casts::CAST_NAN_TO_INT, - casts::CAST_POSSIBLE_TRUNCATION, - casts::CAST_POSSIBLE_WRAP, - casts::CAST_PRECISION_LOSS, - casts::CAST_PTR_ALIGNMENT, - casts::CAST_REF_TO_MUT, - casts::CAST_SIGN_LOSS, - casts::CAST_SLICE_DIFFERENT_SIZES, - casts::CAST_SLICE_FROM_RAW_PARTS, - casts::CHAR_LIT_AS_U8, - casts::FN_TO_NUMERIC_CAST, - casts::FN_TO_NUMERIC_CAST_ANY, - casts::FN_TO_NUMERIC_CAST_WITH_TRUNCATION, - casts::PTR_AS_PTR, - casts::UNNECESSARY_CAST, - checked_conversions::CHECKED_CONVERSIONS, - cognitive_complexity::COGNITIVE_COMPLEXITY, - collapsible_if::COLLAPSIBLE_ELSE_IF, - collapsible_if::COLLAPSIBLE_IF, - comparison_chain::COMPARISON_CHAIN, - copies::BRANCHES_SHARING_CODE, - copies::IFS_SAME_COND, - copies::IF_SAME_THEN_ELSE, - copies::SAME_FUNCTIONS_IN_IF_CONDITION, - copy_iterator::COPY_ITERATOR, - crate_in_macro_def::CRATE_IN_MACRO_DEF, - create_dir::CREATE_DIR, - dbg_macro::DBG_MACRO, - default::DEFAULT_TRAIT_ACCESS, - default::FIELD_REASSIGN_WITH_DEFAULT, - default_instead_of_iter_empty::DEFAULT_INSTEAD_OF_ITER_EMPTY, - default_numeric_fallback::DEFAULT_NUMERIC_FALLBACK, - default_union_representation::DEFAULT_UNION_REPRESENTATION, - dereference::EXPLICIT_AUTO_DEREF, - dereference::EXPLICIT_DEREF_METHODS, - dereference::NEEDLESS_BORROW, - dereference::REF_BINDING_TO_REFERENCE, - derivable_impls::DERIVABLE_IMPLS, - derive::DERIVE_HASH_XOR_EQ, - derive::DERIVE_ORD_XOR_PARTIAL_ORD, - derive::DERIVE_PARTIAL_EQ_WITHOUT_EQ, - derive::EXPL_IMPL_CLONE_ON_COPY, - derive::UNSAFE_DERIVE_DESERIALIZE, - disallowed_macros::DISALLOWED_MACROS, - disallowed_methods::DISALLOWED_METHODS, - disallowed_names::DISALLOWED_NAMES, - disallowed_script_idents::DISALLOWED_SCRIPT_IDENTS, - disallowed_types::DISALLOWED_TYPES, - doc::DOC_LINK_WITH_QUOTES, - doc::DOC_MARKDOWN, - doc::MISSING_ERRORS_DOC, - doc::MISSING_PANICS_DOC, - doc::MISSING_SAFETY_DOC, - doc::NEEDLESS_DOCTEST_MAIN, - double_parens::DOUBLE_PARENS, - drop_forget_ref::DROP_COPY, - drop_forget_ref::DROP_NON_DROP, - drop_forget_ref::DROP_REF, - drop_forget_ref::FORGET_COPY, - drop_forget_ref::FORGET_NON_DROP, - drop_forget_ref::FORGET_REF, - drop_forget_ref::UNDROPPED_MANUALLY_DROPS, - duplicate_mod::DUPLICATE_MOD, - else_if_without_else::ELSE_IF_WITHOUT_ELSE, - empty_drop::EMPTY_DROP, - empty_enum::EMPTY_ENUM, - empty_structs_with_brackets::EMPTY_STRUCTS_WITH_BRACKETS, - entry::MAP_ENTRY, - enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT, - enum_variants::ENUM_VARIANT_NAMES, - enum_variants::MODULE_INCEPTION, - enum_variants::MODULE_NAME_REPETITIONS, - equatable_if_let::EQUATABLE_IF_LET, - escape::BOXED_LOCAL, - eta_reduction::REDUNDANT_CLOSURE, - eta_reduction::REDUNDANT_CLOSURE_FOR_METHOD_CALLS, - excessive_bools::FN_PARAMS_EXCESSIVE_BOOLS, - excessive_bools::STRUCT_EXCESSIVE_BOOLS, - exhaustive_items::EXHAUSTIVE_ENUMS, - exhaustive_items::EXHAUSTIVE_STRUCTS, - exit::EXIT, - explicit_write::EXPLICIT_WRITE, - fallible_impl_from::FALLIBLE_IMPL_FROM, - float_literal::EXCESSIVE_PRECISION, - float_literal::LOSSY_FLOAT_LITERAL, - floating_point_arithmetic::IMPRECISE_FLOPS, - floating_point_arithmetic::SUBOPTIMAL_FLOPS, - format::USELESS_FORMAT, - format_args::FORMAT_IN_FORMAT_ARGS, - format_args::TO_STRING_IN_FORMAT_ARGS, - format_args::UNINLINED_FORMAT_ARGS, - format_args::UNUSED_FORMAT_SPECS, - format_impl::PRINT_IN_FORMAT_IMPL, - format_impl::RECURSIVE_FORMAT_IMPL, - format_push_string::FORMAT_PUSH_STRING, - formatting::POSSIBLE_MISSING_COMMA, - formatting::SUSPICIOUS_ASSIGNMENT_FORMATTING, - formatting::SUSPICIOUS_ELSE_FORMATTING, - formatting::SUSPICIOUS_UNARY_OP_FORMATTING, - from_over_into::FROM_OVER_INTO, - from_str_radix_10::FROM_STR_RADIX_10, - functions::DOUBLE_MUST_USE, - functions::MUST_USE_CANDIDATE, - functions::MUST_USE_UNIT, - functions::NOT_UNSAFE_PTR_ARG_DEREF, - functions::RESULT_LARGE_ERR, - functions::RESULT_UNIT_ERR, - functions::TOO_MANY_ARGUMENTS, - functions::TOO_MANY_LINES, - future_not_send::FUTURE_NOT_SEND, - if_let_mutex::IF_LET_MUTEX, - if_not_else::IF_NOT_ELSE, - if_then_some_else_none::IF_THEN_SOME_ELSE_NONE, - implicit_hasher::IMPLICIT_HASHER, - implicit_return::IMPLICIT_RETURN, - implicit_saturating_add::IMPLICIT_SATURATING_ADD, - implicit_saturating_sub::IMPLICIT_SATURATING_SUB, - inconsistent_struct_constructor::INCONSISTENT_STRUCT_CONSTRUCTOR, - index_refutable_slice::INDEX_REFUTABLE_SLICE, - indexing_slicing::INDEXING_SLICING, - indexing_slicing::OUT_OF_BOUNDS_INDEXING, - infinite_iter::INFINITE_ITER, - infinite_iter::MAYBE_INFINITE_ITER, - inherent_impl::MULTIPLE_INHERENT_IMPL, - inherent_to_string::INHERENT_TO_STRING, - inherent_to_string::INHERENT_TO_STRING_SHADOW_DISPLAY, - init_numbered_fields::INIT_NUMBERED_FIELDS, - inline_fn_without_body::INLINE_FN_WITHOUT_BODY, - int_plus_one::INT_PLUS_ONE, - invalid_upcast_comparisons::INVALID_UPCAST_COMPARISONS, - invalid_utf8_in_unchecked::INVALID_UTF8_IN_UNCHECKED, - items_after_statements::ITEMS_AFTER_STATEMENTS, - iter_not_returning_iterator::ITER_NOT_RETURNING_ITERATOR, - large_const_arrays::LARGE_CONST_ARRAYS, - large_enum_variant::LARGE_ENUM_VARIANT, - large_include_file::LARGE_INCLUDE_FILE, - large_stack_arrays::LARGE_STACK_ARRAYS, - len_zero::COMPARISON_TO_EMPTY, - len_zero::LEN_WITHOUT_IS_EMPTY, - len_zero::LEN_ZERO, - let_if_seq::USELESS_LET_IF_SEQ, - let_underscore::LET_UNDERSCORE_DROP, - let_underscore::LET_UNDERSCORE_LOCK, - let_underscore::LET_UNDERSCORE_MUST_USE, - lifetimes::EXTRA_UNUSED_LIFETIMES, - lifetimes::NEEDLESS_LIFETIMES, - literal_representation::DECIMAL_LITERAL_REPRESENTATION, - literal_representation::INCONSISTENT_DIGIT_GROUPING, - literal_representation::LARGE_DIGIT_GROUPS, - literal_representation::MISTYPED_LITERAL_SUFFIXES, - literal_representation::UNREADABLE_LITERAL, - literal_representation::UNUSUAL_BYTE_GROUPINGS, - loops::EMPTY_LOOP, - loops::EXPLICIT_COUNTER_LOOP, - loops::EXPLICIT_INTO_ITER_LOOP, - loops::EXPLICIT_ITER_LOOP, - loops::FOR_KV_MAP, - loops::ITER_NEXT_LOOP, - loops::MANUAL_FIND, - loops::MANUAL_FLATTEN, - loops::MANUAL_MEMCPY, - loops::MISSING_SPIN_LOOP, - loops::MUT_RANGE_BOUND, - loops::NEEDLESS_COLLECT, - loops::NEEDLESS_RANGE_LOOP, - loops::NEVER_LOOP, - loops::SAME_ITEM_PUSH, - loops::SINGLE_ELEMENT_LOOP, - loops::WHILE_IMMUTABLE_CONDITION, - loops::WHILE_LET_LOOP, - loops::WHILE_LET_ON_ITERATOR, - macro_use::MACRO_USE_IMPORTS, - main_recursion::MAIN_RECURSION, - manual_assert::MANUAL_ASSERT, - manual_async_fn::MANUAL_ASYNC_FN, - manual_bits::MANUAL_BITS, - manual_clamp::MANUAL_CLAMP, - manual_instant_elapsed::MANUAL_INSTANT_ELAPSED, - manual_non_exhaustive::MANUAL_NON_EXHAUSTIVE, - manual_rem_euclid::MANUAL_REM_EUCLID, - manual_retain::MANUAL_RETAIN, - manual_string_new::MANUAL_STRING_NEW, - manual_strip::MANUAL_STRIP, - map_unit_fn::OPTION_MAP_UNIT_FN, - map_unit_fn::RESULT_MAP_UNIT_FN, - match_result_ok::MATCH_RESULT_OK, - matches::COLLAPSIBLE_MATCH, - matches::INFALLIBLE_DESTRUCTURING_MATCH, - matches::MANUAL_FILTER, - matches::MANUAL_MAP, - matches::MANUAL_UNWRAP_OR, - matches::MATCH_AS_REF, - matches::MATCH_BOOL, - matches::MATCH_LIKE_MATCHES_MACRO, - matches::MATCH_ON_VEC_ITEMS, - matches::MATCH_OVERLAPPING_ARM, - matches::MATCH_REF_PATS, - matches::MATCH_SAME_ARMS, - matches::MATCH_SINGLE_BINDING, - matches::MATCH_STR_CASE_MISMATCH, - matches::MATCH_WILDCARD_FOR_SINGLE_VARIANTS, - matches::MATCH_WILD_ERR_ARM, - matches::NEEDLESS_MATCH, - matches::REDUNDANT_PATTERN_MATCHING, - matches::REST_PAT_IN_FULLY_BOUND_STRUCTS, - matches::SIGNIFICANT_DROP_IN_SCRUTINEE, - matches::SINGLE_MATCH, - matches::SINGLE_MATCH_ELSE, - matches::TRY_ERR, - matches::WILDCARD_ENUM_MATCH_ARM, - matches::WILDCARD_IN_OR_PATTERNS, - mem_forget::MEM_FORGET, - mem_replace::MEM_REPLACE_OPTION_WITH_NONE, - mem_replace::MEM_REPLACE_WITH_DEFAULT, - mem_replace::MEM_REPLACE_WITH_UNINIT, - methods::BIND_INSTEAD_OF_MAP, - methods::BYTES_COUNT_TO_LEN, - methods::BYTES_NTH, - methods::CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS, - methods::CHARS_LAST_CMP, - methods::CHARS_NEXT_CMP, - methods::CLONED_INSTEAD_OF_COPIED, - methods::CLONE_DOUBLE_REF, - methods::CLONE_ON_COPY, - methods::CLONE_ON_REF_PTR, - methods::COLLAPSIBLE_STR_REPLACE, - methods::ERR_EXPECT, - methods::EXPECT_FUN_CALL, - methods::EXPECT_USED, - methods::EXTEND_WITH_DRAIN, - methods::FILETYPE_IS_FILE, - methods::FILTER_MAP_IDENTITY, - methods::FILTER_MAP_NEXT, - methods::FILTER_NEXT, - methods::FLAT_MAP_IDENTITY, - methods::FLAT_MAP_OPTION, - methods::FROM_ITER_INSTEAD_OF_COLLECT, - methods::GET_FIRST, - methods::GET_LAST_WITH_LEN, - methods::GET_UNWRAP, - methods::IMPLICIT_CLONE, - methods::INEFFICIENT_TO_STRING, - methods::INSPECT_FOR_EACH, - methods::INTO_ITER_ON_REF, - methods::IS_DIGIT_ASCII_RADIX, - methods::ITERATOR_STEP_BY_ZERO, - methods::ITER_CLONED_COLLECT, - methods::ITER_COUNT, - methods::ITER_KV_MAP, - methods::ITER_NEXT_SLICE, - methods::ITER_NTH, - methods::ITER_NTH_ZERO, - methods::ITER_ON_EMPTY_COLLECTIONS, - methods::ITER_ON_SINGLE_ITEMS, - methods::ITER_OVEREAGER_CLONED, - methods::ITER_SKIP_NEXT, - methods::ITER_WITH_DRAIN, - methods::MANUAL_FILTER_MAP, - methods::MANUAL_FIND_MAP, - methods::MANUAL_OK_OR, - methods::MANUAL_SATURATING_ARITHMETIC, - methods::MANUAL_SPLIT_ONCE, - methods::MANUAL_STR_REPEAT, - methods::MAP_CLONE, - methods::MAP_COLLECT_RESULT_UNIT, - methods::MAP_ERR_IGNORE, - methods::MAP_FLATTEN, - methods::MAP_IDENTITY, - methods::MAP_UNWRAP_OR, - methods::MUT_MUTEX_LOCK, - methods::NAIVE_BYTECOUNT, - methods::NEEDLESS_OPTION_AS_DEREF, - methods::NEEDLESS_OPTION_TAKE, - methods::NEEDLESS_SPLITN, - methods::NEW_RET_NO_SELF, - methods::NONSENSICAL_OPEN_OPTIONS, - methods::NO_EFFECT_REPLACE, - methods::OBFUSCATED_IF_ELSE, - methods::OK_EXPECT, - methods::OPTION_AS_REF_DEREF, - methods::OPTION_FILTER_MAP, - methods::OPTION_MAP_OR_NONE, - methods::OR_FUN_CALL, - methods::OR_THEN_UNWRAP, - methods::PATH_BUF_PUSH_OVERWRITE, - methods::RANGE_ZIP_WITH_LEN, - methods::REPEAT_ONCE, - methods::RESULT_MAP_OR_INTO_OPTION, - methods::SEARCH_IS_SOME, - methods::SHOULD_IMPLEMENT_TRAIT, - methods::SINGLE_CHAR_ADD_STR, - methods::SINGLE_CHAR_PATTERN, - methods::SKIP_WHILE_NEXT, - methods::STABLE_SORT_PRIMITIVE, - methods::STRING_EXTEND_CHARS, - methods::SUSPICIOUS_MAP, - methods::SUSPICIOUS_SPLITN, - methods::SUSPICIOUS_TO_OWNED, - methods::UNINIT_ASSUMED_INIT, - methods::UNIT_HASH, - methods::UNNECESSARY_FILTER_MAP, - methods::UNNECESSARY_FIND_MAP, - methods::UNNECESSARY_FOLD, - methods::UNNECESSARY_JOIN, - methods::UNNECESSARY_LAZY_EVALUATIONS, - methods::UNNECESSARY_SORT_BY, - methods::UNNECESSARY_TO_OWNED, - methods::UNWRAP_OR_ELSE_DEFAULT, - methods::UNWRAP_USED, - methods::USELESS_ASREF, - methods::VEC_RESIZE_TO_ZERO, - methods::VERBOSE_FILE_READS, - methods::WRONG_SELF_CONVENTION, - methods::ZST_OFFSET, - minmax::MIN_MAX, - misc::SHORT_CIRCUIT_STATEMENT, - misc::TOPLEVEL_REF_ARG, - misc::USED_UNDERSCORE_BINDING, - misc::ZERO_PTR, - misc_early::BUILTIN_TYPE_SHADOW, - misc_early::DOUBLE_NEG, - misc_early::DUPLICATE_UNDERSCORE_ARGUMENT, - misc_early::MIXED_CASE_HEX_LITERALS, - misc_early::REDUNDANT_PATTERN, - misc_early::SEPARATED_LITERAL_SUFFIX, - misc_early::UNNEEDED_FIELD_PATTERN, - misc_early::UNNEEDED_WILDCARD_PATTERN, - misc_early::UNSEPARATED_LITERAL_SUFFIX, - misc_early::ZERO_PREFIXED_LITERAL, - mismatching_type_param_order::MISMATCHING_TYPE_PARAM_ORDER, - missing_const_for_fn::MISSING_CONST_FOR_FN, - missing_doc::MISSING_DOCS_IN_PRIVATE_ITEMS, - missing_enforced_import_rename::MISSING_ENFORCED_IMPORT_RENAMES, - missing_inline::MISSING_INLINE_IN_PUBLIC_ITEMS, - missing_trait_methods::MISSING_TRAIT_METHODS, - mixed_read_write_in_expression::DIVERGING_SUB_EXPRESSION, - mixed_read_write_in_expression::MIXED_READ_WRITE_IN_EXPRESSION, - module_style::MOD_MODULE_FILES, - module_style::SELF_NAMED_MODULE_FILES, - multi_assignments::MULTI_ASSIGNMENTS, - mut_key::MUTABLE_KEY_TYPE, - mut_mut::MUT_MUT, - mut_reference::UNNECESSARY_MUT_PASSED, - mutable_debug_assertion::DEBUG_ASSERT_WITH_MUT_CALL, - mutex_atomic::MUTEX_ATOMIC, - mutex_atomic::MUTEX_INTEGER, - needless_arbitrary_self_type::NEEDLESS_ARBITRARY_SELF_TYPE, - needless_bool::BOOL_COMPARISON, - needless_bool::NEEDLESS_BOOL, - needless_borrowed_ref::NEEDLESS_BORROWED_REFERENCE, - needless_continue::NEEDLESS_CONTINUE, - needless_for_each::NEEDLESS_FOR_EACH, - needless_late_init::NEEDLESS_LATE_INIT, - needless_parens_on_range_literals::NEEDLESS_PARENS_ON_RANGE_LITERALS, - needless_pass_by_value::NEEDLESS_PASS_BY_VALUE, - needless_question_mark::NEEDLESS_QUESTION_MARK, - needless_update::NEEDLESS_UPDATE, - neg_cmp_op_on_partial_ord::NEG_CMP_OP_ON_PARTIAL_ORD, - neg_multiply::NEG_MULTIPLY, - new_without_default::NEW_WITHOUT_DEFAULT, - no_effect::NO_EFFECT, - no_effect::NO_EFFECT_UNDERSCORE_BINDING, - no_effect::UNNECESSARY_OPERATION, - non_copy_const::BORROW_INTERIOR_MUTABLE_CONST, - non_copy_const::DECLARE_INTERIOR_MUTABLE_CONST, - non_expressive_names::JUST_UNDERSCORES_AND_DIGITS, - non_expressive_names::MANY_SINGLE_CHAR_NAMES, - non_expressive_names::SIMILAR_NAMES, - non_octal_unix_permissions::NON_OCTAL_UNIX_PERMISSIONS, - non_send_fields_in_send_ty::NON_SEND_FIELDS_IN_SEND_TY, - nonstandard_macro_braces::NONSTANDARD_MACRO_BRACES, - octal_escapes::OCTAL_ESCAPES, - only_used_in_recursion::ONLY_USED_IN_RECURSION, - operators::ABSURD_EXTREME_COMPARISONS, - operators::ARITHMETIC_SIDE_EFFECTS, - operators::ASSIGN_OP_PATTERN, - operators::BAD_BIT_MASK, - operators::CMP_NAN, - operators::CMP_OWNED, - operators::DOUBLE_COMPARISONS, - operators::DURATION_SUBSEC, - operators::EQ_OP, - operators::ERASING_OP, - operators::FLOAT_ARITHMETIC, - operators::FLOAT_CMP, - operators::FLOAT_CMP_CONST, - operators::FLOAT_EQUALITY_WITHOUT_ABS, - operators::IDENTITY_OP, - operators::INEFFECTIVE_BIT_MASK, - operators::INTEGER_ARITHMETIC, - operators::INTEGER_DIVISION, - operators::MISREFACTORED_ASSIGN_OP, - operators::MODULO_ARITHMETIC, - operators::MODULO_ONE, - operators::NEEDLESS_BITWISE_BOOL, - operators::OP_REF, - operators::PTR_EQ, - operators::SELF_ASSIGNMENT, - operators::VERBOSE_BIT_MASK, - option_env_unwrap::OPTION_ENV_UNWRAP, - option_if_let_else::OPTION_IF_LET_ELSE, - overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL, - panic_in_result_fn::PANIC_IN_RESULT_FN, - panic_unimplemented::PANIC, - panic_unimplemented::TODO, - panic_unimplemented::UNIMPLEMENTED, - panic_unimplemented::UNREACHABLE, - partial_pub_fields::PARTIAL_PUB_FIELDS, - partialeq_ne_impl::PARTIALEQ_NE_IMPL, - partialeq_to_none::PARTIALEQ_TO_NONE, - pass_by_ref_or_value::LARGE_TYPES_PASSED_BY_VALUE, - pass_by_ref_or_value::TRIVIALLY_COPY_PASS_BY_REF, - pattern_type_mismatch::PATTERN_TYPE_MISMATCH, - precedence::PRECEDENCE, - ptr::CMP_NULL, - ptr::INVALID_NULL_PTR_USAGE, - ptr::MUT_FROM_REF, - ptr::PTR_ARG, - ptr_offset_with_cast::PTR_OFFSET_WITH_CAST, - pub_use::PUB_USE, - question_mark::QUESTION_MARK, - ranges::MANUAL_RANGE_CONTAINS, - ranges::RANGE_MINUS_ONE, - ranges::RANGE_PLUS_ONE, - ranges::REVERSED_EMPTY_RANGES, - rc_clone_in_vec_init::RC_CLONE_IN_VEC_INIT, - read_zero_byte_vec::READ_ZERO_BYTE_VEC, - redundant_clone::REDUNDANT_CLONE, - redundant_closure_call::REDUNDANT_CLOSURE_CALL, - redundant_else::REDUNDANT_ELSE, - redundant_field_names::REDUNDANT_FIELD_NAMES, - redundant_pub_crate::REDUNDANT_PUB_CRATE, - redundant_slicing::DEREF_BY_SLICING, - redundant_slicing::REDUNDANT_SLICING, - redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES, - ref_option_ref::REF_OPTION_REF, - reference::DEREF_ADDROF, - regex::INVALID_REGEX, - regex::TRIVIAL_REGEX, - return_self_not_must_use::RETURN_SELF_NOT_MUST_USE, - returns::LET_AND_RETURN, - returns::NEEDLESS_RETURN, - same_name_method::SAME_NAME_METHOD, - self_named_constructors::SELF_NAMED_CONSTRUCTORS, - semicolon_if_nothing_returned::SEMICOLON_IF_NOTHING_RETURNED, - serde_api::SERDE_API_MISUSE, - shadow::SHADOW_REUSE, - shadow::SHADOW_SAME, - shadow::SHADOW_UNRELATED, - single_char_lifetime_names::SINGLE_CHAR_LIFETIME_NAMES, - single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS, - size_of_in_element_count::SIZE_OF_IN_ELEMENT_COUNT, - slow_vector_initialization::SLOW_VECTOR_INITIALIZATION, - std_instead_of_core::ALLOC_INSTEAD_OF_CORE, - std_instead_of_core::STD_INSTEAD_OF_ALLOC, - std_instead_of_core::STD_INSTEAD_OF_CORE, - strings::STRING_ADD, - strings::STRING_ADD_ASSIGN, - strings::STRING_FROM_UTF8_AS_BYTES, - strings::STRING_LIT_AS_BYTES, - strings::STRING_SLICE, - strings::STRING_TO_STRING, - strings::STR_TO_STRING, - strings::TRIM_SPLIT_WHITESPACE, - strlen_on_c_strings::STRLEN_ON_C_STRINGS, - suspicious_operation_groupings::SUSPICIOUS_OPERATION_GROUPINGS, - suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL, - suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL, - swap::ALMOST_SWAPPED, - swap::MANUAL_SWAP, - swap_ptr_to_ref::SWAP_PTR_TO_REF, - tabs_in_doc_comments::TABS_IN_DOC_COMMENTS, - temporary_assignment::TEMPORARY_ASSIGNMENT, - to_digit_is_some::TO_DIGIT_IS_SOME, - trailing_empty_array::TRAILING_EMPTY_ARRAY, - trait_bounds::TRAIT_DUPLICATION_IN_BOUNDS, - trait_bounds::TYPE_REPETITION_IN_BOUNDS, - transmute::CROSSPOINTER_TRANSMUTE, - transmute::TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS, - transmute::TRANSMUTE_BYTES_TO_STR, - transmute::TRANSMUTE_FLOAT_TO_INT, - transmute::TRANSMUTE_INT_TO_BOOL, - transmute::TRANSMUTE_INT_TO_CHAR, - transmute::TRANSMUTE_INT_TO_FLOAT, - transmute::TRANSMUTE_NUM_TO_BYTES, - transmute::TRANSMUTE_PTR_TO_PTR, - transmute::TRANSMUTE_PTR_TO_REF, - transmute::TRANSMUTE_UNDEFINED_REPR, - transmute::TRANSMUTING_NULL, - transmute::UNSOUND_COLLECTION_TRANSMUTE, - transmute::USELESS_TRANSMUTE, - transmute::WRONG_TRANSMUTE, - types::BORROWED_BOX, - types::BOX_COLLECTION, - types::LINKEDLIST, - types::OPTION_OPTION, - types::RC_BUFFER, - types::RC_MUTEX, - types::REDUNDANT_ALLOCATION, - types::TYPE_COMPLEXITY, - types::VEC_BOX, - undocumented_unsafe_blocks::UNDOCUMENTED_UNSAFE_BLOCKS, - unicode::INVISIBLE_CHARACTERS, - unicode::NON_ASCII_LITERAL, - unicode::UNICODE_NOT_NFC, - uninit_vec::UNINIT_VEC, - unit_return_expecting_ord::UNIT_RETURN_EXPECTING_ORD, - unit_types::LET_UNIT_VALUE, - unit_types::UNIT_ARG, - unit_types::UNIT_CMP, - unnamed_address::FN_ADDRESS_COMPARISONS, - unnamed_address::VTABLE_ADDRESS_COMPARISONS, - unnecessary_owned_empty_strings::UNNECESSARY_OWNED_EMPTY_STRINGS, - unnecessary_self_imports::UNNECESSARY_SELF_IMPORTS, - unnecessary_wraps::UNNECESSARY_WRAPS, - unnested_or_patterns::UNNESTED_OR_PATTERNS, - unsafe_removed_from_name::UNSAFE_REMOVED_FROM_NAME, - unused_async::UNUSED_ASYNC, - unused_io_amount::UNUSED_IO_AMOUNT, - unused_peekable::UNUSED_PEEKABLE, - unused_rounding::UNUSED_ROUNDING, - unused_self::UNUSED_SELF, - unused_unit::UNUSED_UNIT, - unwrap::PANICKING_UNWRAP, - unwrap::UNNECESSARY_UNWRAP, - unwrap_in_result::UNWRAP_IN_RESULT, - upper_case_acronyms::UPPER_CASE_ACRONYMS, - use_self::USE_SELF, - useless_conversion::USELESS_CONVERSION, - vec::USELESS_VEC, - vec_init_then_push::VEC_INIT_THEN_PUSH, - wildcard_imports::ENUM_GLOB_USE, - wildcard_imports::WILDCARD_IMPORTS, - write::PRINTLN_EMPTY_STRING, - write::PRINT_LITERAL, - write::PRINT_STDERR, - write::PRINT_STDOUT, - write::PRINT_WITH_NEWLINE, - write::USE_DEBUG, - write::WRITELN_EMPTY_STRING, - write::WRITE_LITERAL, - write::WRITE_WITH_NEWLINE, - zero_div_zero::ZERO_DIVIDED_BY_ZERO, - zero_sized_map_values::ZERO_SIZED_MAP_VALUES, -]) diff --git a/clippy_lints/src/lib.register_nursery.rs b/clippy_lints/src/lib.register_nursery.rs deleted file mode 100644 index 65616d28d8f10..0000000000000 --- a/clippy_lints/src/lib.register_nursery.rs +++ /dev/null @@ -1,39 +0,0 @@ -// This file was generated by `cargo dev update_lints`. -// Use that command to update this file and do not edit by hand. -// Manual edits will be overwritten. - -store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![ - LintId::of(attrs::EMPTY_LINE_AFTER_OUTER_ATTR), - LintId::of(casts::AS_PTR_CAST_MUT), - LintId::of(cognitive_complexity::COGNITIVE_COMPLEXITY), - LintId::of(copies::BRANCHES_SHARING_CODE), - LintId::of(derive::DERIVE_PARTIAL_EQ_WITHOUT_EQ), - LintId::of(equatable_if_let::EQUATABLE_IF_LET), - LintId::of(fallible_impl_from::FALLIBLE_IMPL_FROM), - LintId::of(floating_point_arithmetic::IMPRECISE_FLOPS), - LintId::of(floating_point_arithmetic::SUBOPTIMAL_FLOPS), - LintId::of(future_not_send::FUTURE_NOT_SEND), - LintId::of(index_refutable_slice::INDEX_REFUTABLE_SLICE), - LintId::of(let_if_seq::USELESS_LET_IF_SEQ), - LintId::of(matches::SIGNIFICANT_DROP_IN_SCRUTINEE), - LintId::of(methods::ITER_ON_EMPTY_COLLECTIONS), - LintId::of(methods::ITER_ON_SINGLE_ITEMS), - LintId::of(methods::ITER_WITH_DRAIN), - LintId::of(methods::PATH_BUF_PUSH_OVERWRITE), - LintId::of(missing_const_for_fn::MISSING_CONST_FOR_FN), - LintId::of(mutable_debug_assertion::DEBUG_ASSERT_WITH_MUT_CALL), - LintId::of(mutex_atomic::MUTEX_ATOMIC), - LintId::of(mutex_atomic::MUTEX_INTEGER), - LintId::of(non_send_fields_in_send_ty::NON_SEND_FIELDS_IN_SEND_TY), - LintId::of(nonstandard_macro_braces::NONSTANDARD_MACRO_BRACES), - LintId::of(option_if_let_else::OPTION_IF_LET_ELSE), - LintId::of(redundant_pub_crate::REDUNDANT_PUB_CRATE), - LintId::of(regex::TRIVIAL_REGEX), - LintId::of(strings::STRING_LIT_AS_BYTES), - LintId::of(suspicious_operation_groupings::SUSPICIOUS_OPERATION_GROUPINGS), - LintId::of(trailing_empty_array::TRAILING_EMPTY_ARRAY), - LintId::of(transmute::TRANSMUTE_UNDEFINED_REPR), - LintId::of(unused_peekable::UNUSED_PEEKABLE), - LintId::of(unused_rounding::UNUSED_ROUNDING), - LintId::of(use_self::USE_SELF), -]) diff --git a/clippy_lints/src/lib.register_pedantic.rs b/clippy_lints/src/lib.register_pedantic.rs deleted file mode 100644 index 44e969585b50d..0000000000000 --- a/clippy_lints/src/lib.register_pedantic.rs +++ /dev/null @@ -1,104 +0,0 @@ -// This file was generated by `cargo dev update_lints`. -// Use that command to update this file and do not edit by hand. -// Manual edits will be overwritten. - -store.register_group(true, "clippy::pedantic", Some("clippy_pedantic"), vec![ - LintId::of(attrs::INLINE_ALWAYS), - LintId::of(casts::BORROW_AS_PTR), - LintId::of(casts::CAST_LOSSLESS), - LintId::of(casts::CAST_POSSIBLE_TRUNCATION), - LintId::of(casts::CAST_POSSIBLE_WRAP), - LintId::of(casts::CAST_PRECISION_LOSS), - LintId::of(casts::CAST_PTR_ALIGNMENT), - LintId::of(casts::CAST_SIGN_LOSS), - LintId::of(casts::PTR_AS_PTR), - LintId::of(checked_conversions::CHECKED_CONVERSIONS), - LintId::of(copies::SAME_FUNCTIONS_IN_IF_CONDITION), - LintId::of(copy_iterator::COPY_ITERATOR), - LintId::of(default::DEFAULT_TRAIT_ACCESS), - LintId::of(dereference::EXPLICIT_DEREF_METHODS), - LintId::of(dereference::REF_BINDING_TO_REFERENCE), - LintId::of(derive::EXPL_IMPL_CLONE_ON_COPY), - LintId::of(derive::UNSAFE_DERIVE_DESERIALIZE), - LintId::of(doc::DOC_LINK_WITH_QUOTES), - LintId::of(doc::DOC_MARKDOWN), - LintId::of(doc::MISSING_ERRORS_DOC), - LintId::of(doc::MISSING_PANICS_DOC), - LintId::of(empty_enum::EMPTY_ENUM), - LintId::of(enum_variants::MODULE_NAME_REPETITIONS), - LintId::of(eta_reduction::REDUNDANT_CLOSURE_FOR_METHOD_CALLS), - LintId::of(excessive_bools::FN_PARAMS_EXCESSIVE_BOOLS), - LintId::of(excessive_bools::STRUCT_EXCESSIVE_BOOLS), - LintId::of(format_args::UNINLINED_FORMAT_ARGS), - LintId::of(functions::MUST_USE_CANDIDATE), - LintId::of(functions::TOO_MANY_LINES), - LintId::of(if_not_else::IF_NOT_ELSE), - LintId::of(implicit_hasher::IMPLICIT_HASHER), - LintId::of(inconsistent_struct_constructor::INCONSISTENT_STRUCT_CONSTRUCTOR), - LintId::of(infinite_iter::MAYBE_INFINITE_ITER), - LintId::of(invalid_upcast_comparisons::INVALID_UPCAST_COMPARISONS), - LintId::of(items_after_statements::ITEMS_AFTER_STATEMENTS), - LintId::of(iter_not_returning_iterator::ITER_NOT_RETURNING_ITERATOR), - LintId::of(large_stack_arrays::LARGE_STACK_ARRAYS), - LintId::of(let_underscore::LET_UNDERSCORE_DROP), - LintId::of(literal_representation::LARGE_DIGIT_GROUPS), - LintId::of(literal_representation::UNREADABLE_LITERAL), - LintId::of(loops::EXPLICIT_INTO_ITER_LOOP), - LintId::of(loops::EXPLICIT_ITER_LOOP), - LintId::of(macro_use::MACRO_USE_IMPORTS), - LintId::of(manual_assert::MANUAL_ASSERT), - LintId::of(manual_instant_elapsed::MANUAL_INSTANT_ELAPSED), - LintId::of(manual_string_new::MANUAL_STRING_NEW), - LintId::of(matches::MATCH_BOOL), - LintId::of(matches::MATCH_ON_VEC_ITEMS), - LintId::of(matches::MATCH_SAME_ARMS), - LintId::of(matches::MATCH_WILDCARD_FOR_SINGLE_VARIANTS), - LintId::of(matches::MATCH_WILD_ERR_ARM), - LintId::of(matches::SINGLE_MATCH_ELSE), - LintId::of(methods::CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS), - LintId::of(methods::CLONED_INSTEAD_OF_COPIED), - LintId::of(methods::FILTER_MAP_NEXT), - LintId::of(methods::FLAT_MAP_OPTION), - LintId::of(methods::FROM_ITER_INSTEAD_OF_COLLECT), - LintId::of(methods::IMPLICIT_CLONE), - LintId::of(methods::INEFFICIENT_TO_STRING), - LintId::of(methods::MANUAL_OK_OR), - LintId::of(methods::MAP_UNWRAP_OR), - LintId::of(methods::NAIVE_BYTECOUNT), - LintId::of(methods::STABLE_SORT_PRIMITIVE), - LintId::of(methods::UNNECESSARY_JOIN), - LintId::of(misc::USED_UNDERSCORE_BINDING), - LintId::of(mismatching_type_param_order::MISMATCHING_TYPE_PARAM_ORDER), - LintId::of(mut_mut::MUT_MUT), - LintId::of(needless_continue::NEEDLESS_CONTINUE), - LintId::of(needless_for_each::NEEDLESS_FOR_EACH), - LintId::of(needless_pass_by_value::NEEDLESS_PASS_BY_VALUE), - LintId::of(no_effect::NO_EFFECT_UNDERSCORE_BINDING), - LintId::of(non_expressive_names::MANY_SINGLE_CHAR_NAMES), - LintId::of(non_expressive_names::SIMILAR_NAMES), - LintId::of(operators::FLOAT_CMP), - LintId::of(operators::NEEDLESS_BITWISE_BOOL), - LintId::of(operators::VERBOSE_BIT_MASK), - LintId::of(pass_by_ref_or_value::LARGE_TYPES_PASSED_BY_VALUE), - LintId::of(pass_by_ref_or_value::TRIVIALLY_COPY_PASS_BY_REF), - LintId::of(ranges::RANGE_MINUS_ONE), - LintId::of(ranges::RANGE_PLUS_ONE), - LintId::of(redundant_else::REDUNDANT_ELSE), - LintId::of(ref_option_ref::REF_OPTION_REF), - LintId::of(return_self_not_must_use::RETURN_SELF_NOT_MUST_USE), - LintId::of(semicolon_if_nothing_returned::SEMICOLON_IF_NOTHING_RETURNED), - LintId::of(strings::STRING_ADD_ASSIGN), - LintId::of(trait_bounds::TRAIT_DUPLICATION_IN_BOUNDS), - LintId::of(trait_bounds::TYPE_REPETITION_IN_BOUNDS), - LintId::of(transmute::TRANSMUTE_PTR_TO_PTR), - LintId::of(types::LINKEDLIST), - LintId::of(types::OPTION_OPTION), - LintId::of(unicode::UNICODE_NOT_NFC), - LintId::of(unnecessary_wraps::UNNECESSARY_WRAPS), - LintId::of(unnested_or_patterns::UNNESTED_OR_PATTERNS), - LintId::of(unused_async::UNUSED_ASYNC), - LintId::of(unused_self::UNUSED_SELF), - LintId::of(wildcard_imports::ENUM_GLOB_USE), - LintId::of(wildcard_imports::WILDCARD_IMPORTS), - LintId::of(zero_sized_map_values::ZERO_SIZED_MAP_VALUES), -]) diff --git a/clippy_lints/src/lib.register_perf.rs b/clippy_lints/src/lib.register_perf.rs deleted file mode 100644 index 8e927470e02ff..0000000000000 --- a/clippy_lints/src/lib.register_perf.rs +++ /dev/null @@ -1,34 +0,0 @@ -// This file was generated by `cargo dev update_lints`. -// Use that command to update this file and do not edit by hand. -// Manual edits will be overwritten. - -store.register_group(true, "clippy::perf", Some("clippy_perf"), vec![ - LintId::of(box_default::BOX_DEFAULT), - LintId::of(entry::MAP_ENTRY), - LintId::of(escape::BOXED_LOCAL), - LintId::of(format_args::FORMAT_IN_FORMAT_ARGS), - LintId::of(format_args::TO_STRING_IN_FORMAT_ARGS), - LintId::of(functions::RESULT_LARGE_ERR), - LintId::of(large_const_arrays::LARGE_CONST_ARRAYS), - LintId::of(large_enum_variant::LARGE_ENUM_VARIANT), - LintId::of(loops::MANUAL_MEMCPY), - LintId::of(loops::MISSING_SPIN_LOOP), - LintId::of(loops::NEEDLESS_COLLECT), - LintId::of(manual_retain::MANUAL_RETAIN), - LintId::of(methods::COLLAPSIBLE_STR_REPLACE), - LintId::of(methods::EXPECT_FUN_CALL), - LintId::of(methods::EXTEND_WITH_DRAIN), - LintId::of(methods::ITER_NTH), - LintId::of(methods::ITER_OVEREAGER_CLONED), - LintId::of(methods::MANUAL_STR_REPEAT), - LintId::of(methods::OR_FUN_CALL), - LintId::of(methods::SINGLE_CHAR_PATTERN), - LintId::of(methods::UNNECESSARY_TO_OWNED), - LintId::of(operators::CMP_OWNED), - LintId::of(redundant_clone::REDUNDANT_CLONE), - LintId::of(slow_vector_initialization::SLOW_VECTOR_INITIALIZATION), - LintId::of(types::BOX_COLLECTION), - LintId::of(types::REDUNDANT_ALLOCATION), - LintId::of(vec::USELESS_VEC), - LintId::of(vec_init_then_push::VEC_INIT_THEN_PUSH), -]) diff --git a/clippy_lints/src/lib.register_restriction.rs b/clippy_lints/src/lib.register_restriction.rs deleted file mode 100644 index f62d57af5b47f..0000000000000 --- a/clippy_lints/src/lib.register_restriction.rs +++ /dev/null @@ -1,90 +0,0 @@ -// This file was generated by `cargo dev update_lints`. -// Use that command to update this file and do not edit by hand. -// Manual edits will be overwritten. - -store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![ - LintId::of(as_conversions::AS_CONVERSIONS), - LintId::of(asm_syntax::INLINE_ASM_X86_ATT_SYNTAX), - LintId::of(asm_syntax::INLINE_ASM_X86_INTEL_SYNTAX), - LintId::of(assertions_on_result_states::ASSERTIONS_ON_RESULT_STATES), - LintId::of(attrs::ALLOW_ATTRIBUTES_WITHOUT_REASON), - LintId::of(casts::AS_UNDERSCORE), - LintId::of(casts::FN_TO_NUMERIC_CAST_ANY), - LintId::of(create_dir::CREATE_DIR), - LintId::of(dbg_macro::DBG_MACRO), - LintId::of(default_numeric_fallback::DEFAULT_NUMERIC_FALLBACK), - LintId::of(default_union_representation::DEFAULT_UNION_REPRESENTATION), - LintId::of(disallowed_script_idents::DISALLOWED_SCRIPT_IDENTS), - LintId::of(else_if_without_else::ELSE_IF_WITHOUT_ELSE), - LintId::of(empty_drop::EMPTY_DROP), - LintId::of(empty_structs_with_brackets::EMPTY_STRUCTS_WITH_BRACKETS), - LintId::of(exhaustive_items::EXHAUSTIVE_ENUMS), - LintId::of(exhaustive_items::EXHAUSTIVE_STRUCTS), - LintId::of(exit::EXIT), - LintId::of(float_literal::LOSSY_FLOAT_LITERAL), - LintId::of(format_push_string::FORMAT_PUSH_STRING), - LintId::of(if_then_some_else_none::IF_THEN_SOME_ELSE_NONE), - LintId::of(implicit_return::IMPLICIT_RETURN), - LintId::of(indexing_slicing::INDEXING_SLICING), - LintId::of(inherent_impl::MULTIPLE_INHERENT_IMPL), - LintId::of(large_include_file::LARGE_INCLUDE_FILE), - LintId::of(let_underscore::LET_UNDERSCORE_MUST_USE), - LintId::of(literal_representation::DECIMAL_LITERAL_REPRESENTATION), - LintId::of(matches::REST_PAT_IN_FULLY_BOUND_STRUCTS), - LintId::of(matches::TRY_ERR), - LintId::of(matches::WILDCARD_ENUM_MATCH_ARM), - LintId::of(mem_forget::MEM_FORGET), - LintId::of(methods::CLONE_ON_REF_PTR), - LintId::of(methods::EXPECT_USED), - LintId::of(methods::FILETYPE_IS_FILE), - LintId::of(methods::GET_UNWRAP), - LintId::of(methods::MAP_ERR_IGNORE), - LintId::of(methods::UNWRAP_USED), - LintId::of(methods::VERBOSE_FILE_READS), - LintId::of(misc_early::SEPARATED_LITERAL_SUFFIX), - LintId::of(misc_early::UNNEEDED_FIELD_PATTERN), - LintId::of(misc_early::UNSEPARATED_LITERAL_SUFFIX), - LintId::of(missing_doc::MISSING_DOCS_IN_PRIVATE_ITEMS), - LintId::of(missing_enforced_import_rename::MISSING_ENFORCED_IMPORT_RENAMES), - LintId::of(missing_inline::MISSING_INLINE_IN_PUBLIC_ITEMS), - LintId::of(missing_trait_methods::MISSING_TRAIT_METHODS), - LintId::of(mixed_read_write_in_expression::MIXED_READ_WRITE_IN_EXPRESSION), - LintId::of(module_style::MOD_MODULE_FILES), - LintId::of(module_style::SELF_NAMED_MODULE_FILES), - LintId::of(operators::ARITHMETIC_SIDE_EFFECTS), - LintId::of(operators::FLOAT_ARITHMETIC), - LintId::of(operators::FLOAT_CMP_CONST), - LintId::of(operators::INTEGER_ARITHMETIC), - LintId::of(operators::INTEGER_DIVISION), - LintId::of(operators::MODULO_ARITHMETIC), - LintId::of(panic_in_result_fn::PANIC_IN_RESULT_FN), - LintId::of(panic_unimplemented::PANIC), - LintId::of(panic_unimplemented::TODO), - LintId::of(panic_unimplemented::UNIMPLEMENTED), - LintId::of(panic_unimplemented::UNREACHABLE), - LintId::of(partial_pub_fields::PARTIAL_PUB_FIELDS), - LintId::of(pattern_type_mismatch::PATTERN_TYPE_MISMATCH), - LintId::of(pub_use::PUB_USE), - LintId::of(redundant_slicing::DEREF_BY_SLICING), - LintId::of(same_name_method::SAME_NAME_METHOD), - LintId::of(shadow::SHADOW_REUSE), - LintId::of(shadow::SHADOW_SAME), - LintId::of(shadow::SHADOW_UNRELATED), - LintId::of(single_char_lifetime_names::SINGLE_CHAR_LIFETIME_NAMES), - LintId::of(std_instead_of_core::ALLOC_INSTEAD_OF_CORE), - LintId::of(std_instead_of_core::STD_INSTEAD_OF_ALLOC), - LintId::of(std_instead_of_core::STD_INSTEAD_OF_CORE), - LintId::of(strings::STRING_ADD), - LintId::of(strings::STRING_SLICE), - LintId::of(strings::STRING_TO_STRING), - LintId::of(strings::STR_TO_STRING), - LintId::of(types::RC_BUFFER), - LintId::of(types::RC_MUTEX), - LintId::of(undocumented_unsafe_blocks::UNDOCUMENTED_UNSAFE_BLOCKS), - LintId::of(unicode::NON_ASCII_LITERAL), - LintId::of(unnecessary_self_imports::UNNECESSARY_SELF_IMPORTS), - LintId::of(unwrap_in_result::UNWRAP_IN_RESULT), - LintId::of(write::PRINT_STDERR), - LintId::of(write::PRINT_STDOUT), - LintId::of(write::USE_DEBUG), -]) diff --git a/clippy_lints/src/lib.register_style.rs b/clippy_lints/src/lib.register_style.rs deleted file mode 100644 index 3312f5648552e..0000000000000 --- a/clippy_lints/src/lib.register_style.rs +++ /dev/null @@ -1,131 +0,0 @@ -// This file was generated by `cargo dev update_lints`. -// Use that command to update this file and do not edit by hand. -// Manual edits will be overwritten. - -store.register_group(true, "clippy::style", Some("clippy_style"), vec![ - LintId::of(assertions_on_constants::ASSERTIONS_ON_CONSTANTS), - LintId::of(blocks_in_if_conditions::BLOCKS_IN_IF_CONDITIONS), - LintId::of(bool_assert_comparison::BOOL_ASSERT_COMPARISON), - LintId::of(bool_to_int_with_if::BOOL_TO_INT_WITH_IF), - LintId::of(casts::FN_TO_NUMERIC_CAST), - LintId::of(casts::FN_TO_NUMERIC_CAST_WITH_TRUNCATION), - LintId::of(collapsible_if::COLLAPSIBLE_ELSE_IF), - LintId::of(collapsible_if::COLLAPSIBLE_IF), - LintId::of(comparison_chain::COMPARISON_CHAIN), - LintId::of(default::FIELD_REASSIGN_WITH_DEFAULT), - LintId::of(default_instead_of_iter_empty::DEFAULT_INSTEAD_OF_ITER_EMPTY), - LintId::of(dereference::NEEDLESS_BORROW), - LintId::of(disallowed_macros::DISALLOWED_MACROS), - LintId::of(disallowed_methods::DISALLOWED_METHODS), - LintId::of(disallowed_names::DISALLOWED_NAMES), - LintId::of(disallowed_types::DISALLOWED_TYPES), - LintId::of(doc::MISSING_SAFETY_DOC), - LintId::of(doc::NEEDLESS_DOCTEST_MAIN), - LintId::of(enum_variants::ENUM_VARIANT_NAMES), - LintId::of(enum_variants::MODULE_INCEPTION), - LintId::of(eta_reduction::REDUNDANT_CLOSURE), - LintId::of(float_literal::EXCESSIVE_PRECISION), - LintId::of(from_over_into::FROM_OVER_INTO), - LintId::of(from_str_radix_10::FROM_STR_RADIX_10), - LintId::of(functions::DOUBLE_MUST_USE), - LintId::of(functions::MUST_USE_UNIT), - LintId::of(functions::RESULT_UNIT_ERR), - LintId::of(implicit_saturating_add::IMPLICIT_SATURATING_ADD), - LintId::of(implicit_saturating_sub::IMPLICIT_SATURATING_SUB), - LintId::of(inherent_to_string::INHERENT_TO_STRING), - LintId::of(init_numbered_fields::INIT_NUMBERED_FIELDS), - LintId::of(len_zero::COMPARISON_TO_EMPTY), - LintId::of(len_zero::LEN_WITHOUT_IS_EMPTY), - LintId::of(len_zero::LEN_ZERO), - LintId::of(literal_representation::INCONSISTENT_DIGIT_GROUPING), - LintId::of(literal_representation::UNUSUAL_BYTE_GROUPINGS), - LintId::of(loops::FOR_KV_MAP), - LintId::of(loops::NEEDLESS_RANGE_LOOP), - LintId::of(loops::SAME_ITEM_PUSH), - LintId::of(loops::WHILE_LET_ON_ITERATOR), - LintId::of(main_recursion::MAIN_RECURSION), - LintId::of(manual_async_fn::MANUAL_ASYNC_FN), - LintId::of(manual_bits::MANUAL_BITS), - LintId::of(manual_non_exhaustive::MANUAL_NON_EXHAUSTIVE), - LintId::of(match_result_ok::MATCH_RESULT_OK), - LintId::of(matches::COLLAPSIBLE_MATCH), - LintId::of(matches::INFALLIBLE_DESTRUCTURING_MATCH), - LintId::of(matches::MANUAL_MAP), - LintId::of(matches::MATCH_LIKE_MATCHES_MACRO), - LintId::of(matches::MATCH_OVERLAPPING_ARM), - LintId::of(matches::MATCH_REF_PATS), - LintId::of(matches::REDUNDANT_PATTERN_MATCHING), - LintId::of(matches::SINGLE_MATCH), - LintId::of(mem_replace::MEM_REPLACE_OPTION_WITH_NONE), - LintId::of(mem_replace::MEM_REPLACE_WITH_DEFAULT), - LintId::of(methods::BYTES_NTH), - LintId::of(methods::CHARS_LAST_CMP), - LintId::of(methods::CHARS_NEXT_CMP), - LintId::of(methods::ERR_EXPECT), - LintId::of(methods::GET_FIRST), - LintId::of(methods::INTO_ITER_ON_REF), - LintId::of(methods::IS_DIGIT_ASCII_RADIX), - LintId::of(methods::ITER_CLONED_COLLECT), - LintId::of(methods::ITER_NEXT_SLICE), - LintId::of(methods::ITER_NTH_ZERO), - LintId::of(methods::ITER_SKIP_NEXT), - LintId::of(methods::MANUAL_SATURATING_ARITHMETIC), - LintId::of(methods::MAP_CLONE), - LintId::of(methods::MAP_COLLECT_RESULT_UNIT), - LintId::of(methods::MUT_MUTEX_LOCK), - LintId::of(methods::NEW_RET_NO_SELF), - LintId::of(methods::OBFUSCATED_IF_ELSE), - LintId::of(methods::OK_EXPECT), - LintId::of(methods::OPTION_MAP_OR_NONE), - LintId::of(methods::RESULT_MAP_OR_INTO_OPTION), - LintId::of(methods::SHOULD_IMPLEMENT_TRAIT), - LintId::of(methods::SINGLE_CHAR_ADD_STR), - LintId::of(methods::STRING_EXTEND_CHARS), - LintId::of(methods::UNNECESSARY_FOLD), - LintId::of(methods::UNNECESSARY_LAZY_EVALUATIONS), - LintId::of(methods::UNWRAP_OR_ELSE_DEFAULT), - LintId::of(methods::WRONG_SELF_CONVENTION), - LintId::of(misc::TOPLEVEL_REF_ARG), - LintId::of(misc::ZERO_PTR), - LintId::of(misc_early::BUILTIN_TYPE_SHADOW), - LintId::of(misc_early::DOUBLE_NEG), - LintId::of(misc_early::DUPLICATE_UNDERSCORE_ARGUMENT), - LintId::of(misc_early::MIXED_CASE_HEX_LITERALS), - LintId::of(misc_early::REDUNDANT_PATTERN), - LintId::of(mut_reference::UNNECESSARY_MUT_PASSED), - LintId::of(needless_late_init::NEEDLESS_LATE_INIT), - LintId::of(needless_parens_on_range_literals::NEEDLESS_PARENS_ON_RANGE_LITERALS), - LintId::of(neg_multiply::NEG_MULTIPLY), - LintId::of(new_without_default::NEW_WITHOUT_DEFAULT), - LintId::of(non_copy_const::BORROW_INTERIOR_MUTABLE_CONST), - LintId::of(non_copy_const::DECLARE_INTERIOR_MUTABLE_CONST), - LintId::of(non_expressive_names::JUST_UNDERSCORES_AND_DIGITS), - LintId::of(operators::ASSIGN_OP_PATTERN), - LintId::of(operators::OP_REF), - LintId::of(operators::PTR_EQ), - LintId::of(partialeq_to_none::PARTIALEQ_TO_NONE), - LintId::of(ptr::CMP_NULL), - LintId::of(ptr::PTR_ARG), - LintId::of(question_mark::QUESTION_MARK), - LintId::of(ranges::MANUAL_RANGE_CONTAINS), - LintId::of(redundant_field_names::REDUNDANT_FIELD_NAMES), - LintId::of(redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES), - LintId::of(returns::LET_AND_RETURN), - LintId::of(returns::NEEDLESS_RETURN), - LintId::of(self_named_constructors::SELF_NAMED_CONSTRUCTORS), - LintId::of(single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS), - LintId::of(strings::TRIM_SPLIT_WHITESPACE), - LintId::of(tabs_in_doc_comments::TABS_IN_DOC_COMMENTS), - LintId::of(to_digit_is_some::TO_DIGIT_IS_SOME), - LintId::of(unit_types::LET_UNIT_VALUE), - LintId::of(unnecessary_owned_empty_strings::UNNECESSARY_OWNED_EMPTY_STRINGS), - LintId::of(unsafe_removed_from_name::UNSAFE_REMOVED_FROM_NAME), - LintId::of(unused_unit::UNUSED_UNIT), - LintId::of(upper_case_acronyms::UPPER_CASE_ACRONYMS), - LintId::of(write::PRINTLN_EMPTY_STRING), - LintId::of(write::PRINT_LITERAL), - LintId::of(write::PRINT_WITH_NEWLINE), - LintId::of(write::WRITELN_EMPTY_STRING), - LintId::of(write::WRITE_LITERAL), - LintId::of(write::WRITE_WITH_NEWLINE), -]) diff --git a/clippy_lints/src/lib.register_suspicious.rs b/clippy_lints/src/lib.register_suspicious.rs deleted file mode 100644 index b70c4bb73e57d..0000000000000 --- a/clippy_lints/src/lib.register_suspicious.rs +++ /dev/null @@ -1,38 +0,0 @@ -// This file was generated by `cargo dev update_lints`. -// Use that command to update this file and do not edit by hand. -// Manual edits will be overwritten. - -store.register_group(true, "clippy::suspicious", Some("clippy_suspicious"), vec![ - LintId::of(almost_complete_letter_range::ALMOST_COMPLETE_LETTER_RANGE), - LintId::of(attrs::BLANKET_CLIPPY_RESTRICTION_LINTS), - LintId::of(await_holding_invalid::AWAIT_HOLDING_INVALID_TYPE), - LintId::of(await_holding_invalid::AWAIT_HOLDING_LOCK), - LintId::of(await_holding_invalid::AWAIT_HOLDING_REFCELL_REF), - LintId::of(casts::CAST_ABS_TO_UNSIGNED), - LintId::of(casts::CAST_ENUM_CONSTRUCTOR), - LintId::of(casts::CAST_ENUM_TRUNCATION), - LintId::of(casts::CAST_NAN_TO_INT), - LintId::of(casts::CAST_SLICE_FROM_RAW_PARTS), - LintId::of(crate_in_macro_def::CRATE_IN_MACRO_DEF), - LintId::of(drop_forget_ref::DROP_NON_DROP), - LintId::of(drop_forget_ref::FORGET_NON_DROP), - LintId::of(duplicate_mod::DUPLICATE_MOD), - LintId::of(format_impl::PRINT_IN_FORMAT_IMPL), - LintId::of(formatting::SUSPICIOUS_ASSIGNMENT_FORMATTING), - LintId::of(formatting::SUSPICIOUS_ELSE_FORMATTING), - LintId::of(formatting::SUSPICIOUS_UNARY_OP_FORMATTING), - LintId::of(loops::EMPTY_LOOP), - LintId::of(loops::MUT_RANGE_BOUND), - LintId::of(methods::NO_EFFECT_REPLACE), - LintId::of(methods::SUSPICIOUS_MAP), - LintId::of(methods::SUSPICIOUS_TO_OWNED), - LintId::of(multi_assignments::MULTI_ASSIGNMENTS), - LintId::of(mut_key::MUTABLE_KEY_TYPE), - LintId::of(octal_escapes::OCTAL_ESCAPES), - LintId::of(operators::FLOAT_EQUALITY_WITHOUT_ABS), - LintId::of(operators::MISREFACTORED_ASSIGN_OP), - LintId::of(rc_clone_in_vec_init::RC_CLONE_IN_VEC_INIT), - LintId::of(suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL), - LintId::of(suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL), - LintId::of(swap_ptr_to_ref::SWAP_PTR_TO_REF), -]) diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 1307096b28d7b..b481314abedc8 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -32,8 +32,8 @@ extern crate rustc_driver; extern crate rustc_errors; extern crate rustc_hir; extern crate rustc_hir_analysis; -extern crate rustc_hir_typeck; extern crate rustc_hir_pretty; +extern crate rustc_hir_typeck; extern crate rustc_index; extern crate rustc_infer; extern crate rustc_lexer; @@ -47,122 +47,24 @@ extern crate rustc_trait_selection; #[macro_use] extern crate clippy_utils; +#[macro_use] +extern crate declare_clippy_lint; + +use std::io; +use std::path::PathBuf; use clippy_utils::parse_msrv; use rustc_data_structures::fx::FxHashSet; -use rustc_lint::LintId; +use rustc_lint::{Lint, LintId}; use rustc_semver::RustcVersion; use rustc_session::Session; -/// Macro used to declare a Clippy lint. -/// -/// Every lint declaration consists of 4 parts: -/// -/// 1. The documentation, which is used for the website -/// 2. The `LINT_NAME`. See [lint naming][lint_naming] on lint naming conventions. -/// 3. The `lint_level`, which is a mapping from *one* of our lint groups to `Allow`, `Warn` or -/// `Deny`. The lint level here has nothing to do with what lint groups the lint is a part of. -/// 4. The `description` that contains a short explanation on what's wrong with code where the -/// lint is triggered. -/// -/// Currently the categories `style`, `correctness`, `suspicious`, `complexity` and `perf` are -/// enabled by default. As said in the README.md of this repository, if the lint level mapping -/// changes, please update README.md. -/// -/// # Example -/// -/// ``` -/// #![feature(rustc_private)] -/// extern crate rustc_session; -/// use rustc_session::declare_tool_lint; -/// use clippy_lints::declare_clippy_lint; -/// -/// declare_clippy_lint! { -/// /// ### What it does -/// /// Checks for ... (describe what the lint matches). -/// /// -/// /// ### Why is this bad? -/// /// Supply the reason for linting the code. -/// /// -/// /// ### Example -/// /// ```rust -/// /// Insert a short example of code that triggers the lint -/// /// ``` -/// /// -/// /// Use instead: -/// /// ```rust -/// /// Insert a short example of improved code that doesn't trigger the lint -/// /// ``` -/// pub LINT_NAME, -/// pedantic, -/// "description" -/// } -/// ``` -/// [lint_naming]: https://rust-lang.github.io/rfcs/0344-conventions-galore.html#lints -#[macro_export] -macro_rules! declare_clippy_lint { - { $(#[$attr:meta])* pub $name:tt, style, $description:tt } => { - declare_tool_lint! { - $(#[$attr])* pub clippy::$name, Warn, $description, report_in_external_macro: true - } - }; - { $(#[$attr:meta])* pub $name:tt, correctness, $description:tt } => { - declare_tool_lint! { - $(#[$attr])* pub clippy::$name, Deny, $description, report_in_external_macro: true - } - }; - { $(#[$attr:meta])* pub $name:tt, suspicious, $description:tt } => { - declare_tool_lint! { - $(#[$attr])* pub clippy::$name, Warn, $description, report_in_external_macro: true - } - }; - { $(#[$attr:meta])* pub $name:tt, complexity, $description:tt } => { - declare_tool_lint! { - $(#[$attr])* pub clippy::$name, Warn, $description, report_in_external_macro: true - } - }; - { $(#[$attr:meta])* pub $name:tt, perf, $description:tt } => { - declare_tool_lint! { - $(#[$attr])* pub clippy::$name, Warn, $description, report_in_external_macro: true - } - }; - { $(#[$attr:meta])* pub $name:tt, pedantic, $description:tt } => { - declare_tool_lint! { - $(#[$attr])* pub clippy::$name, Allow, $description, report_in_external_macro: true - } - }; - { $(#[$attr:meta])* pub $name:tt, restriction, $description:tt } => { - declare_tool_lint! { - $(#[$attr])* pub clippy::$name, Allow, $description, report_in_external_macro: true - } - }; - { $(#[$attr:meta])* pub $name:tt, cargo, $description:tt } => { - declare_tool_lint! { - $(#[$attr])* pub clippy::$name, Allow, $description, report_in_external_macro: true - } - }; - { $(#[$attr:meta])* pub $name:tt, nursery, $description:tt } => { - declare_tool_lint! { - $(#[$attr])* pub clippy::$name, Allow, $description, report_in_external_macro: true - } - }; - { $(#[$attr:meta])* pub $name:tt, internal, $description:tt } => { - declare_tool_lint! { - $(#[$attr])* pub clippy::$name, Allow, $description, report_in_external_macro: true - } - }; - { $(#[$attr:meta])* pub $name:tt, internal_warn, $description:tt } => { - declare_tool_lint! { - $(#[$attr])* pub clippy::$name, Warn, $description, report_in_external_macro: true - } - }; -} - #[cfg(feature = "internal")] pub mod deprecated_lints; #[cfg_attr(feature = "internal", allow(clippy::missing_clippy_version_attribute))] mod utils; +mod declared_lints; mod renamed_lints; // begin lints modules, do not remove this comment, it’s used in `update_lints` @@ -231,6 +133,7 @@ mod format_impl; mod format_push_string; mod formatting; mod from_over_into; +mod from_raw_with_void_ptr; mod from_str_radix_10; mod functions; mod future_not_send; @@ -249,6 +152,7 @@ mod inherent_impl; mod inherent_to_string; mod init_numbered_fields; mod inline_fn_without_body; +mod instant_subtraction; mod int_plus_one; mod invalid_upcast_comparisons; mod invalid_utf8_in_unchecked; @@ -270,7 +174,8 @@ mod manual_assert; mod manual_async_fn; mod manual_bits; mod manual_clamp; -mod manual_instant_elapsed; +mod manual_is_ascii_check; +mod manual_let_else; mod manual_non_exhaustive; mod manual_rem_euclid; mod manual_retain; @@ -365,6 +270,7 @@ mod strings; mod strlen_on_c_strings; mod suspicious_operation_groupings; mod suspicious_trait_impl; +mod suspicious_xor_used_as_pow; mod swap; mod swap_ptr_to_ref; mod tabs_in_doc_comments; @@ -404,8 +310,8 @@ mod zero_div_zero; mod zero_sized_map_values; // end lints modules, do not remove this comment, it’s used in `update_lints` -pub use crate::utils::conf::Conf; use crate::utils::conf::{format_error, TryConf}; +pub use crate::utils::conf::{lookup_conf_file, Conf}; /// Register all pre expansion lints /// @@ -462,8 +368,8 @@ fn read_msrv(conf: &Conf, sess: &Session) -> Option { } #[doc(hidden)] -pub fn read_conf(sess: &Session) -> Conf { - let file_name = match utils::conf::lookup_conf_file() { +pub fn read_conf(sess: &Session, path: &io::Result>) -> Conf { + let file_name = match path { Ok(Some(path)) => path, Ok(None) => return Conf::default(), Err(error) => { @@ -473,7 +379,7 @@ pub fn read_conf(sess: &Session) -> Conf { }, }; - let TryConf { conf, errors, warnings } = utils::conf::read(&file_name); + let TryConf { conf, errors, warnings } = utils::conf::read(file_name); // all conf errors are non-fatal, we just use the default conf in case of error for error in errors { sess.err(format!( @@ -495,31 +401,121 @@ pub fn read_conf(sess: &Session) -> Conf { conf } +#[derive(Default)] +struct RegistrationGroups { + all: Vec, + cargo: Vec, + complexity: Vec, + correctness: Vec, + nursery: Vec, + pedantic: Vec, + perf: Vec, + restriction: Vec, + style: Vec, + suspicious: Vec, + #[cfg(feature = "internal")] + internal: Vec, +} + +impl RegistrationGroups { + #[rustfmt::skip] + fn register(self, store: &mut rustc_lint::LintStore) { + store.register_group(true, "clippy::all", Some("clippy_all"), self.all); + store.register_group(true, "clippy::cargo", Some("clippy_cargo"), self.cargo); + store.register_group(true, "clippy::complexity", Some("clippy_complexity"), self.complexity); + store.register_group(true, "clippy::correctness", Some("clippy_correctness"), self.correctness); + store.register_group(true, "clippy::nursery", Some("clippy_nursery"), self.nursery); + store.register_group(true, "clippy::pedantic", Some("clippy_pedantic"), self.pedantic); + store.register_group(true, "clippy::perf", Some("clippy_perf"), self.perf); + store.register_group(true, "clippy::restriction", Some("clippy_restriction"), self.restriction); + store.register_group(true, "clippy::style", Some("clippy_style"), self.style); + store.register_group(true, "clippy::suspicious", Some("clippy_suspicious"), self.suspicious); + #[cfg(feature = "internal")] + store.register_group(true, "clippy::internal", Some("clippy_internal"), self.internal); + } +} + +#[derive(Copy, Clone)] +pub(crate) enum LintCategory { + Cargo, + Complexity, + Correctness, + Nursery, + Pedantic, + Perf, + Restriction, + Style, + Suspicious, + #[cfg(feature = "internal")] + Internal, +} +#[allow(clippy::enum_glob_use)] +use LintCategory::*; + +impl LintCategory { + fn is_all(self) -> bool { + matches!(self, Correctness | Suspicious | Style | Complexity | Perf) + } + + fn group(self, groups: &mut RegistrationGroups) -> &mut Vec { + match self { + Cargo => &mut groups.cargo, + Complexity => &mut groups.complexity, + Correctness => &mut groups.correctness, + Nursery => &mut groups.nursery, + Pedantic => &mut groups.pedantic, + Perf => &mut groups.perf, + Restriction => &mut groups.restriction, + Style => &mut groups.style, + Suspicious => &mut groups.suspicious, + #[cfg(feature = "internal")] + Internal => &mut groups.internal, + } + } +} + +pub(crate) struct LintInfo { + /// Double reference to maintain pointer equality + lint: &'static &'static Lint, + category: LintCategory, + explanation: &'static str, +} + +pub fn explain(name: &str) { + let target = format!("clippy::{}", name.to_ascii_uppercase()); + match declared_lints::LINTS.iter().find(|info| info.lint.name == target) { + Some(info) => print!("{}", info.explanation), + None => println!("unknown lint: {name}"), + } +} + +fn register_categories(store: &mut rustc_lint::LintStore) { + let mut groups = RegistrationGroups::default(); + + for LintInfo { lint, category, .. } in declared_lints::LINTS { + if category.is_all() { + groups.all.push(LintId::of(lint)); + } + + category.group(&mut groups).push(LintId::of(lint)); + } + + let lints: Vec<&'static Lint> = declared_lints::LINTS.iter().map(|info| *info.lint).collect(); + + store.register_lints(&lints); + groups.register(store); +} + /// Register all lints and lint groups with the rustc plugin registry /// /// Used in `./src/driver.rs`. #[expect(clippy::too_many_lines)] pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: &Conf) { register_removed_non_tool_lints(store); + register_categories(store); include!("lib.deprecated.rs"); - include!("lib.register_lints.rs"); - include!("lib.register_restriction.rs"); - include!("lib.register_pedantic.rs"); - - #[cfg(feature = "internal")] - include!("lib.register_internal.rs"); - - include!("lib.register_all.rs"); - include!("lib.register_style.rs"); - include!("lib.register_complexity.rs"); - include!("lib.register_correctness.rs"); - include!("lib.register_suspicious.rs"); - include!("lib.register_perf.rs"); - include!("lib.register_cargo.rs"); - include!("lib.register_nursery.rs"); - #[cfg(feature = "internal")] { if std::env::var("ENABLE_METADATA_COLLECTION").eq(&Ok("1".to_string())) { @@ -614,6 +610,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: )) }); store.register_late_pass(move |_| Box::new(matches::Matches::new(msrv))); + let matches_for_let_else = conf.matches_for_let_else; + store.register_late_pass(move |_| Box::new(manual_let_else::ManualLetElse::new(msrv, matches_for_let_else))); store.register_early_pass(move || Box::new(manual_non_exhaustive::ManualNonExhaustiveStruct::new(msrv))); store.register_late_pass(move |_| Box::new(manual_non_exhaustive::ManualNonExhaustiveEnum::new(msrv))); store.register_late_pass(move |_| Box::new(manual_strip::ManualStrip::new(msrv))); @@ -735,7 +733,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: let max_trait_bounds = conf.max_trait_bounds; store.register_late_pass(move |_| Box::new(trait_bounds::TraitBounds::new(max_trait_bounds))); store.register_late_pass(|_| Box::new(comparison_chain::ComparisonChain)); - store.register_late_pass(|_| Box::new(mut_key::MutableKeyType)); + let ignore_interior_mutability = conf.ignore_interior_mutability.clone(); + store.register_late_pass(move |_| Box::new(mut_key::MutableKeyType::new(ignore_interior_mutability.clone()))); store.register_early_pass(|| Box::new(reference::DerefAddrOf)); store.register_early_pass(|| Box::new(double_parens::DoubleParens)); store.register_late_pass(|_| Box::new(format_impl::FormatImpl::new())); @@ -794,10 +793,10 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(floating_point_arithmetic::FloatingPointArithmetic)); store.register_early_pass(|| Box::new(as_conversions::AsConversions)); store.register_late_pass(|_| Box::new(let_underscore::LetUnderscore)); - store.register_early_pass(|| Box::new(single_component_path_imports::SingleComponentPathImports)); + store.register_early_pass(|| Box::::default()); let max_fn_params_bools = conf.max_fn_params_bools; let max_struct_bools = conf.max_struct_bools; - store.register_early_pass(move || { + store.register_late_pass(move |_| { Box::new(excessive_bools::ExcessiveBools::new( max_struct_bools, max_fn_params_bools, @@ -879,13 +878,14 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::::default()); let allow_dbg_in_tests = conf.allow_dbg_in_tests; store.register_late_pass(move |_| Box::new(dbg_macro::DbgMacro::new(allow_dbg_in_tests))); + let allow_print_in_tests = conf.allow_print_in_tests; + store.register_late_pass(move |_| Box::new(write::Write::new(allow_print_in_tests))); let cargo_ignore_publish = conf.cargo_ignore_publish; store.register_late_pass(move |_| { Box::new(cargo::Cargo { ignore_publish: cargo_ignore_publish, }) }); - store.register_late_pass(|_| Box::::default()); store.register_early_pass(|| Box::new(crate_in_macro_def::CrateInMacroDef)); store.register_early_pass(|| Box::new(empty_structs_with_brackets::EmptyStructsWithBrackets)); store.register_late_pass(|_| Box::new(unnecessary_owned_empty_strings::UnnecessaryOwnedEmptyStrings)); @@ -908,7 +908,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(move |_| Box::new(operators::Operators::new(verbose_bit_mask_threshold))); store.register_late_pass(|_| Box::new(invalid_utf8_in_unchecked::InvalidUtf8InUnchecked)); store.register_late_pass(|_| Box::::default()); - store.register_late_pass(|_| Box::new(manual_instant_elapsed::ManualInstantElapsed)); + store.register_late_pass(move |_| Box::new(instant_subtraction::InstantSubtraction::new(msrv))); store.register_late_pass(|_| Box::new(partialeq_to_none::PartialeqToNone)); store.register_late_pass(move |_| Box::new(manual_clamp::ManualClamp::new(msrv))); store.register_late_pass(|_| Box::new(manual_string_new::ManualStringNew)); @@ -919,6 +919,9 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(implicit_saturating_add::ImplicitSaturatingAdd)); store.register_early_pass(|| Box::new(partial_pub_fields::PartialPubFields)); store.register_late_pass(|_| Box::new(missing_trait_methods::MissingTraitMethods)); + store.register_late_pass(|_| Box::new(from_raw_with_void_ptr::FromRawWithVoidPtr)); + store.register_late_pass(|_| Box::new(suspicious_xor_used_as_pow::ConfusingXorAndPow)); + store.register_late_pass(move |_| Box::new(manual_is_ascii_check::ManualIsAsciiCheck::new(msrv))); // add lints here, do not remove this comment, it's used in `new_lint` } diff --git a/clippy_lints/src/lifetimes.rs b/clippy_lints/src/lifetimes.rs index 54c316358a14b..0bb9eca15287d 100644 --- a/clippy_lints/src/lifetimes.rs +++ b/clippy_lints/src/lifetimes.rs @@ -1,4 +1,4 @@ -use clippy_utils::diagnostics::span_lint; +use clippy_utils::diagnostics::{span_lint, span_lint_and_then}; use clippy_utils::trait_ref_of_method; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_hir::intravisit::nested_filter::{self as hir_nested_filter, NestedFilter}; @@ -152,6 +152,7 @@ fn check_fn_inner<'tcx>( .params .iter() .filter(|param| matches!(param.kind, GenericParamKind::Type { .. })); + for typ in types { for pred in generics.bounds_for_param(cx.tcx.hir().local_def_id(typ.hir_id)) { if pred.origin == PredicateOrigin::WhereClause { @@ -188,15 +189,30 @@ fn check_fn_inner<'tcx>( } } } - if could_use_elision(cx, decl, body, trait_sig, generics.params) { - span_lint( + + if let Some(elidable_lts) = could_use_elision(cx, decl, body, trait_sig, generics.params) { + let lts = elidable_lts + .iter() + // In principle, the result of the call to `Node::ident` could be `unwrap`ped, as `DefId` should refer to a + // `Node::GenericParam`. + .filter_map(|&(def_id, _)| cx.tcx.hir().get_by_def_id(def_id).ident()) + .map(|ident| ident.to_string()) + .collect::>() + .join(", "); + + span_lint_and_then( cx, NEEDLESS_LIFETIMES, span.with_hi(decl.output.span().hi()), - "explicit lifetimes given in parameter types where they could be elided \ - (or replaced with `'_` if needed by type declaration)", + &format!("the following explicit lifetimes could be elided: {lts}"), + |diag| { + if let Some(span) = elidable_lts.iter().find_map(|&(_, span)| span) { + diag.span_help(span, "replace with `'_` in generic arguments such as here"); + } + }, ); } + if report_extra_lifetimes { self::report_extra_lifetimes(cx, decl, generics); } @@ -227,7 +243,7 @@ fn could_use_elision<'tcx>( body: Option, trait_sig: Option<&[Ident]>, named_generics: &'tcx [GenericParam<'_>], -) -> bool { +) -> Option)>> { // There are two scenarios where elision works: // * no output references, all input references have different LT // * output references, exactly one input reference with same LT @@ -254,7 +270,7 @@ fn could_use_elision<'tcx>( } if input_visitor.abort() || output_visitor.abort() { - return false; + return None; } let input_lts = input_visitor.lts; @@ -262,7 +278,7 @@ fn could_use_elision<'tcx>( if let Some(trait_sig) = trait_sig { if explicit_self_type(cx, func, trait_sig.first().copied()) { - return false; + return None; } } @@ -271,7 +287,7 @@ fn could_use_elision<'tcx>( let first_ident = body.params.first().and_then(|param| param.pat.simple_ident()); if explicit_self_type(cx, func, first_ident) { - return false; + return None; } let mut checker = BodyLifetimeChecker { @@ -279,14 +295,14 @@ fn could_use_elision<'tcx>( }; checker.visit_expr(body.value); if checker.lifetimes_used_in_body { - return false; + return None; } } // check for lifetimes from higher scopes for lt in input_lts.iter().chain(output_lts.iter()) { if !allowed_lts.contains(lt) { - return false; + return None; } } @@ -302,48 +318,45 @@ fn could_use_elision<'tcx>( for lt in input_visitor.nested_elision_site_lts { if let RefLt::Named(def_id) = lt { if allowed_lts.contains(&cx.tcx.item_name(def_id.to_def_id())) { - return false; + return None; } } } for lt in output_visitor.nested_elision_site_lts { if let RefLt::Named(def_id) = lt { if allowed_lts.contains(&cx.tcx.item_name(def_id.to_def_id())) { - return false; + return None; } } } } - // no input lifetimes? easy case! - if input_lts.is_empty() { - false - } else if output_lts.is_empty() { - // no output lifetimes, check distinctness of input lifetimes + // A lifetime can be newly elided if: + // - It occurs only once among the inputs. + // - If there are multiple input lifetimes, then the newly elided lifetime does not occur among the + // outputs (because eliding such an lifetime would create an ambiguity). + let elidable_lts = named_lifetime_occurrences(&input_lts) + .into_iter() + .filter_map(|(def_id, occurrences)| { + if occurrences == 1 && (input_lts.len() == 1 || !output_lts.contains(&RefLt::Named(def_id))) { + Some(( + def_id, + input_visitor + .lifetime_generic_arg_spans + .get(&def_id) + .or_else(|| output_visitor.lifetime_generic_arg_spans.get(&def_id)) + .copied(), + )) + } else { + None + } + }) + .collect::>(); - // only unnamed and static, ok - let unnamed_and_static = input_lts.iter().all(|lt| *lt == RefLt::Unnamed || *lt == RefLt::Static); - if unnamed_and_static { - return false; - } - // we have no output reference, so we only need all distinct lifetimes - input_lts.len() == unique_lifetimes(&input_lts) + if elidable_lts.is_empty() { + None } else { - // we have output references, so we need one input reference, - // and all output lifetimes must be the same - if unique_lifetimes(&output_lts) > 1 { - return false; - } - if input_lts.len() == 1 { - match (&input_lts[0], &output_lts[0]) { - (&RefLt::Named(n1), &RefLt::Named(n2)) if n1 == n2 => true, - (&RefLt::Named(_), &RefLt::Unnamed) => true, - _ => false, /* already elided, different named lifetimes - * or something static going on */ - } - } else { - false - } + Some(elidable_lts) } } @@ -359,16 +372,31 @@ fn allowed_lts_from(tcx: TyCtxt<'_>, named_generics: &[GenericParam<'_>]) -> FxH allowed_lts } -/// Number of unique lifetimes in the given vector. +/// Number of times each named lifetime occurs in the given slice. Returns a vector to preserve +/// relative order. #[must_use] -fn unique_lifetimes(lts: &[RefLt]) -> usize { - lts.iter().collect::>().len() +fn named_lifetime_occurrences(lts: &[RefLt]) -> Vec<(LocalDefId, usize)> { + let mut occurrences = Vec::new(); + for lt in lts { + if let &RefLt::Named(curr_def_id) = lt { + if let Some(pair) = occurrences + .iter_mut() + .find(|(prev_def_id, _)| *prev_def_id == curr_def_id) + { + pair.1 += 1; + } else { + occurrences.push((curr_def_id, 1)); + } + } + } + occurrences } /// A visitor usable for `rustc_front::visit::walk_ty()`. struct RefVisitor<'a, 'tcx> { cx: &'a LateContext<'tcx>, lts: Vec, + lifetime_generic_arg_spans: FxHashMap, nested_elision_site_lts: Vec, unelided_trait_object_lifetime: bool, } @@ -378,6 +406,7 @@ impl<'a, 'tcx> RefVisitor<'a, 'tcx> { Self { cx, lts: Vec::new(), + lifetime_generic_arg_spans: FxHashMap::default(), nested_elision_site_lts: Vec::new(), unelided_trait_object_lifetime: false, } @@ -467,6 +496,22 @@ impl<'a, 'tcx> Visitor<'tcx> for RefVisitor<'a, 'tcx> { _ => walk_ty(self, ty), } } + + fn visit_generic_arg(&mut self, generic_arg: &'tcx GenericArg<'tcx>) { + if let GenericArg::Lifetime(l) = generic_arg + && let LifetimeName::Param(def_id, _) = l.name + { + self.lifetime_generic_arg_spans.entry(def_id).or_insert(l.span); + } + // Replace with `walk_generic_arg` if/when https://github.com/rust-lang/rust/pull/103692 lands. + // walk_generic_arg(self, generic_arg); + match generic_arg { + GenericArg::Lifetime(lt) => self.visit_lifetime(lt), + GenericArg::Type(ty) => self.visit_ty(ty), + GenericArg::Const(ct) => self.visit_anon_const(&ct.value), + GenericArg::Infer(inf) => self.visit_infer(inf), + } + } } /// Are any lifetimes mentioned in the `where` clause? If so, we don't try to diff --git a/clippy_lints/src/loops/mod.rs b/clippy_lints/src/loops/mod.rs index bcf278d9c8339..8e52cac4323c3 100644 --- a/clippy_lints/src/loops/mod.rs +++ b/clippy_lints/src/loops/mod.rs @@ -9,7 +9,6 @@ mod manual_flatten; mod manual_memcpy; mod missing_spin_loop; mod mut_range_bound; -mod needless_collect; mod needless_range_loop; mod never_loop; mod same_item_push; @@ -205,28 +204,6 @@ declare_clippy_lint! { "`loop { if let { ... } else break }`, which can be written as a `while let` loop" } -declare_clippy_lint! { - /// ### What it does - /// Checks for functions collecting an iterator when collect - /// is not needed. - /// - /// ### Why is this bad? - /// `collect` causes the allocation of a new data structure, - /// when this allocation may not be needed. - /// - /// ### Example - /// ```rust - /// # let iterator = vec![1].into_iter(); - /// let len = iterator.clone().collect::>().len(); - /// // should be - /// let len = iterator.count(); - /// ``` - #[clippy::version = "1.30.0"] - pub NEEDLESS_COLLECT, - perf, - "collecting an iterator when collect is not needed" -} - declare_clippy_lint! { /// ### What it does /// Checks `for` loops over slices with an explicit counter @@ -605,7 +582,6 @@ declare_lint_pass!(Loops => [ EXPLICIT_INTO_ITER_LOOP, ITER_NEXT_LOOP, WHILE_LET_LOOP, - NEEDLESS_COLLECT, EXPLICIT_COUNTER_LOOP, EMPTY_LOOP, WHILE_LET_ON_ITERATOR, @@ -667,8 +643,6 @@ impl<'tcx> LateLintPass<'tcx> for Loops { while_immutable_condition::check(cx, condition, body); missing_spin_loop::check(cx, condition, body); } - - needless_collect::check(expr, cx); } } diff --git a/clippy_lints/src/loops/mut_range_bound.rs b/clippy_lints/src/loops/mut_range_bound.rs index 91b321c447479..4dae93f6028d4 100644 --- a/clippy_lints/src/loops/mut_range_bound.rs +++ b/clippy_lints/src/loops/mut_range_bound.rs @@ -52,8 +52,8 @@ fn check_for_mutability(cx: &LateContext<'_>, bound: &Expr<'_>) -> Option None } -fn check_for_mutation<'tcx>( - cx: &LateContext<'tcx>, +fn check_for_mutation( + cx: &LateContext<'_>, body: &Expr<'_>, bound_id_start: Option, bound_id_end: Option, @@ -113,13 +113,7 @@ impl<'tcx> Delegate<'tcx> for MutatePairDelegate<'_, 'tcx> { } } - fn fake_read( - &mut self, - _: &rustc_hir_typeck::expr_use_visitor::PlaceWithHirId<'tcx>, - _: FakeReadCause, - _: HirId, - ) { - } + fn fake_read(&mut self, _: &rustc_hir_typeck::expr_use_visitor::PlaceWithHirId<'tcx>, _: FakeReadCause, _: HirId) {} } impl MutatePairDelegate<'_, '_> { diff --git a/clippy_lints/src/loops/never_loop.rs b/clippy_lints/src/loops/never_loop.rs index 16b00ad663787..14f161f510265 100644 --- a/clippy_lints/src/loops/never_loop.rs +++ b/clippy_lints/src/loops/never_loop.rs @@ -4,7 +4,7 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::higher::ForLoop; use clippy_utils::source::snippet; use rustc_errors::Applicability; -use rustc_hir::{Block, Expr, ExprKind, HirId, InlineAsmOperand, Pat, Stmt, StmtKind}; +use rustc_hir::{Block, Destination, Expr, ExprKind, HirId, InlineAsmOperand, Pat, Stmt, StmtKind}; use rustc_lint::LateContext; use rustc_span::Span; use std::iter::{once, Iterator}; @@ -16,7 +16,7 @@ pub(super) fn check( span: Span, for_loop: Option<&ForLoop<'_>>, ) { - match never_loop_block(block, loop_id) { + match never_loop_block(block, &mut Vec::new(), loop_id) { NeverLoopResult::AlwaysBreak => { span_lint_and_then(cx, NEVER_LOOP, span, "this loop never actually loops", |diag| { if let Some(ForLoop { @@ -92,35 +92,34 @@ fn combine_branches(b1: NeverLoopResult, b2: NeverLoopResult) -> NeverLoopResult } } -fn never_loop_block(block: &Block<'_>, main_loop_id: HirId) -> NeverLoopResult { - let mut iter = block +fn never_loop_block(block: &Block<'_>, ignore_ids: &mut Vec, main_loop_id: HirId) -> NeverLoopResult { + let iter = block .stmts .iter() .filter_map(stmt_to_expr) .chain(block.expr.map(|expr| (expr, None))); - never_loop_expr_seq(&mut iter, main_loop_id) -} -fn never_loop_expr_seq<'a, T: Iterator, Option<&'a Block<'a>>)>>( - es: &mut T, - main_loop_id: HirId, -) -> NeverLoopResult { - es.map(|(e, els)| { - let e = never_loop_expr(e, main_loop_id); - els.map_or(e, |els| combine_branches(e, never_loop_block(els, main_loop_id))) + iter.map(|(e, els)| { + let e = never_loop_expr(e, ignore_ids, main_loop_id); + // els is an else block in a let...else binding + els.map_or(e, |els| { + combine_branches(e, never_loop_block(els, ignore_ids, main_loop_id)) + }) }) .fold(NeverLoopResult::Otherwise, combine_seq) } fn stmt_to_expr<'tcx>(stmt: &Stmt<'tcx>) -> Option<(&'tcx Expr<'tcx>, Option<&'tcx Block<'tcx>>)> { match stmt.kind { - StmtKind::Semi(e, ..) | StmtKind::Expr(e, ..) => Some((e, None)), + StmtKind::Semi(e) | StmtKind::Expr(e) => Some((e, None)), + // add the let...else expression (if present) StmtKind::Local(local) => local.init.map(|init| (init, local.els)), StmtKind::Item(..) => None, } } -fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult { +#[allow(clippy::too_many_lines)] +fn never_loop_expr(expr: &Expr<'_>, ignore_ids: &mut Vec, main_loop_id: HirId) -> NeverLoopResult { match expr.kind { ExprKind::Box(e) | ExprKind::Unary(_, e) @@ -129,47 +128,56 @@ fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult { | ExprKind::Field(e, _) | ExprKind::AddrOf(_, _, e) | ExprKind::Repeat(e, _) - | ExprKind::DropTemps(e) => never_loop_expr(e, main_loop_id), - ExprKind::Let(let_expr) => never_loop_expr(let_expr.init, main_loop_id), - ExprKind::Array(es) | ExprKind::Tup(es) => never_loop_expr_all(&mut es.iter(), main_loop_id), - ExprKind::MethodCall(_, receiver, es, _) => { - never_loop_expr_all(&mut std::iter::once(receiver).chain(es.iter()), main_loop_id) - }, + | ExprKind::DropTemps(e) => never_loop_expr(e, ignore_ids, main_loop_id), + ExprKind::Let(let_expr) => never_loop_expr(let_expr.init, ignore_ids, main_loop_id), + ExprKind::Array(es) | ExprKind::Tup(es) => never_loop_expr_all(&mut es.iter(), ignore_ids, main_loop_id), + ExprKind::MethodCall(_, receiver, es, _) => never_loop_expr_all( + &mut std::iter::once(receiver).chain(es.iter()), + ignore_ids, + main_loop_id, + ), ExprKind::Struct(_, fields, base) => { - let fields = never_loop_expr_all(&mut fields.iter().map(|f| f.expr), main_loop_id); + let fields = never_loop_expr_all(&mut fields.iter().map(|f| f.expr), ignore_ids, main_loop_id); if let Some(base) = base { - combine_both(fields, never_loop_expr(base, main_loop_id)) + combine_both(fields, never_loop_expr(base, ignore_ids, main_loop_id)) } else { fields } }, - ExprKind::Call(e, es) => never_loop_expr_all(&mut once(e).chain(es.iter()), main_loop_id), + ExprKind::Call(e, es) => never_loop_expr_all(&mut once(e).chain(es.iter()), ignore_ids, main_loop_id), ExprKind::Binary(_, e1, e2) | ExprKind::Assign(e1, e2, _) | ExprKind::AssignOp(_, e1, e2) - | ExprKind::Index(e1, e2) => never_loop_expr_all(&mut [e1, e2].iter().copied(), main_loop_id), + | ExprKind::Index(e1, e2) => never_loop_expr_all(&mut [e1, e2].iter().copied(), ignore_ids, main_loop_id), ExprKind::Loop(b, _, _, _) => { // Break can come from the inner loop so remove them. - absorb_break(never_loop_block(b, main_loop_id)) + absorb_break(never_loop_block(b, ignore_ids, main_loop_id)) }, ExprKind::If(e, e2, e3) => { - let e1 = never_loop_expr(e, main_loop_id); - let e2 = never_loop_expr(e2, main_loop_id); - let e3 = e3 - .as_ref() - .map_or(NeverLoopResult::Otherwise, |e| never_loop_expr(e, main_loop_id)); + let e1 = never_loop_expr(e, ignore_ids, main_loop_id); + let e2 = never_loop_expr(e2, ignore_ids, main_loop_id); + let e3 = e3.as_ref().map_or(NeverLoopResult::Otherwise, |e| { + never_loop_expr(e, ignore_ids, main_loop_id) + }); combine_seq(e1, combine_branches(e2, e3)) }, ExprKind::Match(e, arms, _) => { - let e = never_loop_expr(e, main_loop_id); + let e = never_loop_expr(e, ignore_ids, main_loop_id); if arms.is_empty() { e } else { - let arms = never_loop_expr_branch(&mut arms.iter().map(|a| a.body), main_loop_id); + let arms = never_loop_expr_branch(&mut arms.iter().map(|a| a.body), ignore_ids, main_loop_id); combine_seq(e, arms) } }, - ExprKind::Block(b, _) => never_loop_block(b, main_loop_id), + ExprKind::Block(b, l) => { + if l.is_some() { + ignore_ids.push(b.hir_id); + } + let ret = never_loop_block(b, ignore_ids, main_loop_id); + ignore_ids.pop(); + ret + }, ExprKind::Continue(d) => { let id = d .target_id @@ -180,20 +188,32 @@ fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult { NeverLoopResult::AlwaysBreak } }, + // checks if break targets a block instead of a loop + ExprKind::Break(Destination { target_id: Ok(t), .. }, e) if ignore_ids.contains(&t) => e + .map_or(NeverLoopResult::Otherwise, |e| { + combine_seq(never_loop_expr(e, ignore_ids, main_loop_id), NeverLoopResult::Otherwise) + }), ExprKind::Break(_, e) | ExprKind::Ret(e) => e.as_ref().map_or(NeverLoopResult::AlwaysBreak, |e| { - combine_seq(never_loop_expr(e, main_loop_id), NeverLoopResult::AlwaysBreak) + combine_seq( + never_loop_expr(e, ignore_ids, main_loop_id), + NeverLoopResult::AlwaysBreak, + ) }), ExprKind::InlineAsm(asm) => asm .operands .iter() .map(|(o, _)| match o { InlineAsmOperand::In { expr, .. } | InlineAsmOperand::InOut { expr, .. } => { - never_loop_expr(expr, main_loop_id) + never_loop_expr(expr, ignore_ids, main_loop_id) }, - InlineAsmOperand::Out { expr, .. } => never_loop_expr_all(&mut expr.iter().copied(), main_loop_id), - InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => { - never_loop_expr_all(&mut once(*in_expr).chain(out_expr.iter().copied()), main_loop_id) + InlineAsmOperand::Out { expr, .. } => { + never_loop_expr_all(&mut expr.iter().copied(), ignore_ids, main_loop_id) }, + InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => never_loop_expr_all( + &mut once(*in_expr).chain(out_expr.iter().copied()), + ignore_ids, + main_loop_id, + ), InlineAsmOperand::Const { .. } | InlineAsmOperand::SymFn { .. } | InlineAsmOperand::SymStatic { .. } => NeverLoopResult::Otherwise, @@ -208,13 +228,21 @@ fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult { } } -fn never_loop_expr_all<'a, T: Iterator>>(es: &mut T, main_loop_id: HirId) -> NeverLoopResult { - es.map(|e| never_loop_expr(e, main_loop_id)) +fn never_loop_expr_all<'a, T: Iterator>>( + es: &mut T, + ignore_ids: &mut Vec, + main_loop_id: HirId, +) -> NeverLoopResult { + es.map(|e| never_loop_expr(e, ignore_ids, main_loop_id)) .fold(NeverLoopResult::Otherwise, combine_both) } -fn never_loop_expr_branch<'a, T: Iterator>>(e: &mut T, main_loop_id: HirId) -> NeverLoopResult { - e.map(|e| never_loop_expr(e, main_loop_id)) +fn never_loop_expr_branch<'a, T: Iterator>>( + e: &mut T, + ignore_ids: &mut Vec, + main_loop_id: HirId, +) -> NeverLoopResult { + e.map(|e| never_loop_expr(e, ignore_ids, main_loop_id)) .fold(NeverLoopResult::AlwaysBreak, combine_branches) } diff --git a/clippy_lints/src/manual_instant_elapsed.rs b/clippy_lints/src/manual_instant_elapsed.rs deleted file mode 100644 index 331cda1db8990..0000000000000 --- a/clippy_lints/src/manual_instant_elapsed.rs +++ /dev/null @@ -1,69 +0,0 @@ -use clippy_utils::diagnostics::span_lint_and_sugg; -use rustc_errors::Applicability; -use rustc_hir::{BinOpKind, Expr, ExprKind}; -use rustc_lint::{LateContext, LateLintPass}; -use rustc_session::{declare_lint_pass, declare_tool_lint}; -use rustc_span::source_map::Spanned; - -declare_clippy_lint! { - /// ### What it does - /// Lints subtraction between `Instant::now()` and another `Instant`. - /// - /// ### Why is this bad? - /// It is easy to accidentally write `prev_instant - Instant::now()`, which will always be 0ns - /// as `Instant` subtraction saturates. - /// - /// `prev_instant.elapsed()` also more clearly signals intention. - /// - /// ### Example - /// ```rust - /// use std::time::Instant; - /// let prev_instant = Instant::now(); - /// let duration = Instant::now() - prev_instant; - /// ``` - /// Use instead: - /// ```rust - /// use std::time::Instant; - /// let prev_instant = Instant::now(); - /// let duration = prev_instant.elapsed(); - /// ``` - #[clippy::version = "1.64.0"] - pub MANUAL_INSTANT_ELAPSED, - pedantic, - "subtraction between `Instant::now()` and previous `Instant`" -} - -declare_lint_pass!(ManualInstantElapsed => [MANUAL_INSTANT_ELAPSED]); - -impl LateLintPass<'_> for ManualInstantElapsed { - fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) { - if let ExprKind::Binary(Spanned {node: BinOpKind::Sub, ..}, lhs, rhs) = expr.kind - && check_instant_now_call(cx, lhs) - && let ty_resolved = cx.typeck_results().expr_ty(rhs) - && let rustc_middle::ty::Adt(def, _) = ty_resolved.kind() - && clippy_utils::match_def_path(cx, def.did(), &clippy_utils::paths::INSTANT) - && let Some(sugg) = clippy_utils::sugg::Sugg::hir_opt(cx, rhs) - { - span_lint_and_sugg( - cx, - MANUAL_INSTANT_ELAPSED, - expr.span, - "manual implementation of `Instant::elapsed`", - "try", - format!("{}.elapsed()", sugg.maybe_par()), - Applicability::MachineApplicable, - ); - } - } -} - -fn check_instant_now_call(cx: &LateContext<'_>, expr_block: &'_ Expr<'_>) -> bool { - if let ExprKind::Call(fn_expr, []) = expr_block.kind - && let Some(fn_id) = clippy_utils::path_def_id(cx, fn_expr) - && clippy_utils::match_def_path(cx, fn_id, &clippy_utils::paths::INSTANT_NOW) - { - true - } else { - false - } -} diff --git a/clippy_lints/src/manual_is_ascii_check.rs b/clippy_lints/src/manual_is_ascii_check.rs new file mode 100644 index 0000000000000..bb8c142f8e468 --- /dev/null +++ b/clippy_lints/src/manual_is_ascii_check.rs @@ -0,0 +1,158 @@ +use rustc_ast::LitKind::{Byte, Char}; +use rustc_errors::Applicability; +use rustc_hir::{Expr, ExprKind, PatKind, RangeEnd}; +use rustc_lint::{LateContext, LateLintPass}; +use rustc_semver::RustcVersion; +use rustc_session::{declare_tool_lint, impl_lint_pass}; +use rustc_span::{def_id::DefId, sym}; + +use clippy_utils::{ + diagnostics::span_lint_and_sugg, in_constant, macros::root_macro_call, meets_msrv, msrvs, source::snippet, +}; + +declare_clippy_lint! { + /// ### What it does + /// Suggests to use dedicated built-in methods, + /// `is_ascii_(lowercase|uppercase|digit)` for checking on corresponding ascii range + /// + /// ### Why is this bad? + /// Using the built-in functions is more readable and makes it + /// clear that it's not a specific subset of characters, but all + /// ASCII (lowercase|uppercase|digit) characters. + /// ### Example + /// ```rust + /// fn main() { + /// assert!(matches!('x', 'a'..='z')); + /// assert!(matches!(b'X', b'A'..=b'Z')); + /// assert!(matches!('2', '0'..='9')); + /// assert!(matches!('x', 'A'..='Z' | 'a'..='z')); + /// } + /// ``` + /// Use instead: + /// ```rust + /// fn main() { + /// assert!('x'.is_ascii_lowercase()); + /// assert!(b'X'.is_ascii_uppercase()); + /// assert!('2'.is_ascii_digit()); + /// assert!('x'.is_ascii_alphabetic()); + /// } + /// ``` + #[clippy::version = "1.66.0"] + pub MANUAL_IS_ASCII_CHECK, + style, + "use dedicated method to check ascii range" +} +impl_lint_pass!(ManualIsAsciiCheck => [MANUAL_IS_ASCII_CHECK]); + +pub struct ManualIsAsciiCheck { + msrv: Option, +} + +impl ManualIsAsciiCheck { + #[must_use] + pub fn new(msrv: Option) -> Self { + Self { msrv } + } +} + +#[derive(Debug, PartialEq)] +enum CharRange { + /// 'a'..='z' | b'a'..=b'z' + LowerChar, + /// 'A'..='Z' | b'A'..=b'Z' + UpperChar, + /// AsciiLower | AsciiUpper + FullChar, + /// '0..=9' + Digit, + Otherwise, +} + +impl<'tcx> LateLintPass<'tcx> for ManualIsAsciiCheck { + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { + if !meets_msrv(self.msrv, msrvs::IS_ASCII_DIGIT) { + return; + } + + if in_constant(cx, expr.hir_id) && !meets_msrv(self.msrv, msrvs::IS_ASCII_DIGIT_CONST) { + return; + } + + let Some(macro_call) = root_macro_call(expr.span) else { return }; + + if is_matches_macro(cx, macro_call.def_id) { + if let ExprKind::Match(recv, [arm, ..], _) = expr.kind { + let range = check_pat(&arm.pat.kind); + + if let Some(sugg) = match range { + CharRange::UpperChar => Some("is_ascii_uppercase"), + CharRange::LowerChar => Some("is_ascii_lowercase"), + CharRange::FullChar => Some("is_ascii_alphabetic"), + CharRange::Digit => Some("is_ascii_digit"), + CharRange::Otherwise => None, + } { + let default_snip = ".."; + // `snippet_with_applicability` may set applicability to `MaybeIncorrect` for + // macro span, so we check applicability manually by comparing `recv` is not default. + let recv = snippet(cx, recv.span, default_snip); + + let applicability = if recv == default_snip { + Applicability::HasPlaceholders + } else { + Applicability::MachineApplicable + }; + + span_lint_and_sugg( + cx, + MANUAL_IS_ASCII_CHECK, + macro_call.span, + "manual check for common ascii range", + "try", + format!("{recv}.{sugg}()"), + applicability, + ); + } + } + } + } + + extract_msrv_attr!(LateContext); +} + +fn check_pat(pat_kind: &PatKind<'_>) -> CharRange { + match pat_kind { + PatKind::Or(pats) => { + let ranges = pats.iter().map(|p| check_pat(&p.kind)).collect::>(); + + if ranges.len() == 2 && ranges.contains(&CharRange::UpperChar) && ranges.contains(&CharRange::LowerChar) { + CharRange::FullChar + } else { + CharRange::Otherwise + } + }, + PatKind::Range(Some(start), Some(end), kind) if *kind == RangeEnd::Included => check_range(start, end), + _ => CharRange::Otherwise, + } +} + +fn check_range(start: &Expr<'_>, end: &Expr<'_>) -> CharRange { + if let ExprKind::Lit(start_lit) = &start.kind + && let ExprKind::Lit(end_lit) = &end.kind { + match (&start_lit.node, &end_lit.node) { + (Char('a'), Char('z')) | (Byte(b'a'), Byte(b'z')) => CharRange::LowerChar, + (Char('A'), Char('Z')) | (Byte(b'A'), Byte(b'Z')) => CharRange::UpperChar, + (Char('0'), Char('9')) | (Byte(b'0'), Byte(b'9')) => CharRange::Digit, + _ => CharRange::Otherwise, + } + } else { + CharRange::Otherwise + } +} + +fn is_matches_macro(cx: &LateContext<'_>, macro_def_id: DefId) -> bool { + if let Some(name) = cx.tcx.get_diagnostic_name(macro_def_id) { + return sym::matches_macro == name; + } + + false +} diff --git a/clippy_lints/src/manual_let_else.rs b/clippy_lints/src/manual_let_else.rs new file mode 100644 index 0000000000000..1846596fa4c8e --- /dev/null +++ b/clippy_lints/src/manual_let_else.rs @@ -0,0 +1,297 @@ +use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::higher::IfLetOrMatch; +use clippy_utils::source::snippet_opt; +use clippy_utils::ty::is_type_diagnostic_item; +use clippy_utils::visitors::{for_each_expr, Descend}; +use clippy_utils::{meets_msrv, msrvs, peel_blocks}; +use if_chain::if_chain; +use rustc_data_structures::fx::FxHashSet; +use rustc_errors::Applicability; +use rustc_hir::{Expr, ExprKind, MatchSource, Pat, PatKind, QPath, Stmt, StmtKind}; +use rustc_lint::{LateContext, LateLintPass, LintContext}; +use rustc_middle::lint::in_external_macro; +use rustc_semver::RustcVersion; +use rustc_session::{declare_tool_lint, impl_lint_pass}; +use rustc_span::symbol::sym; +use rustc_span::Span; +use serde::Deserialize; +use std::ops::ControlFlow; + +declare_clippy_lint! { + /// ### What it does + /// + /// Warn of cases where `let...else` could be used + /// + /// ### Why is this bad? + /// + /// `let...else` provides a standard construct for this pattern + /// that people can easily recognize. It's also more compact. + /// + /// ### Example + /// + /// ```rust + /// # let w = Some(0); + /// let v = if let Some(v) = w { v } else { return }; + /// ``` + /// + /// Could be written: + /// + /// ```rust + /// # #![feature(let_else)] + /// # fn main () { + /// # let w = Some(0); + /// let Some(v) = w else { return }; + /// # } + /// ``` + #[clippy::version = "1.67.0"] + pub MANUAL_LET_ELSE, + pedantic, + "manual implementation of a let...else statement" +} + +pub struct ManualLetElse { + msrv: Option, + matches_behaviour: MatchLintBehaviour, +} + +impl ManualLetElse { + #[must_use] + pub fn new(msrv: Option, matches_behaviour: MatchLintBehaviour) -> Self { + Self { + msrv, + matches_behaviour, + } + } +} + +impl_lint_pass!(ManualLetElse => [MANUAL_LET_ELSE]); + +impl<'tcx> LateLintPass<'tcx> for ManualLetElse { + fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &'tcx Stmt<'tcx>) { + let if_let_or_match = if_chain! { + if meets_msrv(self.msrv, msrvs::LET_ELSE); + if !in_external_macro(cx.sess(), stmt.span); + if let StmtKind::Local(local) = stmt.kind; + if let Some(init) = local.init; + if local.els.is_none(); + if local.ty.is_none(); + if init.span.ctxt() == stmt.span.ctxt(); + if let Some(if_let_or_match) = IfLetOrMatch::parse(cx, init); + then { + if_let_or_match + } else { + return; + } + }; + + match if_let_or_match { + IfLetOrMatch::IfLet(if_let_expr, let_pat, if_then, if_else) => if_chain! { + if expr_is_simple_identity(let_pat, if_then); + if let Some(if_else) = if_else; + if expr_diverges(cx, if_else); + then { + emit_manual_let_else(cx, stmt.span, if_let_expr, let_pat, if_else); + } + }, + IfLetOrMatch::Match(match_expr, arms, source) => { + if self.matches_behaviour == MatchLintBehaviour::Never { + return; + } + if source != MatchSource::Normal { + return; + } + // Any other number than two arms doesn't (neccessarily) + // have a trivial mapping to let else. + if arms.len() != 2 { + return; + } + // Guards don't give us an easy mapping either + if arms.iter().any(|arm| arm.guard.is_some()) { + return; + } + let check_types = self.matches_behaviour == MatchLintBehaviour::WellKnownTypes; + let diverging_arm_opt = arms + .iter() + .enumerate() + .find(|(_, arm)| expr_diverges(cx, arm.body) && pat_allowed_for_else(cx, arm.pat, check_types)); + let Some((idx, diverging_arm)) = diverging_arm_opt else { return; }; + let pat_arm = &arms[1 - idx]; + if !expr_is_simple_identity(pat_arm.pat, pat_arm.body) { + return; + } + + emit_manual_let_else(cx, stmt.span, match_expr, pat_arm.pat, diverging_arm.body); + }, + } + } + + extract_msrv_attr!(LateContext); +} + +fn emit_manual_let_else(cx: &LateContext<'_>, span: Span, expr: &Expr<'_>, pat: &Pat<'_>, else_body: &Expr<'_>) { + span_lint_and_then( + cx, + MANUAL_LET_ELSE, + span, + "this could be rewritten as `let...else`", + |diag| { + // This is far from perfect, for example there needs to be: + // * mut additions for the bindings + // * renamings of the bindings + // * unused binding collision detection with existing ones + // * putting patterns with at the top level | inside () + // for this to be machine applicable. + let app = Applicability::HasPlaceholders; + + if let Some(sn_pat) = snippet_opt(cx, pat.span) && + let Some(sn_expr) = snippet_opt(cx, expr.span) && + let Some(sn_else) = snippet_opt(cx, else_body.span) + { + let else_bl = if matches!(else_body.kind, ExprKind::Block(..)) { + sn_else + } else { + format!("{{ {sn_else} }}") + }; + let sugg = format!("let {sn_pat} = {sn_expr} else {else_bl};"); + diag.span_suggestion(span, "consider writing", sugg, app); + } + }, + ); +} + +fn expr_diverges(cx: &LateContext<'_>, expr: &'_ Expr<'_>) -> bool { + fn is_never(cx: &LateContext<'_>, expr: &'_ Expr<'_>) -> bool { + if let Some(ty) = cx.typeck_results().expr_ty_opt(expr) { + return ty.is_never(); + } + false + } + // We can't just call is_never on expr and be done, because the type system + // sometimes coerces the ! type to something different before we can get + // our hands on it. So instead, we do a manual search. We do fall back to + // is_never in some places when there is no better alternative. + for_each_expr(expr, |ex| { + match ex.kind { + ExprKind::Continue(_) | ExprKind::Break(_, _) | ExprKind::Ret(_) => ControlFlow::Break(()), + ExprKind::Call(call, _) => { + if is_never(cx, ex) || is_never(cx, call) { + return ControlFlow::Break(()); + } + ControlFlow::Continue(Descend::Yes) + }, + ExprKind::MethodCall(..) => { + if is_never(cx, ex) { + return ControlFlow::Break(()); + } + ControlFlow::Continue(Descend::Yes) + }, + ExprKind::If(if_expr, if_then, if_else) => { + let else_diverges = if_else.map_or(false, |ex| expr_diverges(cx, ex)); + let diverges = expr_diverges(cx, if_expr) || (else_diverges && expr_diverges(cx, if_then)); + if diverges { + return ControlFlow::Break(()); + } + ControlFlow::Continue(Descend::No) + }, + ExprKind::Match(match_expr, match_arms, _) => { + let diverges = expr_diverges(cx, match_expr) + || match_arms.iter().all(|arm| { + let guard_diverges = arm.guard.as_ref().map_or(false, |g| expr_diverges(cx, g.body())); + guard_diverges || expr_diverges(cx, arm.body) + }); + if diverges { + return ControlFlow::Break(()); + } + ControlFlow::Continue(Descend::No) + }, + + // Don't continue into loops or labeled blocks, as they are breakable, + // and we'd have to start checking labels. + ExprKind::Block(_, Some(_)) | ExprKind::Loop(..) => ControlFlow::Continue(Descend::No), + + // Default: descend + _ => ControlFlow::Continue(Descend::Yes), + } + }) + .is_some() +} + +fn pat_allowed_for_else(cx: &LateContext<'_>, pat: &'_ Pat<'_>, check_types: bool) -> bool { + // Check whether the pattern contains any bindings, as the + // binding might potentially be used in the body. + // TODO: only look for *used* bindings. + let mut has_bindings = false; + pat.each_binding_or_first(&mut |_, _, _, _| has_bindings = true); + if has_bindings { + return false; + } + + // If we shouldn't check the types, exit early. + if !check_types { + return true; + } + + // Check whether any possibly "unknown" patterns are included, + // because users might not know which values some enum has. + // Well-known enums are excepted, as we assume people know them. + // We do a deep check, to be able to disallow Err(En::Foo(_)) + // for usage of the En::Foo variant, as we disallow En::Foo(_), + // but we allow Err(_). + let typeck_results = cx.typeck_results(); + let mut has_disallowed = false; + pat.walk_always(|pat| { + // Only do the check if the type is "spelled out" in the pattern + if !matches!( + pat.kind, + PatKind::Struct(..) | PatKind::TupleStruct(..) | PatKind::Path(..) + ) { + return; + }; + let ty = typeck_results.pat_ty(pat); + // Option and Result are allowed, everything else isn't. + if !(is_type_diagnostic_item(cx, ty, sym::Option) || is_type_diagnostic_item(cx, ty, sym::Result)) { + has_disallowed = true; + } + }); + !has_disallowed +} + +/// Checks if the passed block is a simple identity referring to bindings created by the pattern +fn expr_is_simple_identity(pat: &'_ Pat<'_>, expr: &'_ Expr<'_>) -> bool { + // We support patterns with multiple bindings and tuples, like: + // let ... = if let (Some(foo), bar) = g() { (foo, bar) } else { ... } + let peeled = peel_blocks(expr); + let paths = match peeled.kind { + ExprKind::Tup(exprs) | ExprKind::Array(exprs) => exprs, + ExprKind::Path(_) => std::slice::from_ref(peeled), + _ => return false, + }; + let mut pat_bindings = FxHashSet::default(); + pat.each_binding_or_first(&mut |_ann, _hir_id, _sp, ident| { + pat_bindings.insert(ident); + }); + if pat_bindings.len() < paths.len() { + return false; + } + for path in paths { + if_chain! { + if let ExprKind::Path(QPath::Resolved(_ty, path)) = path.kind; + if let [path_seg] = path.segments; + then { + if !pat_bindings.remove(&path_seg.ident) { + return false; + } + } else { + return false; + } + } + } + true +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Deserialize)] +pub enum MatchLintBehaviour { + AllTypes, + WellKnownTypes, + Never, +} diff --git a/clippy_lints/src/map_unit_fn.rs b/clippy_lints/src/map_unit_fn.rs index 32da37a862d8c..59195d1ae4e0a 100644 --- a/clippy_lints/src/map_unit_fn.rs +++ b/clippy_lints/src/map_unit_fn.rs @@ -119,7 +119,7 @@ fn is_unit_expression(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool { /// semicolons, which causes problems when generating a suggestion. Given an /// expression that evaluates to '()' or '!', recursively remove useless braces /// and semi-colons until is suitable for including in the suggestion template -fn reduce_unit_expression<'a>(cx: &LateContext<'_>, expr: &'a hir::Expr<'_>) -> Option { +fn reduce_unit_expression(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option { if !is_unit_expression(cx, expr) { return None; } diff --git a/clippy_lints/src/matches/infallible_destructuring_match.rs b/clippy_lints/src/matches/infallible_destructuring_match.rs index 2472acb6f6e8b..d18c92caba2a6 100644 --- a/clippy_lints/src/matches/infallible_destructuring_match.rs +++ b/clippy_lints/src/matches/infallible_destructuring_match.rs @@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::source::snippet_with_applicability; use clippy_utils::{path_to_local_id, peel_blocks, strip_pat_refs}; use rustc_errors::Applicability; -use rustc_hir::{ExprKind, Local, MatchSource, PatKind, QPath}; +use rustc_hir::{ByRef, ExprKind, Local, MatchSource, PatKind, QPath}; use rustc_lint::LateContext; use super::INFALLIBLE_DESTRUCTURING_MATCH; @@ -16,7 +16,7 @@ pub(crate) fn check(cx: &LateContext<'_>, local: &Local<'_>) -> bool { if let PatKind::TupleStruct( QPath::Resolved(None, variant_name), args, _) = arms[0].pat.kind; if args.len() == 1; - if let PatKind::Binding(_, arg, ..) = strip_pat_refs(&args[0]).kind; + if let PatKind::Binding(binding, arg, ..) = strip_pat_refs(&args[0]).kind; let body = peel_blocks(arms[0].body); if path_to_local_id(body, arg); @@ -30,8 +30,9 @@ pub(crate) fn check(cx: &LateContext<'_>, local: &Local<'_>) -> bool { Consider using `let`", "try this", format!( - "let {}({}) = {};", + "let {}({}{}) = {};", snippet_with_applicability(cx, variant_name.span, "..", &mut applicability), + if binding.0 == ByRef::Yes { "ref " } else { "" }, snippet_with_applicability(cx, local.pat.span, "..", &mut applicability), snippet_with_applicability(cx, target.span, "..", &mut applicability), ), diff --git a/clippy_lints/src/matches/manual_filter.rs b/clippy_lints/src/matches/manual_filter.rs index 66ba1f6f9c550..d521a529e0d64 100644 --- a/clippy_lints/src/matches/manual_filter.rs +++ b/clippy_lints/src/matches/manual_filter.rs @@ -62,7 +62,7 @@ fn peels_blocks_incl_unsafe<'a>(expr: &'a Expr<'a>) -> &'a Expr<'a> { // // } // Returns true if resolves to `Some(x)`, `false` otherwise -fn is_some_expr<'tcx>(cx: &LateContext<'_>, target: HirId, ctxt: SyntaxContext, expr: &'tcx Expr<'_>) -> bool { +fn is_some_expr(cx: &LateContext<'_>, target: HirId, ctxt: SyntaxContext, expr: &Expr<'_>) -> bool { if let Some(inner_expr) = peels_blocks_incl_unsafe_opt(expr) { // there can be not statements in the block as they would be removed when switching to `.filter` if let ExprKind::Call(callee, [arg]) = inner_expr.kind { diff --git a/clippy_lints/src/matches/significant_drop_in_scrutinee.rs b/clippy_lints/src/matches/significant_drop_in_scrutinee.rs index 85269e533a066..f587c69f7302d 100644 --- a/clippy_lints/src/matches/significant_drop_in_scrutinee.rs +++ b/clippy_lints/src/matches/significant_drop_in_scrutinee.rs @@ -83,8 +83,8 @@ fn set_diagnostic<'tcx>(diag: &mut Diagnostic, cx: &LateContext<'tcx>, expr: &'t /// If the expression is an `ExprKind::Match`, check if the scrutinee has a significant drop that /// may have a surprising lifetime. -fn has_significant_drop_in_scrutinee<'tcx, 'a>( - cx: &'a LateContext<'tcx>, +fn has_significant_drop_in_scrutinee<'tcx>( + cx: &LateContext<'tcx>, scrutinee: &'tcx Expr<'tcx>, source: MatchSource, ) -> Option<(Vec, &'static str)> { @@ -226,7 +226,7 @@ impl<'a, 'tcx> SigDropHelper<'a, 'tcx> { /// This will try to set the current suggestion (so it can be moved into the suggestions vec /// later). If `allow_move_and_clone` is false, the suggestion *won't* be set -- this gives us /// an opportunity to look for another type in the chain that will be trivially copyable. - /// However, if we are at the the end of the chain, we want to accept whatever is there. (The + /// However, if we are at the end of the chain, we want to accept whatever is there. (The /// suggestion won't actually be output, but the diagnostic message will be output, so the user /// can determine the best way to handle the lint.) fn try_setting_current_suggestion(&mut self, expr: &'tcx Expr<'_>, allow_move_and_clone: bool) { @@ -377,7 +377,7 @@ impl<'a, 'tcx> ArmSigDropHelper<'a, 'tcx> { } } -fn has_significant_drop_in_arms<'tcx, 'a>(cx: &'a LateContext<'tcx>, arms: &'tcx [Arm<'_>]) -> FxHashSet { +fn has_significant_drop_in_arms<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) -> FxHashSet { let mut helper = ArmSigDropHelper::new(cx); for arm in arms { helper.visit_expr(arm.body); diff --git a/clippy_lints/src/matches/single_match.rs b/clippy_lints/src/matches/single_match.rs index e5a15b2e1a1d2..19b49c44d5704 100644 --- a/clippy_lints/src/matches/single_match.rs +++ b/clippy_lints/src/matches/single_match.rs @@ -153,7 +153,7 @@ fn pat_in_candidate_enum<'a>(cx: &LateContext<'a>, ty: Ty<'a>, pat: &Pat<'_>) -> } /// Returns `true` if the given type is an enum we know won't be expanded in the future -fn in_candidate_enum<'a>(cx: &LateContext<'a>, ty: Ty<'_>) -> bool { +fn in_candidate_enum(cx: &LateContext<'_>, ty: Ty<'_>) -> bool { // list of candidate `Enum`s we know will never get any more members let candidates = [sym::Cow, sym::Option, sym::Result]; diff --git a/clippy_lints/src/methods/chars_cmp_with_unwrap.rs b/clippy_lints/src/methods/chars_cmp_with_unwrap.rs index 7e808760663a1..27a05337a290f 100644 --- a/clippy_lints/src/methods/chars_cmp_with_unwrap.rs +++ b/clippy_lints/src/methods/chars_cmp_with_unwrap.rs @@ -9,8 +9,8 @@ use rustc_lint::LateContext; use rustc_lint::Lint; /// Wrapper fn for `CHARS_NEXT_CMP` and `CHARS_LAST_CMP` lints with `unwrap()`. -pub(super) fn check<'tcx>( - cx: &LateContext<'tcx>, +pub(super) fn check( + cx: &LateContext<'_>, info: &crate::methods::BinaryExprInfo<'_>, chain_methods: &[&str], lint: &'static Lint, diff --git a/clippy_lints/src/methods/chars_last_cmp.rs b/clippy_lints/src/methods/chars_last_cmp.rs index 07bbc5ca1bf46..2efff4c3c5497 100644 --- a/clippy_lints/src/methods/chars_last_cmp.rs +++ b/clippy_lints/src/methods/chars_last_cmp.rs @@ -4,7 +4,7 @@ use rustc_lint::LateContext; use super::CHARS_LAST_CMP; /// Checks for the `CHARS_LAST_CMP` lint. -pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, info: &crate::methods::BinaryExprInfo<'_>) -> bool { +pub(super) fn check(cx: &LateContext<'_>, info: &crate::methods::BinaryExprInfo<'_>) -> bool { if chars_cmp::check(cx, info, &["chars", "last"], CHARS_LAST_CMP, "ends_with") { true } else { diff --git a/clippy_lints/src/methods/chars_last_cmp_with_unwrap.rs b/clippy_lints/src/methods/chars_last_cmp_with_unwrap.rs index c29ee0ec8c8ca..5b8713f7d7903 100644 --- a/clippy_lints/src/methods/chars_last_cmp_with_unwrap.rs +++ b/clippy_lints/src/methods/chars_last_cmp_with_unwrap.rs @@ -4,7 +4,7 @@ use rustc_lint::LateContext; use super::CHARS_LAST_CMP; /// Checks for the `CHARS_LAST_CMP` lint with `unwrap()`. -pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, info: &crate::methods::BinaryExprInfo<'_>) -> bool { +pub(super) fn check(cx: &LateContext<'_>, info: &crate::methods::BinaryExprInfo<'_>) -> bool { if chars_cmp_with_unwrap::check(cx, info, &["chars", "last", "unwrap"], CHARS_LAST_CMP, "ends_with") { true } else { diff --git a/clippy_lints/src/methods/chars_next_cmp.rs b/clippy_lints/src/methods/chars_next_cmp.rs index a6701d8830e77..b631fecab9729 100644 --- a/clippy_lints/src/methods/chars_next_cmp.rs +++ b/clippy_lints/src/methods/chars_next_cmp.rs @@ -3,6 +3,6 @@ use rustc_lint::LateContext; use super::CHARS_NEXT_CMP; /// Checks for the `CHARS_NEXT_CMP` lint. -pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, info: &crate::methods::BinaryExprInfo<'_>) -> bool { +pub(super) fn check(cx: &LateContext<'_>, info: &crate::methods::BinaryExprInfo<'_>) -> bool { crate::methods::chars_cmp::check(cx, info, &["chars", "next"], CHARS_NEXT_CMP, "starts_with") } diff --git a/clippy_lints/src/methods/chars_next_cmp_with_unwrap.rs b/clippy_lints/src/methods/chars_next_cmp_with_unwrap.rs index 28ede28e9358b..caf21d3ff3bcb 100644 --- a/clippy_lints/src/methods/chars_next_cmp_with_unwrap.rs +++ b/clippy_lints/src/methods/chars_next_cmp_with_unwrap.rs @@ -3,6 +3,6 @@ use rustc_lint::LateContext; use super::CHARS_NEXT_CMP; /// Checks for the `CHARS_NEXT_CMP` lint with `unwrap()`. -pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, info: &crate::methods::BinaryExprInfo<'_>) -> bool { +pub(super) fn check(cx: &LateContext<'_>, info: &crate::methods::BinaryExprInfo<'_>) -> bool { crate::methods::chars_cmp_with_unwrap::check(cx, info, &["chars", "next", "unwrap"], CHARS_NEXT_CMP, "starts_with") } diff --git a/clippy_lints/src/methods/collapsible_str_replace.rs b/clippy_lints/src/methods/collapsible_str_replace.rs index 501646863fe15..ac61b4377885b 100644 --- a/clippy_lints/src/methods/collapsible_str_replace.rs +++ b/clippy_lints/src/methods/collapsible_str_replace.rs @@ -23,7 +23,7 @@ pub(super) fn check<'tcx>( // If the parent node's `to` argument is the same as the `to` argument // of the last replace call in the current chain, don't lint as it was already linted if let Some(parent) = get_parent_expr(cx, expr) - && let Some(("replace", _, [current_from, current_to], _)) = method_call(parent) + && let Some(("replace", _, [current_from, current_to], _, _)) = method_call(parent) && eq_expr_value(cx, to, current_to) && from_kind == cx.typeck_results().expr_ty(current_from).peel_refs().kind() { @@ -48,7 +48,7 @@ fn collect_replace_calls<'tcx>( let mut from_args = VecDeque::new(); let _: Option<()> = for_each_expr(expr, |e| { - if let Some(("replace", _, [from, to], _)) = method_call(e) { + if let Some(("replace", _, [from, to], _, _)) = method_call(e) { if eq_expr_value(cx, to_arg, to) && cx.typeck_results().expr_ty(from).peel_refs().is_char() { methods.push_front(e); from_args.push_front(from); @@ -78,7 +78,7 @@ fn check_consecutive_replace_calls<'tcx>( .collect(); let app = Applicability::MachineApplicable; let earliest_replace_call = replace_methods.methods.front().unwrap(); - if let Some((_, _, [..], span_lo)) = method_call(earliest_replace_call) { + if let Some((_, _, [..], span_lo, _)) = method_call(earliest_replace_call) { span_lint_and_sugg( cx, COLLAPSIBLE_STR_REPLACE, diff --git a/clippy_lints/src/methods/expect_used.rs b/clippy_lints/src/methods/expect_used.rs index d59fefa1ddc0e..cce8f797e98c6 100644 --- a/clippy_lints/src/methods/expect_used.rs +++ b/clippy_lints/src/methods/expect_used.rs @@ -1,5 +1,5 @@ use clippy_utils::diagnostics::span_lint_and_help; -use clippy_utils::is_in_test_function; +use clippy_utils::is_in_cfg_test; use clippy_utils::ty::is_type_diagnostic_item; use rustc_hir as hir; use rustc_lint::LateContext; @@ -18,16 +18,16 @@ pub(super) fn check( let obj_ty = cx.typeck_results().expr_ty(recv).peel_refs(); let mess = if is_type_diagnostic_item(cx, obj_ty, sym::Option) && !is_err { - Some((EXPECT_USED, "an Option", "None", "")) + Some((EXPECT_USED, "an `Option`", "None", "")) } else if is_type_diagnostic_item(cx, obj_ty, sym::Result) { - Some((EXPECT_USED, "a Result", if is_err { "Ok" } else { "Err" }, "an ")) + Some((EXPECT_USED, "a `Result`", if is_err { "Ok" } else { "Err" }, "an ")) } else { None }; let method = if is_err { "expect_err" } else { "expect" }; - if allow_expect_in_tests && is_in_test_function(cx.tcx, expr.hir_id) { + if allow_expect_in_tests && is_in_cfg_test(cx.tcx, expr.hir_id) { return; } @@ -36,7 +36,7 @@ pub(super) fn check( cx, lint, expr.span, - &format!("used `{method}()` on `{kind}` value"), + &format!("used `{method}()` on {kind} value"), None, &format!("if this value is {none_prefix}`{none_value}`, it will panic"), ); diff --git a/clippy_lints/src/methods/filter_map.rs b/clippy_lints/src/methods/filter_map.rs index 9719b2f1c5125..f888c58a72de9 100644 --- a/clippy_lints/src/methods/filter_map.rs +++ b/clippy_lints/src/methods/filter_map.rs @@ -17,7 +17,7 @@ use super::MANUAL_FILTER_MAP; use super::MANUAL_FIND_MAP; use super::OPTION_FILTER_MAP; -fn is_method<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, method_name: Symbol) -> bool { +fn is_method(cx: &LateContext<'_>, expr: &hir::Expr<'_>, method_name: Symbol) -> bool { match &expr.kind { hir::ExprKind::Path(QPath::TypeRelative(_, mname)) => mname.ident.name == method_name, hir::ExprKind::Path(QPath::Resolved(_, segments)) => { @@ -46,7 +46,7 @@ fn is_method<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, method_name: Sy } } -fn is_option_filter_map<'tcx>(cx: &LateContext<'tcx>, filter_arg: &hir::Expr<'_>, map_arg: &hir::Expr<'_>) -> bool { +fn is_option_filter_map(cx: &LateContext<'_>, filter_arg: &hir::Expr<'_>, map_arg: &hir::Expr<'_>) -> bool { is_method(cx, map_arg, sym::unwrap) && is_method(cx, filter_arg, sym!(is_some)) } @@ -66,8 +66,8 @@ fn is_filter_some_map_unwrap( /// lint use of `filter().map()` or `find().map()` for `Iterators` #[allow(clippy::too_many_arguments)] -pub(super) fn check<'tcx>( - cx: &LateContext<'tcx>, +pub(super) fn check( + cx: &LateContext<'_>, expr: &hir::Expr<'_>, filter_recv: &hir::Expr<'_>, filter_arg: &hir::Expr<'_>, diff --git a/clippy_lints/src/methods/inefficient_to_string.rs b/clippy_lints/src/methods/inefficient_to_string.rs index 4f4f543e8a912..d8c821bc9eeee 100644 --- a/clippy_lints/src/methods/inefficient_to_string.rs +++ b/clippy_lints/src/methods/inefficient_to_string.rs @@ -12,8 +12,8 @@ use rustc_span::symbol::{Symbol, sym}; use super::INEFFICIENT_TO_STRING; /// Checks for the `INEFFICIENT_TO_STRING` lint -pub fn check<'tcx>( - cx: &LateContext<'tcx>, +pub fn check( + cx: &LateContext<'_>, expr: &hir::Expr<'_>, method_name: Symbol, receiver: &hir::Expr<'_>, diff --git a/clippy_lints/src/methods/iter_nth_zero.rs b/clippy_lints/src/methods/iter_nth_zero.rs index 68d906c3ea399..c830958d5c80e 100644 --- a/clippy_lints/src/methods/iter_nth_zero.rs +++ b/clippy_lints/src/methods/iter_nth_zero.rs @@ -10,7 +10,7 @@ use rustc_span::sym; use super::ITER_NTH_ZERO; -pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>) { +pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>) { if_chain! { if is_trait_method(cx, expr, sym::Iterator); if let Some((Constant::Int(0), _)) = constant(cx, cx.typeck_results(), arg); diff --git a/clippy_lints/src/methods/iter_on_single_or_empty_collections.rs b/clippy_lints/src/methods/iter_on_single_or_empty_collections.rs index 4f73b3ec42247..70abe4891d985 100644 --- a/clippy_lints/src/methods/iter_on_single_or_empty_collections.rs +++ b/clippy_lints/src/methods/iter_on_single_or_empty_collections.rs @@ -25,7 +25,7 @@ impl IterType { } } -pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, method_name: &str, recv: &Expr<'_>) { +pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, method_name: &str, recv: &Expr<'_>) { let item = match recv.kind { ExprKind::Array([]) => None, ExprKind::Array([e]) => Some(e), diff --git a/clippy_lints/src/methods/manual_saturating_arithmetic.rs b/clippy_lints/src/methods/manual_saturating_arithmetic.rs index b80541b86479a..a7284c6449776 100644 --- a/clippy_lints/src/methods/manual_saturating_arithmetic.rs +++ b/clippy_lints/src/methods/manual_saturating_arithmetic.rs @@ -67,7 +67,7 @@ enum MinMax { Max, } -fn is_min_or_max<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>) -> Option { +fn is_min_or_max(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option { // `T::max_value()` `T::min_value()` inherent methods if_chain! { if let hir::ExprKind::Call(func, args) = &expr.kind; diff --git a/clippy_lints/src/methods/manual_str_repeat.rs b/clippy_lints/src/methods/manual_str_repeat.rs index 13c47c03a80dd..a08f7254053fe 100644 --- a/clippy_lints/src/methods/manual_str_repeat.rs +++ b/clippy_lints/src/methods/manual_str_repeat.rs @@ -59,10 +59,8 @@ pub(super) fn check( if let ExprKind::Call(repeat_fn, [repeat_arg]) = take_self_arg.kind; if is_path_diagnostic_item(cx, repeat_fn, sym::iter_repeat); if is_type_lang_item(cx, cx.typeck_results().expr_ty(collect_expr), LangItem::String); - if let Some(collect_id) = cx.typeck_results().type_dependent_def_id(collect_expr.hir_id); if let Some(take_id) = cx.typeck_results().type_dependent_def_id(take_expr.hir_id); if let Some(iter_trait_id) = cx.tcx.get_diagnostic_item(sym::Iterator); - if cx.tcx.trait_of_item(collect_id) == Some(iter_trait_id); if cx.tcx.trait_of_item(take_id) == Some(iter_trait_id); if let Some(repeat_kind) = parse_repeat_arg(cx, repeat_arg); let ctxt = collect_expr.span.ctxt(); diff --git a/clippy_lints/src/methods/map_clone.rs b/clippy_lints/src/methods/map_clone.rs index 7ce14ec080b15..6bc783c6d505a 100644 --- a/clippy_lints/src/methods/map_clone.rs +++ b/clippy_lints/src/methods/map_clone.rs @@ -15,11 +15,11 @@ use rustc_span::{sym, Span}; use super::MAP_CLONE; -pub(super) fn check<'tcx>( +pub(super) fn check( cx: &LateContext<'_>, e: &hir::Expr<'_>, recv: &hir::Expr<'_>, - arg: &'tcx hir::Expr<'_>, + arg: &hir::Expr<'_>, msrv: Option, ) { if_chain! { diff --git a/clippy_lints/src/methods/map_collect_result_unit.rs b/clippy_lints/src/methods/map_collect_result_unit.rs index d420f144eea1f..a0300d278709d 100644 --- a/clippy_lints/src/methods/map_collect_result_unit.rs +++ b/clippy_lints/src/methods/map_collect_result_unit.rs @@ -1,5 +1,4 @@ use clippy_utils::diagnostics::span_lint_and_sugg; -use clippy_utils::is_trait_method; use clippy_utils::source::snippet; use clippy_utils::ty::is_type_diagnostic_item; use if_chain::if_chain; @@ -11,18 +10,10 @@ use rustc_span::symbol::sym; use super::MAP_COLLECT_RESULT_UNIT; -pub(super) fn check( - cx: &LateContext<'_>, - expr: &hir::Expr<'_>, - iter: &hir::Expr<'_>, - map_fn: &hir::Expr<'_>, - collect_recv: &hir::Expr<'_>, -) { +pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, iter: &hir::Expr<'_>, map_fn: &hir::Expr<'_>) { + // return of collect `Result<(),_>` + let collect_ret_ty = cx.typeck_results().expr_ty(expr); if_chain! { - // called on Iterator - if is_trait_method(cx, collect_recv, sym::Iterator); - // return of collect `Result<(),_>` - let collect_ret_ty = cx.typeck_results().expr_ty(expr); if is_type_diagnostic_item(cx, collect_ret_ty, sym::Result); if let ty::Adt(_, substs) = collect_ret_ty.kind(); if let Some(result_t) = substs.types().next(); diff --git a/clippy_lints/src/methods/map_err_ignore.rs b/clippy_lints/src/methods/map_err_ignore.rs index 1fb6617145e71..b773b3e423f40 100644 --- a/clippy_lints/src/methods/map_err_ignore.rs +++ b/clippy_lints/src/methods/map_err_ignore.rs @@ -6,7 +6,7 @@ use rustc_span::sym; use super::MAP_ERR_IGNORE; -pub(super) fn check<'tcx>(cx: &LateContext<'_>, e: &Expr<'_>, arg: &'tcx Expr<'_>) { +pub(super) fn check(cx: &LateContext<'_>, e: &Expr<'_>, arg: &Expr<'_>) { if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id) && let Some(impl_id) = cx.tcx.impl_of_method(method_id) && is_type_diagnostic_item(cx, cx.tcx.type_of(impl_id), sym::Result) diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 8a76ba0b064b5..38165ab4fb26f 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -54,6 +54,7 @@ mod map_flatten; mod map_identity; mod map_unwrap_or; mod mut_mutex_lock; +mod needless_collect; mod needless_option_as_deref; mod needless_option_take; mod no_effect_replace; @@ -69,6 +70,8 @@ mod path_buf_push_overwrite; mod range_zip_with_len; mod repeat_once; mod search_is_some; +mod seek_from_current; +mod seek_to_start_instead_of_rewind; mod single_char_add_str; mod single_char_insert_string; mod single_char_pattern; @@ -101,12 +104,11 @@ mod zst_offset; use bind_instead_of_map::BindInsteadOfMap; use clippy_utils::consts::{constant, Constant}; use clippy_utils::diagnostics::{span_lint, span_lint_and_help}; -use clippy_utils::ty::{contains_adt_constructor, implements_trait, is_copy, is_type_diagnostic_item}; -use clippy_utils::{contains_return, is_trait_method, iter_input_pats, meets_msrv, msrvs, return_ty}; +use clippy_utils::ty::{contains_ty_adt_constructor_opaque, implements_trait, is_copy, is_type_diagnostic_item}; +use clippy_utils::{contains_return, is_bool, is_trait_method, iter_input_pats, meets_msrv, msrvs, return_ty}; use if_chain::if_chain; use rustc_hir as hir; -use rustc_hir::def::Res; -use rustc_hir::{Expr, ExprKind, PrimTy, QPath, TraitItem, TraitItemKind}; +use rustc_hir::{Expr, ExprKind, TraitItem, TraitItemKind}; use rustc_hir_analysis::hir_ty_to_ty; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; @@ -156,9 +158,9 @@ declare_clippy_lint! { /// ``` /// Use instead: /// ```rust - /// let hello = "hesuo worpd".replace(&['s', 'u', 'p'], "l"); + /// let hello = "hesuo worpd".replace(['s', 'u', 'p'], "l"); /// ``` - #[clippy::version = "1.64.0"] + #[clippy::version = "1.65.0"] pub COLLAPSIBLE_STR_REPLACE, perf, "collapse consecutive calls to str::replace (2 or more) into a single call" @@ -829,32 +831,30 @@ declare_clippy_lint! { /// etc. instead. /// /// ### Why is this bad? - /// The function will always be called and potentially - /// allocate an object acting as the default. + /// The function will always be called. This is only bad if it allocates or + /// does some non-trivial amount of work. /// /// ### Known problems - /// If the function has side-effects, not calling it will - /// change the semantic of the program, but you shouldn't rely on that anyway. + /// If the function has side-effects, not calling it will change the + /// semantic of the program, but you shouldn't rely on that. + /// + /// The lint also cannot figure out whether the function you call is + /// actually expensive to call or not. /// /// ### Example /// ```rust /// # let foo = Some(String::new()); - /// foo.unwrap_or(String::new()); + /// foo.unwrap_or(String::from("empty")); /// ``` /// /// Use instead: /// ```rust /// # let foo = Some(String::new()); - /// foo.unwrap_or_else(String::new); - /// - /// // or - /// - /// # let foo = Some(String::new()); - /// foo.unwrap_or_default(); + /// foo.unwrap_or_else(|| String::from("empty")); /// ``` #[clippy::version = "pre 1.29.0"] pub OR_FUN_CALL, - perf, + nursery, "using any `*or` method with a function call, which suggests `*or_else`" } @@ -1728,7 +1728,7 @@ declare_clippy_lint! { declare_clippy_lint! { /// ### What it does - /// Checks for usage of `_.as_ref().map(Deref::deref)` or it's aliases (such as String::as_str). + /// Checks for usage of `_.as_ref().map(Deref::deref)` or its aliases (such as String::as_str). /// /// ### Why is this bad? /// Readability, this can be written more concisely as @@ -2094,8 +2094,7 @@ declare_clippy_lint! { /// let s = "Hello world!"; /// let cow = Cow::Borrowed(s); /// - /// let data = cow.into_owned(); - /// assert!(matches!(data, String)) + /// let _data: String = cow.into_owned(); /// ``` #[clippy::version = "1.65.0"] pub SUSPICIOUS_TO_OWNED, @@ -2426,7 +2425,7 @@ declare_clippy_lint! { /// ### Known problems /// /// The type of the resulting iterator might become incompatible with its usage - #[clippy::version = "1.64.0"] + #[clippy::version = "1.65.0"] pub ITER_ON_SINGLE_ITEMS, nursery, "Iterator for array of length 1" @@ -2458,7 +2457,7 @@ declare_clippy_lint! { /// ### Known problems /// /// The type of the resulting iterator might become incompatible with its usage - #[clippy::version = "1.64.0"] + #[clippy::version = "1.65.0"] pub ITER_ON_EMPTY_COLLECTIONS, nursery, "Iterator for empty array" @@ -3066,6 +3065,102 @@ declare_clippy_lint! { "iterating on map using `iter` when `keys` or `values` would do" } +declare_clippy_lint! { + /// ### What it does + /// + /// Checks an argument of `seek` method of `Seek` trait + /// and if it start seek from `SeekFrom::Current(0)`, suggests `stream_position` instead. + /// + /// ### Why is this bad? + /// + /// Readability. Use dedicated method. + /// + /// ### Example + /// + /// ```rust,no_run + /// use std::fs::File; + /// use std::io::{self, Write, Seek, SeekFrom}; + /// + /// fn main() -> io::Result<()> { + /// let mut f = File::create("foo.txt")?; + /// f.write_all(b"Hello")?; + /// eprintln!("Written {} bytes", f.seek(SeekFrom::Current(0))?); + /// + /// Ok(()) + /// } + /// ``` + /// Use instead: + /// ```rust,no_run + /// use std::fs::File; + /// use std::io::{self, Write, Seek, SeekFrom}; + /// + /// fn main() -> io::Result<()> { + /// let mut f = File::create("foo.txt")?; + /// f.write_all(b"Hello")?; + /// eprintln!("Written {} bytes", f.stream_position()?); + /// + /// Ok(()) + /// } + /// ``` + #[clippy::version = "1.66.0"] + pub SEEK_FROM_CURRENT, + complexity, + "use dedicated method for seek from current position" +} + +declare_clippy_lint! { + /// ### What it does + /// + /// Checks for jumps to the start of a stream that implements `Seek` + /// and uses the `seek` method providing `Start` as parameter. + /// + /// ### Why is this bad? + /// + /// Readability. There is a specific method that was implemented for + /// this exact scenario. + /// + /// ### Example + /// ```rust + /// # use std::io; + /// fn foo(t: &mut T) { + /// t.seek(io::SeekFrom::Start(0)); + /// } + /// ``` + /// Use instead: + /// ```rust + /// # use std::io; + /// fn foo(t: &mut T) { + /// t.rewind(); + /// } + /// ``` + #[clippy::version = "1.66.0"] + pub SEEK_TO_START_INSTEAD_OF_REWIND, + complexity, + "jumping to the start of stream using `seek` method" +} + +declare_clippy_lint! { + /// ### What it does + /// Checks for functions collecting an iterator when collect + /// is not needed. + /// + /// ### Why is this bad? + /// `collect` causes the allocation of a new data structure, + /// when this allocation may not be needed. + /// + /// ### Example + /// ```rust + /// # let iterator = vec![1].into_iter(); + /// let len = iterator.clone().collect::>().len(); + /// // should be + /// let len = iterator.count(); + /// ``` + #[clippy::version = "1.30.0"] + pub NEEDLESS_COLLECT, + nursery, + "collecting an iterator when collect is not needed" +} + pub struct Methods { avoid_breaking_exported_api: bool, msrv: Option, @@ -3190,16 +3285,19 @@ impl_lint_pass!(Methods => [ VEC_RESIZE_TO_ZERO, VERBOSE_FILE_READS, ITER_KV_MAP, + SEEK_FROM_CURRENT, + SEEK_TO_START_INSTEAD_OF_REWIND, + NEEDLESS_COLLECT, ]); /// Extracts a method call name, args, and `Span` of the method name. fn method_call<'tcx>( recv: &'tcx hir::Expr<'tcx>, -) -> Option<(&'tcx str, &'tcx hir::Expr<'tcx>, &'tcx [hir::Expr<'tcx>], Span)> { - if let ExprKind::MethodCall(path, receiver, args, _) = recv.kind { +) -> Option<(&'tcx str, &'tcx hir::Expr<'tcx>, &'tcx [hir::Expr<'tcx>], Span, Span)> { + if let ExprKind::MethodCall(path, receiver, args, call_span) = recv.kind { if !args.iter().any(|e| e.span.from_expansion()) && !receiver.span.from_expansion() { let name = path.ident.name.as_str(); - return Some((name, receiver, args, path.ident.span)); + return Some((name, receiver, args, path.ident.span, call_span)); } } None @@ -3316,36 +3414,10 @@ impl<'tcx> LateLintPass<'tcx> for Methods { if let hir::ImplItemKind::Fn(_, _) = impl_item.kind { let ret_ty = return_ty(cx, impl_item.hir_id()); - // walk the return type and check for Self (this does not check associated types) - if let Some(self_adt) = self_ty.ty_adt_def() { - if contains_adt_constructor(ret_ty, self_adt) { - return; - } - } else if ret_ty.contains(self_ty) { + if contains_ty_adt_constructor_opaque(cx, ret_ty, self_ty) { return; } - // if return type is impl trait, check the associated types - if let ty::Opaque(def_id, _) = *ret_ty.kind() { - // one of the associated types must be Self - for &(predicate, _span) in cx.tcx.explicit_item_bounds(def_id) { - if let ty::PredicateKind::Projection(projection_predicate) = predicate.kind().skip_binder() { - let assoc_ty = match projection_predicate.term.unpack() { - ty::TermKind::Ty(ty) => ty, - ty::TermKind::Const(_c) => continue, - }; - // walk the associated type and check for Self - if let Some(self_adt) = self_ty.ty_adt_def() { - if contains_adt_constructor(assoc_ty, self_adt) { - return; - } - } else if assoc_ty.contains(self_ty) { - return; - } - } - } - } - if name == "new" && ret_ty != self_ty { span_lint( cx, @@ -3411,7 +3483,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods { impl Methods { #[allow(clippy::too_many_lines)] fn check_methods<'tcx>(&self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - if let Some((name, recv, args, span)) = method_call(expr) { + if let Some((name, recv, args, span, call_span)) = method_call(expr) { match (name, args) { ("add" | "offset" | "sub" | "wrapping_offset" | "wrapping_add" | "wrapping_sub", [_arg]) => { zst_offset::check(cx, expr, recv); @@ -3430,28 +3502,31 @@ impl Methods { ("as_ref", []) => useless_asref::check(cx, expr, "as_ref", recv), ("assume_init", []) => uninit_assumed_init::check(cx, expr, recv), ("cloned", []) => cloned_instead_of_copied::check(cx, expr, recv, span, self.msrv), - ("collect", []) => match method_call(recv) { - Some((name @ ("cloned" | "copied"), recv2, [], _)) => { - iter_cloned_collect::check(cx, name, expr, recv2); - }, - Some(("map", m_recv, [m_arg], _)) => { - map_collect_result_unit::check(cx, expr, m_recv, m_arg, recv); - }, - Some(("take", take_self_arg, [take_arg], _)) => { - if meets_msrv(self.msrv, msrvs::STR_REPEAT) { - manual_str_repeat::check(cx, expr, recv, take_self_arg, take_arg); - } - }, - _ => {}, + ("collect", []) if is_trait_method(cx, expr, sym::Iterator) => { + needless_collect::check(cx, span, expr, recv, call_span); + match method_call(recv) { + Some((name @ ("cloned" | "copied"), recv2, [], _, _)) => { + iter_cloned_collect::check(cx, name, expr, recv2); + }, + Some(("map", m_recv, [m_arg], _, _)) => { + map_collect_result_unit::check(cx, expr, m_recv, m_arg); + }, + Some(("take", take_self_arg, [take_arg], _, _)) => { + if meets_msrv(self.msrv, msrvs::STR_REPEAT) { + manual_str_repeat::check(cx, expr, recv, take_self_arg, take_arg); + } + }, + _ => {}, + } }, ("count", []) if is_trait_method(cx, expr, sym::Iterator) => match method_call(recv) { - Some(("cloned", recv2, [], _)) => iter_overeager_cloned::check(cx, expr, recv, recv2, true, false), - Some((name2 @ ("into_iter" | "iter" | "iter_mut"), recv2, [], _)) => { + Some(("cloned", recv2, [], _, _)) => iter_overeager_cloned::check(cx, expr, recv, recv2, true, false), + Some((name2 @ ("into_iter" | "iter" | "iter_mut"), recv2, [], _, _)) => { iter_count::check(cx, expr, recv2, name2); }, - Some(("map", _, [arg], _)) => suspicious_map::check(cx, expr, recv, arg), - Some(("filter", recv2, [arg], _)) => bytecount::check(cx, expr, recv2, arg), - Some(("bytes", recv2, [], _)) => bytes_count_to_len::check(cx, expr, recv, recv2), + Some(("map", _, [arg], _, _)) => suspicious_map::check(cx, expr, recv, arg), + Some(("filter", recv2, [arg], _, _)) => bytecount::check(cx, expr, recv2, arg), + Some(("bytes", recv2, [], _, _)) => bytes_count_to_len::check(cx, expr, recv, recv2), _ => {}, }, ("drain", [arg]) => { @@ -3463,8 +3538,8 @@ impl Methods { } }, ("expect", [_]) => match method_call(recv) { - Some(("ok", recv, [], _)) => ok_expect::check(cx, expr, recv), - Some(("err", recv, [], err_span)) => err_expect::check(cx, expr, recv, self.msrv, span, err_span), + Some(("ok", recv, [], _, _)) => ok_expect::check(cx, expr, recv), + Some(("err", recv, [], err_span, _)) => err_expect::check(cx, expr, recv, self.msrv, span, err_span), _ => expect_used::check(cx, expr, recv, false, self.allow_expect_in_tests), }, ("expect_err", [_]) => expect_used::check(cx, expr, recv, true, self.allow_expect_in_tests), @@ -3484,13 +3559,13 @@ impl Methods { flat_map_option::check(cx, expr, arg, span); }, ("flatten", []) => match method_call(recv) { - Some(("map", recv, [map_arg], map_span)) => map_flatten::check(cx, expr, recv, map_arg, map_span), - Some(("cloned", recv2, [], _)) => iter_overeager_cloned::check(cx, expr, recv, recv2, false, true), + Some(("map", recv, [map_arg], map_span, _)) => map_flatten::check(cx, expr, recv, map_arg, map_span), + Some(("cloned", recv2, [], _, _)) => iter_overeager_cloned::check(cx, expr, recv, recv2, false, true), _ => {}, }, ("fold", [init, acc]) => unnecessary_fold::check(cx, expr, init, acc, span), ("for_each", [_]) => { - if let Some(("inspect", _, [_], span2)) = method_call(recv) { + if let Some(("inspect", _, [_], span2, _)) = method_call(recv) { inspect_for_each::check(cx, expr, span2); } }, @@ -3510,12 +3585,12 @@ impl Methods { iter_on_single_or_empty_collections::check(cx, expr, name, recv); }, ("join", [join_arg]) => { - if let Some(("collect", _, _, span)) = method_call(recv) { + if let Some(("collect", _, _, span, _)) = method_call(recv) { unnecessary_join::check(cx, expr, recv, join_arg, span); } }, ("last", []) | ("skip", [_]) => { - if let Some((name2, recv2, args2, _span2)) = method_call(recv) { + if let Some((name2, recv2, args2, _span2, _)) = method_call(recv) { if let ("cloned", []) = (name2, args2) { iter_overeager_cloned::check(cx, expr, recv, recv2, false, false); } @@ -3527,13 +3602,13 @@ impl Methods { (name @ ("map" | "map_err"), [m_arg]) => { if name == "map" { map_clone::check(cx, expr, recv, m_arg, self.msrv); - if let Some((map_name @ ("iter" | "into_iter"), recv2, _, _)) = method_call(recv) { + if let Some((map_name @ ("iter" | "into_iter"), recv2, _, _, _)) = method_call(recv) { iter_kv_map::check(cx, map_name, expr, recv2, m_arg); } } else { map_err_ignore::check(cx, expr, m_arg); } - if let Some((name, recv2, args, span2)) = method_call(recv) { + if let Some((name, recv2, args, span2,_)) = method_call(recv) { match (name, args) { ("as_mut", []) => option_as_ref_deref::check(cx, expr, recv2, m_arg, true, self.msrv), ("as_ref", []) => option_as_ref_deref::check(cx, expr, recv2, m_arg, false, self.msrv), @@ -3553,7 +3628,7 @@ impl Methods { manual_ok_or::check(cx, expr, recv, def, map); }, ("next", []) => { - if let Some((name2, recv2, args2, _)) = method_call(recv) { + if let Some((name2, recv2, args2, _, _)) = method_call(recv) { match (name2, args2) { ("cloned", []) => iter_overeager_cloned::check(cx, expr, recv, recv2, false, false), ("filter", [arg]) => filter_next::check(cx, expr, recv2, arg), @@ -3566,10 +3641,10 @@ impl Methods { } }, ("nth", [n_arg]) => match method_call(recv) { - Some(("bytes", recv2, [], _)) => bytes_nth::check(cx, expr, recv2, n_arg), - Some(("cloned", recv2, [], _)) => iter_overeager_cloned::check(cx, expr, recv, recv2, false, false), - Some(("iter", recv2, [], _)) => iter_nth::check(cx, expr, recv2, recv, n_arg, false), - Some(("iter_mut", recv2, [], _)) => iter_nth::check(cx, expr, recv2, recv, n_arg, true), + Some(("bytes", recv2, [], _, _)) => bytes_nth::check(cx, expr, recv2, n_arg), + Some(("cloned", recv2, [], _, _)) => iter_overeager_cloned::check(cx, expr, recv, recv2, false, false), + Some(("iter", recv2, [], _, _)) => iter_nth::check(cx, expr, recv2, recv, n_arg, false), + Some(("iter_mut", recv2, [], _, _)) => iter_nth::check(cx, expr, recv2, recv, n_arg, true), _ => iter_nth_zero::check(cx, expr, recv, n_arg), }, ("ok_or_else", [arg]) => unnecessary_lazy_eval::check(cx, expr, recv, arg, "ok_or"), @@ -3604,6 +3679,14 @@ impl Methods { ("resize", [count_arg, default_arg]) => { vec_resize_to_zero::check(cx, expr, count_arg, default_arg, span); }, + ("seek", [arg]) => { + if meets_msrv(self.msrv, msrvs::SEEK_FROM_CURRENT) { + seek_from_current::check(cx, expr, recv, arg); + } + if meets_msrv(self.msrv, msrvs::SEEK_REWIND) { + seek_to_start_instead_of_rewind::check(cx, expr, recv, arg, span); + } + }, ("sort", []) => { stable_sort_primitive::check(cx, expr, recv); }, @@ -3626,7 +3709,7 @@ impl Methods { }, ("step_by", [arg]) => iterator_step_by_zero::check(cx, expr, arg), ("take", [_arg]) => { - if let Some((name2, recv2, args2, _span2)) = method_call(recv) { + if let Some((name2, recv2, args2, _span2, _)) = method_call(recv) { if let ("cloned", []) = (name2, args2) { iter_overeager_cloned::check(cx, expr, recv, recv2, false, false); } @@ -3649,13 +3732,13 @@ impl Methods { }, ("unwrap", []) => { match method_call(recv) { - Some(("get", recv, [get_arg], _)) => { + Some(("get", recv, [get_arg], _, _)) => { get_unwrap::check(cx, expr, recv, get_arg, false); }, - Some(("get_mut", recv, [get_arg], _)) => { + Some(("get_mut", recv, [get_arg], _, _)) => { get_unwrap::check(cx, expr, recv, get_arg, true); }, - Some(("or", recv, [or_arg], or_span)) => { + Some(("or", recv, [or_arg], or_span, _)) => { or_then_unwrap::check(cx, expr, recv, or_arg, or_span); }, _ => {}, @@ -3664,19 +3747,19 @@ impl Methods { }, ("unwrap_err", []) => unwrap_used::check(cx, expr, recv, true, self.allow_unwrap_in_tests), ("unwrap_or", [u_arg]) => match method_call(recv) { - Some((arith @ ("checked_add" | "checked_sub" | "checked_mul"), lhs, [rhs], _)) => { + Some((arith @ ("checked_add" | "checked_sub" | "checked_mul"), lhs, [rhs], _, _)) => { manual_saturating_arithmetic::check(cx, expr, lhs, rhs, u_arg, &arith["checked_".len()..]); }, - Some(("map", m_recv, [m_arg], span)) => { + Some(("map", m_recv, [m_arg], span, _)) => { option_map_unwrap_or::check(cx, expr, m_recv, m_arg, recv, u_arg, span); }, - Some(("then_some", t_recv, [t_arg], _)) => { + Some(("then_some", t_recv, [t_arg], _, _)) => { obfuscated_if_else::check(cx, expr, t_recv, t_arg, u_arg); }, _ => {}, }, ("unwrap_or_else", [u_arg]) => match method_call(recv) { - Some(("map", recv, [map_arg], _)) + Some(("map", recv, [map_arg], _, _)) if map_unwrap_or::check(cx, expr, recv, map_arg, u_arg, self.msrv) => {}, _ => { unwrap_or_else_default::check(cx, expr, recv, u_arg); @@ -3697,7 +3780,7 @@ impl Methods { } fn check_is_some_is_none(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, is_some: bool) { - if let Some((name @ ("find" | "position" | "rposition"), f_recv, [arg], span)) = method_call(recv) { + if let Some((name @ ("find" | "position" | "rposition"), f_recv, [arg], span, _)) = method_call(recv) { search_is_some::check(cx, expr, name, is_some, f_recv, arg, recv, span); } } @@ -3906,14 +3989,6 @@ impl OutType { } } -fn is_bool(ty: &hir::Ty<'_>) -> bool { - if let hir::TyKind::Path(QPath::Resolved(_, path)) = ty.kind { - matches!(path.res, Res::PrimTy(PrimTy::Bool)) - } else { - false - } -} - fn fn_header_equals(expected: hir::FnHeader, actual: hir::FnHeader) -> bool { expected.constness == actual.constness && expected.unsafety == actual.unsafety diff --git a/clippy_lints/src/loops/needless_collect.rs b/clippy_lints/src/methods/needless_collect.rs similarity index 62% rename from clippy_lints/src/loops/needless_collect.rs rename to clippy_lints/src/methods/needless_collect.rs index 66f9e28596e87..b088e642e0e9a 100644 --- a/clippy_lints/src/loops/needless_collect.rs +++ b/clippy_lints/src/methods/needless_collect.rs @@ -3,94 +3,99 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then}; use clippy_utils::higher; use clippy_utils::source::{snippet, snippet_with_applicability}; use clippy_utils::sugg::Sugg; -use clippy_utils::ty::is_type_diagnostic_item; -use clippy_utils::{can_move_expr_to_closure, is_trait_method, path_to_local, path_to_local_id, CaptureKind}; -use if_chain::if_chain; +use clippy_utils::ty::{is_type_diagnostic_item, make_normalized_projection, make_projection}; +use clippy_utils::{ + can_move_expr_to_closure, get_enclosing_block, get_parent_node, is_trait_method, path_to_local, path_to_local_id, + CaptureKind, +}; use rustc_data_structures::fx::FxHashMap; use rustc_errors::{Applicability, MultiSpan}; use rustc_hir::intravisit::{walk_block, walk_expr, Visitor}; -use rustc_hir::{Block, Expr, ExprKind, HirId, HirIdSet, Local, Mutability, Node, PatKind, Stmt, StmtKind}; +use rustc_hir::{ + BindingAnnotation, Block, Expr, ExprKind, HirId, HirIdSet, Local, Mutability, Node, PatKind, Stmt, StmtKind, +}; use rustc_lint::LateContext; use rustc_middle::hir::nested_filter; -use rustc_middle::ty::subst::GenericArgKind; -use rustc_middle::ty::{self, Ty}; -use rustc_span::sym; -use rustc_span::Span; +use rustc_middle::ty::{self, AssocKind, EarlyBinder, GenericArg, GenericArgKind, Ty}; +use rustc_span::symbol::Ident; +use rustc_span::{sym, Span, Symbol}; const NEEDLESS_COLLECT_MSG: &str = "avoid using `collect()` when not needed"; -pub(super) fn check<'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) { - check_needless_collect_direct_usage(expr, cx); - check_needless_collect_indirect_usage(expr, cx); -} -fn check_needless_collect_direct_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) { - if_chain! { - if let ExprKind::MethodCall(method, receiver, args, _) = expr.kind; - if let ExprKind::MethodCall(chain_method, ..) = receiver.kind; - if chain_method.ident.name == sym!(collect) && is_trait_method(cx, receiver, sym::Iterator); - then { - let ty = cx.typeck_results().expr_ty(receiver); - let mut applicability = Applicability::MaybeIncorrect; - let is_empty_sugg = "next().is_none()".to_string(); - let method_name = method.ident.name.as_str(); - let sugg = if is_type_diagnostic_item(cx, ty, sym::Vec) || - is_type_diagnostic_item(cx, ty, sym::VecDeque) || - is_type_diagnostic_item(cx, ty, sym::LinkedList) || - is_type_diagnostic_item(cx, ty, sym::BinaryHeap) { - match method_name { - "len" => "count()".to_string(), - "is_empty" => is_empty_sugg, - "contains" => { - let contains_arg = snippet_with_applicability(cx, args[0].span, "??", &mut applicability); - let (arg, pred) = contains_arg - .strip_prefix('&') - .map_or(("&x", &*contains_arg), |s| ("x", s)); - format!("any(|{arg}| x == {pred})") - } - _ => return, - } - } - else if is_type_diagnostic_item(cx, ty, sym::BTreeMap) || - is_type_diagnostic_item(cx, ty, sym::HashMap) { - match method_name { - "is_empty" => is_empty_sugg, - _ => return, - } - } - else { - return; - }; - span_lint_and_sugg( - cx, - NEEDLESS_COLLECT, - chain_method.ident.span.with_hi(expr.span.hi()), - NEEDLESS_COLLECT_MSG, - "replace with", - sugg, - applicability, - ); - } - } -} +pub(super) fn check<'tcx>( + cx: &LateContext<'tcx>, + name_span: Span, + collect_expr: &'tcx Expr<'_>, + iter_expr: &'tcx Expr<'tcx>, + call_span: Span, +) { + if let Some(parent) = get_parent_node(cx.tcx, collect_expr.hir_id) { + match parent { + Node::Expr(parent) => { + if let ExprKind::MethodCall(name, _, args @ ([] | [_]), _) = parent.kind { + let mut app = Applicability::MachineApplicable; + let name = name.ident.as_str(); + let collect_ty = cx.typeck_results().expr_ty(collect_expr); -fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) { - if let ExprKind::Block(block, _) = expr.kind { - for stmt in block.stmts { - if_chain! { - if let StmtKind::Local(local) = stmt.kind; - if let PatKind::Binding(_, id, ..) = local.pat.kind; - if let Some(init_expr) = local.init; - if let ExprKind::MethodCall(method_name, iter_source, [], ..) = init_expr.kind; - if method_name.ident.name == sym!(collect) && is_trait_method(cx, init_expr, sym::Iterator); - let ty = cx.typeck_results().expr_ty(init_expr); - if is_type_diagnostic_item(cx, ty, sym::Vec) || - is_type_diagnostic_item(cx, ty, sym::VecDeque) || - is_type_diagnostic_item(cx, ty, sym::BinaryHeap) || - is_type_diagnostic_item(cx, ty, sym::LinkedList); - let iter_ty = cx.typeck_results().expr_ty(iter_source); - if let Some(iter_calls) = detect_iter_and_into_iters(block, id, cx, get_captured_ids(cx, iter_ty)); - if let [iter_call] = &*iter_calls; - then { + let sugg: String = match name { + "len" => { + if let Some(adt) = collect_ty.ty_adt_def() + && matches!( + cx.tcx.get_diagnostic_name(adt.did()), + Some(sym::Vec | sym::VecDeque | sym::LinkedList | sym::BinaryHeap) + ) + { + "count()".into() + } else { + return; + } + }, + "is_empty" + if is_is_empty_sig(cx, parent.hir_id) + && iterates_same_ty(cx, cx.typeck_results().expr_ty(iter_expr), collect_ty) => + { + "next().is_none()".into() + }, + "contains" => { + if is_contains_sig(cx, parent.hir_id, iter_expr) + && let Some(arg) = args.first() + { + let (span, prefix) = if let ExprKind::AddrOf(_, _, arg) = arg.kind { + (arg.span, "") + } else { + (arg.span, "*") + }; + let snip = snippet_with_applicability(cx, span, "??", &mut app); + format!("any(|x| x == {prefix}{snip})") + } else { + return; + } + }, + _ => return, + }; + + span_lint_and_sugg( + cx, + NEEDLESS_COLLECT, + call_span.with_hi(parent.span.hi()), + NEEDLESS_COLLECT_MSG, + "replace with", + sugg, + app, + ); + } + }, + Node::Local(l) => { + if let PatKind::Binding(BindingAnnotation::NONE | BindingAnnotation::MUT, id, _, None) + = l.pat.kind + && let ty = cx.typeck_results().expr_ty(collect_expr) + && [sym::Vec, sym::VecDeque, sym::BinaryHeap, sym::LinkedList].into_iter() + .any(|item| is_type_diagnostic_item(cx, ty, item)) + && let iter_ty = cx.typeck_results().expr_ty(iter_expr) + && let Some(block) = get_enclosing_block(cx, l.hir_id) + && let Some(iter_calls) = detect_iter_and_into_iters(block, id, cx, get_captured_ids(cx, iter_ty)) + && let [iter_call] = &*iter_calls + { let mut used_count_visitor = UsedCountVisitor { cx, id, @@ -102,20 +107,20 @@ fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCo } // Suggest replacing iter_call with iter_replacement, and removing stmt - let mut span = MultiSpan::from_span(method_name.ident.span); + let mut span = MultiSpan::from_span(name_span); span.push_span_label(iter_call.span, "the iterator could be used here instead"); span_lint_hir_and_then( cx, super::NEEDLESS_COLLECT, - init_expr.hir_id, + collect_expr.hir_id, span, NEEDLESS_COLLECT_MSG, |diag| { - let iter_replacement = format!("{}{}", Sugg::hir(cx, iter_source, ".."), iter_call.get_iter_method(cx)); + let iter_replacement = format!("{}{}", Sugg::hir(cx, iter_expr, ".."), iter_call.get_iter_method(cx)); diag.multipart_suggestion( iter_call.get_suggestion_text(), vec![ - (stmt.span, String::new()), + (l.span, String::new()), (iter_call.span, iter_replacement) ], Applicability::MaybeIncorrect, @@ -123,11 +128,61 @@ fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCo }, ); } - } + }, + _ => (), } } } +/// Checks if the given method call matches the expected signature of `([&[mut]] self) -> bool` +fn is_is_empty_sig(cx: &LateContext<'_>, call_id: HirId) -> bool { + cx.typeck_results().type_dependent_def_id(call_id).map_or(false, |id| { + let sig = cx.tcx.fn_sig(id).skip_binder(); + sig.inputs().len() == 1 && sig.output().is_bool() + }) +} + +/// Checks if `::Item` is the same as `::Item` +fn iterates_same_ty<'tcx>(cx: &LateContext<'tcx>, iter_ty: Ty<'tcx>, collect_ty: Ty<'tcx>) -> bool { + let item = Symbol::intern("Item"); + if let Some(iter_trait) = cx.tcx.get_diagnostic_item(sym::Iterator) + && let Some(into_iter_trait) = cx.tcx.get_diagnostic_item(sym::IntoIterator) + && let Some(iter_item_ty) = make_normalized_projection(cx.tcx, cx.param_env, iter_trait, item, [iter_ty]) + && let Some(into_iter_item_proj) = make_projection(cx.tcx, into_iter_trait, item, [collect_ty]) + && let Ok(into_iter_item_ty) = cx.tcx.try_normalize_erasing_regions( + cx.param_env, + cx.tcx.mk_projection(into_iter_item_proj.item_def_id, into_iter_item_proj.substs) + ) + { + iter_item_ty == into_iter_item_ty + } else { + false + } +} + +/// Checks if the given method call matches the expected signature of +/// `([&[mut]] self, &::Item) -> bool` +fn is_contains_sig(cx: &LateContext<'_>, call_id: HirId, iter_expr: &Expr<'_>) -> bool { + let typeck = cx.typeck_results(); + if let Some(id) = typeck.type_dependent_def_id(call_id) + && let sig = cx.tcx.fn_sig(id) + && sig.skip_binder().output().is_bool() + && let [_, search_ty] = *sig.skip_binder().inputs() + && let ty::Ref(_, search_ty, Mutability::Not) = *cx.tcx.erase_late_bound_regions(sig.rebind(search_ty)).kind() + && let Some(iter_trait) = cx.tcx.get_diagnostic_item(sym::Iterator) + && let Some(iter_item) = cx.tcx + .associated_items(iter_trait) + .find_by_name_and_kind(cx.tcx, Ident::with_dummy_span(Symbol::intern("Item")), AssocKind::Type, iter_trait) + && let substs = cx.tcx.mk_substs([GenericArg::from(typeck.expr_ty_adjusted(iter_expr))].into_iter()) + && let proj_ty = cx.tcx.mk_projection(iter_item.def_id, substs) + && let Ok(item_ty) = cx.tcx.try_normalize_erasing_regions(cx.param_env, proj_ty) + { + item_ty == EarlyBinder(search_ty).subst(cx.tcx, cx.typeck_results().node_substs(call_id)) + } else { + false + } +} + struct IterFunction { func: IterFunctionKind, span: Span, diff --git a/clippy_lints/src/methods/option_as_ref_deref.rs b/clippy_lints/src/methods/option_as_ref_deref.rs index 742483e6b2e55..e6eb64bcbde64 100644 --- a/clippy_lints/src/methods/option_as_ref_deref.rs +++ b/clippy_lints/src/methods/option_as_ref_deref.rs @@ -13,8 +13,8 @@ use rustc_span::sym; use super::OPTION_AS_REF_DEREF; /// lint use of `_.as_ref().map(Deref::deref)` for `Option`s -pub(super) fn check<'tcx>( - cx: &LateContext<'tcx>, +pub(super) fn check( + cx: &LateContext<'_>, expr: &hir::Expr<'_>, as_ref_recv: &hir::Expr<'_>, map_arg: &hir::Expr<'_>, diff --git a/clippy_lints/src/methods/or_fun_call.rs b/clippy_lints/src/methods/or_fun_call.rs index 991d3dd538bf8..4460f38fcc18f 100644 --- a/clippy_lints/src/methods/or_fun_call.rs +++ b/clippy_lints/src/methods/or_fun_call.rs @@ -83,6 +83,8 @@ pub(super) fn check<'tcx>( method_span: Span, self_expr: &hir::Expr<'_>, arg: &'tcx hir::Expr<'_>, + // `Some` if fn has second argument + second_arg: Option<&hir::Expr<'_>>, span: Span, // None if lambda is required fun_span: Option, @@ -109,30 +111,40 @@ pub(super) fn check<'tcx>( if poss.contains(&name); then { - let macro_expanded_snipped; - let sugg: Cow<'_, str> = { + let sugg = { let (snippet_span, use_lambda) = match (fn_has_arguments, fun_span) { (false, Some(fun_span)) => (fun_span, false), _ => (arg.span, true), }; - let snippet = { - let not_macro_argument_snippet = snippet_with_macro_callsite(cx, snippet_span, ".."); - if not_macro_argument_snippet == "vec![]" { - macro_expanded_snipped = snippet(cx, snippet_span, ".."); + + let format_span = |span: Span| { + let not_macro_argument_snippet = snippet_with_macro_callsite(cx, span, ".."); + let snip = if not_macro_argument_snippet == "vec![]" { + let macro_expanded_snipped = snippet(cx, snippet_span, ".."); match macro_expanded_snipped.strip_prefix("$crate::vec::") { - Some(stripped) => Cow::from(stripped), + Some(stripped) => Cow::Owned(stripped.to_owned()), None => macro_expanded_snipped, } } else { not_macro_argument_snippet - } + }; + + snip.to_string() }; - if use_lambda { + let snip = format_span(snippet_span); + let snip = if use_lambda { let l_arg = if fn_has_arguments { "_" } else { "" }; - format!("|{l_arg}| {snippet}").into() + format!("|{l_arg}| {snip}") } else { - snippet + snip + }; + + if let Some(f) = second_arg { + let f = format_span(f.span); + format!("{snip}, {f}") + } else { + snip } }; let span_replace_word = method_span.with_hi(span.hi()); @@ -149,8 +161,8 @@ pub(super) fn check<'tcx>( } } - if let [arg] = args { - let inner_arg = if let hir::ExprKind::Block( + let extract_inner_arg = |arg: &'tcx hir::Expr<'_>| { + if let hir::ExprKind::Block( hir::Block { stmts: [], expr: Some(expr), @@ -162,19 +174,32 @@ pub(super) fn check<'tcx>( expr } else { arg - }; + } + }; + + if let [arg] = args { + let inner_arg = extract_inner_arg(arg); match inner_arg.kind { hir::ExprKind::Call(fun, or_args) => { let or_has_args = !or_args.is_empty(); if !check_unwrap_or_default(cx, name, fun, arg, or_has_args, expr.span, method_span) { let fun_span = if or_has_args { None } else { Some(fun.span) }; - check_general_case(cx, name, method_span, receiver, arg, expr.span, fun_span); + check_general_case(cx, name, method_span, receiver, arg, None, expr.span, fun_span); } }, hir::ExprKind::Index(..) | hir::ExprKind::MethodCall(..) => { - check_general_case(cx, name, method_span, receiver, arg, expr.span, None); + check_general_case(cx, name, method_span, receiver, arg, None, expr.span, None); }, _ => (), } } + + // `map_or` takes two arguments + if let [arg, lambda] = args { + let inner_arg = extract_inner_arg(arg); + if let hir::ExprKind::Call(fun, or_args) = inner_arg.kind { + let fun_span = if or_args.is_empty() { Some(fun.span) } else { None }; + check_general_case(cx, name, method_span, receiver, arg, Some(lambda), expr.span, fun_span); + } + } } diff --git a/clippy_lints/src/methods/seek_from_current.rs b/clippy_lints/src/methods/seek_from_current.rs new file mode 100644 index 0000000000000..361a3082f949a --- /dev/null +++ b/clippy_lints/src/methods/seek_from_current.rs @@ -0,0 +1,48 @@ +use rustc_ast::ast::{LitIntType, LitKind}; +use rustc_errors::Applicability; +use rustc_hir::{Expr, ExprKind}; +use rustc_lint::LateContext; + +use clippy_utils::{ + diagnostics::span_lint_and_sugg, get_trait_def_id, match_def_path, paths, source::snippet_with_applicability, + ty::implements_trait, +}; + +use super::SEEK_FROM_CURRENT; + +pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>, arg: &'tcx Expr<'_>) { + let ty = cx.typeck_results().expr_ty(recv); + + if let Some(def_id) = get_trait_def_id(cx, &paths::STD_IO_SEEK) { + if implements_trait(cx, ty, def_id, &[]) && arg_is_seek_from_current(cx, arg) { + let mut applicability = Applicability::MachineApplicable; + let snip = snippet_with_applicability(cx, recv.span, "..", &mut applicability); + + span_lint_and_sugg( + cx, + SEEK_FROM_CURRENT, + expr.span, + "using `SeekFrom::Current` to start from current position", + "replace with", + format!("{snip}.stream_position()"), + applicability, + ); + } + } +} + +fn arg_is_seek_from_current<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> bool { + if let ExprKind::Call(f, args) = expr.kind && + let ExprKind::Path(ref path) = f.kind && + let Some(def_id) = cx.qpath_res(path, f.hir_id).opt_def_id() && + match_def_path(cx, def_id, &paths::STD_IO_SEEK_FROM_CURRENT) { + // check if argument of `SeekFrom::Current` is `0` + if args.len() == 1 && + let ExprKind::Lit(ref lit) = args[0].kind && + let LitKind::Int(0, LitIntType::Unsuffixed) = lit.node { + return true + } + } + + false +} diff --git a/clippy_lints/src/methods/seek_to_start_instead_of_rewind.rs b/clippy_lints/src/methods/seek_to_start_instead_of_rewind.rs new file mode 100644 index 0000000000000..7e3bed1e41a94 --- /dev/null +++ b/clippy_lints/src/methods/seek_to_start_instead_of_rewind.rs @@ -0,0 +1,45 @@ +use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::ty::implements_trait; +use clippy_utils::{get_trait_def_id, match_def_path, paths}; +use rustc_ast::ast::{LitIntType, LitKind}; +use rustc_errors::Applicability; +use rustc_hir::{Expr, ExprKind}; +use rustc_lint::LateContext; +use rustc_span::Span; + +use super::SEEK_TO_START_INSTEAD_OF_REWIND; + +pub(super) fn check<'tcx>( + cx: &LateContext<'tcx>, + expr: &'tcx Expr<'_>, + recv: &'tcx Expr<'_>, + arg: &'tcx Expr<'_>, + name_span: Span, +) { + // Get receiver type + let ty = cx.typeck_results().expr_ty(recv).peel_refs(); + + if let Some(seek_trait_id) = get_trait_def_id(cx, &paths::STD_IO_SEEK) && + implements_trait(cx, ty, seek_trait_id, &[]) && + let ExprKind::Call(func, args1) = arg.kind && + let ExprKind::Path(ref path) = func.kind && + let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id() && + match_def_path(cx, def_id, &paths::STD_IO_SEEKFROM_START) && + args1.len() == 1 && + let ExprKind::Lit(ref lit) = args1[0].kind && + let LitKind::Int(0, LitIntType::Unsuffixed) = lit.node + { + let method_call_span = expr.span.with_lo(name_span.lo()); + span_lint_and_then( + cx, + SEEK_TO_START_INSTEAD_OF_REWIND, + method_call_span, + "used `seek` to go to the start of the stream", + |diag| { + let app = Applicability::MachineApplicable; + + diag.span_suggestion(method_call_span, "replace with", "rewind()", app); + }, + ); + } +} diff --git a/clippy_lints/src/methods/string_extend_chars.rs b/clippy_lints/src/methods/string_extend_chars.rs index 6f4cec546e969..f35d81cee8e97 100644 --- a/clippy_lints/src/methods/string_extend_chars.rs +++ b/clippy_lints/src/methods/string_extend_chars.rs @@ -18,7 +18,11 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr let target = &arglists[0].0; let self_ty = cx.typeck_results().expr_ty(target).peel_refs(); let ref_str = if *self_ty.kind() == ty::Str { - "" + if matches!(target.kind, hir::ExprKind::Index(..)) { + "&" + } else { + "" + } } else if is_type_lang_item(cx, self_ty, hir::LangItem::String) { "&" } else { diff --git a/clippy_lints/src/methods/suspicious_map.rs b/clippy_lints/src/methods/suspicious_map.rs index 851cdf544550f..2ac0786b37b1e 100644 --- a/clippy_lints/src/methods/suspicious_map.rs +++ b/clippy_lints/src/methods/suspicious_map.rs @@ -8,7 +8,7 @@ use rustc_span::sym; use super::SUSPICIOUS_MAP; -pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, count_recv: &hir::Expr<'_>, map_arg: &hir::Expr<'_>) { +pub fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, count_recv: &hir::Expr<'_>, map_arg: &hir::Expr<'_>) { if_chain! { if is_trait_method(cx, count_recv, sym::Iterator); let closure = expr_or_init(cx, map_arg); diff --git a/clippy_lints/src/methods/unnecessary_join.rs b/clippy_lints/src/methods/unnecessary_join.rs index c9b87bc6bf29d..087e1e4343b70 100644 --- a/clippy_lints/src/methods/unnecessary_join.rs +++ b/clippy_lints/src/methods/unnecessary_join.rs @@ -31,7 +31,7 @@ pub(super) fn check<'tcx>( cx, UNNECESSARY_JOIN, span.with_hi(expr.span.hi()), - r#"called `.collect>().join("")` on an iterator"#, + r#"called `.collect::>().join("")` on an iterator"#, "try using", "collect::()".to_owned(), applicability, diff --git a/clippy_lints/src/methods/unwrap_used.rs b/clippy_lints/src/methods/unwrap_used.rs index ee17f2d7889ee..90983f249cd59 100644 --- a/clippy_lints/src/methods/unwrap_used.rs +++ b/clippy_lints/src/methods/unwrap_used.rs @@ -1,6 +1,6 @@ use clippy_utils::diagnostics::span_lint_and_help; use clippy_utils::ty::is_type_diagnostic_item; -use clippy_utils::{is_in_test_function, is_lint_allowed}; +use clippy_utils::{is_in_cfg_test, is_lint_allowed}; use rustc_hir as hir; use rustc_lint::LateContext; use rustc_span::sym; @@ -18,16 +18,16 @@ pub(super) fn check( let obj_ty = cx.typeck_results().expr_ty(recv).peel_refs(); let mess = if is_type_diagnostic_item(cx, obj_ty, sym::Option) && !is_err { - Some((UNWRAP_USED, "an Option", "None", "")) + Some((UNWRAP_USED, "an `Option`", "None", "")) } else if is_type_diagnostic_item(cx, obj_ty, sym::Result) { - Some((UNWRAP_USED, "a Result", if is_err { "Ok" } else { "Err" }, "an ")) + Some((UNWRAP_USED, "a `Result`", if is_err { "Ok" } else { "Err" }, "an ")) } else { None }; let method_suffix = if is_err { "_err" } else { "" }; - if allow_unwrap_in_tests && is_in_test_function(cx.tcx, expr.hir_id) { + if allow_unwrap_in_tests && is_in_cfg_test(cx.tcx, expr.hir_id) { return; } @@ -45,7 +45,7 @@ pub(super) fn check( cx, lint, expr.span, - &format!("used `unwrap{method_suffix}()` on `{kind}` value"), + &format!("used `unwrap{method_suffix}()` on {kind} value"), None, &help, ); diff --git a/clippy_lints/src/missing_enforced_import_rename.rs b/clippy_lints/src/missing_enforced_import_rename.rs index 872679f25ab5a..4712846939e6a 100644 --- a/clippy_lints/src/missing_enforced_import_rename.rs +++ b/clippy_lints/src/missing_enforced_import_rename.rs @@ -59,7 +59,7 @@ impl LateLintPass<'_> for ImportRename { fn check_crate(&mut self, cx: &LateContext<'_>) { for Rename { path, rename } in &self.conf_renames { let segs = path.split("::").collect::>(); - if let Res::Def(_, id) = clippy_utils::def_path_res(cx, &segs, None) { + for id in clippy_utils::def_path_def_ids(cx, &segs) { self.renames.insert(id, Symbol::intern(rename)); } } diff --git a/clippy_lints/src/mixed_read_write_in_expression.rs b/clippy_lints/src/mixed_read_write_in_expression.rs index 6752976348f60..321fa4b7f9996 100644 --- a/clippy_lints/src/mixed_read_write_in_expression.rs +++ b/clippy_lints/src/mixed_read_write_in_expression.rs @@ -218,7 +218,7 @@ enum StopEarly { Stop, } -fn check_expr<'a, 'tcx>(vis: &mut ReadVisitor<'a, 'tcx>, expr: &'tcx Expr<'_>) -> StopEarly { +fn check_expr<'tcx>(vis: &mut ReadVisitor<'_, 'tcx>, expr: &'tcx Expr<'_>) -> StopEarly { if expr.hir_id == vis.last_expr.hir_id { return StopEarly::KeepGoing; } @@ -265,7 +265,7 @@ fn check_expr<'a, 'tcx>(vis: &mut ReadVisitor<'a, 'tcx>, expr: &'tcx Expr<'_>) - StopEarly::KeepGoing } -fn check_stmt<'a, 'tcx>(vis: &mut ReadVisitor<'a, 'tcx>, stmt: &'tcx Stmt<'_>) -> StopEarly { +fn check_stmt<'tcx>(vis: &mut ReadVisitor<'_, 'tcx>, stmt: &'tcx Stmt<'_>) -> StopEarly { match stmt.kind { StmtKind::Expr(expr) | StmtKind::Semi(expr) => check_expr(vis, expr), // If the declaration is of a local variable, check its initializer diff --git a/clippy_lints/src/mut_key.rs b/clippy_lints/src/mut_key.rs index 4b62dcdffe2fc..a651020ca6566 100644 --- a/clippy_lints/src/mut_key.rs +++ b/clippy_lints/src/mut_key.rs @@ -1,10 +1,11 @@ use clippy_utils::diagnostics::span_lint; -use clippy_utils::trait_ref_of_method; +use clippy_utils::{def_path_def_ids, trait_ref_of_method}; +use rustc_data_structures::fx::FxHashSet; use rustc_hir as hir; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty::TypeVisitable; use rustc_middle::ty::{Adt, Array, Ref, Slice, Tuple, Ty}; -use rustc_session::{declare_lint_pass, declare_tool_lint}; +use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::source_map::Span; use rustc_span::symbol::sym; use std::iter; @@ -78,26 +79,44 @@ declare_clippy_lint! { "Check for mutable `Map`/`Set` key type" } -declare_lint_pass!(MutableKeyType => [ MUTABLE_KEY_TYPE ]); +#[derive(Clone)] +pub struct MutableKeyType { + ignore_interior_mutability: Vec, + ignore_mut_def_ids: FxHashSet, +} + +impl_lint_pass!(MutableKeyType => [ MUTABLE_KEY_TYPE ]); impl<'tcx> LateLintPass<'tcx> for MutableKeyType { + fn check_crate(&mut self, cx: &LateContext<'tcx>) { + self.ignore_mut_def_ids.clear(); + let mut path = Vec::new(); + for ty in &self.ignore_interior_mutability { + path.extend(ty.split("::")); + for id in def_path_def_ids(cx, &path[..]) { + self.ignore_mut_def_ids.insert(id); + } + path.clear(); + } + } + fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) { if let hir::ItemKind::Fn(ref sig, ..) = item.kind { - check_sig(cx, item.hir_id(), sig.decl); + self.check_sig(cx, item.hir_id(), sig.decl); } } fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'tcx>) { if let hir::ImplItemKind::Fn(ref sig, ..) = item.kind { if trait_ref_of_method(cx, item.owner_id.def_id).is_none() { - check_sig(cx, item.hir_id(), sig.decl); + self.check_sig(cx, item.hir_id(), sig.decl); } } } fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'tcx>) { if let hir::TraitItemKind::Fn(ref sig, ..) = item.kind { - check_sig(cx, item.hir_id(), sig.decl); + self.check_sig(cx, item.hir_id(), sig.decl); } } @@ -105,73 +124,81 @@ impl<'tcx> LateLintPass<'tcx> for MutableKeyType { if let hir::PatKind::Wild = local.pat.kind { return; } - check_ty(cx, local.span, cx.typeck_results().pat_ty(local.pat)); + self.check_ty_(cx, local.span, cx.typeck_results().pat_ty(local.pat)); } } -fn check_sig<'tcx>(cx: &LateContext<'tcx>, item_hir_id: hir::HirId, decl: &hir::FnDecl<'_>) { - let fn_def_id = cx.tcx.hir().local_def_id(item_hir_id); - let fn_sig = cx.tcx.fn_sig(fn_def_id); - for (hir_ty, ty) in iter::zip(decl.inputs, fn_sig.inputs().skip_binder()) { - check_ty(cx, hir_ty.span, *ty); +impl MutableKeyType { + pub fn new(ignore_interior_mutability: Vec) -> Self { + Self { + ignore_interior_mutability, + ignore_mut_def_ids: FxHashSet::default(), + } } - check_ty(cx, decl.output.span(), cx.tcx.erase_late_bound_regions(fn_sig.output())); -} -// We want to lint 1. sets or maps with 2. not immutable key types and 3. no unerased -// generics (because the compiler cannot ensure immutability for unknown types). -fn check_ty<'tcx>(cx: &LateContext<'tcx>, span: Span, ty: Ty<'tcx>) { - let ty = ty.peel_refs(); - if let Adt(def, substs) = ty.kind() { - let is_keyed_type = [sym::HashMap, sym::BTreeMap, sym::HashSet, sym::BTreeSet] - .iter() - .any(|diag_item| cx.tcx.is_diagnostic_item(*diag_item, def.did())); - if is_keyed_type && is_interior_mutable_type(cx, substs.type_at(0), span) { - span_lint(cx, MUTABLE_KEY_TYPE, span, "mutable key type"); + fn check_sig(&self, cx: &LateContext<'_>, item_hir_id: hir::HirId, decl: &hir::FnDecl<'_>) { + let fn_def_id = cx.tcx.hir().local_def_id(item_hir_id); + let fn_sig = cx.tcx.fn_sig(fn_def_id); + for (hir_ty, ty) in iter::zip(decl.inputs, fn_sig.inputs().skip_binder()) { + self.check_ty_(cx, hir_ty.span, *ty); } + self.check_ty_(cx, decl.output.span(), cx.tcx.erase_late_bound_regions(fn_sig.output())); } -} -/// Determines if a type contains interior mutability which would affect its implementation of -/// [`Hash`] or [`Ord`]. -fn is_interior_mutable_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, span: Span) -> bool { - match *ty.kind() { - Ref(_, inner_ty, mutbl) => { - mutbl == hir::Mutability::Mut || is_interior_mutable_type(cx, inner_ty, span) - } - Slice(inner_ty) => is_interior_mutable_type(cx, inner_ty, span), - Array(inner_ty, size) => { - size.try_eval_usize(cx.tcx, cx.param_env).map_or(true, |u| u != 0) - && is_interior_mutable_type(cx, inner_ty, span) - } - Tuple(fields) => fields.iter().any(|ty| is_interior_mutable_type(cx, ty, span)), - Adt(def, substs) => { - // Special case for collections in `std` who's impl of `Hash` or `Ord` delegates to - // that of their type parameters. Note: we don't include `HashSet` and `HashMap` - // because they have no impl for `Hash` or `Ord`. - let is_std_collection = [ - sym::Option, - sym::Result, - sym::LinkedList, - sym::Vec, - sym::VecDeque, - sym::BTreeMap, - sym::BTreeSet, - sym::Rc, - sym::Arc, - ] - .iter() - .any(|diag_item| cx.tcx.is_diagnostic_item(*diag_item, def.did())); - let is_box = Some(def.did()) == cx.tcx.lang_items().owned_box(); - if is_std_collection || is_box { - // The type is mutable if any of its type parameters are - substs.types().any(|ty| is_interior_mutable_type(cx, ty, span)) - } else { - !ty.has_escaping_bound_vars() - && cx.tcx.layout_of(cx.param_env.and(ty)).is_ok() - && !ty.is_freeze(cx.tcx, cx.param_env) + // We want to lint 1. sets or maps with 2. not immutable key types and 3. no unerased + // generics (because the compiler cannot ensure immutability for unknown types). + fn check_ty_<'tcx>(&self, cx: &LateContext<'tcx>, span: Span, ty: Ty<'tcx>) { + let ty = ty.peel_refs(); + if let Adt(def, substs) = ty.kind() { + let is_keyed_type = [sym::HashMap, sym::BTreeMap, sym::HashSet, sym::BTreeSet] + .iter() + .any(|diag_item| cx.tcx.is_diagnostic_item(*diag_item, def.did())); + if is_keyed_type && self.is_interior_mutable_type(cx, substs.type_at(0)) { + span_lint(cx, MUTABLE_KEY_TYPE, span, "mutable key type"); } } - _ => false, + } + + /// Determines if a type contains interior mutability which would affect its implementation of + /// [`Hash`] or [`Ord`]. + fn is_interior_mutable_type<'tcx>(&self, cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool { + match *ty.kind() { + Ref(_, inner_ty, mutbl) => mutbl == hir::Mutability::Mut || self.is_interior_mutable_type(cx, inner_ty), + Slice(inner_ty) => self.is_interior_mutable_type(cx, inner_ty), + Array(inner_ty, size) => { + size.try_eval_usize(cx.tcx, cx.param_env).map_or(true, |u| u != 0) + && self.is_interior_mutable_type(cx, inner_ty) + }, + Tuple(fields) => fields.iter().any(|ty| self.is_interior_mutable_type(cx, ty)), + Adt(def, substs) => { + // Special case for collections in `std` who's impl of `Hash` or `Ord` delegates to + // that of their type parameters. Note: we don't include `HashSet` and `HashMap` + // because they have no impl for `Hash` or `Ord`. + let def_id = def.did(); + let is_std_collection = [ + sym::Option, + sym::Result, + sym::LinkedList, + sym::Vec, + sym::VecDeque, + sym::BTreeMap, + sym::BTreeSet, + sym::Rc, + sym::Arc, + ] + .iter() + .any(|diag_item| cx.tcx.is_diagnostic_item(*diag_item, def_id)); + let is_box = Some(def_id) == cx.tcx.lang_items().owned_box(); + if is_std_collection || is_box || self.ignore_mut_def_ids.contains(&def_id) { + // The type is mutable if any of its type parameters are + substs.types().any(|ty| self.is_interior_mutable_type(cx, ty)) + } else { + !ty.has_escaping_bound_vars() + && cx.tcx.layout_of(cx.param_env.and(ty)).is_ok() + && !ty.is_freeze(cx.tcx, cx.param_env) + } + }, + _ => false, + } } } diff --git a/clippy_lints/src/mut_mut.rs b/clippy_lints/src/mut_mut.rs index cb16f00047a39..bc90e131b7f3b 100644 --- a/clippy_lints/src/mut_mut.rs +++ b/clippy_lints/src/mut_mut.rs @@ -68,13 +68,15 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for MutVisitor<'a, 'tcx> { expr.span, "generally you want to avoid `&mut &mut _` if possible", ); - } else if let ty::Ref(_, _, hir::Mutability::Mut) = self.cx.typeck_results().expr_ty(e).kind() { - span_lint( - self.cx, - MUT_MUT, - expr.span, - "this expression mutably borrows a mutable reference. Consider reborrowing", - ); + } else if let ty::Ref(_, ty, hir::Mutability::Mut) = self.cx.typeck_results().expr_ty(e).kind() { + if ty.peel_refs().is_sized(self.cx.tcx, self.cx.param_env) { + span_lint( + self.cx, + MUT_MUT, + expr.span, + "this expression mutably borrows a mutable reference. Consider reborrowing", + ); + } } } } diff --git a/clippy_lints/src/needless_borrowed_ref.rs b/clippy_lints/src/needless_borrowed_ref.rs index 10c3ff026b6d6..498e1408e52a0 100644 --- a/clippy_lints/src/needless_borrowed_ref.rs +++ b/clippy_lints/src/needless_borrowed_ref.rs @@ -36,14 +36,14 @@ declare_clippy_lint! { declare_lint_pass!(NeedlessBorrowedRef => [NEEDLESS_BORROWED_REFERENCE]); impl<'tcx> LateLintPass<'tcx> for NeedlessBorrowedRef { - fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) { - if pat.span.from_expansion() { + fn check_pat(&mut self, cx: &LateContext<'tcx>, ref_pat: &'tcx Pat<'_>) { + if ref_pat.span.from_expansion() { // OK, simple enough, lints doesn't check in macro. return; } // Do not lint patterns that are part of an OR `|` pattern, the binding mode must match in all arms - for (_, node) in cx.tcx.hir().parent_iter(pat.hir_id) { + for (_, node) in cx.tcx.hir().parent_iter(ref_pat.hir_id) { let Node::Pat(pat) = node else { break }; if matches!(pat.kind, PatKind::Or(_)) { @@ -52,20 +52,20 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrowedRef { } // Only lint immutable refs, because `&mut ref T` may be useful. - let PatKind::Ref(sub_pat, Mutability::Not) = pat.kind else { return }; + let PatKind::Ref(pat, Mutability::Not) = ref_pat.kind else { return }; - match sub_pat.kind { + match pat.kind { // Check sub_pat got a `ref` keyword (excluding `ref mut`). PatKind::Binding(BindingAnnotation::REF, _, ident, None) => { span_lint_and_then( cx, NEEDLESS_BORROWED_REFERENCE, - pat.span, + ref_pat.span, "this pattern takes a reference on something that is being dereferenced", |diag| { // `&ref ident` // ^^^^^ - let span = pat.span.until(ident.span); + let span = ref_pat.span.until(ident.span); diag.span_suggestion_verbose( span, "try removing the `&ref` part", @@ -84,41 +84,71 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrowedRef { }), after, ) => { - let mut suggestions = Vec::new(); - - for element_pat in itertools::chain(before, after) { - if let PatKind::Binding(BindingAnnotation::REF, _, ident, None) = element_pat.kind { - // `&[..., ref ident, ...]` - // ^^^^ - let span = element_pat.span.until(ident.span); - suggestions.push((span, String::new())); - } else { - return; - } - } + check_subpatterns( + cx, + "dereferencing a slice pattern where every element takes a reference", + ref_pat, + pat, + itertools::chain(before, after), + ); + }, + PatKind::Tuple(subpatterns, _) | PatKind::TupleStruct(_, subpatterns, _) => { + check_subpatterns( + cx, + "dereferencing a tuple pattern where every element takes a reference", + ref_pat, + pat, + subpatterns, + ); + }, + PatKind::Struct(_, fields, _) => { + check_subpatterns( + cx, + "dereferencing a struct pattern where every field's pattern takes a reference", + ref_pat, + pat, + fields.iter().map(|field| field.pat), + ); + }, + _ => {}, + } + } +} - if !suggestions.is_empty() { - span_lint_and_then( - cx, - NEEDLESS_BORROWED_REFERENCE, - pat.span, - "dereferencing a slice pattern where every element takes a reference", - |diag| { - // `&[...]` - // ^ - let span = pat.span.until(sub_pat.span); - suggestions.push((span, String::new())); +fn check_subpatterns<'tcx>( + cx: &LateContext<'tcx>, + message: &str, + ref_pat: &Pat<'_>, + pat: &Pat<'_>, + subpatterns: impl IntoIterator>, +) { + let mut suggestions = Vec::new(); - diag.multipart_suggestion( - "try removing the `&` and `ref` parts", - suggestions, - Applicability::MachineApplicable, - ); - }, - ); - } + for subpattern in subpatterns { + match subpattern.kind { + PatKind::Binding(BindingAnnotation::REF, _, ident, None) => { + // `ref ident` + // ^^^^ + let span = subpattern.span.until(ident.span); + suggestions.push((span, String::new())); }, - _ => {}, + PatKind::Wild => {}, + _ => return, } } + + if !suggestions.is_empty() { + span_lint_and_then(cx, NEEDLESS_BORROWED_REFERENCE, ref_pat.span, message, |diag| { + // `&pat` + // ^ + let span = ref_pat.span.until(pat.span); + suggestions.push((span, String::new())); + + diag.multipart_suggestion( + "try removing the `&` and `ref` parts", + suggestions, + Applicability::MachineApplicable, + ); + }); + } } diff --git a/clippy_lints/src/needless_continue.rs b/clippy_lints/src/needless_continue.rs index 6f0e755466e5a..38a75034cd314 100644 --- a/clippy_lints/src/needless_continue.rs +++ b/clippy_lints/src/needless_continue.rs @@ -287,7 +287,7 @@ const DROP_ELSE_BLOCK_MSG: &str = "consider dropping the `else` clause"; const DROP_CONTINUE_EXPRESSION_MSG: &str = "consider dropping the `continue` expression"; -fn emit_warning<'a>(cx: &EarlyContext<'_>, data: &'a LintData<'_>, header: &str, typ: LintType) { +fn emit_warning(cx: &EarlyContext<'_>, data: &LintData<'_>, header: &str, typ: LintType) { // snip is the whole *help* message that appears after the warning. // message is the warning message. // expr is the expression which the lint warning message refers to. @@ -313,7 +313,7 @@ fn emit_warning<'a>(cx: &EarlyContext<'_>, data: &'a LintData<'_>, header: &str, ); } -fn suggestion_snippet_for_continue_inside_if<'a>(cx: &EarlyContext<'_>, data: &'a LintData<'_>) -> String { +fn suggestion_snippet_for_continue_inside_if(cx: &EarlyContext<'_>, data: &LintData<'_>) -> String { let cond_code = snippet(cx, data.if_cond.span, ".."); let continue_code = snippet_block(cx, data.if_block.span, "..", Some(data.if_expr.span)); @@ -327,7 +327,7 @@ fn suggestion_snippet_for_continue_inside_if<'a>(cx: &EarlyContext<'_>, data: &' ) } -fn suggestion_snippet_for_continue_inside_else<'a>(cx: &EarlyContext<'_>, data: &'a LintData<'_>) -> String { +fn suggestion_snippet_for_continue_inside_else(cx: &EarlyContext<'_>, data: &LintData<'_>) -> String { let cond_code = snippet(cx, data.if_cond.span, ".."); // Region B @@ -361,7 +361,7 @@ fn suggestion_snippet_for_continue_inside_else<'a>(cx: &EarlyContext<'_>, data: ) } -fn check_and_warn<'a>(cx: &EarlyContext<'_>, expr: &'a ast::Expr) { +fn check_and_warn(cx: &EarlyContext<'_>, expr: &ast::Expr) { if_chain! { if let ast::ExprKind::Loop(loop_block, ..) = &expr.kind; if let Some(last_stmt) = loop_block.stmts.last(); diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index 79aa15b06ef4d..2ef902965f66d 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -340,11 +340,5 @@ impl<'tcx> euv::Delegate<'tcx> for MovedVariablesCtxt { fn mutate(&mut self, _: &euv::PlaceWithHirId<'tcx>, _: HirId) {} - fn fake_read( - &mut self, - _: &rustc_hir_typeck::expr_use_visitor::PlaceWithHirId<'tcx>, - _: FakeReadCause, - _: HirId, - ) { - } + fn fake_read(&mut self, _: &rustc_hir_typeck::expr_use_visitor::PlaceWithHirId<'tcx>, _: FakeReadCause, _: HirId) {} } diff --git a/clippy_lints/src/octal_escapes.rs b/clippy_lints/src/octal_escapes.rs index 2a7159764e463..ae0a41db918a3 100644 --- a/clippy_lints/src/octal_escapes.rs +++ b/clippy_lints/src/octal_escapes.rs @@ -58,9 +58,9 @@ impl EarlyLintPass for OctalEscapes { if let ExprKind::Lit(token_lit) = &expr.kind { if matches!(token_lit.kind, LitKind::Str) { - check_lit(cx, &token_lit, expr.span, true); + check_lit(cx, token_lit, expr.span, true); } else if matches!(token_lit.kind, LitKind::ByteStr) { - check_lit(cx, &token_lit, expr.span, false); + check_lit(cx, token_lit, expr.span, false); } } } diff --git a/clippy_lints/src/operators/arithmetic_side_effects.rs b/clippy_lints/src/operators/arithmetic_side_effects.rs index 8827daaa3ee7c..20b82d81a2aeb 100644 --- a/clippy_lints/src/operators/arithmetic_side_effects.rs +++ b/clippy_lints/src/operators/arithmetic_side_effects.rs @@ -1,5 +1,9 @@ use super::ARITHMETIC_SIDE_EFFECTS; -use clippy_utils::{consts::constant_simple, diagnostics::span_lint}; +use clippy_utils::{ + consts::{constant, constant_simple}, + diagnostics::span_lint, + peel_hir_expr_refs, +}; use rustc_ast as ast; use rustc_data_structures::fx::FxHashSet; use rustc_hir as hir; @@ -38,24 +42,6 @@ impl ArithmeticSideEffects { } } - /// Assuming that `expr` is a literal integer, checks operators (+=, -=, *, /) in a - /// non-constant environment that won't overflow. - fn has_valid_op(op: &Spanned, expr: &hir::Expr<'_>) -> bool { - if let hir::ExprKind::Lit(ref lit) = expr.kind && - let ast::LitKind::Int(value, _) = lit.node - { - match (&op.node, value) { - (hir::BinOpKind::Div | hir::BinOpKind::Rem, 0) => false, - (hir::BinOpKind::Add | hir::BinOpKind::Sub, 0) - | (hir::BinOpKind::Div | hir::BinOpKind::Rem, _) - | (hir::BinOpKind::Mul, 0 | 1) => true, - _ => false, - } - } else { - false - } - } - /// Checks if the given `expr` has any of the inner `allowed` elements. fn is_allowed_ty(&self, ty: Ty<'_>) -> bool { self.allowed @@ -74,15 +60,14 @@ impl ArithmeticSideEffects { self.expr_span = Some(expr.span); } - /// If `expr` does not match any variant of `LiteralIntegerTy`, returns `None`. - fn literal_integer<'expr, 'tcx>(expr: &'expr hir::Expr<'tcx>) -> Option> { - if matches!(expr.kind, hir::ExprKind::Lit(_)) { - return Some(LiteralIntegerTy::Value(expr)); + /// If `expr` is not a literal integer like `1`, returns `None`. + fn literal_integer(expr: &hir::Expr<'_>) -> Option { + if let hir::ExprKind::Lit(ref lit) = expr.kind && let ast::LitKind::Int(n, _) = lit.node { + Some(n) } - if let hir::ExprKind::AddrOf(.., inn) = expr.kind && let hir::ExprKind::Lit(_) = inn.kind { - return Some(LiteralIntegerTy::Ref(inn)); + else { + None } - None } /// Manages when the lint should be triggered. Operations in constant environments, hard coded @@ -117,10 +102,20 @@ impl ArithmeticSideEffects { return; } let has_valid_op = if Self::is_integral(lhs_ty) && Self::is_integral(rhs_ty) { - match (Self::literal_integer(lhs), Self::literal_integer(rhs)) { - (None, Some(lit_int_ty)) | (Some(lit_int_ty), None) => Self::has_valid_op(op, lit_int_ty.into()), - (Some(LiteralIntegerTy::Value(_)), Some(LiteralIntegerTy::Value(_))) => true, - (None, None) | (Some(_), Some(_)) => false, + let (actual_lhs, lhs_ref_counter) = peel_hir_expr_refs(lhs); + let (actual_rhs, rhs_ref_counter) = peel_hir_expr_refs(rhs); + match (Self::literal_integer(actual_lhs), Self::literal_integer(actual_rhs)) { + (None, None) => false, + (None, Some(n)) | (Some(n), None) => match (&op.node, n) { + (hir::BinOpKind::Div | hir::BinOpKind::Rem, 0) => false, + (hir::BinOpKind::Add | hir::BinOpKind::Sub, 0) + | (hir::BinOpKind::Div | hir::BinOpKind::Rem, _) + | (hir::BinOpKind::Mul, 0 | 1) => true, + _ => false, + }, + (Some(_), Some(_)) => { + matches!((lhs_ref_counter, rhs_ref_counter), (0, 0)) + }, } } else { false @@ -129,21 +124,45 @@ impl ArithmeticSideEffects { self.issue_lint(cx, expr); } } + + fn manage_unary_ops<'tcx>( + &mut self, + cx: &LateContext<'tcx>, + expr: &hir::Expr<'tcx>, + un_expr: &hir::Expr<'tcx>, + un_op: hir::UnOp, + ) { + let hir::UnOp::Neg = un_op else { return; }; + if constant(cx, cx.typeck_results(), un_expr).is_some() { + return; + } + let ty = cx.typeck_results().expr_ty(expr).peel_refs(); + if self.is_allowed_ty(ty) { + return; + } + let actual_un_expr = peel_hir_expr_refs(un_expr).0; + if Self::literal_integer(actual_un_expr).is_some() { + return; + } + self.issue_lint(cx, expr); + } + + fn should_skip_expr(&mut self, expr: &hir::Expr<'_>) -> bool { + self.expr_span.is_some() || self.const_span.map_or(false, |sp| sp.contains(expr.span)) + } } impl<'tcx> LateLintPass<'tcx> for ArithmeticSideEffects { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &hir::Expr<'tcx>) { - if self.expr_span.is_some() || self.const_span.map_or(false, |sp| sp.contains(expr.span)) { + if self.should_skip_expr(expr) { return; } match &expr.kind { - hir::ExprKind::Binary(op, lhs, rhs) | hir::ExprKind::AssignOp(op, lhs, rhs) => { + hir::ExprKind::AssignOp(op, lhs, rhs) | hir::ExprKind::Binary(op, lhs, rhs) => { self.manage_bin_ops(cx, expr, op, lhs, rhs); }, - hir::ExprKind::Unary(hir::UnOp::Neg, _) => { - if constant_simple(cx, cx.typeck_results(), expr).is_none() { - self.issue_lint(cx, expr); - } + hir::ExprKind::Unary(un_op, un_expr) => { + self.manage_unary_ops(cx, expr, un_expr, *un_op); }, _ => {}, } @@ -177,22 +196,3 @@ impl<'tcx> LateLintPass<'tcx> for ArithmeticSideEffects { } } } - -/// Tells if an expression is a integer declared by value or by reference. -/// -/// If `LiteralIntegerTy::Ref`, then the contained value will be `hir::ExprKind::Lit` rather -/// than `hirExprKind::Addr`. -enum LiteralIntegerTy<'expr, 'tcx> { - /// For example, `&199` - Ref(&'expr hir::Expr<'tcx>), - /// For example, `1` or `i32::MAX` - Value(&'expr hir::Expr<'tcx>), -} - -impl<'expr, 'tcx> From> for &'expr hir::Expr<'tcx> { - fn from(from: LiteralIntegerTy<'expr, 'tcx>) -> Self { - match from { - LiteralIntegerTy::Ref(elem) | LiteralIntegerTy::Value(elem) => elem, - } - } -} diff --git a/clippy_lints/src/operators/op_ref.rs b/clippy_lints/src/operators/op_ref.rs index 71b31b5e4a562..d7917e86a861f 100644 --- a/clippy_lints/src/operators/op_ref.rs +++ b/clippy_lints/src/operators/op_ref.rs @@ -199,7 +199,7 @@ fn in_impl<'tcx>( } } -fn are_equal<'tcx>(cx: &LateContext<'tcx>, middle_ty: Ty<'_>, hir_ty: &rustc_hir::Ty<'_>) -> bool { +fn are_equal(cx: &LateContext<'_>, middle_ty: Ty<'_>, hir_ty: &rustc_hir::Ty<'_>) -> bool { if_chain! { if let ty::Adt(adt_def, _) = middle_ty.kind(); if let Some(local_did) = adt_def.did().as_local(); diff --git a/clippy_lints/src/option_if_let_else.rs b/clippy_lints/src/option_if_let_else.rs index 4eb42da1fed02..472f52380bbf4 100644 --- a/clippy_lints/src/option_if_let_else.rs +++ b/clippy_lints/src/option_if_let_else.rs @@ -213,11 +213,14 @@ fn try_convert_match<'tcx>( cx: &LateContext<'tcx>, arms: &[Arm<'tcx>], ) -> Option<(&'tcx Pat<'tcx>, &'tcx Expr<'tcx>, &'tcx Expr<'tcx>)> { - if arms.len() == 2 { - return if is_none_or_err_arm(cx, &arms[1]) { - Some((arms[0].pat, arms[0].body, arms[1].body)) - } else if is_none_or_err_arm(cx, &arms[0]) { - Some((arms[1].pat, arms[1].body, arms[0].body)) + if let [first_arm, second_arm] = arms + && first_arm.guard.is_none() + && second_arm.guard.is_none() + { + return if is_none_or_err_arm(cx, second_arm) { + Some((first_arm.pat, first_arm.body, second_arm.body)) + } else if is_none_or_err_arm(cx, first_arm) { + Some((second_arm.pat, second_arm.body, first_arm.body)) } else { None }; diff --git a/clippy_lints/src/partialeq_to_none.rs b/clippy_lints/src/partialeq_to_none.rs index 6810a24317589..456ded3fc0268 100644 --- a/clippy_lints/src/partialeq_to_none.rs +++ b/clippy_lints/src/partialeq_to_none.rs @@ -33,7 +33,7 @@ declare_clippy_lint! { /// if f.is_some() { "yay" } else { "nay" } /// } /// ``` - #[clippy::version = "1.64.0"] + #[clippy::version = "1.65.0"] pub PARTIALEQ_TO_NONE, style, "Binary comparison to `Option::None` relies on `T: PartialEq`, which is unneeded" diff --git a/clippy_lints/src/pattern_type_mismatch.rs b/clippy_lints/src/pattern_type_mismatch.rs index a4d265111f9ae..97b5a4ce36413 100644 --- a/clippy_lints/src/pattern_type_mismatch.rs +++ b/clippy_lints/src/pattern_type_mismatch.rs @@ -130,7 +130,7 @@ enum DerefPossible { Impossible, } -fn apply_lint<'tcx>(cx: &LateContext<'tcx>, pat: &Pat<'_>, deref_possible: DerefPossible) -> bool { +fn apply_lint(cx: &LateContext<'_>, pat: &Pat<'_>, deref_possible: DerefPossible) -> bool { let maybe_mismatch = find_first_mismatch(cx, pat); if let Some((span, mutability, level)) = maybe_mismatch { span_lint_and_help( @@ -163,7 +163,7 @@ enum Level { Lower, } -fn find_first_mismatch<'tcx>(cx: &LateContext<'tcx>, pat: &Pat<'_>) -> Option<(Span, Mutability, Level)> { +fn find_first_mismatch(cx: &LateContext<'_>, pat: &Pat<'_>) -> Option<(Span, Mutability, Level)> { let mut result = None; pat.walk(|p| { if result.is_some() { diff --git a/clippy_lints/src/ptr_offset_with_cast.rs b/clippy_lints/src/ptr_offset_with_cast.rs index 72dda67c72b25..47b8891e12302 100644 --- a/clippy_lints/src/ptr_offset_with_cast.rs +++ b/clippy_lints/src/ptr_offset_with_cast.rs @@ -105,17 +105,17 @@ fn expr_as_ptr_offset_call<'tcx>( } // Is the type of the expression a usize? -fn is_expr_ty_usize<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) -> bool { +fn is_expr_ty_usize(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { cx.typeck_results().expr_ty(expr) == cx.tcx.types.usize } // Is the type of the expression a raw pointer? -fn is_expr_ty_raw_ptr<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) -> bool { +fn is_expr_ty_raw_ptr(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { cx.typeck_results().expr_ty(expr).is_unsafe_ptr() } -fn build_suggestion<'tcx>( - cx: &LateContext<'tcx>, +fn build_suggestion( + cx: &LateContext<'_>, method: Method, receiver_expr: &Expr<'_>, cast_lhs_expr: &Expr<'_>, diff --git a/clippy_lints/src/question_mark.rs b/clippy_lints/src/question_mark.rs index bb86fb3b7d42f..5269bbd1f1acc 100644 --- a/clippy_lints/src/question_mark.rs +++ b/clippy_lints/src/question_mark.rs @@ -189,6 +189,7 @@ fn is_early_return(smbl: Symbol, cx: &LateContext<'_>, if_block: &IfBlockType<'_ && expr_return_none_or_err(smbl, cx, if_else.unwrap(), let_expr, Some(let_pat_sym))) || is_res_lang_ctor(cx, res, ResultErr) && expr_return_none_or_err(smbl, cx, if_then, let_expr, Some(let_pat_sym)) + && if_else.is_none() }, _ => false, } diff --git a/clippy_lints/src/redundant_closure_call.rs b/clippy_lints/src/redundant_closure_call.rs index 4cbe9597c5393..8e675d34a1836 100644 --- a/clippy_lints/src/redundant_closure_call.rs +++ b/clippy_lints/src/redundant_closure_call.rs @@ -105,8 +105,8 @@ impl EarlyLintPass for RedundantClosureCall { impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall { fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx hir::Block<'_>) { - fn count_closure_usage<'a, 'tcx>( - cx: &'a LateContext<'tcx>, + fn count_closure_usage<'tcx>( + cx: &LateContext<'tcx>, block: &'tcx hir::Block<'_>, path: &'tcx hir::Path<'tcx>, ) -> usize { diff --git a/clippy_lints/src/redundant_pub_crate.rs b/clippy_lints/src/redundant_pub_crate.rs index 26075e9f70faa..833dc4913b469 100644 --- a/clippy_lints/src/redundant_pub_crate.rs +++ b/clippy_lints/src/redundant_pub_crate.rs @@ -70,7 +70,8 @@ impl<'tcx> LateLintPass<'tcx> for RedundantPubCrate { } if let ItemKind::Mod { .. } = item.kind { - self.is_exported.push(cx.effective_visibilities.is_exported(item.owner_id.def_id)); + self.is_exported + .push(cx.effective_visibilities.is_exported(item.owner_id.def_id)); } } diff --git a/clippy_lints/src/renamed_lints.rs b/clippy_lints/src/renamed_lints.rs index 76d6ad0b23e6a..8e214218f23ae 100644 --- a/clippy_lints/src/renamed_lints.rs +++ b/clippy_lints/src/renamed_lints.rs @@ -11,8 +11,6 @@ pub static RENAMED_LINTS: &[(&str, &str)] = &[ ("clippy::disallowed_method", "clippy::disallowed_methods"), ("clippy::disallowed_type", "clippy::disallowed_types"), ("clippy::eval_order_dependence", "clippy::mixed_read_write_in_expression"), - ("clippy::for_loop_over_option", "for_loops_over_fallibles"), - ("clippy::for_loop_over_result", "for_loops_over_fallibles"), ("clippy::identity_conversion", "clippy::useless_conversion"), ("clippy::if_let_some_result", "clippy::match_result_ok"), ("clippy::logic_bug", "clippy::overly_complex_bool_expr"), @@ -31,10 +29,13 @@ pub static RENAMED_LINTS: &[(&str, &str)] = &[ ("clippy::to_string_in_display", "clippy::recursive_format_impl"), ("clippy::zero_width_space", "clippy::invisible_characters"), ("clippy::drop_bounds", "drop_bounds"), + ("clippy::for_loop_over_option", "for_loops_over_fallibles"), + ("clippy::for_loop_over_result", "for_loops_over_fallibles"), ("clippy::for_loops_over_fallibles", "for_loops_over_fallibles"), ("clippy::into_iter_on_array", "array_into_iter"), ("clippy::invalid_atomic_ordering", "invalid_atomic_ordering"), ("clippy::invalid_ref", "invalid_value"), + ("clippy::let_underscore_drop", "let_underscore_drop"), ("clippy::mem_discriminant_non_enum", "enum_intrinsics_non_enums"), ("clippy::panic_params", "non_fmt_panics"), ("clippy::positional_named_format_parameters", "named_arguments_used_positionally"), diff --git a/clippy_lints/src/single_component_path_imports.rs b/clippy_lints/src/single_component_path_imports.rs index 66b79513032f6..2036e85db7e8c 100644 --- a/clippy_lints/src/single_component_path_imports.rs +++ b/clippy_lints/src/single_component_path_imports.rs @@ -1,8 +1,9 @@ use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg}; +use rustc_ast::node_id::{NodeId, NodeMap}; use rustc_ast::{ptr::P, Crate, Item, ItemKind, MacroDef, ModKind, UseTreeKind}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; -use rustc_session::{declare_lint_pass, declare_tool_lint}; +use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::{edition::Edition, symbol::kw, Span, Symbol}; declare_clippy_lint! { @@ -33,51 +34,32 @@ declare_clippy_lint! { "imports with single component path are redundant" } -declare_lint_pass!(SingleComponentPathImports => [SINGLE_COMPONENT_PATH_IMPORTS]); +#[derive(Default)] +pub struct SingleComponentPathImports { + /// Buffer found usages to emit when visiting that item so that `#[allow]` works as expected + found: NodeMap>, +} + +struct SingleUse { + name: Symbol, + span: Span, + item_id: NodeId, + can_suggest: bool, +} + +impl_lint_pass!(SingleComponentPathImports => [SINGLE_COMPONENT_PATH_IMPORTS]); impl EarlyLintPass for SingleComponentPathImports { fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) { if cx.sess().opts.edition < Edition::Edition2018 { return; } - check_mod(cx, &krate.items); - } -} -fn check_mod(cx: &EarlyContext<'_>, items: &[P]) { - // keep track of imports reused with `self` keyword, - // such as `self::crypto_hash` in the example below - // ```rust,ignore - // use self::crypto_hash::{Algorithm, Hasher}; - // ``` - let mut imports_reused_with_self = Vec::new(); - - // keep track of single use statements - // such as `crypto_hash` in the example below - // ```rust,ignore - // use crypto_hash; - // ``` - let mut single_use_usages = Vec::new(); - - // keep track of macros defined in the module as we don't want it to trigger on this (#7106) - // ```rust,ignore - // macro_rules! foo { () => {} }; - // pub(crate) use foo; - // ``` - let mut macros = Vec::new(); - - for item in items { - track_uses( - cx, - item, - &mut imports_reused_with_self, - &mut single_use_usages, - &mut macros, - ); + self.check_mod(cx, &krate.items); } - for (name, span, can_suggest) in single_use_usages { - if !imports_reused_with_self.contains(&name) { + fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { + for SingleUse { span, can_suggest, .. } in self.found.remove(&item.id).into_iter().flatten() { if can_suggest { span_lint_and_sugg( cx, @@ -102,74 +84,127 @@ fn check_mod(cx: &EarlyContext<'_>, items: &[P]) { } } -fn track_uses( - cx: &EarlyContext<'_>, - item: &Item, - imports_reused_with_self: &mut Vec, - single_use_usages: &mut Vec<(Symbol, Span, bool)>, - macros: &mut Vec, -) { - if item.span.from_expansion() || item.vis.kind.is_pub() { - return; - } +impl SingleComponentPathImports { + fn check_mod(&mut self, cx: &EarlyContext<'_>, items: &[P]) { + // keep track of imports reused with `self` keyword, such as `self::crypto_hash` in the example + // below. Removing the `use crypto_hash;` would make this a compile error + // ``` + // use crypto_hash; + // + // use self::crypto_hash::{Algorithm, Hasher}; + // ``` + let mut imports_reused_with_self = Vec::new(); - match &item.kind { - ItemKind::Mod(_, ModKind::Loaded(ref items, ..)) => { - check_mod(cx, items); - }, - ItemKind::MacroDef(MacroDef { macro_rules: true, .. }) => { - macros.push(item.ident.name); - }, - ItemKind::Use(use_tree) => { - let segments = &use_tree.prefix.segments; - - // keep track of `use some_module;` usages - if segments.len() == 1 { - if let UseTreeKind::Simple(None, _, _) = use_tree.kind { - let name = segments[0].ident.name; - if !macros.contains(&name) { - single_use_usages.push((name, item.span, true)); - } - } - return; + // keep track of single use statements such as `crypto_hash` in the example below + // ``` + // use crypto_hash; + // ``` + let mut single_use_usages = Vec::new(); + + // keep track of macros defined in the module as we don't want it to trigger on this (#7106) + // ``` + // macro_rules! foo { () => {} }; + // pub(crate) use foo; + // ``` + let mut macros = Vec::new(); + + for item in items { + self.track_uses( + cx, + item, + &mut imports_reused_with_self, + &mut single_use_usages, + &mut macros, + ); + } + + for usage in single_use_usages { + if !imports_reused_with_self.contains(&usage.name) { + self.found.entry(usage.item_id).or_default().push(usage); } + } + } - if segments.is_empty() { - // keep track of `use {some_module, some_other_module};` usages - if let UseTreeKind::Nested(trees) = &use_tree.kind { - for tree in trees { - let segments = &tree.0.prefix.segments; - if segments.len() == 1 { - if let UseTreeKind::Simple(None, _, _) = tree.0.kind { - let name = segments[0].ident.name; - if !macros.contains(&name) { - single_use_usages.push((name, tree.0.span, false)); - } - } + fn track_uses( + &mut self, + cx: &EarlyContext<'_>, + item: &Item, + imports_reused_with_self: &mut Vec, + single_use_usages: &mut Vec, + macros: &mut Vec, + ) { + if item.span.from_expansion() || item.vis.kind.is_pub() { + return; + } + + match &item.kind { + ItemKind::Mod(_, ModKind::Loaded(ref items, ..)) => { + self.check_mod(cx, items); + }, + ItemKind::MacroDef(MacroDef { macro_rules: true, .. }) => { + macros.push(item.ident.name); + }, + ItemKind::Use(use_tree) => { + let segments = &use_tree.prefix.segments; + + // keep track of `use some_module;` usages + if segments.len() == 1 { + if let UseTreeKind::Simple(None, _, _) = use_tree.kind { + let name = segments[0].ident.name; + if !macros.contains(&name) { + single_use_usages.push(SingleUse { + name, + span: item.span, + item_id: item.id, + can_suggest: true, + }); } } + return; } - } else { - // keep track of `use self::some_module` usages - if segments[0].ident.name == kw::SelfLower { - // simple case such as `use self::module::SomeStruct` - if segments.len() > 1 { - imports_reused_with_self.push(segments[1].ident.name); - return; - } - // nested case such as `use self::{module1::Struct1, module2::Struct2}` + if segments.is_empty() { + // keep track of `use {some_module, some_other_module};` usages if let UseTreeKind::Nested(trees) = &use_tree.kind { for tree in trees { let segments = &tree.0.prefix.segments; - if !segments.is_empty() { - imports_reused_with_self.push(segments[0].ident.name); + if segments.len() == 1 { + if let UseTreeKind::Simple(None, _, _) = tree.0.kind { + let name = segments[0].ident.name; + if !macros.contains(&name) { + single_use_usages.push(SingleUse { + name, + span: tree.0.span, + item_id: item.id, + can_suggest: false, + }); + } + } + } + } + } + } else { + // keep track of `use self::some_module` usages + if segments[0].ident.name == kw::SelfLower { + // simple case such as `use self::module::SomeStruct` + if segments.len() > 1 { + imports_reused_with_self.push(segments[1].ident.name); + return; + } + + // nested case such as `use self::{module1::Struct1, module2::Struct2}` + if let UseTreeKind::Nested(trees) = &use_tree.kind { + for tree in trees { + let segments = &tree.0.prefix.segments; + if !segments.is_empty() { + imports_reused_with_self.push(segments[0].ident.name); + } } } } } - } - }, - _ => {}, + }, + _ => {}, + } } } diff --git a/clippy_lints/src/slow_vector_initialization.rs b/clippy_lints/src/slow_vector_initialization.rs index 760399231513f..a2109038a0578 100644 --- a/clippy_lints/src/slow_vector_initialization.rs +++ b/clippy_lints/src/slow_vector_initialization.rs @@ -168,7 +168,7 @@ impl SlowVectorInit { }; } - fn emit_lint<'tcx>(cx: &LateContext<'tcx>, slow_fill: &Expr<'_>, vec_alloc: &VecAllocation<'_>, msg: &str) { + fn emit_lint(cx: &LateContext<'_>, slow_fill: &Expr<'_>, vec_alloc: &VecAllocation<'_>, msg: &str) { let len_expr = Sugg::hir(cx, vec_alloc.len_expr, "len"); span_lint_and_then(cx, SLOW_VECTOR_INITIALIZATION, slow_fill.span, msg, |diag| { diff --git a/clippy_lints/src/suspicious_xor_used_as_pow.rs b/clippy_lints/src/suspicious_xor_used_as_pow.rs new file mode 100644 index 0000000000000..301aa5798bf55 --- /dev/null +++ b/clippy_lints/src/suspicious_xor_used_as_pow.rs @@ -0,0 +1,53 @@ +use clippy_utils::{numeric_literal::NumericLiteral, source::snippet_with_context}; +use rustc_errors::Applicability; +use rustc_hir::{BinOpKind, Expr, ExprKind}; +use rustc_lint::{LateContext, LateLintPass, LintContext}; +use rustc_middle::lint::in_external_macro; +use rustc_session::{declare_lint_pass, declare_tool_lint}; + +declare_clippy_lint! { + /// ### What it does + /// Warns for a Bitwise XOR (`^`) operator being probably confused as a powering. It will not trigger if any of the numbers are not in decimal. + /// ### Why is this bad? + /// It's most probably a typo and may lead to unexpected behaviours. + /// ### Example + /// ```rust + /// let x = 3_i32 ^ 4_i32; + /// ``` + /// Use instead: + /// ```rust + /// let x = 3_i32.pow(4); + /// ``` + #[clippy::version = "1.66.0"] + pub SUSPICIOUS_XOR_USED_AS_POW, + restriction, + "XOR (`^`) operator possibly used as exponentiation operator" +} +declare_lint_pass!(ConfusingXorAndPow => [SUSPICIOUS_XOR_USED_AS_POW]); + +impl LateLintPass<'_> for ConfusingXorAndPow { + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { + if !in_external_macro(cx.sess(), expr.span) && + let ExprKind::Binary(op, left, right) = &expr.kind && + op.node == BinOpKind::BitXor && + left.span.ctxt() == right.span.ctxt() && + let ExprKind::Lit(lit_left) = &left.kind && + let ExprKind::Lit(lit_right) = &right.kind && + let snip_left = snippet_with_context(cx, lit_left.span, lit_left.span.ctxt(), "..", &mut Applicability::MaybeIncorrect) && + let snip_right = snippet_with_context(cx, lit_right.span, lit_right.span.ctxt(), "..", &mut Applicability::MaybeIncorrect) && + let Some(left_val) = NumericLiteral::from_lit_kind(&snip_left.0, &lit_left.node) && + let Some(right_val) = NumericLiteral::from_lit_kind(&snip_right.0, &lit_right.node) && + left_val.is_decimal() && + right_val.is_decimal() { + clippy_utils::diagnostics::span_lint_and_sugg( + cx, + SUSPICIOUS_XOR_USED_AS_POW, + expr.span, + "`^` is not the exponentiation operator", + "did you mean to write", + format!("{}.pow({})", left_val.format(), right_val.format()), + Applicability::MaybeIncorrect, + ); + } + } +} diff --git a/clippy_lints/src/swap.rs b/clippy_lints/src/swap.rs index f46c21e126552..c374529d1ea9f 100644 --- a/clippy_lints/src/swap.rs +++ b/clippy_lints/src/swap.rs @@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then}; use clippy_utils::source::snippet_with_applicability; use clippy_utils::sugg::Sugg; use clippy_utils::ty::is_type_diagnostic_item; -use clippy_utils::{can_mut_borrow_both, eq_expr_value, std_or_core}; +use clippy_utils::{can_mut_borrow_both, eq_expr_value, in_constant, std_or_core}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind}; @@ -16,6 +16,8 @@ declare_clippy_lint! { /// ### What it does /// Checks for manual swapping. /// + /// Note that the lint will not be emitted in const blocks, as the suggestion would not be applicable. + /// /// ### Why is this bad? /// The `std::mem::swap` function exposes the intent better /// without deinitializing or copying either variable. @@ -138,6 +140,10 @@ fn generate_swap_warning(cx: &LateContext<'_>, e1: &Expr<'_>, e2: &Expr<'_>, spa /// Implementation of the `MANUAL_SWAP` lint. fn check_manual_swap(cx: &LateContext<'_>, block: &Block<'_>) { + if in_constant(cx, block.hir_id) { + return; + } + for w in block.stmts.windows(3) { if_chain! { // let t = foo(); diff --git a/clippy_lints/src/trailing_empty_array.rs b/clippy_lints/src/trailing_empty_array.rs index 8cf3efc8dc73e..63b326048a48f 100644 --- a/clippy_lints/src/trailing_empty_array.rs +++ b/clippy_lints/src/trailing_empty_array.rs @@ -1,9 +1,9 @@ use clippy_utils::diagnostics::span_lint_and_help; -use rustc_hir::{HirId, Item, ItemKind}; +use clippy_utils::has_repr_attr; +use rustc_hir::{Item, ItemKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty::Const; use rustc_session::{declare_lint_pass, declare_tool_lint}; -use rustc_span::sym; declare_clippy_lint! { /// ### What it does @@ -72,7 +72,3 @@ fn is_struct_with_trailing_zero_sized_array(cx: &LateContext<'_>, item: &Item<'_ } } } - -fn has_repr_attr(cx: &LateContext<'_>, hir_id: HirId) -> bool { - cx.tcx.hir().attrs(hir_id).iter().any(|attr| attr.has_name(sym::repr)) -} diff --git a/clippy_lints/src/transmute/transmute_undefined_repr.rs b/clippy_lints/src/transmute/transmute_undefined_repr.rs index 3d4bbbf648c65..34642f4b122f2 100644 --- a/clippy_lints/src/transmute/transmute_undefined_repr.rs +++ b/clippy_lints/src/transmute/transmute_undefined_repr.rs @@ -27,32 +27,24 @@ pub(super) fn check<'tcx>( // `Repr(C)` <-> unordered type. // If the first field of the `Repr(C)` type matches then the transmute is ok - ( - ReducedTy::OrderedFields(_, Some(from_sub_ty)), - ReducedTy::UnorderedFields(to_sub_ty), - ) - | ( - ReducedTy::UnorderedFields(from_sub_ty), - ReducedTy::OrderedFields(_, Some(to_sub_ty)), - ) => { + (ReducedTy::OrderedFields(_, Some(from_sub_ty)), ReducedTy::UnorderedFields(to_sub_ty)) + | (ReducedTy::UnorderedFields(from_sub_ty), ReducedTy::OrderedFields(_, Some(to_sub_ty))) => { from_ty = from_sub_ty; to_ty = to_sub_ty; continue; - } - (ReducedTy::OrderedFields(_, Some(from_sub_ty)), ReducedTy::Other(to_sub_ty)) - if reduced_tys.to_fat_ptr => - { + }, + (ReducedTy::OrderedFields(_, Some(from_sub_ty)), ReducedTy::Other(to_sub_ty)) if reduced_tys.to_fat_ptr => { from_ty = from_sub_ty; to_ty = to_sub_ty; continue; - } + }, (ReducedTy::Other(from_sub_ty), ReducedTy::OrderedFields(_, Some(to_sub_ty))) if reduced_tys.from_fat_ptr => { from_ty = from_sub_ty; to_ty = to_sub_ty; continue; - } + }, // ptr <-> ptr (ReducedTy::Other(from_sub_ty), ReducedTy::Other(to_sub_ty)) @@ -62,19 +54,19 @@ pub(super) fn check<'tcx>( from_ty = from_sub_ty; to_ty = to_sub_ty; continue; - } + }, // fat ptr <-> (*size, *size) (ReducedTy::Other(_), ReducedTy::UnorderedFields(to_ty)) if reduced_tys.from_fat_ptr && is_size_pair(to_ty) => { return false; - } + }, (ReducedTy::UnorderedFields(from_ty), ReducedTy::Other(_)) if reduced_tys.to_fat_ptr && is_size_pair(from_ty) => { return false; - } + }, // fat ptr -> some struct | some struct -> fat ptr (ReducedTy::Other(_), _) if reduced_tys.from_fat_ptr => { @@ -85,14 +77,12 @@ pub(super) fn check<'tcx>( &format!("transmute from `{from_ty_orig}` which has an undefined layout"), |diag| { if from_ty_orig.peel_refs() != from_ty.peel_refs() { - diag.note(&format!( - "the contained type `{from_ty}` has an undefined layout" - )); + diag.note(&format!("the contained type `{from_ty}` has an undefined layout")); } }, ); return true; - } + }, (_, ReducedTy::Other(_)) if reduced_tys.to_fat_ptr => { span_lint_and_then( cx, @@ -101,18 +91,14 @@ pub(super) fn check<'tcx>( &format!("transmute to `{to_ty_orig}` which has an undefined layout"), |diag| { if to_ty_orig.peel_refs() != to_ty.peel_refs() { - diag.note(&format!( - "the contained type `{to_ty}` has an undefined layout" - )); + diag.note(&format!("the contained type `{to_ty}` has an undefined layout")); } }, ); return true; - } + }, - (ReducedTy::UnorderedFields(from_ty), ReducedTy::UnorderedFields(to_ty)) - if from_ty != to_ty => - { + (ReducedTy::UnorderedFields(from_ty), ReducedTy::UnorderedFields(to_ty)) if from_ty != to_ty => { let same_adt_did = if let (ty::Adt(from_def, from_subs), ty::Adt(to_def, to_subs)) = (from_ty.kind(), to_ty.kind()) && from_def == to_def @@ -139,25 +125,19 @@ pub(super) fn check<'tcx>( )); } else { if from_ty_orig.peel_refs() != from_ty { - diag.note(&format!( - "the contained type `{from_ty}` has an undefined layout" - )); + diag.note(&format!("the contained type `{from_ty}` has an undefined layout")); } if to_ty_orig.peel_refs() != to_ty { - diag.note(&format!( - "the contained type `{to_ty}` has an undefined layout" - )); + diag.note(&format!("the contained type `{to_ty}` has an undefined layout")); } } }, ); return true; - } + }, ( ReducedTy::UnorderedFields(from_ty), - ReducedTy::Other(_) - | ReducedTy::OrderedFields(..) - | ReducedTy::TypeErasure { raw_ptr_only: true }, + ReducedTy::Other(_) | ReducedTy::OrderedFields(..) | ReducedTy::TypeErasure { raw_ptr_only: true }, ) => { span_lint_and_then( cx, @@ -166,18 +146,14 @@ pub(super) fn check<'tcx>( &format!("transmute from `{from_ty_orig}` which has an undefined layout"), |diag| { if from_ty_orig.peel_refs() != from_ty { - diag.note(&format!( - "the contained type `{from_ty}` has an undefined layout" - )); + diag.note(&format!("the contained type `{from_ty}` has an undefined layout")); } }, ); return true; - } + }, ( - ReducedTy::Other(_) - | ReducedTy::OrderedFields(..) - | ReducedTy::TypeErasure { raw_ptr_only: true }, + ReducedTy::Other(_) | ReducedTy::OrderedFields(..) | ReducedTy::TypeErasure { raw_ptr_only: true }, ReducedTy::UnorderedFields(to_ty), ) => { span_lint_and_then( @@ -187,25 +163,19 @@ pub(super) fn check<'tcx>( &format!("transmute into `{to_ty_orig}` which has an undefined layout"), |diag| { if to_ty_orig.peel_refs() != to_ty { - diag.note(&format!( - "the contained type `{to_ty}` has an undefined layout" - )); + diag.note(&format!("the contained type `{to_ty}` has an undefined layout")); } }, ); return true; - } + }, ( - ReducedTy::OrderedFields(..) - | ReducedTy::Other(_) - | ReducedTy::TypeErasure { raw_ptr_only: true }, - ReducedTy::OrderedFields(..) - | ReducedTy::Other(_) - | ReducedTy::TypeErasure { raw_ptr_only: true }, + ReducedTy::OrderedFields(..) | ReducedTy::Other(_) | ReducedTy::TypeErasure { raw_ptr_only: true }, + ReducedTy::OrderedFields(..) | ReducedTy::Other(_) | ReducedTy::TypeErasure { raw_ptr_only: true }, ) | (ReducedTy::UnorderedFields(_), ReducedTy::UnorderedFields(_)) => { break; - } + }, } } @@ -223,38 +193,42 @@ struct ReducedTys<'tcx> { } /// Remove references so long as both types are references. -fn reduce_refs<'tcx>( - cx: &LateContext<'tcx>, - mut from_ty: Ty<'tcx>, - mut to_ty: Ty<'tcx>, -) -> ReducedTys<'tcx> { +fn reduce_refs<'tcx>(cx: &LateContext<'tcx>, mut from_ty: Ty<'tcx>, mut to_ty: Ty<'tcx>) -> ReducedTys<'tcx> { let mut from_raw_ptr = false; let mut to_raw_ptr = false; - let (from_fat_ptr, to_fat_ptr) = - loop { - break match (from_ty.kind(), to_ty.kind()) { - ( - &(ty::Ref(_, from_sub_ty, _) | ty::RawPtr(TypeAndMut { ty: from_sub_ty, .. })), - &(ty::Ref(_, to_sub_ty, _) | ty::RawPtr(TypeAndMut { ty: to_sub_ty, .. })), - ) => { - from_raw_ptr = matches!(*from_ty.kind(), ty::RawPtr(_)); - from_ty = from_sub_ty; - to_raw_ptr = matches!(*to_ty.kind(), ty::RawPtr(_)); - to_ty = to_sub_ty; - continue; - } - ( - &(ty::Ref(_, unsized_ty, _) | ty::RawPtr(TypeAndMut { ty: unsized_ty, .. })), - _, - ) if !unsized_ty.is_sized(cx.tcx, cx.param_env) => (true, false), - ( - _, - &(ty::Ref(_, unsized_ty, _) | ty::RawPtr(TypeAndMut { ty: unsized_ty, .. })), - ) if !unsized_ty.is_sized(cx.tcx, cx.param_env) => (false, true), - _ => (false, false), - }; + let (from_fat_ptr, to_fat_ptr) = loop { + break match (from_ty.kind(), to_ty.kind()) { + ( + &(ty::Ref(_, from_sub_ty, _) | ty::RawPtr(TypeAndMut { ty: from_sub_ty, .. })), + &(ty::Ref(_, to_sub_ty, _) | ty::RawPtr(TypeAndMut { ty: to_sub_ty, .. })), + ) => { + from_raw_ptr = matches!(*from_ty.kind(), ty::RawPtr(_)); + from_ty = from_sub_ty; + to_raw_ptr = matches!(*to_ty.kind(), ty::RawPtr(_)); + to_ty = to_sub_ty; + continue; + }, + (&(ty::Ref(_, unsized_ty, _) | ty::RawPtr(TypeAndMut { ty: unsized_ty, .. })), _) + if !unsized_ty.is_sized(cx.tcx, cx.param_env) => + { + (true, false) + }, + (_, &(ty::Ref(_, unsized_ty, _) | ty::RawPtr(TypeAndMut { ty: unsized_ty, .. }))) + if !unsized_ty.is_sized(cx.tcx, cx.param_env) => + { + (false, true) + }, + _ => (false, false), }; - ReducedTys { from_ty, to_ty, from_raw_ptr, to_raw_ptr, from_fat_ptr, to_fat_ptr } + }; + ReducedTys { + from_ty, + to_ty, + from_raw_ptr, + to_raw_ptr, + from_fat_ptr, + to_fat_ptr, + } } enum ReducedTy<'tcx> { @@ -277,11 +251,11 @@ fn reduce_ty<'tcx>(cx: &LateContext<'tcx>, mut ty: Ty<'tcx>) -> ReducedTy<'tcx> return match *ty.kind() { ty::Array(sub_ty, _) if matches!(sub_ty.kind(), ty::Int(_) | ty::Uint(_)) => { ReducedTy::TypeErasure { raw_ptr_only: false } - } + }, ty::Array(sub_ty, _) | ty::Slice(sub_ty) => { ty = sub_ty; continue; - } + }, ty::Tuple(args) if args.is_empty() => ReducedTy::TypeErasure { raw_ptr_only: false }, ty::Tuple(args) => { let mut iter = args.iter(); @@ -293,7 +267,7 @@ fn reduce_ty<'tcx>(cx: &LateContext<'tcx>, mut ty: Ty<'tcx>) -> ReducedTy<'tcx> continue; } ReducedTy::UnorderedFields(ty) - } + }, ty::Adt(def, substs) if def.is_struct() => { let mut iter = def .non_enum_variant() @@ -312,12 +286,10 @@ fn reduce_ty<'tcx>(cx: &LateContext<'tcx>, mut ty: Ty<'tcx>) -> ReducedTy<'tcx> } else { ReducedTy::UnorderedFields(ty) } - } - ty::Adt(def, _) - if def.is_enum() && (def.variants().is_empty() || is_c_void(cx, ty)) => - { + }, + ty::Adt(def, _) if def.is_enum() && (def.variants().is_empty() || is_c_void(cx, ty)) => { ReducedTy::TypeErasure { raw_ptr_only: false } - } + }, // TODO: Check if the conversion to or from at least one of a union's fields is valid. ty::Adt(def, _) if def.is_union() => ReducedTy::TypeErasure { raw_ptr_only: false }, ty::Foreign(_) | ty::Param(_) => ReducedTy::TypeErasure { raw_ptr_only: false }, @@ -356,11 +328,7 @@ fn same_except_params<'tcx>(subs1: SubstsRef<'tcx>, subs2: SubstsRef<'tcx>) -> b for (ty1, ty2) in subs1.types().zip(subs2.types()).filter(|(ty1, ty2)| ty1 != ty2) { match (ty1.kind(), ty2.kind()) { (ty::Param(_), _) | (_, ty::Param(_)) => (), - (ty::Adt(adt1, subs1), ty::Adt(adt2, subs2)) - if adt1 == adt2 && same_except_params(subs1, subs2) => - { - () - } + (ty::Adt(adt1, subs1), ty::Adt(adt2, subs2)) if adt1 == adt2 && same_except_params(subs1, subs2) => (), _ => return false, } } diff --git a/clippy_lints/src/transmute/utils.rs b/clippy_lints/src/transmute/utils.rs index 641cdf5d330e1..49d863ec03f1d 100644 --- a/clippy_lints/src/transmute/utils.rs +++ b/clippy_lints/src/transmute/utils.rs @@ -1,9 +1,9 @@ +use rustc_hir as hir; use rustc_hir::Expr; use rustc_hir_typeck::{cast, FnCtxt, Inherited}; use rustc_lint::LateContext; use rustc_middle::ty::{cast::CastKind, Ty}; use rustc_span::DUMMY_SP; -use rustc_hir as hir; // check if the component types of the transmuted collection and the result have different ABI, // size or alignment @@ -55,9 +55,14 @@ fn check_cast<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx> ); if let Ok(check) = cast::CastCheck::new( - &fn_ctxt, e, from_ty, to_ty, + &fn_ctxt, + e, + from_ty, + to_ty, // We won't show any error to the user, so we don't care what the span is here. - DUMMY_SP, DUMMY_SP, hir::Constness::NotConst, + DUMMY_SP, + DUMMY_SP, + hir::Constness::NotConst, ) { let res = check.do_check(&fn_ctxt); diff --git a/clippy_lints/src/types/box_collection.rs b/clippy_lints/src/types/box_collection.rs index 802415e163df5..43665a922d444 100644 --- a/clippy_lints/src/types/box_collection.rs +++ b/clippy_lints/src/types/box_collection.rs @@ -39,12 +39,19 @@ fn get_std_collection(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option let id = path_def_id(cx, param)?; cx.tcx .get_diagnostic_name(id) - .filter(|&name| matches!(name, sym::HashMap | sym::Vec | sym::HashSet - | sym::VecDeque - | sym::LinkedList - | sym::BTreeMap - | sym::BTreeSet - | sym::BinaryHeap)) + .filter(|&name| { + matches!( + name, + sym::HashMap + | sym::Vec + | sym::HashSet + | sym::VecDeque + | sym::LinkedList + | sym::BTreeMap + | sym::BTreeSet + | sym::BinaryHeap + ) + }) .or_else(|| { cx.tcx .lang_items() diff --git a/clippy_lints/src/types/mod.rs b/clippy_lints/src/types/mod.rs index f6de87b0526cd..20978e81dc584 100644 --- a/clippy_lints/src/types/mod.rs +++ b/clippy_lints/src/types/mod.rs @@ -379,7 +379,9 @@ impl<'tcx> LateLintPass<'tcx> for Types { } fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) { - let is_exported = cx.effective_visibilities.is_exported(cx.tcx.hir().local_def_id(field.hir_id)); + let is_exported = cx + .effective_visibilities + .is_exported(cx.tcx.hir().local_def_id(field.hir_id)); self.check_ty( cx, diff --git a/clippy_lints/src/types/redundant_allocation.rs b/clippy_lints/src/types/redundant_allocation.rs index 2b964b64a3305..fae5385ffc848 100644 --- a/clippy_lints/src/types/redundant_allocation.rs +++ b/clippy_lints/src/types/redundant_allocation.rs @@ -5,16 +5,12 @@ use rustc_errors::Applicability; use rustc_hir::{self as hir, def_id::DefId, QPath, TyKind}; use rustc_hir_analysis::hir_ty_to_ty; use rustc_lint::LateContext; +use rustc_middle::ty::TypeVisitable; use rustc_span::symbol::sym; use super::{utils, REDUNDANT_ALLOCATION}; -pub(super) fn check( - cx: &LateContext<'_>, - hir_ty: &hir::Ty<'_>, - qpath: &QPath<'_>, - def_id: DefId, -) -> bool { +pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool { let mut applicability = Applicability::MaybeIncorrect; let outer_sym = if Some(def_id) == cx.tcx.lang_items().owned_box() { "Box" @@ -34,12 +30,7 @@ pub(super) fn check( hir_ty.span, &format!("usage of `{outer_sym}<{generic_snippet}>`"), |diag| { - diag.span_suggestion( - hir_ty.span, - "try", - format!("{generic_snippet}"), - applicability, - ); + diag.span_suggestion(hir_ty.span, "try", format!("{generic_snippet}"), applicability); diag.note(&format!( "`{generic_snippet}` is already a pointer, `{outer_sym}<{generic_snippet}>` allocates a pointer on the heap" )); @@ -61,15 +52,16 @@ pub(super) fn check( return false }; let inner_span = match qpath_generic_tys(inner_qpath).next() { - Some(ty) => { + Some(hir_ty) => { // Reallocation of a fat pointer causes it to become thin. `hir_ty_to_ty` is safe to use // here because `mod.rs` guarantees this lint is only run on types outside of bodies and // is not run on locals. - if !hir_ty_to_ty(cx.tcx, ty).is_sized(cx.tcx, cx.param_env) { + let ty = hir_ty_to_ty(cx.tcx, hir_ty); + if ty.has_escaping_bound_vars() || !ty.is_sized(cx.tcx, cx.param_env) { return false; } - ty.span - } + hir_ty.span + }, None => return false, }; if inner_sym == outer_sym { diff --git a/clippy_lints/src/types/vec_box.rs b/clippy_lints/src/types/vec_box.rs index 9ad2cb853d39a..7a3c7cd8a99fc 100644 --- a/clippy_lints/src/types/vec_box.rs +++ b/clippy_lints/src/types/vec_box.rs @@ -42,7 +42,7 @@ pub(super) fn check( if !ty_ty.has_escaping_bound_vars(); if ty_ty.is_sized(cx.tcx, cx.param_env); if let Ok(ty_ty_size) = cx.layout_of(ty_ty).map(|l| l.size.bytes()); - if ty_ty_size <= box_size_threshold; + if ty_ty_size < box_size_threshold; then { span_lint_and_sugg( cx, diff --git a/clippy_lints/src/undocumented_unsafe_blocks.rs b/clippy_lints/src/undocumented_unsafe_blocks.rs index d2e675a783eaa..e8f15a4447352 100644 --- a/clippy_lints/src/undocumented_unsafe_blocks.rs +++ b/clippy_lints/src/undocumented_unsafe_blocks.rs @@ -68,7 +68,8 @@ impl LateLintPass<'_> for UndocumentedUnsafeBlocks { && !in_external_macro(cx.tcx.sess, block.span) && !is_lint_allowed(cx, UNDOCUMENTED_UNSAFE_BLOCKS, block.hir_id) && !is_unsafe_from_proc_macro(cx, block.span) - && !block_has_safety_comment(cx, block) + && !block_has_safety_comment(cx, block.span) + && !block_parents_have_safety_comment(cx, block.hir_id) { let source_map = cx.tcx.sess.source_map(); let span = if source_map.is_multiline(block.span) { @@ -126,8 +127,41 @@ fn is_unsafe_from_proc_macro(cx: &LateContext<'_>, span: Span) -> bool { .map_or(true, |src| !src.starts_with("unsafe")) } +// Checks if any parent {expression, statement, block, local, const, static} +// has a safety comment +fn block_parents_have_safety_comment(cx: &LateContext<'_>, id: hir::HirId) -> bool { + if let Some(node) = get_parent_node(cx.tcx, id) { + return match node { + Node::Expr(expr) => !is_branchy(expr) && span_in_body_has_safety_comment(cx, expr.span), + Node::Stmt(hir::Stmt { + kind: + hir::StmtKind::Local(hir::Local { span, .. }) + | hir::StmtKind::Expr(hir::Expr { span, .. }) + | hir::StmtKind::Semi(hir::Expr { span, .. }), + .. + }) + | Node::Local(hir::Local { span, .. }) + | Node::Item(hir::Item { + kind: hir::ItemKind::Const(..) | ItemKind::Static(..), + span, + .. + }) => span_in_body_has_safety_comment(cx, *span), + _ => false, + }; + } + false +} + +/// Checks if an expression is "branchy", e.g. loop, match/if/etc. +fn is_branchy(expr: &hir::Expr<'_>) -> bool { + matches!( + expr.kind, + hir::ExprKind::If(..) | hir::ExprKind::Loop(..) | hir::ExprKind::Match(..) + ) +} + /// Checks if the lines immediately preceding the block contain a safety comment. -fn block_has_safety_comment(cx: &LateContext<'_>, block: &hir::Block<'_>) -> bool { +fn block_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool { // This intentionally ignores text before the start of a function so something like: // ``` // // SAFETY: reason @@ -136,7 +170,7 @@ fn block_has_safety_comment(cx: &LateContext<'_>, block: &hir::Block<'_>) -> boo // won't work. This is to avoid dealing with where such a comment should be place relative to // attributes and doc comments. - span_from_macro_expansion_has_safety_comment(cx, block.span) || span_in_body_has_safety_comment(cx, block.span) + span_from_macro_expansion_has_safety_comment(cx, span) || span_in_body_has_safety_comment(cx, span) } /// Checks if the lines immediately preceding the item contain a safety comment. diff --git a/clippy_lints/src/unsafe_removed_from_name.rs b/clippy_lints/src/unsafe_removed_from_name.rs index 32cd468120141..952586527689a 100644 --- a/clippy_lints/src/unsafe_removed_from_name.rs +++ b/clippy_lints/src/unsafe_removed_from_name.rs @@ -50,7 +50,7 @@ fn check_use_tree(use_tree: &UseTree, cx: &EarlyContext<'_>, span: Span) { }, UseTreeKind::Simple(None, ..) | UseTreeKind::Glob => {}, UseTreeKind::Nested(ref nested_use_tree) => { - for &(ref use_tree, _) in nested_use_tree { + for (use_tree, _) in nested_use_tree { check_use_tree(use_tree, cx, span); } }, diff --git a/clippy_lints/src/unused_peekable.rs b/clippy_lints/src/unused_peekable.rs index b452be0840948..4ee16d9a5e4ad 100644 --- a/clippy_lints/src/unused_peekable.rs +++ b/clippy_lints/src/unused_peekable.rs @@ -36,7 +36,7 @@ declare_clippy_lint! { /// // ... /// } /// ``` - #[clippy::version = "1.64.0"] + #[clippy::version = "1.65.0"] pub UNUSED_PEEKABLE, nursery, "creating a peekable iterator without using any of its methods" diff --git a/clippy_lints/src/unused_rounding.rs b/clippy_lints/src/unused_rounding.rs index 5ab351bc29ca0..aac6719a8dc0f 100644 --- a/clippy_lints/src/unused_rounding.rs +++ b/clippy_lints/src/unused_rounding.rs @@ -30,16 +30,15 @@ declare_clippy_lint! { declare_lint_pass!(UnusedRounding => [UNUSED_ROUNDING]); fn is_useless_rounding(expr: &Expr) -> Option<(&str, String)> { - if let ExprKind::MethodCall(box MethodCall { seg, receiver, .. }) = &expr.kind - && let method_name = seg.ident.name.as_str() + if let ExprKind::MethodCall(box MethodCall { seg:name_ident, receiver, .. }) = &expr.kind + && let method_name = name_ident.ident.name.as_str() && (method_name == "ceil" || method_name == "round" || method_name == "floor") && let ExprKind::Lit(token_lit) = &receiver.kind && token_lit.is_semantic_float() { - let f = token_lit.symbol.as_str().parse::().unwrap(); let mut f_str = token_lit.symbol.to_string(); - match token_lit.suffix { - Some(suffix) => f_str.push_str(suffix.as_str()), - None => {} + let f = f_str.trim_end_matches('_').parse::().unwrap(); + if let Some(suffix) = token_lit.suffix { + f_str.push_str(suffix.as_str()); } if f.fract() == 0.0 { Some((method_name, f_str)) diff --git a/clippy_lints/src/unused_unit.rs b/clippy_lints/src/unused_unit.rs index cd1d90e860b9f..cad8da18c2fbc 100644 --- a/clippy_lints/src/unused_unit.rs +++ b/clippy_lints/src/unused_unit.rs @@ -1,8 +1,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::source::{position_before_rarrow, snippet_opt}; use if_chain::if_chain; -use rustc_ast::ast; -use rustc_ast::visit::FnKind; +use rustc_ast::{ast, visit::FnKind, ClosureBinder}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; @@ -43,6 +42,11 @@ impl EarlyLintPass for UnusedUnit { if let ast::TyKind::Tup(ref vals) = ty.kind; if vals.is_empty() && !ty.span.from_expansion() && get_def(span) == get_def(ty.span); then { + // implicit types in closure signatures are forbidden when `for<...>` is present + if let FnKind::Closure(&ClosureBinder::For { .. }, ..) = kind { + return; + } + lint_unneeded_unit_return(cx, ty, span); } } diff --git a/clippy_lints/src/use_self.rs b/clippy_lints/src/use_self.rs index c6cdf3f85fc3d..e2860db71a5a7 100644 --- a/clippy_lints/src/use_self.rs +++ b/clippy_lints/src/use_self.rs @@ -30,7 +30,6 @@ declare_clippy_lint! { /// /// ### Known problems /// - Unaddressed false negative in fn bodies of trait implementations - /// - False positive with associated types in traits (#4140) /// /// ### Example /// ```rust @@ -103,6 +102,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf { if parameters.as_ref().map_or(true, |params| { !params.parenthesized && !params.args.iter().any(|arg| matches!(arg, GenericArg::Lifetime(_))) }); + if !item.span.from_expansion(); if !is_from_proc_macro(cx, item); // expensive, should be last check then { StackItem::Check { @@ -234,24 +234,13 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf { then {} else { return; } } match expr.kind { - ExprKind::Struct(QPath::Resolved(_, path), ..) => match path.res { - Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } => (), - Res::Def(DefKind::Variant, _) => lint_path_to_variant(cx, path), - _ => span_lint(cx, path.span), - }, - // tuple struct instantiation (`Foo(arg)` or `Enum::Foo(arg)`) + ExprKind::Struct(QPath::Resolved(_, path), ..) => check_path(cx, path), ExprKind::Call(fun, _) => { if let ExprKind::Path(QPath::Resolved(_, path)) = fun.kind { - if let Res::Def(DefKind::Ctor(ctor_of, _), ..) = path.res { - match ctor_of { - CtorOf::Variant => lint_path_to_variant(cx, path), - CtorOf::Struct => span_lint(cx, path.span), - } - } + check_path(cx, path); } }, - // unit enum variants (`Enum::A`) - ExprKind::Path(QPath::Resolved(_, path)) => lint_path_to_variant(cx, path), + ExprKind::Path(QPath::Resolved(_, path)) => check_path(cx, path), _ => (), } } @@ -267,15 +256,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf { | PatKind::Struct(QPath::Resolved(_, path), _, _) = pat.kind; if cx.typeck_results().pat_ty(pat) == cx.tcx.type_of(impl_id); then { - match path.res { - Res::Def(DefKind::Ctor(ctor_of, _), ..) => match ctor_of { - CtorOf::Variant => lint_path_to_variant(cx, path), - CtorOf::Struct => span_lint(cx, path.span), - }, - Res::Def(DefKind::Variant, ..) => lint_path_to_variant(cx, path), - Res::Def(DefKind::Struct, ..) => span_lint(cx, path.span), - _ => () - } + check_path(cx, path); } } } @@ -313,6 +294,16 @@ fn span_lint(cx: &LateContext<'_>, span: Span) { ); } +fn check_path(cx: &LateContext<'_>, path: &Path<'_>) { + match path.res { + Res::Def(DefKind::Ctor(CtorOf::Variant, _) | DefKind::Variant, ..) => { + lint_path_to_variant(cx, path); + }, + Res::Def(DefKind::Ctor(CtorOf::Struct, _) | DefKind::Struct, ..) => span_lint(cx, path.span), + _ => (), + } +} + fn lint_path_to_variant(cx: &LateContext<'_>, path: &Path<'_>) { if let [.., self_seg, _variant] = path.segments { let span = path diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index 668123e4d6e39..b37d4239477ea 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -53,11 +53,11 @@ impl DisallowedPath { path } - pub fn reason(&self) -> Option<&str> { + pub fn reason(&self) -> Option { match self { Self::WithReason { reason: Some(reason), .. - } => Some(reason), + } => Some(format!("{reason} (from clippy.toml)")), _ => None, } } @@ -213,7 +213,7 @@ define_Conf! { /// /// Suppress lints whenever the suggested change would cause breakage for other crates. (avoid_breaking_exported_api: bool = true), - /// Lint: MANUAL_SPLIT_ONCE, MANUAL_STR_REPEAT, CLONED_INSTEAD_OF_COPIED, REDUNDANT_FIELD_NAMES, REDUNDANT_STATIC_LIFETIMES, FILTER_MAP_NEXT, CHECKED_CONVERSIONS, MANUAL_RANGE_CONTAINS, USE_SELF, MEM_REPLACE_WITH_DEFAULT, MANUAL_NON_EXHAUSTIVE, OPTION_AS_REF_DEREF, MAP_UNWRAP_OR, MATCH_LIKE_MATCHES_MACRO, MANUAL_STRIP, MISSING_CONST_FOR_FN, UNNESTED_OR_PATTERNS, FROM_OVER_INTO, PTR_AS_PTR, IF_THEN_SOME_ELSE_NONE, APPROX_CONSTANT, DEPRECATED_CFG_ATTR, INDEX_REFUTABLE_SLICE, MAP_CLONE, BORROW_AS_PTR, MANUAL_BITS, ERR_EXPECT, CAST_ABS_TO_UNSIGNED, UNINLINED_FORMAT_ARGS, MANUAL_CLAMP. + /// Lint: MANUAL_SPLIT_ONCE, MANUAL_STR_REPEAT, CLONED_INSTEAD_OF_COPIED, REDUNDANT_FIELD_NAMES, REDUNDANT_STATIC_LIFETIMES, FILTER_MAP_NEXT, CHECKED_CONVERSIONS, MANUAL_RANGE_CONTAINS, USE_SELF, MEM_REPLACE_WITH_DEFAULT, MANUAL_NON_EXHAUSTIVE, OPTION_AS_REF_DEREF, MAP_UNWRAP_OR, MATCH_LIKE_MATCHES_MACRO, MANUAL_STRIP, MISSING_CONST_FOR_FN, UNNESTED_OR_PATTERNS, FROM_OVER_INTO, PTR_AS_PTR, IF_THEN_SOME_ELSE_NONE, APPROX_CONSTANT, DEPRECATED_CFG_ATTR, INDEX_REFUTABLE_SLICE, MAP_CLONE, BORROW_AS_PTR, MANUAL_BITS, ERR_EXPECT, CAST_ABS_TO_UNSIGNED, UNINLINED_FORMAT_ARGS, MANUAL_CLAMP, MANUAL_LET_ELSE, UNCHECKED_DURATION_SUBTRACTION. /// /// The minimum rust version that the project supports (msrv: Option = None), @@ -335,6 +335,12 @@ define_Conf! { /// /// Enables verbose mode. Triggers if there is more than one uppercase char next to each other (upper_case_acronyms_aggressive: bool = false), + /// Lint: MANUAL_LET_ELSE. + /// + /// Whether the matches should be considered by the lint, and whether there should + /// be filtering for common types. + (matches_for_let_else: crate::manual_let_else::MatchLintBehaviour = + crate::manual_let_else::MatchLintBehaviour::WellKnownTypes), /// Lint: _CARGO_COMMON_METADATA. /// /// For internal testing only, ignores the current `publish` settings in the Cargo manifest. @@ -373,23 +379,36 @@ define_Conf! { (max_include_file_size: u64 = 1_000_000), /// Lint: EXPECT_USED. /// - /// Whether `expect` should be allowed in test functions + /// Whether `expect` should be allowed within `#[cfg(test)]` (allow_expect_in_tests: bool = false), /// Lint: UNWRAP_USED. /// - /// Whether `unwrap` should be allowed in test functions + /// Whether `unwrap` should be allowed in test cfg (allow_unwrap_in_tests: bool = false), /// Lint: DBG_MACRO. /// /// Whether `dbg!` should be allowed in test functions (allow_dbg_in_tests: bool = false), - /// Lint: RESULT_LARGE_ERR + /// Lint: PRINT_STDOUT, PRINT_STDERR. + /// + /// Whether print macros (ex. `println!`) should be allowed in test functions + (allow_print_in_tests: bool = false), + /// Lint: RESULT_LARGE_ERR. /// /// The maximum size of the `Err`-variant in a `Result` returned from a function (large_error_threshold: u64 = 128), + /// Lint: MUTABLE_KEY. + /// + /// A list of paths to types that should be treated like `Arc`, i.e. ignored but + /// for the generic parameters for determining interior mutability + (ignore_interior_mutability: Vec = Vec::from(["bytes::Bytes".into()])), } /// Search for the configuration file. +/// +/// # Errors +/// +/// Returns any unexpected filesystem error encountered when searching for the config file pub fn lookup_conf_file() -> io::Result> { /// Possible filename to search for. const CONFIG_FILE_NAMES: [&str; 2] = [".clippy.toml", "clippy.toml"]; diff --git a/clippy_lints/src/utils/internal_lints/interning_defined_symbol.rs b/clippy_lints/src/utils/internal_lints/interning_defined_symbol.rs index 096b601572b4d..4b33d492a0e47 100644 --- a/clippy_lints/src/utils/internal_lints/interning_defined_symbol.rs +++ b/clippy_lints/src/utils/internal_lints/interning_defined_symbol.rs @@ -2,7 +2,7 @@ use clippy_utils::consts::{constant_simple, Constant}; use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::source::snippet; use clippy_utils::ty::match_type; -use clippy_utils::{def_path_res, is_expn_of, match_def_path, paths}; +use clippy_utils::{def_path_def_ids, is_expn_of, match_def_path, paths}; use if_chain::if_chain; use rustc_data_structures::fx::FxHashMap; use rustc_errors::Applicability; @@ -74,7 +74,7 @@ impl<'tcx> LateLintPass<'tcx> for InterningDefinedSymbol { } for &module in &[&paths::KW_MODULE, &paths::SYM_MODULE] { - if let Some(def_id) = def_path_res(cx, module, None).opt_def_id() { + for def_id in def_path_def_ids(cx, module) { for item in cx.tcx.module_children(def_id).iter() { if_chain! { if let Res::Def(DefKind::Const, item_def_id) = item.res; diff --git a/clippy_lints/src/utils/internal_lints/invalid_paths.rs b/clippy_lints/src/utils/internal_lints/invalid_paths.rs index 22a5aa5351ad5..680935f2329e4 100644 --- a/clippy_lints/src/utils/internal_lints/invalid_paths.rs +++ b/clippy_lints/src/utils/internal_lints/invalid_paths.rs @@ -3,7 +3,7 @@ use clippy_utils::def_path_res; use clippy_utils::diagnostics::span_lint; use if_chain::if_chain; use rustc_hir as hir; -use rustc_hir::def::{DefKind, Res}; +use rustc_hir::def::DefKind; use rustc_hir::Item; use rustc_hir_analysis::hir_ty_to_ty; use rustc_lint::{LateContext, LateLintPass}; @@ -63,7 +63,7 @@ impl<'tcx> LateLintPass<'tcx> for InvalidPaths { // This is not a complete resolver for paths. It works on all the paths currently used in the paths // module. That's all it does and all it needs to do. pub fn check_path(cx: &LateContext<'_>, path: &[&str]) -> bool { - if def_path_res(cx, path, None) != Res::Err { + if !def_path_res(cx, path).is_empty() { return true; } diff --git a/clippy_lints/src/utils/internal_lints/lint_without_lint_pass.rs b/clippy_lints/src/utils/internal_lints/lint_without_lint_pass.rs index 0dac64376b065..1aebb8b3104ba 100644 --- a/clippy_lints/src/utils/internal_lints/lint_without_lint_pass.rs +++ b/clippy_lints/src/utils/internal_lints/lint_without_lint_pass.rs @@ -256,7 +256,7 @@ impl<'tcx> LateLintPass<'tcx> for LintWithoutLintPass { } } -pub(super) fn is_lint_ref_type<'tcx>(cx: &LateContext<'tcx>, ty: &hir::Ty<'_>) -> bool { +pub(super) fn is_lint_ref_type(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> bool { if let TyKind::Rptr( _, MutTy { diff --git a/clippy_lints/src/utils/internal_lints/unnecessary_def_path.rs b/clippy_lints/src/utils/internal_lints/unnecessary_def_path.rs index cfba7fa8791de..08980cb12ed6b 100644 --- a/clippy_lints/src/utils/internal_lints/unnecessary_def_path.rs +++ b/clippy_lints/src/utils/internal_lints/unnecessary_def_path.rs @@ -1,6 +1,6 @@ use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_then}; use clippy_utils::source::snippet_with_applicability; -use clippy_utils::{def_path_res, is_lint_allowed, match_any_def_paths, peel_hir_expr_refs}; +use clippy_utils::{def_path_def_ids, is_lint_allowed, match_any_def_paths, peel_hir_expr_refs}; use if_chain::if_chain; use rustc_ast::ast::LitKind; use rustc_data_structures::fx::FxHashSet; @@ -11,9 +11,9 @@ use rustc_hir::def_id::DefId; use rustc_hir::{Expr, ExprKind, Local, Mutability, Node}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::mir::interpret::{Allocation, ConstValue, GlobalAlloc}; -use rustc_middle::ty::{self, AssocKind, DefIdTree, Ty}; +use rustc_middle::ty::{self, DefIdTree, Ty}; use rustc_session::{declare_tool_lint, impl_lint_pass}; -use rustc_span::symbol::{Ident, Symbol}; +use rustc_span::symbol::Symbol; use rustc_span::Span; use std::str; @@ -110,7 +110,7 @@ impl UnnecessaryDefPath { // Extract the path to the matched type if let Some(segments) = path_to_matched_type(cx, item_arg); let segments: Vec<&str> = segments.iter().map(|sym| &**sym).collect(); - if let Some(def_id) = inherent_def_path_res(cx, &segments[..]); + if let Some(def_id) = def_path_def_ids(cx, &segments[..]).next(); then { // Check if the target item is a diagnostic item or LangItem. #[rustfmt::skip] @@ -209,7 +209,7 @@ impl UnnecessaryDefPath { fn check_array(&mut self, cx: &LateContext<'_>, elements: &[Expr<'_>], span: Span) { let Some(path) = path_from_array(elements) else { return }; - if let Some(def_id) = inherent_def_path_res(cx, &path.iter().map(AsRef::as_ref).collect::>()) { + for def_id in def_path_def_ids(cx, &path.iter().map(AsRef::as_ref).collect::>()) { self.array_def_ids.insert((def_id, span)); } } @@ -246,7 +246,7 @@ fn path_to_matched_type(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option(cx: &LateContext<'tcx>, alloc: &'tcx Allocation, ty: Ty<'_>) -> Option> { let (alloc, ty) = if let ty::Ref(_, ty, Mutability::Not) = *ty.kind() { - let &alloc = alloc.provenance().values().next()?; + let &alloc = alloc.provenance().ptrs().values().next()?; if let GlobalAlloc::Memory(alloc) = cx.tcx.global_alloc(alloc) { (alloc.inner(), ty) } else { @@ -262,6 +262,7 @@ fn read_mir_alloc_def_path<'tcx>(cx: &LateContext<'tcx>, alloc: &'tcx Allocation { alloc .provenance() + .ptrs() .values() .map(|&alloc| { if let GlobalAlloc::Memory(alloc) = cx.tcx.global_alloc(alloc) { @@ -293,38 +294,6 @@ fn path_from_array(exprs: &[Expr<'_>]) -> Option> { .collect() } -// def_path_res will match field names before anything else, but for this we want to match -// inherent functions first. -fn inherent_def_path_res(cx: &LateContext<'_>, segments: &[&str]) -> Option { - def_path_res(cx, segments, None).opt_def_id().map(|def_id| { - if cx.tcx.def_kind(def_id) == DefKind::Field { - let method_name = *segments.last().unwrap(); - cx.tcx - .def_key(def_id) - .parent - .and_then(|parent_idx| { - cx.tcx - .inherent_impls(DefId { - index: parent_idx, - krate: def_id.krate, - }) - .iter() - .find_map(|impl_id| { - cx.tcx.associated_items(*impl_id).find_by_name_and_kind( - cx.tcx, - Ident::from_str(method_name), - AssocKind::Fn, - *impl_id, - ) - }) - }) - .map_or(def_id, |item| item.def_id) - } else { - def_id - } - }) -} - fn get_lang_item_name(cx: &LateContext<'_>, def_id: DefId) -> Option<&'static str> { if let Some((lang_item, _)) = cx.tcx.lang_items().iter().find(|(_, id)| *id == def_id) { Some(lang_item.variant_name()) diff --git a/clippy_lints/src/write.rs b/clippy_lints/src/write.rs index 36574198f9174..6b321765bc082 100644 --- a/clippy_lints/src/write.rs +++ b/clippy_lints/src/write.rs @@ -1,6 +1,7 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_then}; use clippy_utils::macros::{root_macro_call_first_node, FormatArgsExpn, MacroCall}; use clippy_utils::source::{expand_past_previous_comma, snippet_opt}; +use clippy_utils::{is_in_cfg_test, is_in_test_function}; use rustc_ast::LitKind; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, HirIdMap, Impl, Item, ItemKind}; @@ -232,6 +233,16 @@ declare_clippy_lint! { #[derive(Default)] pub struct Write { in_debug_impl: bool, + allow_print_in_tests: bool, +} + +impl Write { + pub fn new(allow_print_in_tests: bool) -> Self { + Self { + allow_print_in_tests, + ..Default::default() + } + } } impl_lint_pass!(Write => [ @@ -271,13 +282,15 @@ impl<'tcx> LateLintPass<'tcx> for Write { .as_ref() .map_or(false, |crate_name| crate_name == "build_script_build"); + let allowed_in_tests = self.allow_print_in_tests + && (is_in_test_function(cx.tcx, expr.hir_id) || is_in_cfg_test(cx.tcx, expr.hir_id)); match diag_name { - sym::print_macro | sym::println_macro => { + sym::print_macro | sym::println_macro if !allowed_in_tests => { if !is_build_script { span_lint(cx, PRINT_STDOUT, macro_call.span, &format!("use of `{name}!`")); } }, - sym::eprint_macro | sym::eprintln_macro => { + sym::eprint_macro | sym::eprintln_macro if !allowed_in_tests => { span_lint(cx, PRINT_STDERR, macro_call.span, &format!("use of `{name}!`")); }, sym::write_macro | sym::writeln_macro => {}, diff --git a/clippy_utils/Cargo.toml b/clippy_utils/Cargo.toml index 83fee7bb39c22..fb9f4740ecc50 100644 --- a/clippy_utils/Cargo.toml +++ b/clippy_utils/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "clippy_utils" -version = "0.1.66" +version = "0.1.67" edition = "2021" publish = false diff --git a/clippy_utils/src/ast_utils.rs b/clippy_utils/src/ast_utils.rs index 23aed4b5ba2f4..939c61189ec8d 100644 --- a/clippy_utils/src/ast_utils.rs +++ b/clippy_utils/src/ast_utils.rs @@ -148,11 +148,19 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool { (Repeat(le, ls), Repeat(re, rs)) => eq_expr(le, re) && eq_expr(&ls.value, &rs.value), (Call(lc, la), Call(rc, ra)) => eq_expr(lc, rc) && over(la, ra, |l, r| eq_expr(l, r)), ( - MethodCall(box ast::MethodCall { seg: ls, receiver: lr, args: la, .. }), - MethodCall(box ast::MethodCall { seg: rs, receiver: rr, args: ra, .. }) - ) => { - eq_path_seg(ls, rs) && eq_expr(lr, rr) && over(la, ra, |l, r| eq_expr(l, r)) - }, + MethodCall(box ast::MethodCall { + seg: ls, + receiver: lr, + args: la, + .. + }), + MethodCall(box ast::MethodCall { + seg: rs, + receiver: rr, + args: ra, + .. + }), + ) => eq_path_seg(ls, rs) && eq_expr(lr, rr) && over(la, ra, |l, r| eq_expr(l, r)), (Binary(lo, ll, lr), Binary(ro, rl, rr)) => lo.node == ro.node && eq_expr(ll, rl) && eq_expr(lr, rr), (Unary(lo, l), Unary(ro, r)) => mem::discriminant(lo) == mem::discriminant(ro) && eq_expr(l, r), (Lit(l), Lit(r)) => l == r, @@ -191,7 +199,7 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool { fn_decl: rf, body: re, .. - }) + }), ) => { eq_closure_binder(lb, rb) && lc == rc diff --git a/clippy_utils/src/consts.rs b/clippy_utils/src/consts.rs index 07e4ef6a2fef3..315aea9aa091b 100644 --- a/clippy_utils/src/consts.rs +++ b/clippy_utils/src/consts.rs @@ -51,8 +51,8 @@ pub enum Constant { impl PartialEq for Constant { fn eq(&self, other: &Self) -> bool { match (self, other) { - (&Self::Str(ref ls), &Self::Str(ref rs)) => ls == rs, - (&Self::Binary(ref l), &Self::Binary(ref r)) => l == r, + (Self::Str(ls), Self::Str(rs)) => ls == rs, + (Self::Binary(l), Self::Binary(r)) => l == r, (&Self::Char(l), &Self::Char(r)) => l == r, (&Self::Int(l), &Self::Int(r)) => l == r, (&Self::F64(l), &Self::F64(r)) => { @@ -69,8 +69,8 @@ impl PartialEq for Constant { }, (&Self::Bool(l), &Self::Bool(r)) => l == r, (&Self::Vec(ref l), &Self::Vec(ref r)) | (&Self::Tuple(ref l), &Self::Tuple(ref r)) => l == r, - (&Self::Repeat(ref lv, ref ls), &Self::Repeat(ref rv, ref rs)) => ls == rs && lv == rv, - (&Self::Ref(ref lb), &Self::Ref(ref rb)) => *lb == *rb, + (Self::Repeat(lv, ls), Self::Repeat(rv, rs)) => ls == rs && lv == rv, + (Self::Ref(lb), Self::Ref(rb)) => *lb == *rb, // TODO: are there inter-type equalities? _ => false, } @@ -126,8 +126,8 @@ impl Hash for Constant { impl Constant { pub fn partial_cmp(tcx: TyCtxt<'_>, cmp_type: Ty<'_>, left: &Self, right: &Self) -> Option { match (left, right) { - (&Self::Str(ref ls), &Self::Str(ref rs)) => Some(ls.cmp(rs)), - (&Self::Char(ref l), &Self::Char(ref r)) => Some(l.cmp(r)), + (Self::Str(ls), Self::Str(rs)) => Some(ls.cmp(rs)), + (Self::Char(l), Self::Char(r)) => Some(l.cmp(r)), (&Self::Int(l), &Self::Int(r)) => match *cmp_type.kind() { ty::Int(int_ty) => Some(sext(tcx, l, int_ty).cmp(&sext(tcx, r, int_ty))), ty::Uint(_) => Some(l.cmp(&r)), @@ -135,8 +135,8 @@ impl Constant { }, (&Self::F64(l), &Self::F64(r)) => l.partial_cmp(&r), (&Self::F32(l), &Self::F32(r)) => l.partial_cmp(&r), - (&Self::Bool(ref l), &Self::Bool(ref r)) => Some(l.cmp(r)), - (&Self::Tuple(ref l), &Self::Tuple(ref r)) if l.len() == r.len() => match *cmp_type.kind() { + (Self::Bool(l), Self::Bool(r)) => Some(l.cmp(r)), + (Self::Tuple(l), Self::Tuple(r)) if l.len() == r.len() => match *cmp_type.kind() { ty::Tuple(tys) if tys.len() == l.len() => l .iter() .zip(r) @@ -146,17 +146,16 @@ impl Constant { .unwrap_or_else(|| Some(l.len().cmp(&r.len()))), _ => None, }, - (&Self::Vec(ref l), &Self::Vec(ref r)) => { - let cmp_type = match *cmp_type.kind() { - ty::Array(ty, _) | ty::Slice(ty) => ty, - _ => return None, + (Self::Vec(l), Self::Vec(r)) => { + let (ty::Array(cmp_type, _) | ty::Slice(cmp_type)) = *cmp_type.kind() else { + return None }; iter::zip(l, r) .map(|(li, ri)| Self::partial_cmp(tcx, cmp_type, li, ri)) .find(|r| r.map_or(true, |o| o != Ordering::Equal)) .unwrap_or_else(|| Some(l.len().cmp(&r.len()))) }, - (&Self::Repeat(ref lv, ref ls), &Self::Repeat(ref rv, ref rs)) => { + (Self::Repeat(lv, ls), Self::Repeat(rv, rs)) => { match Self::partial_cmp( tcx, match *cmp_type.kind() { @@ -170,7 +169,7 @@ impl Constant { x => x, } }, - (&Self::Ref(ref lb), &Self::Ref(ref rb)) => Self::partial_cmp( + (Self::Ref(lb), Self::Ref(rb)) => Self::partial_cmp( tcx, match *cmp_type.kind() { ty::Ref(_, ty, _) => ty, @@ -401,10 +400,7 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> { use self::Constant::{Int, F32, F64}; match *o { Int(value) => { - let ity = match *ty.kind() { - ty::Int(ity) => ity, - _ => return None, - }; + let ty::Int(ity) = *ty.kind() else { return None }; // sign extend let value = sext(self.lcx.tcx, value, ity); let value = value.checked_neg()?; diff --git a/clippy_utils/src/diagnostics.rs b/clippy_utils/src/diagnostics.rs index 78f93755b72d7..16b160b6fd27e 100644 --- a/clippy_utils/src/diagnostics.rs +++ b/clippy_utils/src/diagnostics.rs @@ -72,8 +72,8 @@ pub fn span_lint(cx: &T, lint: &'static Lint, sp: impl Into( - cx: &'a T, +pub fn span_lint_and_help( + cx: &T, lint: &'static Lint, span: impl Into, msg: &str, @@ -114,8 +114,8 @@ pub fn span_lint_and_help<'a, T: LintContext>( /// 10 | forget(&SomeStruct); /// | ^^^^^^^^^^^ /// ``` -pub fn span_lint_and_note<'a, T: LintContext>( - cx: &'a T, +pub fn span_lint_and_note( + cx: &T, lint: &'static Lint, span: impl Into, msg: &str, @@ -192,8 +192,8 @@ pub fn span_lint_hir_and_then( /// = note: `-D fold-any` implied by `-D warnings` /// ``` #[cfg_attr(feature = "internal", allow(clippy::collapsible_span_lint_calls))] -pub fn span_lint_and_sugg<'a, T: LintContext>( - cx: &'a T, +pub fn span_lint_and_sugg( + cx: &T, lint: &'static Lint, sp: Span, msg: &str, diff --git a/clippy_utils/src/hir_utils.rs b/clippy_utils/src/hir_utils.rs index cf24ec8b67b90..0231a51adf482 100644 --- a/clippy_utils/src/hir_utils.rs +++ b/clippy_utils/src/hir_utils.rs @@ -8,7 +8,7 @@ use rustc_hir::HirIdMap; use rustc_hir::{ ArrayLen, BinOpKind, BindingAnnotation, Block, BodyId, Closure, Expr, ExprField, ExprKind, FnRetTy, GenericArg, GenericArgs, Guard, HirId, InlineAsmOperand, Let, Lifetime, LifetimeName, ParamName, Pat, PatField, PatKind, Path, - PathSegment, QPath, Stmt, StmtKind, Ty, TyKind, TypeBinding, + PathSegment, PrimTy, QPath, Stmt, StmtKind, Ty, TyKind, TypeBinding, }; use rustc_lexer::{tokenize, TokenKind}; use rustc_lint::LateContext; @@ -131,13 +131,10 @@ impl HirEqInterExpr<'_, '_, '_> { ([], None, [], None) => { // For empty blocks, check to see if the tokens are equal. This will catch the case where a macro // expanded to nothing, or the cfg attribute was used. - let (left, right) = match ( + let (Some(left), Some(right)) = ( snippet_opt(self.inner.cx, left.span), snippet_opt(self.inner.cx, right.span), - ) { - (Some(left), Some(right)) => (left, right), - _ => return true, - }; + ) else { return true }; let mut left_pos = 0; let left = tokenize(&left) .map(|t| { @@ -269,7 +266,7 @@ impl HirEqInterExpr<'_, '_, '_> { (&ExprKind::Let(l), &ExprKind::Let(r)) => { self.eq_pat(l.pat, r.pat) && both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r)) && self.eq_expr(l.init, r.init) }, - (&ExprKind::Lit(ref l), &ExprKind::Lit(ref r)) => l.node == r.node, + (ExprKind::Lit(l), ExprKind::Lit(r)) => l.node == r.node, (&ExprKind::Loop(lb, ref ll, ref lls, _), &ExprKind::Loop(rb, ref rl, ref rls, _)) => { lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.name == r.ident.name) }, @@ -294,8 +291,8 @@ impl HirEqInterExpr<'_, '_, '_> { (&ExprKind::Repeat(le, ll), &ExprKind::Repeat(re, rl)) => { self.eq_expr(le, re) && self.eq_array_length(ll, rl) }, - (&ExprKind::Ret(ref l), &ExprKind::Ret(ref r)) => both(l, r, |l, r| self.eq_expr(l, r)), - (&ExprKind::Path(ref l), &ExprKind::Path(ref r)) => self.eq_qpath(l, r), + (ExprKind::Ret(l), ExprKind::Ret(r)) => both(l, r, |l, r| self.eq_expr(l, r)), + (ExprKind::Path(l), ExprKind::Path(r)) => self.eq_qpath(l, r), (&ExprKind::Struct(l_path, lf, ref lo), &ExprKind::Struct(r_path, rf, ref ro)) => { self.eq_qpath(l_path, r_path) && both(lo, ro, |l, r| self.eq_expr(l, r)) @@ -365,7 +362,7 @@ impl HirEqInterExpr<'_, '_, '_> { } eq }, - (&PatKind::Path(ref l), &PatKind::Path(ref r)) => self.eq_qpath(l, r), + (PatKind::Path(l), PatKind::Path(r)) => self.eq_qpath(l, r), (&PatKind::Lit(l), &PatKind::Lit(r)) => self.eq_expr(l, r), (&PatKind::Tuple(l, ls), &PatKind::Tuple(r, rs)) => ls == rs && over(l, r, |l, r| self.eq_pat(l, r)), (&PatKind::Range(ref ls, ref le, li), &PatKind::Range(ref rs, ref re, ri)) => { @@ -432,13 +429,11 @@ impl HirEqInterExpr<'_, '_, '_> { match (&left.kind, &right.kind) { (&TyKind::Slice(l_vec), &TyKind::Slice(r_vec)) => self.eq_ty(l_vec, r_vec), (&TyKind::Array(lt, ll), &TyKind::Array(rt, rl)) => self.eq_ty(lt, rt) && self.eq_array_length(ll, rl), - (&TyKind::Ptr(ref l_mut), &TyKind::Ptr(ref r_mut)) => { - l_mut.mutbl == r_mut.mutbl && self.eq_ty(l_mut.ty, r_mut.ty) - }, - (&TyKind::Rptr(_, ref l_rmut), &TyKind::Rptr(_, ref r_rmut)) => { + (TyKind::Ptr(l_mut), TyKind::Ptr(r_mut)) => l_mut.mutbl == r_mut.mutbl && self.eq_ty(l_mut.ty, r_mut.ty), + (TyKind::Rptr(_, l_rmut), TyKind::Rptr(_, r_rmut)) => { l_rmut.mutbl == r_rmut.mutbl && self.eq_ty(l_rmut.ty, r_rmut.ty) }, - (&TyKind::Path(ref l), &TyKind::Path(ref r)) => self.eq_qpath(l, r), + (TyKind::Path(l), TyKind::Path(r)) => self.eq_qpath(l, r), (&TyKind::Tup(l), &TyKind::Tup(r)) => over(l, r, |l, r| self.eq_ty(l, r)), (&TyKind::Infer, &TyKind::Infer) => true, _ => false, @@ -1033,6 +1028,14 @@ pub fn hash_stmt(cx: &LateContext<'_>, s: &Stmt<'_>) -> u64 { h.finish() } +pub fn is_bool(ty: &Ty<'_>) -> bool { + if let TyKind::Path(QPath::Resolved(_, path)) = ty.kind { + matches!(path.res, Res::PrimTy(PrimTy::Bool)) + } else { + false + } +} + pub fn hash_expr(cx: &LateContext<'_>, e: &Expr<'_>) -> u64 { let mut h = SpanlessHash::new(cx); h.hash_expr(e); diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index 3f93b9b491d4c..9e2682925a221 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -66,7 +66,7 @@ pub mod visitors; pub use self::attrs::*; pub use self::check_proc_macro::{is_from_proc_macro, is_span_if, is_span_match}; pub use self::hir_utils::{ - both, count_eq, eq_expr_value, hash_expr, hash_stmt, over, HirEqInterExpr, SpanlessEq, SpanlessHash, + both, count_eq, eq_expr_value, hash_expr, hash_stmt, is_bool, over, HirEqInterExpr, SpanlessEq, SpanlessHash, }; use core::ops::ControlFlow; @@ -80,17 +80,16 @@ use rustc_ast::ast::{self, LitKind}; use rustc_ast::Attribute; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::unhash::UnhashMap; -use rustc_hir as hir; -use rustc_hir::def::{DefKind, Namespace, Res}; -use rustc_hir::def_id::{CrateNum, DefId, LocalDefId}; +use rustc_hir::def::{DefKind, Res}; +use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE}; use rustc_hir::hir_id::{HirIdMap, HirIdSet}; use rustc_hir::intravisit::{walk_expr, FnKind, Visitor}; use rustc_hir::LangItem::{OptionNone, ResultErr, ResultOk}; use rustc_hir::{ - def, Arm, ArrayLen, BindingAnnotation, Block, BlockCheckMode, Body, Closure, Constness, - Destination, Expr, ExprKind, FnDecl, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind, - LangItem, Local, MatchSource, Mutability, Node, Param, Pat, PatKind, Path, PathSegment, PrimTy, - QPath, Stmt, StmtKind, TraitItem, TraitItemKind, TraitRef, TyKind, UnOp, + self as hir, def, Arm, ArrayLen, BindingAnnotation, Block, BlockCheckMode, Body, Closure, Constness, Destination, + Expr, ExprKind, FnDecl, HirId, Impl, ImplItem, ImplItemKind, ImplItemRef, IsAsync, Item, ItemKind, LangItem, Local, + MatchSource, Mutability, Node, OwnerId, Param, Pat, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, + TraitItem, TraitItemKind, TraitItemRef, TraitRef, TyKind, UnOp, }; use rustc_lexer::{tokenize, TokenKind}; use rustc_lint::{LateContext, Level, Lint, LintContext}; @@ -109,11 +108,10 @@ use rustc_middle::ty::{FloatTy, IntTy, UintTy}; use rustc_semver::RustcVersion; use rustc_session::Session; use rustc_span::hygiene::{ExpnKind, MacroKind}; -use rustc_span::source_map::original_sp; use rustc_span::source_map::SourceMap; use rustc_span::sym; -use rustc_span::symbol::{kw, Symbol}; -use rustc_span::{Span, DUMMY_SP}; +use rustc_span::symbol::{kw, Ident, Symbol}; +use rustc_span::Span; use rustc_target::abi::Integer; use crate::consts::{constant, Constant}; @@ -436,11 +434,7 @@ pub fn is_expr_path_def_path(cx: &LateContext<'_>, expr: &Expr<'_>, segments: &[ /// If `maybe_path` is a path node which resolves to an item, resolves it to a `DefId` and checks if /// it matches the given lang item. -pub fn is_path_lang_item<'tcx>( - cx: &LateContext<'_>, - maybe_path: &impl MaybePath<'tcx>, - lang_item: LangItem, -) -> bool { +pub fn is_path_lang_item<'tcx>(cx: &LateContext<'_>, maybe_path: &impl MaybePath<'tcx>, lang_item: LangItem) -> bool { path_def_id(cx, maybe_path).map_or(false, |id| cx.tcx.lang_items().get(lang_item) == Some(id)) } @@ -535,125 +529,177 @@ pub fn path_def_id<'tcx>(cx: &LateContext<'_>, maybe_path: &impl MaybePath<'tcx> path_res(cx, maybe_path).opt_def_id() } -fn find_primitive<'tcx>(tcx: TyCtxt<'tcx>, name: &str) -> impl Iterator + 'tcx { - let single = |ty| tcx.incoherent_impls(ty).iter().copied(); - let empty = || [].iter().copied(); - match name { - "bool" => single(BoolSimplifiedType), - "char" => single(CharSimplifiedType), - "str" => single(StrSimplifiedType), - "array" => single(ArraySimplifiedType), - "slice" => single(SliceSimplifiedType), +fn find_primitive_impls<'tcx>(tcx: TyCtxt<'tcx>, name: &str) -> impl Iterator + 'tcx { + let ty = match name { + "bool" => BoolSimplifiedType, + "char" => CharSimplifiedType, + "str" => StrSimplifiedType, + "array" => ArraySimplifiedType, + "slice" => SliceSimplifiedType, // FIXME: rustdoc documents these two using just `pointer`. // // Maybe this is something we should do here too. - "const_ptr" => single(PtrSimplifiedType(Mutability::Not)), - "mut_ptr" => single(PtrSimplifiedType(Mutability::Mut)), - "isize" => single(IntSimplifiedType(IntTy::Isize)), - "i8" => single(IntSimplifiedType(IntTy::I8)), - "i16" => single(IntSimplifiedType(IntTy::I16)), - "i32" => single(IntSimplifiedType(IntTy::I32)), - "i64" => single(IntSimplifiedType(IntTy::I64)), - "i128" => single(IntSimplifiedType(IntTy::I128)), - "usize" => single(UintSimplifiedType(UintTy::Usize)), - "u8" => single(UintSimplifiedType(UintTy::U8)), - "u16" => single(UintSimplifiedType(UintTy::U16)), - "u32" => single(UintSimplifiedType(UintTy::U32)), - "u64" => single(UintSimplifiedType(UintTy::U64)), - "u128" => single(UintSimplifiedType(UintTy::U128)), - "f32" => single(FloatSimplifiedType(FloatTy::F32)), - "f64" => single(FloatSimplifiedType(FloatTy::F64)), - _ => empty(), - } -} - -/// Resolves a def path like `std::vec::Vec`. `namespace_hint` can be supplied to disambiguate -/// between `std::vec` the module and `std::vec` the macro -/// -/// This function is expensive and should be used sparingly. -pub fn def_path_res(cx: &LateContext<'_>, path: &[&str], namespace_hint: Option) -> Res { - fn item_child_by_name(tcx: TyCtxt<'_>, def_id: DefId, name: &str, matches_ns: impl Fn(Res) -> bool) -> Option { - match tcx.def_kind(def_id) { - DefKind::Mod | DefKind::Enum | DefKind::Trait => tcx - .module_children(def_id) - .iter() - .find(|item| item.ident.name.as_str() == name && matches_ns(item.res.expect_non_local())) - .map(|child| child.res.expect_non_local()), - DefKind::Impl => tcx - .associated_item_def_ids(def_id) - .iter() - .copied() - .find(|assoc_def_id| tcx.item_name(*assoc_def_id).as_str() == name) - .map(|assoc_def_id| Res::Def(tcx.def_kind(assoc_def_id), assoc_def_id)), - DefKind::Struct | DefKind::Union => tcx - .adt_def(def_id) - .non_enum_variant() - .fields - .iter() - .find(|f| f.name.as_str() == name) - .map(|f| Res::Def(DefKind::Field, f.did)), - _ => None, + "const_ptr" => PtrSimplifiedType(Mutability::Not), + "mut_ptr" => PtrSimplifiedType(Mutability::Mut), + "isize" => IntSimplifiedType(IntTy::Isize), + "i8" => IntSimplifiedType(IntTy::I8), + "i16" => IntSimplifiedType(IntTy::I16), + "i32" => IntSimplifiedType(IntTy::I32), + "i64" => IntSimplifiedType(IntTy::I64), + "i128" => IntSimplifiedType(IntTy::I128), + "usize" => UintSimplifiedType(UintTy::Usize), + "u8" => UintSimplifiedType(UintTy::U8), + "u16" => UintSimplifiedType(UintTy::U16), + "u32" => UintSimplifiedType(UintTy::U32), + "u64" => UintSimplifiedType(UintTy::U64), + "u128" => UintSimplifiedType(UintTy::U128), + "f32" => FloatSimplifiedType(FloatTy::F32), + "f64" => FloatSimplifiedType(FloatTy::F64), + _ => return [].iter().copied(), + }; + + tcx.incoherent_impls(ty).iter().copied() +} + +fn non_local_item_children_by_name(tcx: TyCtxt<'_>, def_id: DefId, name: Symbol) -> Vec { + match tcx.def_kind(def_id) { + DefKind::Mod | DefKind::Enum | DefKind::Trait => tcx + .module_children(def_id) + .iter() + .filter(|item| item.ident.name == name) + .map(|child| child.res.expect_non_local()) + .collect(), + DefKind::Impl => tcx + .associated_item_def_ids(def_id) + .iter() + .copied() + .filter(|assoc_def_id| tcx.item_name(*assoc_def_id) == name) + .map(|assoc_def_id| Res::Def(tcx.def_kind(assoc_def_id), assoc_def_id)) + .collect(), + _ => Vec::new(), + } +} + +fn local_item_children_by_name(tcx: TyCtxt<'_>, local_id: LocalDefId, name: Symbol) -> Vec { + let hir = tcx.hir(); + + let root_mod; + let item_kind = match hir.find_by_def_id(local_id) { + Some(Node::Crate(r#mod)) => { + root_mod = ItemKind::Mod(r#mod); + &root_mod + }, + Some(Node::Item(item)) => &item.kind, + _ => return Vec::new(), + }; + + let res = |ident: Ident, owner_id: OwnerId| { + if ident.name == name { + let def_id = owner_id.to_def_id(); + Some(Res::Def(tcx.def_kind(def_id), def_id)) + } else { + None } + }; + + match item_kind { + ItemKind::Mod(r#mod) => r#mod + .item_ids + .iter() + .filter_map(|&item_id| res(hir.item(item_id).ident, item_id.owner_id)) + .collect(), + ItemKind::Impl(r#impl) => r#impl + .items + .iter() + .filter_map(|&ImplItemRef { ident, id, .. }| res(ident, id.owner_id)) + .collect(), + ItemKind::Trait(.., trait_item_refs) => trait_item_refs + .iter() + .filter_map(|&TraitItemRef { ident, id, .. }| res(ident, id.owner_id)) + .collect(), + _ => Vec::new(), } +} + +fn item_children_by_name(tcx: TyCtxt<'_>, def_id: DefId, name: Symbol) -> Vec { + if let Some(local_id) = def_id.as_local() { + local_item_children_by_name(tcx, local_id, name) + } else { + non_local_item_children_by_name(tcx, def_id, name) + } +} - fn find_crate(tcx: TyCtxt<'_>, name: &str) -> Option { +/// Resolves a def path like `std::vec::Vec`. +/// +/// Can return multiple resolutions when there are multiple versions of the same crate, e.g. +/// `memchr::memchr` could return the functions from both memchr 1.0 and memchr 2.0. +/// +/// Also returns multiple results when there are mulitple paths under the same name e.g. `std::vec` +/// would have both a [`DefKind::Mod`] and [`DefKind::Macro`]. +/// +/// This function is expensive and should be used sparingly. +pub fn def_path_res(cx: &LateContext<'_>, path: &[&str]) -> Vec { + fn find_crates(tcx: TyCtxt<'_>, name: Symbol) -> impl Iterator + '_ { tcx.crates(()) .iter() .copied() - .find(|&num| tcx.crate_name(num).as_str() == name) + .filter(move |&num| tcx.crate_name(num) == name) .map(CrateNum::as_def_id) } - let (base, path) = match *path { + let tcx = cx.tcx; + + let (base, mut path) = match *path { [primitive] => { - return PrimTy::from_name(Symbol::intern(primitive)).map_or(Res::Err, Res::PrimTy); + return vec![PrimTy::from_name(Symbol::intern(primitive)).map_or(Res::Err, Res::PrimTy)]; }, [base, ref path @ ..] => (base, path), - _ => return Res::Err, + _ => return Vec::new(), }; - let tcx = cx.tcx; - let starts = find_primitive(tcx, base) - .chain(find_crate(tcx, base)) + + let base_sym = Symbol::intern(base); + + let local_crate = if tcx.crate_name(LOCAL_CRATE) == base_sym { + Some(LOCAL_CRATE.as_def_id()) + } else { + None + }; + + let starts = find_primitive_impls(tcx, base) + .chain(find_crates(tcx, base_sym)) + .chain(local_crate) .map(|id| Res::Def(tcx.def_kind(id), id)); - for first in starts { - let last = path - .iter() - .copied() - .enumerate() - // for each segment, find the child item - .try_fold(first, |res, (idx, segment)| { - let matches_ns = |res: Res| { - // If at the last segment in the path, respect the namespace hint - if idx == path.len() - 1 { - match namespace_hint { - Some(ns) => res.matches_ns(ns), - None => true, - } - } else { - res.matches_ns(Namespace::TypeNS) - } - }; - - let def_id = res.def_id(); - if let Some(item) = item_child_by_name(tcx, def_id, segment, matches_ns) { - Some(item) - } else if matches!(res, Res::Def(DefKind::Enum | DefKind::Struct, _)) { - // it is not a child item so check inherent impl items - tcx.inherent_impls(def_id) - .iter() - .find_map(|&impl_def_id| item_child_by_name(tcx, impl_def_id, segment, matches_ns)) - } else { - None - } - }); + let mut resolutions: Vec = starts.collect(); - if let Some(last) = last { - return last; - } + while let [segment, rest @ ..] = path { + path = rest; + let segment = Symbol::intern(segment); + + resolutions = resolutions + .into_iter() + .filter_map(|res| res.opt_def_id()) + .flat_map(|def_id| { + // When the current def_id is e.g. `struct S`, check the impl items in + // `impl S { ... }` + let inherent_impl_children = tcx + .inherent_impls(def_id) + .iter() + .flat_map(|&impl_def_id| item_children_by_name(tcx, impl_def_id, segment)); + + let direct_children = item_children_by_name(tcx, def_id, segment); + + inherent_impl_children.chain(direct_children) + }) + .collect(); } - Res::Err + resolutions +} + +/// Resolves a def path like `std::vec::Vec` to its [`DefId`]s, see [`def_path_res`]. +pub fn def_path_def_ids(cx: &LateContext<'_>, path: &[&str]) -> impl Iterator { + def_path_res(cx, path).into_iter().filter_map(|res| res.opt_def_id()) } /// Convenience function to get the `DefId` of a trait by path. @@ -661,10 +707,10 @@ pub fn def_path_res(cx: &LateContext<'_>, path: &[&str], namespace_hint: Option< /// /// This function is expensive and should be used sparingly. pub fn get_trait_def_id(cx: &LateContext<'_>, path: &[&str]) -> Option { - match def_path_res(cx, path, Some(Namespace::TypeNS)) { + def_path_res(cx, path).into_iter().find_map(|res| match res { Res::Def(DefKind::Trait | DefKind::TraitAlias, trait_id) => Some(trait_id), _ => None, - } + }) } /// Gets the `hir::TraitRef` of the trait the given method is implemented for. @@ -784,9 +830,9 @@ fn is_default_equivalent_ctor(cx: &LateContext<'_>, def_id: DefId, path: &QPath< if method.ident.name == sym::new { if let Some(impl_did) = cx.tcx.impl_of_method(def_id) { if let Some(adt) = cx.tcx.type_of(impl_did).ty_adt_def() { - return std_types_symbols - .iter() - .any(|&symbol| cx.tcx.is_diagnostic_item(symbol, adt.did()) || Some(adt.did()) == cx.tcx.lang_items().string()); + return std_types_symbols.iter().any(|&symbol| { + cx.tcx.is_diagnostic_item(symbol, adt.did()) || Some(adt.did()) == cx.tcx.lang_items().string() + }); } } } @@ -963,7 +1009,7 @@ impl std::ops::BitOrAssign for CaptureKind { /// Note as this will walk up to parent expressions until the capture can be determined it should /// only be used while making a closure somewhere a value is consumed. e.g. a block, match arm, or /// function argument (other than a receiver). -pub fn capture_local_usage<'tcx>(cx: &LateContext<'tcx>, e: &Expr<'_>) -> CaptureKind { +pub fn capture_local_usage(cx: &LateContext<'_>, e: &Expr<'_>) -> CaptureKind { fn pat_capture_kind(cx: &LateContext<'_>, pat: &Pat<'_>) -> CaptureKind { let mut capture = CaptureKind::Ref(Mutability::Not); pat.each_binding_or_first(&mut |_, id, span, _| match cx @@ -1260,23 +1306,6 @@ pub fn contains_return(expr: &hir::Expr<'_>) -> bool { .is_some() } -/// Extends the span to the beginning of the spans line, incl. whitespaces. -/// -/// ```rust -/// let x = (); -/// // ^^ -/// // will be converted to -/// let x = (); -/// // ^^^^^^^^^^^^^^ -/// ``` -fn line_span(cx: &T, span: Span) -> Span { - let span = original_sp(span, DUMMY_SP); - let source_map_and_line = cx.sess().source_map().lookup_line(span.lo()).unwrap(); - let line_no = source_map_and_line.line; - let line_start = source_map_and_line.sf.lines(|lines| lines[line_no]); - span.with_lo(line_start) -} - /// Gets the parent node, if any. pub fn get_parent_node(tcx: TyCtxt<'_>, id: HirId) -> Option> { tcx.hir().parent_iter(id).next().map(|(_, node)| node) @@ -1749,6 +1778,10 @@ pub fn has_attr(attrs: &[ast::Attribute], symbol: Symbol) -> bool { attrs.iter().any(|attr| attr.has_name(symbol)) } +pub fn has_repr_attr(cx: &LateContext<'_>, hir_id: HirId) -> bool { + has_attr(cx.tcx.hir().attrs(hir_id), sym::repr) +} + pub fn any_parent_has_attr(tcx: TyCtxt<'_>, node: HirId, symbol: Symbol) -> bool { let map = &tcx.hir(); let mut prev_enclosing_node = None; @@ -1821,7 +1854,7 @@ pub fn match_any_def_paths(cx: &LateContext<'_>, did: DefId, paths: &[&[&str]]) } /// Checks if the given `DefId` matches the path. -pub fn match_def_path<'tcx>(cx: &LateContext<'tcx>, did: DefId, syms: &[&str]) -> bool { +pub fn match_def_path(cx: &LateContext<'_>, did: DefId, syms: &[&str]) -> bool { // We should probably move to Symbols in Clippy as well rather than interning every time. let path = cx.get_def_path(did); syms.iter().map(|x| Symbol::intern(x)).eq(path.iter().copied()) @@ -1870,7 +1903,11 @@ pub fn if_sequence<'tcx>(mut expr: &'tcx Expr<'tcx>) -> (Vec<&'tcx Expr<'tcx>>, /// Checks if the given function kind is an async function. pub fn is_async_fn(kind: FnKind<'_>) -> bool { - matches!(kind, FnKind::ItemFn(_, _, header) if header.asyncness.is_async()) + match kind { + FnKind::ItemFn(_, _, header) => header.asyncness == IsAsync::Async, + FnKind::Method(_, sig) => sig.header.asyncness == IsAsync::Async, + FnKind::Closure => false, + } } /// Peels away all the compiler generated code surrounding the body of an async function, diff --git a/clippy_utils/src/msrvs.rs b/clippy_utils/src/msrvs.rs index 8b843732a236b..79b19e6fb3eb0 100644 --- a/clippy_utils/src/msrvs.rs +++ b/clippy_utils/src/msrvs.rs @@ -12,13 +12,14 @@ macro_rules! msrv_aliases { // names may refer to stabilized feature flags or library items msrv_aliases! { + 1,65,0 { LET_ELSE } 1,62,0 { BOOL_THEN_SOME } 1,58,0 { FORMAT_ARGS_CAPTURE } 1,53,0 { OR_PATTERNS, MANUAL_BITS, BTREE_MAP_RETAIN, BTREE_SET_RETAIN, ARRAY_INTO_ITERATOR } 1,52,0 { STR_SPLIT_ONCE, REM_EUCLID_CONST } - 1,51,0 { BORROW_AS_PTR, UNSIGNED_ABS } + 1,51,0 { BORROW_AS_PTR, SEEK_FROM_CURRENT, UNSIGNED_ABS } 1,50,0 { BOOL_THEN, CLAMP } - 1,47,0 { TAU } + 1,47,0 { TAU, IS_ASCII_DIGIT_CONST } 1,46,0 { CONST_IF_MATCH } 1,45,0 { STR_STRIP_PREFIX } 1,43,0 { LOG2_10, LOG10_2 } @@ -37,4 +38,5 @@ msrv_aliases! { 1,18,0 { HASH_MAP_RETAIN, HASH_SET_RETAIN } 1,17,0 { FIELD_INIT_SHORTHAND, STATIC_IN_CONST, EXPECT_ERR } 1,16,0 { STR_REPEAT } + 1,55,0 { SEEK_REWIND } } diff --git a/clippy_utils/src/paths.rs b/clippy_utils/src/paths.rs index bc85147343045..6c09c146082ab 100644 --- a/clippy_utils/src/paths.rs +++ b/clippy_utils/src/paths.rs @@ -115,6 +115,9 @@ pub const STDERR: [&str; 4] = ["std", "io", "stdio", "stderr"]; pub const STDOUT: [&str; 4] = ["std", "io", "stdio", "stdout"]; pub const CONVERT_IDENTITY: [&str; 3] = ["core", "convert", "identity"]; pub const STD_FS_CREATE_DIR: [&str; 3] = ["std", "fs", "create_dir"]; +pub const STD_IO_SEEK: [&str; 3] = ["std", "io", "Seek"]; +pub const STD_IO_SEEK_FROM_CURRENT: [&str; 4] = ["std", "io", "SeekFrom", "Current"]; +pub const STD_IO_SEEKFROM_START: [&str; 4] = ["std", "io", "SeekFrom", "Start"]; pub const STRING_AS_MUT_STR: [&str; 4] = ["alloc", "string", "String", "as_mut_str"]; pub const STRING_AS_STR: [&str; 4] = ["alloc", "string", "String", "as_str"]; pub const STRING_NEW: [&str; 4] = ["alloc", "string", "String", "new"]; diff --git a/clippy_utils/src/qualify_min_const_fn.rs b/clippy_utils/src/qualify_min_const_fn.rs index 45b63a4aa5df8..65722f142aa69 100644 --- a/clippy_utils/src/qualify_min_const_fn.rs +++ b/clippy_utils/src/qualify_min_const_fn.rs @@ -18,7 +18,7 @@ use std::borrow::Cow; type McfResult = Result<(), (Span, Cow<'static, str>)>; -pub fn is_min_const_fn<'a, 'tcx>(tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, msrv: Option) -> McfResult { +pub fn is_min_const_fn<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, msrv: Option) -> McfResult { let def_id = body.source.def_id(); let mut current = def_id; loop { @@ -276,9 +276,9 @@ fn check_place<'tcx>(tcx: TyCtxt<'tcx>, place: Place<'tcx>, span: Span, body: &B Ok(()) } -fn check_terminator<'a, 'tcx>( +fn check_terminator<'tcx>( tcx: TyCtxt<'tcx>, - body: &'a Body<'tcx>, + body: &Body<'tcx>, terminator: &Terminator<'tcx>, msrv: Option, ) -> McfResult { diff --git a/clippy_utils/src/source.rs b/clippy_utils/src/source.rs index d28bd92d708ba..eacfa91ba556d 100644 --- a/clippy_utils/src/source.rs +++ b/clippy_utils/src/source.rs @@ -2,13 +2,12 @@ #![allow(clippy::module_name_repetitions)] -use crate::line_span; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LintContext}; use rustc_span::hygiene; -use rustc_span::source_map::SourceMap; -use rustc_span::{BytePos, Pos, Span, SpanData, SyntaxContext}; +use rustc_span::source_map::{original_sp, SourceMap}; +use rustc_span::{BytePos, Pos, Span, SpanData, SyntaxContext, DUMMY_SP}; use std::borrow::Cow; /// Like `snippet_block`, but add braces if the expr is not an `ExprKind::Block`. @@ -55,6 +54,23 @@ fn first_char_in_first_line(cx: &T, span: Span) -> Option(cx: &T, span: Span) -> Span { + let span = original_sp(span, DUMMY_SP); + let source_map_and_line = cx.sess().source_map().lookup_line(span.lo()).unwrap(); + let line_no = source_map_and_line.line; + let line_start = source_map_and_line.sf.lines(|lines| lines[line_no]); + span.with_lo(line_start) +} + /// Returns the indentation of the line of a span /// /// ```rust,ignore diff --git a/clippy_utils/src/sugg.rs b/clippy_utils/src/sugg.rs index eefba8cd29c41..3cacdb4937721 100644 --- a/clippy_utils/src/sugg.rs +++ b/clippy_utils/src/sugg.rs @@ -801,7 +801,7 @@ pub struct DerefClosure { /// Returns `None` if no such use cases have been triggered in closure body /// /// note: this only works on single line immutable closures with exactly one input parameter. -pub fn deref_closure_args<'tcx>(cx: &LateContext<'_>, closure: &'tcx hir::Expr<'_>) -> Option { +pub fn deref_closure_args(cx: &LateContext<'_>, closure: &hir::Expr<'_>) -> Option { if let hir::ExprKind::Closure(&Closure { fn_decl, body, .. }) = closure.kind { let closure_body = cx.tcx.hir().body(body); // is closure arg a type annotated double reference (i.e.: `|x: &&i32| ...`) diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index 3a144c2bb2239..897edfc5495f4 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -13,8 +13,9 @@ use rustc_infer::infer::TyCtxtInferExt; use rustc_lint::LateContext; use rustc_middle::mir::interpret::{ConstValue, Scalar}; use rustc_middle::ty::{ - self, AdtDef, Binder, BoundRegion, DefIdTree, FnSig, IntTy, ParamEnv, Predicate, PredicateKind, ProjectionTy, - Region, RegionKind, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor, UintTy, VariantDef, VariantDiscr, + self, AdtDef, AssocKind, Binder, BoundRegion, DefIdTree, FnSig, GenericParamDefKind, IntTy, List, ParamEnv, + Predicate, PredicateKind, ProjectionTy, Region, RegionKind, SubstsRef, Ty, TyCtxt, TypeSuperVisitable, + TypeVisitable, TypeVisitor, UintTy, VariantDef, VariantDiscr, }; use rustc_middle::ty::{GenericArg, GenericArgKind}; use rustc_span::symbol::Ident; @@ -59,6 +60,58 @@ pub fn contains_adt_constructor<'tcx>(ty: Ty<'tcx>, adt: AdtDef<'tcx>) -> bool { }) } +/// Walks into `ty` and returns `true` if any inner type is an instance of the given type, or adt +/// constructor of the same type. +/// +/// This method also recurses into opaque type predicates, so call it with `impl Trait` and `U` +/// will also return `true`. +pub fn contains_ty_adt_constructor_opaque<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, needle: Ty<'tcx>) -> bool { + ty.walk().any(|inner| match inner.unpack() { + GenericArgKind::Type(inner_ty) => { + if inner_ty == needle { + return true; + } + + if inner_ty.ty_adt_def() == needle.ty_adt_def() { + return true; + } + + if let ty::Opaque(def_id, _) = *inner_ty.kind() { + for &(predicate, _span) in cx.tcx.explicit_item_bounds(def_id) { + match predicate.kind().skip_binder() { + // For `impl Trait`, it will register a predicate of `T: Trait`, so we go through + // and check substituions to find `U`. + ty::PredicateKind::Trait(trait_predicate) => { + if trait_predicate + .trait_ref + .substs + .types() + .skip(1) // Skip the implicit `Self` generic parameter + .any(|ty| contains_ty_adt_constructor_opaque(cx, ty, needle)) + { + return true; + } + }, + // For `impl Trait`, it will register a predicate of `::Assoc = U`, + // so we check the term for `U`. + ty::PredicateKind::Projection(projection_predicate) => { + if let ty::TermKind::Ty(ty) = projection_predicate.term.unpack() { + if contains_ty_adt_constructor_opaque(cx, ty, needle) { + return true; + } + }; + }, + _ => (), + } + } + } + + false + }, + GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false, + }) +} + /// Resolves `::Item` for `T` /// Do not invoke without first verifying that the type implements `Iterator` pub fn get_iterator_item_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option> { @@ -701,7 +754,7 @@ impl core::ops::Add for EnumValue { } } -/// Attempts to read the given constant as though it were an an enum value. +/// Attempts to read the given constant as though it were an enum value. #[expect(clippy::cast_possible_truncation, clippy::cast_possible_wrap)] pub fn read_explicit_enum_value(tcx: TyCtxt<'_>, id: DefId) -> Option { if let Ok(ConstValue::Scalar(Scalar::Int(value))) = tcx.const_eval_poly(id) { @@ -782,6 +835,42 @@ pub fn for_each_top_level_late_bound_region( ty.visit_with(&mut V { index: 0, f }) } +pub struct AdtVariantInfo { + pub ind: usize, + pub size: u64, + + /// (ind, size) + pub fields_size: Vec<(usize, u64)>, +} + +impl AdtVariantInfo { + /// Returns ADT variants ordered by size + pub fn new<'tcx>(cx: &LateContext<'tcx>, adt: AdtDef<'tcx>, subst: &'tcx List>) -> Vec { + let mut variants_size = adt + .variants() + .iter() + .enumerate() + .map(|(i, variant)| { + let mut fields_size = variant + .fields + .iter() + .enumerate() + .map(|(i, f)| (i, approx_ty_size(cx, f.ty(cx.tcx, subst)))) + .collect::>(); + fields_size.sort_by(|(_, a_size), (_, b_size)| (a_size.cmp(b_size))); + + Self { + ind: i, + size: fields_size.iter().map(|(_, size)| size).sum(), + fields_size, + } + }) + .collect::>(); + variants_size.sort_by(|a, b| (b.size.cmp(&a.size))); + variants_size + } +} + /// Gets the struct or enum variant from the given `Res` pub fn variant_of_res<'tcx>(cx: &LateContext<'tcx>, res: Res) -> Option<&'tcx VariantDef> { match res { @@ -876,3 +965,132 @@ pub fn approx_ty_size<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> u64 { (Err(_), _) => 0, } } + +/// Makes the projection type for the named associated type in the given impl or trait impl. +/// +/// This function is for associated types which are "known" to exist, and as such, will only return +/// `None` when debug assertions are disabled in order to prevent ICE's. With debug assertions +/// enabled this will check that the named associated type exists, the correct number of +/// substitutions are given, and that the correct kinds of substitutions are given (lifetime, +/// constant or type). This will not check if type normalization would succeed. +pub fn make_projection<'tcx>( + tcx: TyCtxt<'tcx>, + container_id: DefId, + assoc_ty: Symbol, + substs: impl IntoIterator>>, +) -> Option> { + fn helper<'tcx>( + tcx: TyCtxt<'tcx>, + container_id: DefId, + assoc_ty: Symbol, + substs: SubstsRef<'tcx>, + ) -> Option> { + let Some(assoc_item) = tcx + .associated_items(container_id) + .find_by_name_and_kind(tcx, Ident::with_dummy_span(assoc_ty), AssocKind::Type, container_id) + else { + debug_assert!(false, "type `{assoc_ty}` not found in `{container_id:?}`"); + return None; + }; + #[cfg(debug_assertions)] + { + let generics = tcx.generics_of(assoc_item.def_id); + let generic_count = generics.parent_count + generics.params.len(); + let params = generics + .parent + .map_or([].as_slice(), |id| &*tcx.generics_of(id).params) + .iter() + .chain(&generics.params) + .map(|x| &x.kind); + + debug_assert!( + generic_count == substs.len(), + "wrong number of substs for `{:?}`: found `{}` expected `{}`.\n\ + note: the expected parameters are: {:#?}\n\ + the given arguments are: `{:#?}`", + assoc_item.def_id, + substs.len(), + generic_count, + params.map(GenericParamDefKind::descr).collect::>(), + substs, + ); + + if let Some((idx, (param, arg))) = params + .clone() + .zip(substs.iter().map(GenericArg::unpack)) + .enumerate() + .find(|(_, (param, arg))| { + !matches!( + (param, arg), + (GenericParamDefKind::Lifetime, GenericArgKind::Lifetime(_)) + | (GenericParamDefKind::Type { .. }, GenericArgKind::Type(_)) + | (GenericParamDefKind::Const { .. }, GenericArgKind::Const(_)) + ) + }) + { + debug_assert!( + false, + "mismatched subst type at index {}: expected a {}, found `{:?}`\n\ + note: the expected parameters are {:#?}\n\ + the given arguments are {:#?}", + idx, + param.descr(), + arg, + params.map(GenericParamDefKind::descr).collect::>(), + substs, + ); + } + } + + Some(ProjectionTy { + substs, + item_def_id: assoc_item.def_id, + }) + } + helper( + tcx, + container_id, + assoc_ty, + tcx.mk_substs(substs.into_iter().map(Into::into)), + ) +} + +/// Normalizes the named associated type in the given impl or trait impl. +/// +/// This function is for associated types which are "known" to be valid with the given +/// substitutions, and as such, will only return `None` when debug assertions are disabled in order +/// to prevent ICE's. With debug assertions enabled this will check that that type normalization +/// succeeds as well as everything checked by `make_projection`. +pub fn make_normalized_projection<'tcx>( + tcx: TyCtxt<'tcx>, + param_env: ParamEnv<'tcx>, + container_id: DefId, + assoc_ty: Symbol, + substs: impl IntoIterator>>, +) -> Option> { + fn helper<'tcx>(tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, ty: ProjectionTy<'tcx>) -> Option> { + #[cfg(debug_assertions)] + if let Some((i, subst)) = ty + .substs + .iter() + .enumerate() + .find(|(_, subst)| subst.has_late_bound_regions()) + { + debug_assert!( + false, + "substs contain late-bound region at index `{i}` which can't be normalized.\n\ + use `TyCtxt::erase_late_bound_regions`\n\ + note: subst is `{subst:#?}`", + ); + return None; + } + match tcx.try_normalize_erasing_regions(param_env, tcx.mk_projection(ty.item_def_id, ty.substs)) { + Ok(ty) => Some(ty), + Err(e) => { + debug_assert!(false, "failed to normalize type `{ty}`: {e:#?}"); + None + }, + } + } + helper(tcx, param_env, make_projection(tcx, container_id, assoc_ty, substs)?) +} diff --git a/clippy_utils/src/usage.rs b/clippy_utils/src/usage.rs index 000fb51c01857..797722cfc1fcc 100644 --- a/clippy_utils/src/usage.rs +++ b/clippy_utils/src/usage.rs @@ -73,12 +73,7 @@ impl<'tcx> Delegate<'tcx> for MutVarsDelegate { self.update(cmt); } - fn fake_read( - &mut self, - _: &rustc_hir_typeck::expr_use_visitor::PlaceWithHirId<'tcx>, - _: FakeReadCause, - _: HirId, - ) {} + fn fake_read(&mut self, _: &rustc_hir_typeck::expr_use_visitor::PlaceWithHirId<'tcx>, _: FakeReadCause, _: HirId) {} } pub struct ParamBindingIdCollector { diff --git a/declare_clippy_lint/Cargo.toml b/declare_clippy_lint/Cargo.toml new file mode 100644 index 0000000000000..578109840fb70 --- /dev/null +++ b/declare_clippy_lint/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "declare_clippy_lint" +version = "0.1.67" +edition = "2021" +publish = false + +[lib] +proc-macro = true + +[dependencies] +itertools = "0.10.1" +quote = "1.0.21" +syn = "1.0.100" diff --git a/declare_clippy_lint/src/lib.rs b/declare_clippy_lint/src/lib.rs new file mode 100644 index 0000000000000..962766916dd1f --- /dev/null +++ b/declare_clippy_lint/src/lib.rs @@ -0,0 +1,173 @@ +#![feature(let_chains)] +#![cfg_attr(feature = "deny-warnings", deny(warnings))] + +use proc_macro::TokenStream; +use quote::{format_ident, quote}; +use syn::parse::{Parse, ParseStream}; +use syn::{parse_macro_input, Attribute, Error, Ident, Lit, LitStr, Meta, Result, Token}; + +fn parse_attr(path: [&'static str; LEN], attr: &Attribute) -> Option { + if let Meta::NameValue(name_value) = attr.parse_meta().ok()? { + let path_idents = name_value.path.segments.iter().map(|segment| &segment.ident); + + if itertools::equal(path_idents, path) + && let Lit::Str(lit) = name_value.lit + { + return Some(lit); + } + } + + None +} + +struct ClippyLint { + attrs: Vec, + explanation: String, + name: Ident, + category: Ident, + description: LitStr, +} + +impl Parse for ClippyLint { + fn parse(input: ParseStream) -> Result { + let attrs = input.call(Attribute::parse_outer)?; + + let mut in_code = false; + let mut explanation = String::new(); + let mut version = None; + for attr in &attrs { + if let Some(lit) = parse_attr(["doc"], attr) { + let value = lit.value(); + let line = value.strip_prefix(' ').unwrap_or(&value); + + if line.starts_with("```") { + explanation += "```\n"; + in_code = !in_code; + } else if !(in_code && line.starts_with("# ")) { + explanation += line; + explanation.push('\n'); + } + } else if let Some(lit) = parse_attr(["clippy", "version"], attr) { + if let Some(duplicate) = version.replace(lit) { + return Err(Error::new_spanned(duplicate, "duplicate clippy::version")); + } + } else { + return Err(Error::new_spanned(attr, "unexpected attribute")); + } + } + + input.parse::()?; + let name = input.parse()?; + input.parse::()?; + + let category = input.parse()?; + input.parse::()?; + + let description = input.parse()?; + + Ok(Self { + attrs, + explanation, + name, + category, + description, + }) + } +} + +/// Macro used to declare a Clippy lint. +/// +/// Every lint declaration consists of 4 parts: +/// +/// 1. The documentation, which is used for the website and `cargo clippy --explain` +/// 2. The `LINT_NAME`. See [lint naming][lint_naming] on lint naming conventions. +/// 3. The `lint_level`, which is a mapping from *one* of our lint groups to `Allow`, `Warn` or +/// `Deny`. The lint level here has nothing to do with what lint groups the lint is a part of. +/// 4. The `description` that contains a short explanation on what's wrong with code where the +/// lint is triggered. +/// +/// Currently the categories `style`, `correctness`, `suspicious`, `complexity` and `perf` are +/// enabled by default. As said in the README.md of this repository, if the lint level mapping +/// changes, please update README.md. +/// +/// # Example +/// +/// ``` +/// use rustc_session::declare_tool_lint; +/// +/// declare_clippy_lint! { +/// /// ### What it does +/// /// Checks for ... (describe what the lint matches). +/// /// +/// /// ### Why is this bad? +/// /// Supply the reason for linting the code. +/// /// +/// /// ### Example +/// /// ```rust +/// /// Insert a short example of code that triggers the lint +/// /// ``` +/// /// +/// /// Use instead: +/// /// ```rust +/// /// Insert a short example of improved code that doesn't trigger the lint +/// /// ``` +/// #[clippy::version = "1.65.0"] +/// pub LINT_NAME, +/// pedantic, +/// "description" +/// } +/// ``` +/// [lint_naming]: https://rust-lang.github.io/rfcs/0344-conventions-galore.html#lints +#[proc_macro] +pub fn declare_clippy_lint(input: TokenStream) -> TokenStream { + let ClippyLint { + attrs, + explanation, + name, + category, + description, + } = parse_macro_input!(input as ClippyLint); + + let mut category = category.to_string(); + + let level = format_ident!( + "{}", + match category.as_str() { + "correctness" => "Deny", + "style" | "suspicious" | "complexity" | "perf" | "internal_warn" => "Warn", + "pedantic" | "restriction" | "cargo" | "nursery" | "internal" => "Allow", + _ => panic!("unknown category {category}"), + }, + ); + + let info = if category == "internal_warn" { + None + } else { + let info_name = format_ident!("{name}_INFO"); + + (&mut category[0..1]).make_ascii_uppercase(); + let category_variant = format_ident!("{category}"); + + Some(quote! { + pub(crate) static #info_name: &'static crate::LintInfo = &crate::LintInfo { + lint: &#name, + category: crate::LintCategory::#category_variant, + explanation: #explanation, + }; + }) + }; + + let output = quote! { + declare_tool_lint! { + #(#attrs)* + pub clippy::#name, + #level, + #description, + report_in_external_macro: true + } + + #info + }; + + TokenStream::from(output) +} diff --git a/lintcheck/src/main.rs b/lintcheck/src/main.rs index 54c1b80c42dbf..ee8ab7c1d7cbb 100644 --- a/lintcheck/src/main.rs +++ b/lintcheck/src/main.rs @@ -544,34 +544,6 @@ fn gather_stats(clippy_warnings: &[ClippyWarning]) -> (String, HashMap<&String, (stats_string, counter) } -/// check if the latest modification of the logfile is older than the modification date of the -/// clippy binary, if this is true, we should clean the lintchec shared target directory and recheck -fn lintcheck_needs_rerun(lintcheck_logs_path: &Path, paths: [&Path; 2]) -> bool { - if !lintcheck_logs_path.exists() { - return true; - } - - let clippy_modified: std::time::SystemTime = { - let [cargo, driver] = paths.map(|p| { - std::fs::metadata(p) - .expect("failed to get metadata of file") - .modified() - .expect("failed to get modification date") - }); - // the oldest modification of either of the binaries - std::cmp::max(cargo, driver) - }; - - let logs_modified: std::time::SystemTime = std::fs::metadata(lintcheck_logs_path) - .expect("failed to get metadata of file") - .modified() - .expect("failed to get modification date"); - - // time is represented in seconds since X - // logs_modified 2 and clippy_modified 5 means clippy binary is older and we need to recheck - logs_modified < clippy_modified -} - #[allow(clippy::too_many_lines)] fn main() { // We're being executed as a `RUSTC_WRAPPER` as part of `--recursive` @@ -594,23 +566,6 @@ fn main() { let cargo_clippy_path = fs::canonicalize(format!("target/debug/cargo-clippy{EXE_SUFFIX}")).unwrap(); let clippy_driver_path = fs::canonicalize(format!("target/debug/clippy-driver{EXE_SUFFIX}")).unwrap(); - // if the clippy bin is newer than our logs, throw away target dirs to force clippy to - // refresh the logs - if lintcheck_needs_rerun( - &config.lintcheck_results_path, - [&cargo_clippy_path, &clippy_driver_path], - ) { - let shared_target_dir = "target/lintcheck/shared_target_dir"; - // if we get an Err here, the shared target dir probably does simply not exist - if let Ok(metadata) = std::fs::metadata(shared_target_dir) { - if metadata.is_dir() { - println!("Clippy is newer than lint check logs, clearing lintcheck shared target dir..."); - std::fs::remove_dir_all(shared_target_dir) - .expect("failed to remove target/lintcheck/shared_target_dir"); - } - } - } - // assert that clippy is found assert!( cargo_clippy_path.is_file(), @@ -678,7 +633,7 @@ fn main() { .unwrap(); let server = config.recursive.then(|| { - fs::remove_dir_all("target/lintcheck/shared_target_dir/recursive").unwrap_or_default(); + let _ = fs::remove_dir_all("target/lintcheck/shared_target_dir/recursive"); LintcheckServer::spawn(recursive_options) }); diff --git a/rust-toolchain b/rust-toolchain index 748d8a317160f..a806c1564796a 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1,3 +1,3 @@ [toolchain] -channel = "nightly-2022-10-20" -components = ["cargo", "llvm-tools-preview", "rust-src", "rust-std", "rustc", "rustc-dev", "rustfmt"] +channel = "nightly-2022-11-21" +components = ["cargo", "llvm-tools", "rust-src", "rust-std", "rustc", "rustc-dev", "rustfmt"] diff --git a/src/docs.rs b/src/docs.rs deleted file mode 100644 index c033ad294a38d..0000000000000 --- a/src/docs.rs +++ /dev/null @@ -1,606 +0,0 @@ -// autogenerated. Please look at /clippy_dev/src/update_lints.rs - -macro_rules! include_lint { - ($file_name: expr) => { - include_str!($file_name) - }; -} - -macro_rules! docs { - ($($lint_name: expr,)*) => { - pub fn explain(lint: &str) { - println!("{}", match lint { - $( - $lint_name => include_lint!(concat!("docs/", concat!($lint_name, ".txt"))), - )* - _ => "unknown lint", - }) - } - } -} - -docs! { - "absurd_extreme_comparisons", - "alloc_instead_of_core", - "allow_attributes_without_reason", - "almost_complete_letter_range", - "almost_swapped", - "approx_constant", - "arithmetic_side_effects", - "as_conversions", - "as_ptr_cast_mut", - "as_underscore", - "assertions_on_constants", - "assertions_on_result_states", - "assign_op_pattern", - "async_yields_async", - "await_holding_invalid_type", - "await_holding_lock", - "await_holding_refcell_ref", - "bad_bit_mask", - "bind_instead_of_map", - "blanket_clippy_restriction_lints", - "blocks_in_if_conditions", - "bool_assert_comparison", - "bool_comparison", - "bool_to_int_with_if", - "borrow_as_ptr", - "borrow_deref_ref", - "borrow_interior_mutable_const", - "borrowed_box", - "box_collection", - "box_default", - "boxed_local", - "branches_sharing_code", - "builtin_type_shadow", - "bytes_count_to_len", - "bytes_nth", - "cargo_common_metadata", - "case_sensitive_file_extension_comparisons", - "cast_abs_to_unsigned", - "cast_enum_constructor", - "cast_enum_truncation", - "cast_lossless", - "cast_nan_to_int", - "cast_possible_truncation", - "cast_possible_wrap", - "cast_precision_loss", - "cast_ptr_alignment", - "cast_ref_to_mut", - "cast_sign_loss", - "cast_slice_different_sizes", - "cast_slice_from_raw_parts", - "char_lit_as_u8", - "chars_last_cmp", - "chars_next_cmp", - "checked_conversions", - "clone_double_ref", - "clone_on_copy", - "clone_on_ref_ptr", - "cloned_instead_of_copied", - "cmp_nan", - "cmp_null", - "cmp_owned", - "cognitive_complexity", - "collapsible_else_if", - "collapsible_if", - "collapsible_match", - "collapsible_str_replace", - "comparison_chain", - "comparison_to_empty", - "copy_iterator", - "crate_in_macro_def", - "create_dir", - "crosspointer_transmute", - "dbg_macro", - "debug_assert_with_mut_call", - "decimal_literal_representation", - "declare_interior_mutable_const", - "default_instead_of_iter_empty", - "default_numeric_fallback", - "default_trait_access", - "default_union_representation", - "deprecated_cfg_attr", - "deprecated_semver", - "deref_addrof", - "deref_by_slicing", - "derivable_impls", - "derive_hash_xor_eq", - "derive_ord_xor_partial_ord", - "derive_partial_eq_without_eq", - "disallowed_macros", - "disallowed_methods", - "disallowed_names", - "disallowed_script_idents", - "disallowed_types", - "diverging_sub_expression", - "doc_link_with_quotes", - "doc_markdown", - "double_comparisons", - "double_must_use", - "double_neg", - "double_parens", - "drop_copy", - "drop_non_drop", - "drop_ref", - "duplicate_mod", - "duplicate_underscore_argument", - "duration_subsec", - "else_if_without_else", - "empty_drop", - "empty_enum", - "empty_line_after_outer_attr", - "empty_loop", - "empty_structs_with_brackets", - "enum_clike_unportable_variant", - "enum_glob_use", - "enum_variant_names", - "eq_op", - "equatable_if_let", - "erasing_op", - "err_expect", - "excessive_precision", - "exhaustive_enums", - "exhaustive_structs", - "exit", - "expect_fun_call", - "expect_used", - "expl_impl_clone_on_copy", - "explicit_auto_deref", - "explicit_counter_loop", - "explicit_deref_methods", - "explicit_into_iter_loop", - "explicit_iter_loop", - "explicit_write", - "extend_with_drain", - "extra_unused_lifetimes", - "fallible_impl_from", - "field_reassign_with_default", - "filetype_is_file", - "filter_map_identity", - "filter_map_next", - "filter_next", - "flat_map_identity", - "flat_map_option", - "float_arithmetic", - "float_cmp", - "float_cmp_const", - "float_equality_without_abs", - "fn_address_comparisons", - "fn_params_excessive_bools", - "fn_to_numeric_cast", - "fn_to_numeric_cast_any", - "fn_to_numeric_cast_with_truncation", - "for_kv_map", - "forget_copy", - "forget_non_drop", - "forget_ref", - "format_in_format_args", - "format_push_string", - "from_iter_instead_of_collect", - "from_over_into", - "from_str_radix_10", - "future_not_send", - "get_first", - "get_last_with_len", - "get_unwrap", - "identity_op", - "if_let_mutex", - "if_not_else", - "if_same_then_else", - "if_then_some_else_none", - "ifs_same_cond", - "implicit_clone", - "implicit_hasher", - "implicit_return", - "implicit_saturating_add", - "implicit_saturating_sub", - "imprecise_flops", - "inconsistent_digit_grouping", - "inconsistent_struct_constructor", - "index_refutable_slice", - "indexing_slicing", - "ineffective_bit_mask", - "inefficient_to_string", - "infallible_destructuring_match", - "infinite_iter", - "inherent_to_string", - "inherent_to_string_shadow_display", - "init_numbered_fields", - "inline_always", - "inline_asm_x86_att_syntax", - "inline_asm_x86_intel_syntax", - "inline_fn_without_body", - "inspect_for_each", - "int_plus_one", - "integer_arithmetic", - "integer_division", - "into_iter_on_ref", - "invalid_null_ptr_usage", - "invalid_regex", - "invalid_upcast_comparisons", - "invalid_utf8_in_unchecked", - "invisible_characters", - "is_digit_ascii_radix", - "items_after_statements", - "iter_cloned_collect", - "iter_count", - "iter_kv_map", - "iter_next_loop", - "iter_next_slice", - "iter_not_returning_iterator", - "iter_nth", - "iter_nth_zero", - "iter_on_empty_collections", - "iter_on_single_items", - "iter_overeager_cloned", - "iter_skip_next", - "iter_with_drain", - "iterator_step_by_zero", - "just_underscores_and_digits", - "large_const_arrays", - "large_digit_groups", - "large_enum_variant", - "large_include_file", - "large_stack_arrays", - "large_types_passed_by_value", - "len_without_is_empty", - "len_zero", - "let_and_return", - "let_underscore_drop", - "let_underscore_lock", - "let_underscore_must_use", - "let_unit_value", - "linkedlist", - "lossy_float_literal", - "macro_use_imports", - "main_recursion", - "manual_assert", - "manual_async_fn", - "manual_bits", - "manual_clamp", - "manual_filter", - "manual_filter_map", - "manual_find", - "manual_find_map", - "manual_flatten", - "manual_instant_elapsed", - "manual_map", - "manual_memcpy", - "manual_non_exhaustive", - "manual_ok_or", - "manual_range_contains", - "manual_rem_euclid", - "manual_retain", - "manual_saturating_arithmetic", - "manual_split_once", - "manual_str_repeat", - "manual_string_new", - "manual_strip", - "manual_swap", - "manual_unwrap_or", - "many_single_char_names", - "map_clone", - "map_collect_result_unit", - "map_entry", - "map_err_ignore", - "map_flatten", - "map_identity", - "map_unwrap_or", - "match_as_ref", - "match_bool", - "match_like_matches_macro", - "match_on_vec_items", - "match_overlapping_arm", - "match_ref_pats", - "match_result_ok", - "match_same_arms", - "match_single_binding", - "match_str_case_mismatch", - "match_wild_err_arm", - "match_wildcard_for_single_variants", - "maybe_infinite_iter", - "mem_forget", - "mem_replace_option_with_none", - "mem_replace_with_default", - "mem_replace_with_uninit", - "min_max", - "mismatched_target_os", - "mismatching_type_param_order", - "misrefactored_assign_op", - "missing_const_for_fn", - "missing_docs_in_private_items", - "missing_enforced_import_renames", - "missing_errors_doc", - "missing_inline_in_public_items", - "missing_panics_doc", - "missing_safety_doc", - "missing_spin_loop", - "missing_trait_methods", - "mistyped_literal_suffixes", - "mixed_case_hex_literals", - "mixed_read_write_in_expression", - "mod_module_files", - "module_inception", - "module_name_repetitions", - "modulo_arithmetic", - "modulo_one", - "multi_assignments", - "multiple_crate_versions", - "multiple_inherent_impl", - "must_use_candidate", - "must_use_unit", - "mut_from_ref", - "mut_mut", - "mut_mutex_lock", - "mut_range_bound", - "mutable_key_type", - "mutex_atomic", - "mutex_integer", - "naive_bytecount", - "needless_arbitrary_self_type", - "needless_bitwise_bool", - "needless_bool", - "needless_borrow", - "needless_borrowed_reference", - "needless_collect", - "needless_continue", - "needless_doctest_main", - "needless_for_each", - "needless_late_init", - "needless_lifetimes", - "needless_match", - "needless_option_as_deref", - "needless_option_take", - "needless_parens_on_range_literals", - "needless_pass_by_value", - "needless_question_mark", - "needless_range_loop", - "needless_return", - "needless_splitn", - "needless_update", - "neg_cmp_op_on_partial_ord", - "neg_multiply", - "negative_feature_names", - "never_loop", - "new_ret_no_self", - "new_without_default", - "no_effect", - "no_effect_replace", - "no_effect_underscore_binding", - "non_ascii_literal", - "non_octal_unix_permissions", - "non_send_fields_in_send_ty", - "nonminimal_bool", - "nonsensical_open_options", - "nonstandard_macro_braces", - "not_unsafe_ptr_arg_deref", - "obfuscated_if_else", - "octal_escapes", - "ok_expect", - "only_used_in_recursion", - "op_ref", - "option_as_ref_deref", - "option_env_unwrap", - "option_filter_map", - "option_if_let_else", - "option_map_or_none", - "option_map_unit_fn", - "option_option", - "or_fun_call", - "or_then_unwrap", - "out_of_bounds_indexing", - "overflow_check_conditional", - "overly_complex_bool_expr", - "panic", - "panic_in_result_fn", - "panicking_unwrap", - "partial_pub_fields", - "partialeq_ne_impl", - "partialeq_to_none", - "path_buf_push_overwrite", - "pattern_type_mismatch", - "possible_missing_comma", - "precedence", - "print_in_format_impl", - "print_literal", - "print_stderr", - "print_stdout", - "print_with_newline", - "println_empty_string", - "ptr_arg", - "ptr_as_ptr", - "ptr_eq", - "ptr_offset_with_cast", - "pub_use", - "question_mark", - "range_minus_one", - "range_plus_one", - "range_zip_with_len", - "rc_buffer", - "rc_clone_in_vec_init", - "rc_mutex", - "read_zero_byte_vec", - "recursive_format_impl", - "redundant_allocation", - "redundant_clone", - "redundant_closure", - "redundant_closure_call", - "redundant_closure_for_method_calls", - "redundant_else", - "redundant_feature_names", - "redundant_field_names", - "redundant_pattern", - "redundant_pattern_matching", - "redundant_pub_crate", - "redundant_slicing", - "redundant_static_lifetimes", - "ref_binding_to_reference", - "ref_option_ref", - "repeat_once", - "rest_pat_in_fully_bound_structs", - "result_large_err", - "result_map_or_into_option", - "result_map_unit_fn", - "result_unit_err", - "return_self_not_must_use", - "reversed_empty_ranges", - "same_functions_in_if_condition", - "same_item_push", - "same_name_method", - "search_is_some", - "self_assignment", - "self_named_constructors", - "self_named_module_files", - "semicolon_if_nothing_returned", - "separated_literal_suffix", - "serde_api_misuse", - "shadow_reuse", - "shadow_same", - "shadow_unrelated", - "short_circuit_statement", - "should_implement_trait", - "significant_drop_in_scrutinee", - "similar_names", - "single_char_add_str", - "single_char_lifetime_names", - "single_char_pattern", - "single_component_path_imports", - "single_element_loop", - "single_match", - "single_match_else", - "size_of_in_element_count", - "skip_while_next", - "slow_vector_initialization", - "stable_sort_primitive", - "std_instead_of_alloc", - "std_instead_of_core", - "str_to_string", - "string_add", - "string_add_assign", - "string_extend_chars", - "string_from_utf8_as_bytes", - "string_lit_as_bytes", - "string_slice", - "string_to_string", - "strlen_on_c_strings", - "struct_excessive_bools", - "suboptimal_flops", - "suspicious_arithmetic_impl", - "suspicious_assignment_formatting", - "suspicious_else_formatting", - "suspicious_map", - "suspicious_op_assign_impl", - "suspicious_operation_groupings", - "suspicious_splitn", - "suspicious_to_owned", - "suspicious_unary_op_formatting", - "swap_ptr_to_ref", - "tabs_in_doc_comments", - "temporary_assignment", - "to_digit_is_some", - "to_string_in_format_args", - "todo", - "too_many_arguments", - "too_many_lines", - "toplevel_ref_arg", - "trailing_empty_array", - "trait_duplication_in_bounds", - "transmute_bytes_to_str", - "transmute_float_to_int", - "transmute_int_to_bool", - "transmute_int_to_char", - "transmute_int_to_float", - "transmute_num_to_bytes", - "transmute_ptr_to_ptr", - "transmute_ptr_to_ref", - "transmute_undefined_repr", - "transmutes_expressible_as_ptr_casts", - "transmuting_null", - "trim_split_whitespace", - "trivial_regex", - "trivially_copy_pass_by_ref", - "try_err", - "type_complexity", - "type_repetition_in_bounds", - "undocumented_unsafe_blocks", - "undropped_manually_drops", - "unicode_not_nfc", - "unimplemented", - "uninit_assumed_init", - "uninit_vec", - "uninlined_format_args", - "unit_arg", - "unit_cmp", - "unit_hash", - "unit_return_expecting_ord", - "unnecessary_cast", - "unnecessary_filter_map", - "unnecessary_find_map", - "unnecessary_fold", - "unnecessary_join", - "unnecessary_lazy_evaluations", - "unnecessary_mut_passed", - "unnecessary_operation", - "unnecessary_owned_empty_strings", - "unnecessary_self_imports", - "unnecessary_sort_by", - "unnecessary_to_owned", - "unnecessary_unwrap", - "unnecessary_wraps", - "unneeded_field_pattern", - "unneeded_wildcard_pattern", - "unnested_or_patterns", - "unreachable", - "unreadable_literal", - "unsafe_derive_deserialize", - "unsafe_removed_from_name", - "unseparated_literal_suffix", - "unsound_collection_transmute", - "unused_async", - "unused_format_specs", - "unused_io_amount", - "unused_peekable", - "unused_rounding", - "unused_self", - "unused_unit", - "unusual_byte_groupings", - "unwrap_in_result", - "unwrap_or_else_default", - "unwrap_used", - "upper_case_acronyms", - "use_debug", - "use_self", - "used_underscore_binding", - "useless_asref", - "useless_attribute", - "useless_conversion", - "useless_format", - "useless_let_if_seq", - "useless_transmute", - "useless_vec", - "vec_box", - "vec_init_then_push", - "vec_resize_to_zero", - "verbose_bit_mask", - "verbose_file_reads", - "vtable_address_comparisons", - "while_immutable_condition", - "while_let_loop", - "while_let_on_iterator", - "wildcard_dependencies", - "wildcard_enum_match_arm", - "wildcard_imports", - "wildcard_in_or_patterns", - "write_literal", - "write_with_newline", - "writeln_empty_string", - "wrong_self_convention", - "wrong_transmute", - "zero_divided_by_zero", - "zero_prefixed_literal", - "zero_ptr", - "zero_sized_map_values", - "zst_offset", - -} diff --git a/src/docs/absurd_extreme_comparisons.txt b/src/docs/absurd_extreme_comparisons.txt deleted file mode 100644 index 590bee28aa237..0000000000000 --- a/src/docs/absurd_extreme_comparisons.txt +++ /dev/null @@ -1,25 +0,0 @@ -### What it does -Checks for comparisons where one side of the relation is -either the minimum or maximum value for its type and warns if it involves a -case that is always true or always false. Only integer and boolean types are -checked. - -### Why is this bad? -An expression like `min <= x` may misleadingly imply -that it is possible for `x` to be less than the minimum. Expressions like -`max < x` are probably mistakes. - -### Known problems -For `usize` the size of the current compile target will -be assumed (e.g., 64 bits on 64 bit systems). This means code that uses such -a comparison to detect target pointer width will trigger this lint. One can -use `mem::sizeof` and compare its value or conditional compilation -attributes -like `#[cfg(target_pointer_width = "64")] ..` instead. - -### Example -``` -let vec: Vec = Vec::new(); -if vec.len() <= 0 {} -if 100 > i32::MAX {} -``` \ No newline at end of file diff --git a/src/docs/alloc_instead_of_core.txt b/src/docs/alloc_instead_of_core.txt deleted file mode 100644 index 488a36e9276c2..0000000000000 --- a/src/docs/alloc_instead_of_core.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does - -Finds items imported through `alloc` when available through `core`. - -### Why is this bad? - -Crates which have `no_std` compatibility and may optionally require alloc may wish to ensure types are -imported from core to ensure disabling `alloc` does not cause the crate to fail to compile. This lint -is also useful for crates migrating to become `no_std` compatible. - -### Example -``` -use alloc::slice::from_ref; -``` -Use instead: -``` -use core::slice::from_ref; -``` \ No newline at end of file diff --git a/src/docs/allow_attributes_without_reason.txt b/src/docs/allow_attributes_without_reason.txt deleted file mode 100644 index fcc4f49b08b37..0000000000000 --- a/src/docs/allow_attributes_without_reason.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for attributes that allow lints without a reason. - -(This requires the `lint_reasons` feature) - -### Why is this bad? -Allowing a lint should always have a reason. This reason should be documented to -ensure that others understand the reasoning - -### Example -``` -#![feature(lint_reasons)] - -#![allow(clippy::some_lint)] -``` - -Use instead: -``` -#![feature(lint_reasons)] - -#![allow(clippy::some_lint, reason = "False positive rust-lang/rust-clippy#1002020")] -``` \ No newline at end of file diff --git a/src/docs/almost_complete_letter_range.txt b/src/docs/almost_complete_letter_range.txt deleted file mode 100644 index 01cbaf9eae259..0000000000000 --- a/src/docs/almost_complete_letter_range.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for ranges which almost include the entire range of letters from 'a' to 'z', but -don't because they're a half open range. - -### Why is this bad? -This (`'a'..'z'`) is almost certainly a typo meant to include all letters. - -### Example -``` -let _ = 'a'..'z'; -``` -Use instead: -``` -let _ = 'a'..='z'; -``` \ No newline at end of file diff --git a/src/docs/almost_swapped.txt b/src/docs/almost_swapped.txt deleted file mode 100644 index cd10a8d5409b1..0000000000000 --- a/src/docs/almost_swapped.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for `foo = bar; bar = foo` sequences. - -### Why is this bad? -This looks like a failed attempt to swap. - -### Example -``` -a = b; -b = a; -``` -If swapping is intended, use `swap()` instead: -``` -std::mem::swap(&mut a, &mut b); -``` \ No newline at end of file diff --git a/src/docs/approx_constant.txt b/src/docs/approx_constant.txt deleted file mode 100644 index 393fa4b5ef7ec..0000000000000 --- a/src/docs/approx_constant.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks for floating point literals that approximate -constants which are defined in -[`std::f32::consts`](https://doc.rust-lang.org/stable/std/f32/consts/#constants) -or -[`std::f64::consts`](https://doc.rust-lang.org/stable/std/f64/consts/#constants), -respectively, suggesting to use the predefined constant. - -### Why is this bad? -Usually, the definition in the standard library is more -precise than what people come up with. If you find that your definition is -actually more precise, please [file a Rust -issue](https://github.com/rust-lang/rust/issues). - -### Example -``` -let x = 3.14; -let y = 1_f64 / x; -``` -Use instead: -``` -let x = std::f32::consts::PI; -let y = std::f64::consts::FRAC_1_PI; -``` \ No newline at end of file diff --git a/src/docs/arithmetic_side_effects.txt b/src/docs/arithmetic_side_effects.txt deleted file mode 100644 index 4ae8bce88ad5f..0000000000000 --- a/src/docs/arithmetic_side_effects.txt +++ /dev/null @@ -1,33 +0,0 @@ -### What it does -Checks any kind of arithmetic operation of any type. - -Operators like `+`, `-`, `*` or `<<` are usually capable of overflowing according to the [Rust -Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow), -or can panic (`/`, `%`). - -Known safe built-in types like `Wrapping` or `Saturating`, floats, operations in constant -environments, allowed types and non-constant operations that won't overflow are ignored. - -### Why is this bad? -For integers, overflow will trigger a panic in debug builds or wrap the result in -release mode; division by zero will cause a panic in either mode. As a result, it is -desirable to explicitly call checked, wrapping or saturating arithmetic methods. - -#### Example -``` -// `n` can be any number, including `i32::MAX`. -fn foo(n: i32) -> i32 { - n + 1 -} -``` - -Third-party types can also overflow or present unwanted side-effects. - -#### Example -``` -use rust_decimal::Decimal; -let _n = Decimal::MAX + Decimal::MAX; -``` - -### Allowed types -Custom allowed types can be specified through the "arithmetic-side-effects-allowed" filter. \ No newline at end of file diff --git a/src/docs/as_conversions.txt b/src/docs/as_conversions.txt deleted file mode 100644 index 4af479bd8111c..0000000000000 --- a/src/docs/as_conversions.txt +++ /dev/null @@ -1,32 +0,0 @@ -### What it does -Checks for usage of `as` conversions. - -Note that this lint is specialized in linting *every single* use of `as` -regardless of whether good alternatives exist or not. -If you want more precise lints for `as`, please consider using these separate lints: -`unnecessary_cast`, `cast_lossless/cast_possible_truncation/cast_possible_wrap/cast_precision_loss/cast_sign_loss`, -`fn_to_numeric_cast(_with_truncation)`, `char_lit_as_u8`, `ref_to_mut` and `ptr_as_ptr`. -There is a good explanation the reason why this lint should work in this way and how it is useful -[in this issue](https://github.com/rust-lang/rust-clippy/issues/5122). - -### Why is this bad? -`as` conversions will perform many kinds of -conversions, including silently lossy conversions and dangerous coercions. -There are cases when it makes sense to use `as`, so the lint is -Allow by default. - -### Example -``` -let a: u32; -... -f(a as u16); -``` - -Use instead: -``` -f(a.try_into()?); - -// or - -f(a.try_into().expect("Unexpected u16 overflow in f")); -``` \ No newline at end of file diff --git a/src/docs/as_ptr_cast_mut.txt b/src/docs/as_ptr_cast_mut.txt deleted file mode 100644 index 228dde996bb2f..0000000000000 --- a/src/docs/as_ptr_cast_mut.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for the result of a `&self`-taking `as_ptr` being cast to a mutable pointer - -### Why is this bad? -Since `as_ptr` takes a `&self`, the pointer won't have write permissions unless interior -mutability is used, making it unlikely that having it as a mutable pointer is correct. - -### Example -``` -let string = String::with_capacity(1); -let ptr = string.as_ptr() as *mut u8; -unsafe { ptr.write(4) }; // UNDEFINED BEHAVIOUR -``` -Use instead: -``` -let mut string = String::with_capacity(1); -let ptr = string.as_mut_ptr(); -unsafe { ptr.write(4) }; -``` \ No newline at end of file diff --git a/src/docs/as_underscore.txt b/src/docs/as_underscore.txt deleted file mode 100644 index 2d9b0c358936b..0000000000000 --- a/src/docs/as_underscore.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Check for the usage of `as _` conversion using inferred type. - -### Why is this bad? -The conversion might include lossy conversion and dangerous cast that might go -undetected due to the type being inferred. - -The lint is allowed by default as using `_` is less wordy than always specifying the type. - -### Example -``` -fn foo(n: usize) {} -let n: u16 = 256; -foo(n as _); -``` -Use instead: -``` -fn foo(n: usize) {} -let n: u16 = 256; -foo(n as usize); -``` \ No newline at end of file diff --git a/src/docs/assertions_on_constants.txt b/src/docs/assertions_on_constants.txt deleted file mode 100644 index 270c1e3b639d9..0000000000000 --- a/src/docs/assertions_on_constants.txt +++ /dev/null @@ -1,14 +0,0 @@ -### What it does -Checks for `assert!(true)` and `assert!(false)` calls. - -### Why is this bad? -Will be optimized out by the compiler or should probably be replaced by a -`panic!()` or `unreachable!()` - -### Example -``` -assert!(false) -assert!(true) -const B: bool = false; -assert!(B) -``` \ No newline at end of file diff --git a/src/docs/assertions_on_result_states.txt b/src/docs/assertions_on_result_states.txt deleted file mode 100644 index 0889084fd3ad6..0000000000000 --- a/src/docs/assertions_on_result_states.txt +++ /dev/null @@ -1,14 +0,0 @@ -### What it does -Checks for `assert!(r.is_ok())` or `assert!(r.is_err())` calls. - -### Why is this bad? -An assertion failure cannot output an useful message of the error. - -### Known problems -The suggested replacement decreases the readability of code and log output. - -### Example -``` -assert!(r.is_ok()); -assert!(r.is_err()); -``` \ No newline at end of file diff --git a/src/docs/assign_op_pattern.txt b/src/docs/assign_op_pattern.txt deleted file mode 100644 index f355c0cc18d37..0000000000000 --- a/src/docs/assign_op_pattern.txt +++ /dev/null @@ -1,28 +0,0 @@ -### What it does -Checks for `a = a op b` or `a = b commutative_op a` -patterns. - -### Why is this bad? -These can be written as the shorter `a op= b`. - -### Known problems -While forbidden by the spec, `OpAssign` traits may have -implementations that differ from the regular `Op` impl. - -### Example -``` -let mut a = 5; -let b = 0; -// ... - -a = a + b; -``` - -Use instead: -``` -let mut a = 5; -let b = 0; -// ... - -a += b; -``` \ No newline at end of file diff --git a/src/docs/async_yields_async.txt b/src/docs/async_yields_async.txt deleted file mode 100644 index a40de6d2e4736..0000000000000 --- a/src/docs/async_yields_async.txt +++ /dev/null @@ -1,28 +0,0 @@ -### What it does -Checks for async blocks that yield values of types -that can themselves be awaited. - -### Why is this bad? -An await is likely missing. - -### Example -``` -async fn foo() {} - -fn bar() { - let x = async { - foo() - }; -} -``` - -Use instead: -``` -async fn foo() {} - -fn bar() { - let x = async { - foo().await - }; -} -``` \ No newline at end of file diff --git a/src/docs/await_holding_invalid_type.txt b/src/docs/await_holding_invalid_type.txt deleted file mode 100644 index e9c768772ff6b..0000000000000 --- a/src/docs/await_holding_invalid_type.txt +++ /dev/null @@ -1,29 +0,0 @@ -### What it does -Allows users to configure types which should not be held across `await` -suspension points. - -### Why is this bad? -There are some types which are perfectly "safe" to be used concurrently -from a memory access perspective but will cause bugs at runtime if they -are held in such a way. - -### Example - -``` -await-holding-invalid-types = [ - # You can specify a type name - "CustomLockType", - # You can (optionally) specify a reason - { path = "OtherCustomLockType", reason = "Relies on a thread local" } -] -``` - -``` -struct CustomLockType; -struct OtherCustomLockType; -async fn foo() { - let _x = CustomLockType; - let _y = OtherCustomLockType; - baz().await; // Lint violation -} -``` \ No newline at end of file diff --git a/src/docs/await_holding_lock.txt b/src/docs/await_holding_lock.txt deleted file mode 100644 index 0f450a11160cc..0000000000000 --- a/src/docs/await_holding_lock.txt +++ /dev/null @@ -1,51 +0,0 @@ -### What it does -Checks for calls to await while holding a non-async-aware MutexGuard. - -### Why is this bad? -The Mutex types found in std::sync and parking_lot -are not designed to operate in an async context across await points. - -There are two potential solutions. One is to use an async-aware Mutex -type. Many asynchronous foundation crates provide such a Mutex type. The -other solution is to ensure the mutex is unlocked before calling await, -either by introducing a scope or an explicit call to Drop::drop. - -### Known problems -Will report false positive for explicitly dropped guards -([#6446](https://github.com/rust-lang/rust-clippy/issues/6446)). A workaround for this is -to wrap the `.lock()` call in a block instead of explicitly dropping the guard. - -### Example -``` -async fn foo(x: &Mutex) { - let mut guard = x.lock().unwrap(); - *guard += 1; - baz().await; -} - -async fn bar(x: &Mutex) { - let mut guard = x.lock().unwrap(); - *guard += 1; - drop(guard); // explicit drop - baz().await; -} -``` - -Use instead: -``` -async fn foo(x: &Mutex) { - { - let mut guard = x.lock().unwrap(); - *guard += 1; - } - baz().await; -} - -async fn bar(x: &Mutex) { - { - let mut guard = x.lock().unwrap(); - *guard += 1; - } // guard dropped here at end of scope - baz().await; -} -``` \ No newline at end of file diff --git a/src/docs/await_holding_refcell_ref.txt b/src/docs/await_holding_refcell_ref.txt deleted file mode 100644 index 226a261b9cc52..0000000000000 --- a/src/docs/await_holding_refcell_ref.txt +++ /dev/null @@ -1,47 +0,0 @@ -### What it does -Checks for calls to await while holding a `RefCell` `Ref` or `RefMut`. - -### Why is this bad? -`RefCell` refs only check for exclusive mutable access -at runtime. Holding onto a `RefCell` ref across an `await` suspension point -risks panics from a mutable ref shared while other refs are outstanding. - -### Known problems -Will report false positive for explicitly dropped refs -([#6353](https://github.com/rust-lang/rust-clippy/issues/6353)). A workaround for this is -to wrap the `.borrow[_mut]()` call in a block instead of explicitly dropping the ref. - -### Example -``` -async fn foo(x: &RefCell) { - let mut y = x.borrow_mut(); - *y += 1; - baz().await; -} - -async fn bar(x: &RefCell) { - let mut y = x.borrow_mut(); - *y += 1; - drop(y); // explicit drop - baz().await; -} -``` - -Use instead: -``` -async fn foo(x: &RefCell) { - { - let mut y = x.borrow_mut(); - *y += 1; - } - baz().await; -} - -async fn bar(x: &RefCell) { - { - let mut y = x.borrow_mut(); - *y += 1; - } // y dropped here at end of scope - baz().await; -} -``` \ No newline at end of file diff --git a/src/docs/bad_bit_mask.txt b/src/docs/bad_bit_mask.txt deleted file mode 100644 index d40024ee56209..0000000000000 --- a/src/docs/bad_bit_mask.txt +++ /dev/null @@ -1,30 +0,0 @@ -### What it does -Checks for incompatible bit masks in comparisons. - -The formula for detecting if an expression of the type `_ m - c` (where `` is one of {`&`, `|`} and `` is one of -{`!=`, `>=`, `>`, `!=`, `>=`, `>`}) can be determined from the following -table: - -|Comparison |Bit Op|Example |is always|Formula | -|------------|------|-------------|---------|----------------------| -|`==` or `!=`| `&` |`x & 2 == 3` |`false` |`c & m != c` | -|`<` or `>=`| `&` |`x & 2 < 3` |`true` |`m < c` | -|`>` or `<=`| `&` |`x & 1 > 1` |`false` |`m <= c` | -|`==` or `!=`| `\|` |`x \| 1 == 0`|`false` |`c \| m != c` | -|`<` or `>=`| `\|` |`x \| 1 < 1` |`false` |`m >= c` | -|`<=` or `>` | `\|` |`x \| 1 > 0` |`true` |`m > c` | - -### Why is this bad? -If the bits that the comparison cares about are always -set to zero or one by the bit mask, the comparison is constant `true` or -`false` (depending on mask, compared value, and operators). - -So the code is actively misleading, and the only reason someone would write -this intentionally is to win an underhanded Rust contest or create a -test-case for this lint. - -### Example -``` -if (x & 1 == 2) { } -``` \ No newline at end of file diff --git a/src/docs/bind_instead_of_map.txt b/src/docs/bind_instead_of_map.txt deleted file mode 100644 index 148575803d389..0000000000000 --- a/src/docs/bind_instead_of_map.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for usage of `_.and_then(|x| Some(y))`, `_.and_then(|x| Ok(y))` or -`_.or_else(|x| Err(y))`. - -### Why is this bad? -Readability, this can be written more concisely as -`_.map(|x| y)` or `_.map_err(|x| y)`. - -### Example -``` -let _ = opt().and_then(|s| Some(s.len())); -let _ = res().and_then(|s| if s.len() == 42 { Ok(10) } else { Ok(20) }); -let _ = res().or_else(|s| if s.len() == 42 { Err(10) } else { Err(20) }); -``` - -The correct use would be: - -``` -let _ = opt().map(|s| s.len()); -let _ = res().map(|s| if s.len() == 42 { 10 } else { 20 }); -let _ = res().map_err(|s| if s.len() == 42 { 10 } else { 20 }); -``` \ No newline at end of file diff --git a/src/docs/blanket_clippy_restriction_lints.txt b/src/docs/blanket_clippy_restriction_lints.txt deleted file mode 100644 index 28a4ebf7169bf..0000000000000 --- a/src/docs/blanket_clippy_restriction_lints.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for `warn`/`deny`/`forbid` attributes targeting the whole clippy::restriction category. - -### Why is this bad? -Restriction lints sometimes are in contrast with other lints or even go against idiomatic rust. -These lints should only be enabled on a lint-by-lint basis and with careful consideration. - -### Example -``` -#![deny(clippy::restriction)] -``` - -Use instead: -``` -#![deny(clippy::as_conversions)] -``` \ No newline at end of file diff --git a/src/docs/blocks_in_if_conditions.txt b/src/docs/blocks_in_if_conditions.txt deleted file mode 100644 index 3afa14853fd21..0000000000000 --- a/src/docs/blocks_in_if_conditions.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for `if` conditions that use blocks containing an -expression, statements or conditions that use closures with blocks. - -### Why is this bad? -Style, using blocks in the condition makes it hard to read. - -### Examples -``` -if { true } { /* ... */ } - -if { let x = somefunc(); x } { /* ... */ } -``` - -Use instead: -``` -if true { /* ... */ } - -let res = { let x = somefunc(); x }; -if res { /* ... */ } -``` \ No newline at end of file diff --git a/src/docs/bool_assert_comparison.txt b/src/docs/bool_assert_comparison.txt deleted file mode 100644 index df7ca00cc2ba4..0000000000000 --- a/src/docs/bool_assert_comparison.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -This lint warns about boolean comparisons in assert-like macros. - -### Why is this bad? -It is shorter to use the equivalent. - -### Example -``` -assert_eq!("a".is_empty(), false); -assert_ne!("a".is_empty(), true); -``` - -Use instead: -``` -assert!(!"a".is_empty()); -``` \ No newline at end of file diff --git a/src/docs/bool_comparison.txt b/src/docs/bool_comparison.txt deleted file mode 100644 index 0996f60cec44c..0000000000000 --- a/src/docs/bool_comparison.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for expressions of the form `x == true`, -`x != true` and order comparisons such as `x < true` (or vice versa) and -suggest using the variable directly. - -### Why is this bad? -Unnecessary code. - -### Example -``` -if x == true {} -if y == false {} -``` -use `x` directly: -``` -if x {} -if !y {} -``` \ No newline at end of file diff --git a/src/docs/bool_to_int_with_if.txt b/src/docs/bool_to_int_with_if.txt deleted file mode 100644 index 63535b454c9f1..0000000000000 --- a/src/docs/bool_to_int_with_if.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Instead of using an if statement to convert a bool to an int, -this lint suggests using a `from()` function or an `as` coercion. - -### Why is this bad? -Coercion or `from()` is idiomatic way to convert bool to a number. -Both methods are guaranteed to return 1 for true, and 0 for false. - -See https://doc.rust-lang.org/std/primitive.bool.html#impl-From%3Cbool%3E - -### Example -``` -if condition { - 1_i64 -} else { - 0 -}; -``` -Use instead: -``` -i64::from(condition); -``` -or -``` -condition as i64; -``` \ No newline at end of file diff --git a/src/docs/borrow_as_ptr.txt b/src/docs/borrow_as_ptr.txt deleted file mode 100644 index 0be865abd5780..0000000000000 --- a/src/docs/borrow_as_ptr.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Checks for the usage of `&expr as *const T` or -`&mut expr as *mut T`, and suggest using `ptr::addr_of` or -`ptr::addr_of_mut` instead. - -### Why is this bad? -This would improve readability and avoid creating a reference -that points to an uninitialized value or unaligned place. -Read the `ptr::addr_of` docs for more information. - -### Example -``` -let val = 1; -let p = &val as *const i32; - -let mut val_mut = 1; -let p_mut = &mut val_mut as *mut i32; -``` -Use instead: -``` -let val = 1; -let p = std::ptr::addr_of!(val); - -let mut val_mut = 1; -let p_mut = std::ptr::addr_of_mut!(val_mut); -``` \ No newline at end of file diff --git a/src/docs/borrow_deref_ref.txt b/src/docs/borrow_deref_ref.txt deleted file mode 100644 index 352480d3f26a7..0000000000000 --- a/src/docs/borrow_deref_ref.txt +++ /dev/null @@ -1,27 +0,0 @@ -### What it does -Checks for `&*(&T)`. - -### Why is this bad? -Dereferencing and then borrowing a reference value has no effect in most cases. - -### Known problems -False negative on such code: -``` -let x = &12; -let addr_x = &x as *const _ as usize; -let addr_y = &&*x as *const _ as usize; // assert ok now, and lint triggered. - // But if we fix it, assert will fail. -assert_ne!(addr_x, addr_y); -``` - -### Example -``` -let s = &String::new(); - -let a: &String = &* s; -``` - -Use instead: -``` -let a: &String = s; -``` \ No newline at end of file diff --git a/src/docs/borrow_interior_mutable_const.txt b/src/docs/borrow_interior_mutable_const.txt deleted file mode 100644 index e55b6a77e6667..0000000000000 --- a/src/docs/borrow_interior_mutable_const.txt +++ /dev/null @@ -1,40 +0,0 @@ -### What it does -Checks if `const` items which is interior mutable (e.g., -contains a `Cell`, `Mutex`, `AtomicXxxx`, etc.) has been borrowed directly. - -### Why is this bad? -Consts are copied everywhere they are referenced, i.e., -every time you refer to the const a fresh instance of the `Cell` or `Mutex` -or `AtomicXxxx` will be created, which defeats the whole purpose of using -these types in the first place. - -The `const` value should be stored inside a `static` item. - -### Known problems -When an enum has variants with interior mutability, use of its non -interior mutable variants can generate false positives. See issue -[#3962](https://github.com/rust-lang/rust-clippy/issues/3962) - -Types that have underlying or potential interior mutability trigger the lint whether -the interior mutable field is used or not. See issues -[#5812](https://github.com/rust-lang/rust-clippy/issues/5812) and -[#3825](https://github.com/rust-lang/rust-clippy/issues/3825) - -### Example -``` -use std::sync::atomic::{AtomicUsize, Ordering::SeqCst}; -const CONST_ATOM: AtomicUsize = AtomicUsize::new(12); - -CONST_ATOM.store(6, SeqCst); // the content of the atomic is unchanged -assert_eq!(CONST_ATOM.load(SeqCst), 12); // because the CONST_ATOM in these lines are distinct -``` - -Use instead: -``` -use std::sync::atomic::{AtomicUsize, Ordering::SeqCst}; -const CONST_ATOM: AtomicUsize = AtomicUsize::new(12); - -static STATIC_ATOM: AtomicUsize = CONST_ATOM; -STATIC_ATOM.store(9, SeqCst); -assert_eq!(STATIC_ATOM.load(SeqCst), 9); // use a `static` item to refer to the same instance -``` \ No newline at end of file diff --git a/src/docs/borrowed_box.txt b/src/docs/borrowed_box.txt deleted file mode 100644 index d7089be662a58..0000000000000 --- a/src/docs/borrowed_box.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for use of `&Box` anywhere in the code. -Check the [Box documentation](https://doc.rust-lang.org/std/boxed/index.html) for more information. - -### Why is this bad? -A `&Box` parameter requires the function caller to box `T` first before passing it to a function. -Using `&T` defines a concrete type for the parameter and generalizes the function, this would also -auto-deref to `&T` at the function call site if passed a `&Box`. - -### Example -``` -fn foo(bar: &Box) { ... } -``` - -Better: - -``` -fn foo(bar: &T) { ... } -``` \ No newline at end of file diff --git a/src/docs/box_collection.txt b/src/docs/box_collection.txt deleted file mode 100644 index 053f24c46281d..0000000000000 --- a/src/docs/box_collection.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for use of `Box` where T is a collection such as Vec anywhere in the code. -Check the [Box documentation](https://doc.rust-lang.org/std/boxed/index.html) for more information. - -### Why is this bad? -Collections already keeps their contents in a separate area on -the heap. So if you `Box` them, you just add another level of indirection -without any benefit whatsoever. - -### Example -``` -struct X { - values: Box>, -} -``` - -Better: - -``` -struct X { - values: Vec, -} -``` \ No newline at end of file diff --git a/src/docs/box_default.txt b/src/docs/box_default.txt deleted file mode 100644 index 1c670c7733377..0000000000000 --- a/src/docs/box_default.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -checks for `Box::new(T::default())`, which is better written as -`Box::::default()`. - -### Why is this bad? -First, it's more complex, involving two calls instead of one. -Second, `Box::default()` can be faster -[in certain cases](https://nnethercote.github.io/perf-book/standard-library-types.html#box). - -### Example -``` -let x: Box = Box::new(Default::default()); -``` -Use instead: -``` -let x: Box = Box::default(); -``` \ No newline at end of file diff --git a/src/docs/boxed_local.txt b/src/docs/boxed_local.txt deleted file mode 100644 index 8b1febf1455fd..0000000000000 --- a/src/docs/boxed_local.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for usage of `Box` where an unboxed `T` would -work fine. - -### Why is this bad? -This is an unnecessary allocation, and bad for -performance. It is only necessary to allocate if you wish to move the box -into something. - -### Example -``` -fn foo(x: Box) {} -``` - -Use instead: -``` -fn foo(x: u32) {} -``` \ No newline at end of file diff --git a/src/docs/branches_sharing_code.txt b/src/docs/branches_sharing_code.txt deleted file mode 100644 index 79be6124798a2..0000000000000 --- a/src/docs/branches_sharing_code.txt +++ /dev/null @@ -1,32 +0,0 @@ -### What it does -Checks if the `if` and `else` block contain shared code that can be -moved out of the blocks. - -### Why is this bad? -Duplicate code is less maintainable. - -### Known problems -* The lint doesn't check if the moved expressions modify values that are being used in - the if condition. The suggestion can in that case modify the behavior of the program. - See [rust-clippy#7452](https://github.com/rust-lang/rust-clippy/issues/7452) - -### Example -``` -let foo = if … { - println!("Hello World"); - 13 -} else { - println!("Hello World"); - 42 -}; -``` - -Use instead: -``` -println!("Hello World"); -let foo = if … { - 13 -} else { - 42 -}; -``` \ No newline at end of file diff --git a/src/docs/builtin_type_shadow.txt b/src/docs/builtin_type_shadow.txt deleted file mode 100644 index 15b1c9df7baab..0000000000000 --- a/src/docs/builtin_type_shadow.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Warns if a generic shadows a built-in type. - -### Why is this bad? -This gives surprising type errors. - -### Example - -``` -impl Foo { - fn impl_func(&self) -> u32 { - 42 - } -} -``` \ No newline at end of file diff --git a/src/docs/bytes_count_to_len.txt b/src/docs/bytes_count_to_len.txt deleted file mode 100644 index ca7bf9a38da8e..0000000000000 --- a/src/docs/bytes_count_to_len.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -It checks for `str::bytes().count()` and suggests replacing it with -`str::len()`. - -### Why is this bad? -`str::bytes().count()` is longer and may not be as performant as using -`str::len()`. - -### Example -``` -"hello".bytes().count(); -String::from("hello").bytes().count(); -``` -Use instead: -``` -"hello".len(); -String::from("hello").len(); -``` \ No newline at end of file diff --git a/src/docs/bytes_nth.txt b/src/docs/bytes_nth.txt deleted file mode 100644 index 260de343353d8..0000000000000 --- a/src/docs/bytes_nth.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for the use of `.bytes().nth()`. - -### Why is this bad? -`.as_bytes().get()` is more efficient and more -readable. - -### Example -``` -"Hello".bytes().nth(3); -``` - -Use instead: -``` -"Hello".as_bytes().get(3); -``` \ No newline at end of file diff --git a/src/docs/cargo_common_metadata.txt b/src/docs/cargo_common_metadata.txt deleted file mode 100644 index 1998647a92740..0000000000000 --- a/src/docs/cargo_common_metadata.txt +++ /dev/null @@ -1,33 +0,0 @@ -### What it does -Checks to see if all common metadata is defined in -`Cargo.toml`. See: https://rust-lang-nursery.github.io/api-guidelines/documentation.html#cargotoml-includes-all-common-metadata-c-metadata - -### Why is this bad? -It will be more difficult for users to discover the -purpose of the crate, and key information related to it. - -### Example -``` -[package] -name = "clippy" -version = "0.0.212" -repository = "https://github.com/rust-lang/rust-clippy" -readme = "README.md" -license = "MIT OR Apache-2.0" -keywords = ["clippy", "lint", "plugin"] -categories = ["development-tools", "development-tools::cargo-plugins"] -``` - -Should include a description field like: - -``` -[package] -name = "clippy" -version = "0.0.212" -description = "A bunch of helpful lints to avoid common pitfalls in Rust" -repository = "https://github.com/rust-lang/rust-clippy" -readme = "README.md" -license = "MIT OR Apache-2.0" -keywords = ["clippy", "lint", "plugin"] -categories = ["development-tools", "development-tools::cargo-plugins"] -``` \ No newline at end of file diff --git a/src/docs/case_sensitive_file_extension_comparisons.txt b/src/docs/case_sensitive_file_extension_comparisons.txt deleted file mode 100644 index 8e6e18ed4e23a..0000000000000 --- a/src/docs/case_sensitive_file_extension_comparisons.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for calls to `ends_with` with possible file extensions -and suggests to use a case-insensitive approach instead. - -### Why is this bad? -`ends_with` is case-sensitive and may not detect files with a valid extension. - -### Example -``` -fn is_rust_file(filename: &str) -> bool { - filename.ends_with(".rs") -} -``` -Use instead: -``` -fn is_rust_file(filename: &str) -> bool { - let filename = std::path::Path::new(filename); - filename.extension() - .map_or(false, |ext| ext.eq_ignore_ascii_case("rs")) -} -``` \ No newline at end of file diff --git a/src/docs/cast_abs_to_unsigned.txt b/src/docs/cast_abs_to_unsigned.txt deleted file mode 100644 index c5d8ee034ce59..0000000000000 --- a/src/docs/cast_abs_to_unsigned.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for uses of the `abs()` method that cast the result to unsigned. - -### Why is this bad? -The `unsigned_abs()` method avoids panic when called on the MIN value. - -### Example -``` -let x: i32 = -42; -let y: u32 = x.abs() as u32; -``` -Use instead: -``` -let x: i32 = -42; -let y: u32 = x.unsigned_abs(); -``` \ No newline at end of file diff --git a/src/docs/cast_enum_constructor.txt b/src/docs/cast_enum_constructor.txt deleted file mode 100644 index 675c03a42bc23..0000000000000 --- a/src/docs/cast_enum_constructor.txt +++ /dev/null @@ -1,11 +0,0 @@ -### What it does -Checks for casts from an enum tuple constructor to an integer. - -### Why is this bad? -The cast is easily confused with casting a c-like enum value to an integer. - -### Example -``` -enum E { X(i32) }; -let _ = E::X as usize; -``` \ No newline at end of file diff --git a/src/docs/cast_enum_truncation.txt b/src/docs/cast_enum_truncation.txt deleted file mode 100644 index abe32a8296da2..0000000000000 --- a/src/docs/cast_enum_truncation.txt +++ /dev/null @@ -1,12 +0,0 @@ -### What it does -Checks for casts from an enum type to an integral type which will definitely truncate the -value. - -### Why is this bad? -The resulting integral value will not match the value of the variant it came from. - -### Example -``` -enum E { X = 256 }; -let _ = E::X as u8; -``` \ No newline at end of file diff --git a/src/docs/cast_lossless.txt b/src/docs/cast_lossless.txt deleted file mode 100644 index c3a61dd470fc9..0000000000000 --- a/src/docs/cast_lossless.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Checks for casts between numerical types that may -be replaced by safe conversion functions. - -### Why is this bad? -Rust's `as` keyword will perform many kinds of -conversions, including silently lossy conversions. Conversion functions such -as `i32::from` will only perform lossless conversions. Using the conversion -functions prevents conversions from turning into silent lossy conversions if -the types of the input expressions ever change, and make it easier for -people reading the code to know that the conversion is lossless. - -### Example -``` -fn as_u64(x: u8) -> u64 { - x as u64 -} -``` - -Using `::from` would look like this: - -``` -fn as_u64(x: u8) -> u64 { - u64::from(x) -} -``` \ No newline at end of file diff --git a/src/docs/cast_nan_to_int.txt b/src/docs/cast_nan_to_int.txt deleted file mode 100644 index 122f5da0c9218..0000000000000 --- a/src/docs/cast_nan_to_int.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for a known NaN float being cast to an integer - -### Why is this bad? -NaNs are cast into zero, so one could simply use this and make the -code more readable. The lint could also hint at a programmer error. - -### Example -``` -let _: (0.0_f32 / 0.0) as u64; -``` -Use instead: -``` -let _: = 0_u64; -``` \ No newline at end of file diff --git a/src/docs/cast_possible_truncation.txt b/src/docs/cast_possible_truncation.txt deleted file mode 100644 index 0b164848cc7c2..0000000000000 --- a/src/docs/cast_possible_truncation.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for casts between numerical types that may -truncate large values. This is expected behavior, so the cast is `Allow` by -default. - -### Why is this bad? -In some problem domains, it is good practice to avoid -truncation. This lint can be activated to help assess where additional -checks could be beneficial. - -### Example -``` -fn as_u8(x: u64) -> u8 { - x as u8 -} -``` \ No newline at end of file diff --git a/src/docs/cast_possible_wrap.txt b/src/docs/cast_possible_wrap.txt deleted file mode 100644 index f883fc9cfb994..0000000000000 --- a/src/docs/cast_possible_wrap.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for casts from an unsigned type to a signed type of -the same size. Performing such a cast is a 'no-op' for the compiler, -i.e., nothing is changed at the bit level, and the binary representation of -the value is reinterpreted. This can cause wrapping if the value is too big -for the target signed type. However, the cast works as defined, so this lint -is `Allow` by default. - -### Why is this bad? -While such a cast is not bad in itself, the results can -be surprising when this is not the intended behavior, as demonstrated by the -example below. - -### Example -``` -u32::MAX as i32; // will yield a value of `-1` -``` \ No newline at end of file diff --git a/src/docs/cast_precision_loss.txt b/src/docs/cast_precision_loss.txt deleted file mode 100644 index f915d9f8a6d0d..0000000000000 --- a/src/docs/cast_precision_loss.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for casts from any numerical to a float type where -the receiving type cannot store all values from the original type without -rounding errors. This possible rounding is to be expected, so this lint is -`Allow` by default. - -Basically, this warns on casting any integer with 32 or more bits to `f32` -or any 64-bit integer to `f64`. - -### Why is this bad? -It's not bad at all. But in some applications it can be -helpful to know where precision loss can take place. This lint can help find -those places in the code. - -### Example -``` -let x = u64::MAX; -x as f64; -``` \ No newline at end of file diff --git a/src/docs/cast_ptr_alignment.txt b/src/docs/cast_ptr_alignment.txt deleted file mode 100644 index 6a6d4dcaa2ae5..0000000000000 --- a/src/docs/cast_ptr_alignment.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for casts, using `as` or `pointer::cast`, -from a less-strictly-aligned pointer to a more-strictly-aligned pointer - -### Why is this bad? -Dereferencing the resulting pointer may be undefined -behavior. - -### Known problems -Using `std::ptr::read_unaligned` and `std::ptr::write_unaligned` or similar -on the resulting pointer is fine. Is over-zealous: Casts with manual alignment checks or casts like -u64-> u8 -> u16 can be fine. Miri is able to do a more in-depth analysis. - -### Example -``` -let _ = (&1u8 as *const u8) as *const u16; -let _ = (&mut 1u8 as *mut u8) as *mut u16; - -(&1u8 as *const u8).cast::(); -(&mut 1u8 as *mut u8).cast::(); -``` \ No newline at end of file diff --git a/src/docs/cast_ref_to_mut.txt b/src/docs/cast_ref_to_mut.txt deleted file mode 100644 index fb5b4dbb62d82..0000000000000 --- a/src/docs/cast_ref_to_mut.txt +++ /dev/null @@ -1,28 +0,0 @@ -### What it does -Checks for casts of `&T` to `&mut T` anywhere in the code. - -### Why is this bad? -It’s basically guaranteed to be undefined behavior. -`UnsafeCell` is the only way to obtain aliasable data that is considered -mutable. - -### Example -``` -fn x(r: &i32) { - unsafe { - *(r as *const _ as *mut _) += 1; - } -} -``` - -Instead consider using interior mutability types. - -``` -use std::cell::UnsafeCell; - -fn x(r: &UnsafeCell) { - unsafe { - *r.get() += 1; - } -} -``` \ No newline at end of file diff --git a/src/docs/cast_sign_loss.txt b/src/docs/cast_sign_loss.txt deleted file mode 100644 index d64fe1b07f469..0000000000000 --- a/src/docs/cast_sign_loss.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for casts from a signed to an unsigned numerical -type. In this case, negative values wrap around to large positive values, -which can be quite surprising in practice. However, as the cast works as -defined, this lint is `Allow` by default. - -### Why is this bad? -Possibly surprising results. You can activate this lint -as a one-time check to see where numerical wrapping can arise. - -### Example -``` -let y: i8 = -1; -y as u128; // will return 18446744073709551615 -``` \ No newline at end of file diff --git a/src/docs/cast_slice_different_sizes.txt b/src/docs/cast_slice_different_sizes.txt deleted file mode 100644 index c01ef0ba92c03..0000000000000 --- a/src/docs/cast_slice_different_sizes.txt +++ /dev/null @@ -1,38 +0,0 @@ -### What it does -Checks for `as` casts between raw pointers to slices with differently sized elements. - -### Why is this bad? -The produced raw pointer to a slice does not update its length metadata. The produced -pointer will point to a different number of bytes than the original pointer because the -length metadata of a raw slice pointer is in elements rather than bytes. -Producing a slice reference from the raw pointer will either create a slice with -less data (which can be surprising) or create a slice with more data and cause Undefined Behavior. - -### Example -// Missing data -``` -let a = [1_i32, 2, 3, 4]; -let p = &a as *const [i32] as *const [u8]; -unsafe { - println!("{:?}", &*p); -} -``` -// Undefined Behavior (note: also potential alignment issues) -``` -let a = [1_u8, 2, 3, 4]; -let p = &a as *const [u8] as *const [u32]; -unsafe { - println!("{:?}", &*p); -} -``` -Instead use `ptr::slice_from_raw_parts` to construct a slice from a data pointer and the correct length -``` -let a = [1_i32, 2, 3, 4]; -let old_ptr = &a as *const [i32]; -// The data pointer is cast to a pointer to the target `u8` not `[u8]` -// The length comes from the known length of 4 i32s times the 4 bytes per i32 -let new_ptr = core::ptr::slice_from_raw_parts(old_ptr as *const u8, 16); -unsafe { - println!("{:?}", &*new_ptr); -} -``` \ No newline at end of file diff --git a/src/docs/cast_slice_from_raw_parts.txt b/src/docs/cast_slice_from_raw_parts.txt deleted file mode 100644 index b58c739766aab..0000000000000 --- a/src/docs/cast_slice_from_raw_parts.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Checks for a raw slice being cast to a slice pointer - -### Why is this bad? -This can result in multiple `&mut` references to the same location when only a pointer is -required. -`ptr::slice_from_raw_parts` is a safe alternative that doesn't require -the same [safety requirements] to be upheld. - -### Example -``` -let _: *const [u8] = std::slice::from_raw_parts(ptr, len) as *const _; -let _: *mut [u8] = std::slice::from_raw_parts_mut(ptr, len) as *mut _; -``` -Use instead: -``` -let _: *const [u8] = std::ptr::slice_from_raw_parts(ptr, len); -let _: *mut [u8] = std::ptr::slice_from_raw_parts_mut(ptr, len); -``` -[safety requirements]: https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html#safety \ No newline at end of file diff --git a/src/docs/char_lit_as_u8.txt b/src/docs/char_lit_as_u8.txt deleted file mode 100644 index 00d60b9a451b9..0000000000000 --- a/src/docs/char_lit_as_u8.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for expressions where a character literal is cast -to `u8` and suggests using a byte literal instead. - -### Why is this bad? -In general, casting values to smaller types is -error-prone and should be avoided where possible. In the particular case of -converting a character literal to u8, it is easy to avoid by just using a -byte literal instead. As an added bonus, `b'a'` is even slightly shorter -than `'a' as u8`. - -### Example -``` -'x' as u8 -``` - -A better version, using the byte literal: - -``` -b'x' -``` \ No newline at end of file diff --git a/src/docs/chars_last_cmp.txt b/src/docs/chars_last_cmp.txt deleted file mode 100644 index 4c1d8838973a7..0000000000000 --- a/src/docs/chars_last_cmp.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for usage of `_.chars().last()` or -`_.chars().next_back()` on a `str` to check if it ends with a given char. - -### Why is this bad? -Readability, this can be written more concisely as -`_.ends_with(_)`. - -### Example -``` -name.chars().last() == Some('_') || name.chars().next_back() == Some('-'); -``` - -Use instead: -``` -name.ends_with('_') || name.ends_with('-'); -``` \ No newline at end of file diff --git a/src/docs/chars_next_cmp.txt b/src/docs/chars_next_cmp.txt deleted file mode 100644 index 77cbce2de00f9..0000000000000 --- a/src/docs/chars_next_cmp.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for usage of `.chars().next()` on a `str` to check -if it starts with a given char. - -### Why is this bad? -Readability, this can be written more concisely as -`_.starts_with(_)`. - -### Example -``` -let name = "foo"; -if name.chars().next() == Some('_') {}; -``` - -Use instead: -``` -let name = "foo"; -if name.starts_with('_') {}; -``` \ No newline at end of file diff --git a/src/docs/checked_conversions.txt b/src/docs/checked_conversions.txt deleted file mode 100644 index 536b01294ee17..0000000000000 --- a/src/docs/checked_conversions.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for explicit bounds checking when casting. - -### Why is this bad? -Reduces the readability of statements & is error prone. - -### Example -``` -foo <= i32::MAX as u32; -``` - -Use instead: -``` -i32::try_from(foo).is_ok(); -``` \ No newline at end of file diff --git a/src/docs/clone_double_ref.txt b/src/docs/clone_double_ref.txt deleted file mode 100644 index 2729635bd246f..0000000000000 --- a/src/docs/clone_double_ref.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for usage of `.clone()` on an `&&T`. - -### Why is this bad? -Cloning an `&&T` copies the inner `&T`, instead of -cloning the underlying `T`. - -### Example -``` -fn main() { - let x = vec![1]; - let y = &&x; - let z = y.clone(); - println!("{:p} {:p}", *y, z); // prints out the same pointer -} -``` \ No newline at end of file diff --git a/src/docs/clone_on_copy.txt b/src/docs/clone_on_copy.txt deleted file mode 100644 index 99a0bdb4c4ac0..0000000000000 --- a/src/docs/clone_on_copy.txt +++ /dev/null @@ -1,11 +0,0 @@ -### What it does -Checks for usage of `.clone()` on a `Copy` type. - -### Why is this bad? -The only reason `Copy` types implement `Clone` is for -generics, not for using the `clone` method on a concrete type. - -### Example -``` -42u64.clone(); -``` \ No newline at end of file diff --git a/src/docs/clone_on_ref_ptr.txt b/src/docs/clone_on_ref_ptr.txt deleted file mode 100644 index 2d83f8fefc122..0000000000000 --- a/src/docs/clone_on_ref_ptr.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for usage of `.clone()` on a ref-counted pointer, -(`Rc`, `Arc`, `rc::Weak`, or `sync::Weak`), and suggests calling Clone via unified -function syntax instead (e.g., `Rc::clone(foo)`). - -### Why is this bad? -Calling '.clone()' on an Rc, Arc, or Weak -can obscure the fact that only the pointer is being cloned, not the underlying -data. - -### Example -``` -let x = Rc::new(1); - -x.clone(); -``` - -Use instead: -``` -Rc::clone(&x); -``` \ No newline at end of file diff --git a/src/docs/cloned_instead_of_copied.txt b/src/docs/cloned_instead_of_copied.txt deleted file mode 100644 index 2f2014d5fd299..0000000000000 --- a/src/docs/cloned_instead_of_copied.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for usages of `cloned()` on an `Iterator` or `Option` where -`copied()` could be used instead. - -### Why is this bad? -`copied()` is better because it guarantees that the type being cloned -implements `Copy`. - -### Example -``` -[1, 2, 3].iter().cloned(); -``` -Use instead: -``` -[1, 2, 3].iter().copied(); -``` \ No newline at end of file diff --git a/src/docs/cmp_nan.txt b/src/docs/cmp_nan.txt deleted file mode 100644 index e2ad04d932359..0000000000000 --- a/src/docs/cmp_nan.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for comparisons to NaN. - -### Why is this bad? -NaN does not compare meaningfully to anything – not -even itself – so those comparisons are simply wrong. - -### Example -``` -if x == f32::NAN { } -``` - -Use instead: -``` -if x.is_nan() { } -``` \ No newline at end of file diff --git a/src/docs/cmp_null.txt b/src/docs/cmp_null.txt deleted file mode 100644 index 02fd15124f038..0000000000000 --- a/src/docs/cmp_null.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -This lint checks for equality comparisons with `ptr::null` - -### Why is this bad? -It's easier and more readable to use the inherent -`.is_null()` -method instead - -### Example -``` -use std::ptr; - -if x == ptr::null { - // .. -} -``` - -Use instead: -``` -if x.is_null() { - // .. -} -``` \ No newline at end of file diff --git a/src/docs/cmp_owned.txt b/src/docs/cmp_owned.txt deleted file mode 100644 index f8d4956ff1d4b..0000000000000 --- a/src/docs/cmp_owned.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for conversions to owned values just for the sake -of a comparison. - -### Why is this bad? -The comparison can operate on a reference, so creating -an owned value effectively throws it away directly afterwards, which is -needlessly consuming code and heap space. - -### Example -``` -if x.to_owned() == y {} -``` - -Use instead: -``` -if x == y {} -``` \ No newline at end of file diff --git a/src/docs/cognitive_complexity.txt b/src/docs/cognitive_complexity.txt deleted file mode 100644 index fdd75f6479cd0..0000000000000 --- a/src/docs/cognitive_complexity.txt +++ /dev/null @@ -1,13 +0,0 @@ -### What it does -Checks for methods with high cognitive complexity. - -### Why is this bad? -Methods of high cognitive complexity tend to be hard to -both read and maintain. Also LLVM will tend to optimize small methods better. - -### Known problems -Sometimes it's hard to find a way to reduce the -complexity. - -### Example -You'll see it when you get the warning. \ No newline at end of file diff --git a/src/docs/collapsible_else_if.txt b/src/docs/collapsible_else_if.txt deleted file mode 100644 index 4ddfca17731f7..0000000000000 --- a/src/docs/collapsible_else_if.txt +++ /dev/null @@ -1,29 +0,0 @@ -### What it does -Checks for collapsible `else { if ... }` expressions -that can be collapsed to `else if ...`. - -### Why is this bad? -Each `if`-statement adds one level of nesting, which -makes code look more complex than it really is. - -### Example -``` - -if x { - … -} else { - if y { - … - } -} -``` - -Should be written: - -``` -if x { - … -} else if y { - … -} -``` \ No newline at end of file diff --git a/src/docs/collapsible_if.txt b/src/docs/collapsible_if.txt deleted file mode 100644 index e1264ee062e95..0000000000000 --- a/src/docs/collapsible_if.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for nested `if` statements which can be collapsed -by `&&`-combining their conditions. - -### Why is this bad? -Each `if`-statement adds one level of nesting, which -makes code look more complex than it really is. - -### Example -``` -if x { - if y { - // … - } -} -``` - -Use instead: -``` -if x && y { - // … -} -``` \ No newline at end of file diff --git a/src/docs/collapsible_match.txt b/src/docs/collapsible_match.txt deleted file mode 100644 index 0d59594a03a27..0000000000000 --- a/src/docs/collapsible_match.txt +++ /dev/null @@ -1,31 +0,0 @@ -### What it does -Finds nested `match` or `if let` expressions where the patterns may be "collapsed" together -without adding any branches. - -Note that this lint is not intended to find _all_ cases where nested match patterns can be merged, but only -cases where merging would most likely make the code more readable. - -### Why is this bad? -It is unnecessarily verbose and complex. - -### Example -``` -fn func(opt: Option>) { - let n = match opt { - Some(n) => match n { - Ok(n) => n, - _ => return, - } - None => return, - }; -} -``` -Use instead: -``` -fn func(opt: Option>) { - let n = match opt { - Some(Ok(n)) => n, - _ => return, - }; -} -``` \ No newline at end of file diff --git a/src/docs/collapsible_str_replace.txt b/src/docs/collapsible_str_replace.txt deleted file mode 100644 index c24c25a3028a5..0000000000000 --- a/src/docs/collapsible_str_replace.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for consecutive calls to `str::replace` (2 or more) -that can be collapsed into a single call. - -### Why is this bad? -Consecutive `str::replace` calls scan the string multiple times -with repetitive code. - -### Example -``` -let hello = "hesuo worpd" - .replace('s', "l") - .replace("u", "l") - .replace('p', "l"); -``` -Use instead: -``` -let hello = "hesuo worpd".replace(&['s', 'u', 'p'], "l"); -``` \ No newline at end of file diff --git a/src/docs/comparison_chain.txt b/src/docs/comparison_chain.txt deleted file mode 100644 index 43b09f31ff4aa..0000000000000 --- a/src/docs/comparison_chain.txt +++ /dev/null @@ -1,36 +0,0 @@ -### What it does -Checks comparison chains written with `if` that can be -rewritten with `match` and `cmp`. - -### Why is this bad? -`if` is not guaranteed to be exhaustive and conditionals can get -repetitive - -### Known problems -The match statement may be slower due to the compiler -not inlining the call to cmp. See issue [#5354](https://github.com/rust-lang/rust-clippy/issues/5354) - -### Example -``` -fn f(x: u8, y: u8) { - if x > y { - a() - } else if x < y { - b() - } else { - c() - } -} -``` - -Use instead: -``` -use std::cmp::Ordering; -fn f(x: u8, y: u8) { - match x.cmp(&y) { - Ordering::Greater => a(), - Ordering::Less => b(), - Ordering::Equal => c() - } -} -``` \ No newline at end of file diff --git a/src/docs/comparison_to_empty.txt b/src/docs/comparison_to_empty.txt deleted file mode 100644 index db6f74fe2706b..0000000000000 --- a/src/docs/comparison_to_empty.txt +++ /dev/null @@ -1,31 +0,0 @@ -### What it does -Checks for comparing to an empty slice such as `""` or `[]`, -and suggests using `.is_empty()` where applicable. - -### Why is this bad? -Some structures can answer `.is_empty()` much faster -than checking for equality. So it is good to get into the habit of using -`.is_empty()`, and having it is cheap. -Besides, it makes the intent clearer than a manual comparison in some contexts. - -### Example - -``` -if s == "" { - .. -} - -if arr == [] { - .. -} -``` -Use instead: -``` -if s.is_empty() { - .. -} - -if arr.is_empty() { - .. -} -``` \ No newline at end of file diff --git a/src/docs/copy_iterator.txt b/src/docs/copy_iterator.txt deleted file mode 100644 index 5f9a2a015b86c..0000000000000 --- a/src/docs/copy_iterator.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Checks for types that implement `Copy` as well as -`Iterator`. - -### Why is this bad? -Implicit copies can be confusing when working with -iterator combinators. - -### Example -``` -#[derive(Copy, Clone)] -struct Countdown(u8); - -impl Iterator for Countdown { - // ... -} - -let a: Vec<_> = my_iterator.take(1).collect(); -let b: Vec<_> = my_iterator.collect(); -``` \ No newline at end of file diff --git a/src/docs/crate_in_macro_def.txt b/src/docs/crate_in_macro_def.txt deleted file mode 100644 index 047e986dee71f..0000000000000 --- a/src/docs/crate_in_macro_def.txt +++ /dev/null @@ -1,35 +0,0 @@ -### What it does -Checks for use of `crate` as opposed to `$crate` in a macro definition. - -### Why is this bad? -`crate` refers to the macro call's crate, whereas `$crate` refers to the macro definition's -crate. Rarely is the former intended. See: -https://doc.rust-lang.org/reference/macros-by-example.html#hygiene - -### Example -``` -#[macro_export] -macro_rules! print_message { - () => { - println!("{}", crate::MESSAGE); - }; -} -pub const MESSAGE: &str = "Hello!"; -``` -Use instead: -``` -#[macro_export] -macro_rules! print_message { - () => { - println!("{}", $crate::MESSAGE); - }; -} -pub const MESSAGE: &str = "Hello!"; -``` - -Note that if the use of `crate` is intentional, an `allow` attribute can be applied to the -macro definition, e.g.: -``` -#[allow(clippy::crate_in_macro_def)] -macro_rules! ok { ... crate::foo ... } -``` \ No newline at end of file diff --git a/src/docs/create_dir.txt b/src/docs/create_dir.txt deleted file mode 100644 index e4e7937684e6e..0000000000000 --- a/src/docs/create_dir.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks usage of `std::fs::create_dir` and suggest using `std::fs::create_dir_all` instead. - -### Why is this bad? -Sometimes `std::fs::create_dir` is mistakenly chosen over `std::fs::create_dir_all`. - -### Example -``` -std::fs::create_dir("foo"); -``` - -Use instead: -``` -std::fs::create_dir_all("foo"); -``` \ No newline at end of file diff --git a/src/docs/crosspointer_transmute.txt b/src/docs/crosspointer_transmute.txt deleted file mode 100644 index 49dea154970e5..0000000000000 --- a/src/docs/crosspointer_transmute.txt +++ /dev/null @@ -1,12 +0,0 @@ -### What it does -Checks for transmutes between a type `T` and `*T`. - -### Why is this bad? -It's easy to mistakenly transmute between a type and a -pointer to that type. - -### Example -``` -core::intrinsics::transmute(t) // where the result type is the same as - // `*t` or `&t`'s -``` \ No newline at end of file diff --git a/src/docs/dbg_macro.txt b/src/docs/dbg_macro.txt deleted file mode 100644 index 3e1a9a043f9f4..0000000000000 --- a/src/docs/dbg_macro.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for usage of dbg!() macro. - -### Why is this bad? -`dbg!` macro is intended as a debugging tool. It -should not be in version control. - -### Example -``` -dbg!(true) -``` - -Use instead: -``` -true -``` \ No newline at end of file diff --git a/src/docs/debug_assert_with_mut_call.txt b/src/docs/debug_assert_with_mut_call.txt deleted file mode 100644 index 2c44abe1f05c8..0000000000000 --- a/src/docs/debug_assert_with_mut_call.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for function/method calls with a mutable -parameter in `debug_assert!`, `debug_assert_eq!` and `debug_assert_ne!` macros. - -### Why is this bad? -In release builds `debug_assert!` macros are optimized out by the -compiler. -Therefore mutating something in a `debug_assert!` macro results in different behavior -between a release and debug build. - -### Example -``` -debug_assert_eq!(vec![3].pop(), Some(3)); - -// or - -debug_assert!(takes_a_mut_parameter(&mut x)); -``` \ No newline at end of file diff --git a/src/docs/decimal_literal_representation.txt b/src/docs/decimal_literal_representation.txt deleted file mode 100644 index daca9bbb3a848..0000000000000 --- a/src/docs/decimal_literal_representation.txt +++ /dev/null @@ -1,13 +0,0 @@ -### What it does -Warns if there is a better representation for a numeric literal. - -### Why is this bad? -Especially for big powers of 2 a hexadecimal representation is more -readable than a decimal representation. - -### Example -``` -`255` => `0xFF` -`65_535` => `0xFFFF` -`4_042_322_160` => `0xF0F0_F0F0` -``` \ No newline at end of file diff --git a/src/docs/declare_interior_mutable_const.txt b/src/docs/declare_interior_mutable_const.txt deleted file mode 100644 index 2801b5ccff802..0000000000000 --- a/src/docs/declare_interior_mutable_const.txt +++ /dev/null @@ -1,46 +0,0 @@ -### What it does -Checks for declaration of `const` items which is interior -mutable (e.g., contains a `Cell`, `Mutex`, `AtomicXxxx`, etc.). - -### Why is this bad? -Consts are copied everywhere they are referenced, i.e., -every time you refer to the const a fresh instance of the `Cell` or `Mutex` -or `AtomicXxxx` will be created, which defeats the whole purpose of using -these types in the first place. - -The `const` should better be replaced by a `static` item if a global -variable is wanted, or replaced by a `const fn` if a constructor is wanted. - -### Known problems -A "non-constant" const item is a legacy way to supply an -initialized value to downstream `static` items (e.g., the -`std::sync::ONCE_INIT` constant). In this case the use of `const` is legit, -and this lint should be suppressed. - -Even though the lint avoids triggering on a constant whose type has enums that have variants -with interior mutability, and its value uses non interior mutable variants (see -[#3962](https://github.com/rust-lang/rust-clippy/issues/3962) and -[#3825](https://github.com/rust-lang/rust-clippy/issues/3825) for examples); -it complains about associated constants without default values only based on its types; -which might not be preferable. -There're other enums plus associated constants cases that the lint cannot handle. - -Types that have underlying or potential interior mutability trigger the lint whether -the interior mutable field is used or not. See issues -[#5812](https://github.com/rust-lang/rust-clippy/issues/5812) and - -### Example -``` -use std::sync::atomic::{AtomicUsize, Ordering::SeqCst}; - -const CONST_ATOM: AtomicUsize = AtomicUsize::new(12); -CONST_ATOM.store(6, SeqCst); // the content of the atomic is unchanged -assert_eq!(CONST_ATOM.load(SeqCst), 12); // because the CONST_ATOM in these lines are distinct -``` - -Use instead: -``` -static STATIC_ATOM: AtomicUsize = AtomicUsize::new(15); -STATIC_ATOM.store(9, SeqCst); -assert_eq!(STATIC_ATOM.load(SeqCst), 9); // use a `static` item to refer to the same instance -``` \ No newline at end of file diff --git a/src/docs/default_instead_of_iter_empty.txt b/src/docs/default_instead_of_iter_empty.txt deleted file mode 100644 index b63ef3d18fc41..0000000000000 --- a/src/docs/default_instead_of_iter_empty.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -It checks for `std::iter::Empty::default()` and suggests replacing it with -`std::iter::empty()`. -### Why is this bad? -`std::iter::empty()` is the more idiomatic way. -### Example -``` -let _ = std::iter::Empty::::default(); -let iter: std::iter::Empty = std::iter::Empty::default(); -``` -Use instead: -``` -let _ = std::iter::empty::(); -let iter: std::iter::Empty = std::iter::empty(); -``` \ No newline at end of file diff --git a/src/docs/default_numeric_fallback.txt b/src/docs/default_numeric_fallback.txt deleted file mode 100644 index 15076a0a68bf6..0000000000000 --- a/src/docs/default_numeric_fallback.txt +++ /dev/null @@ -1,28 +0,0 @@ -### What it does -Checks for usage of unconstrained numeric literals which may cause default numeric fallback in type -inference. - -Default numeric fallback means that if numeric types have not yet been bound to concrete -types at the end of type inference, then integer type is bound to `i32`, and similarly -floating type is bound to `f64`. - -See [RFC0212](https://github.com/rust-lang/rfcs/blob/master/text/0212-restore-int-fallback.md) for more information about the fallback. - -### Why is this bad? -For those who are very careful about types, default numeric fallback -can be a pitfall that cause unexpected runtime behavior. - -### Known problems -This lint can only be allowed at the function level or above. - -### Example -``` -let i = 10; -let f = 1.23; -``` - -Use instead: -``` -let i = 10i32; -let f = 1.23f64; -``` \ No newline at end of file diff --git a/src/docs/default_trait_access.txt b/src/docs/default_trait_access.txt deleted file mode 100644 index e69298969c8ed..0000000000000 --- a/src/docs/default_trait_access.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for literal calls to `Default::default()`. - -### Why is this bad? -It's easier for the reader if the name of the type is used, rather than the -generic `Default`. - -### Example -``` -let s: String = Default::default(); -``` - -Use instead: -``` -let s = String::default(); -``` \ No newline at end of file diff --git a/src/docs/default_union_representation.txt b/src/docs/default_union_representation.txt deleted file mode 100644 index f79ff9760e57e..0000000000000 --- a/src/docs/default_union_representation.txt +++ /dev/null @@ -1,36 +0,0 @@ -### What it does -Displays a warning when a union is declared with the default representation (without a `#[repr(C)]` attribute). - -### Why is this bad? -Unions in Rust have unspecified layout by default, despite many people thinking that they -lay out each field at the start of the union (like C does). That is, there are no guarantees -about the offset of the fields for unions with multiple non-ZST fields without an explicitly -specified layout. These cases may lead to undefined behavior in unsafe blocks. - -### Example -``` -union Foo { - a: i32, - b: u32, -} - -fn main() { - let _x: u32 = unsafe { - Foo { a: 0_i32 }.b // Undefined behavior: `b` is allowed to be padding - }; -} -``` -Use instead: -``` -#[repr(C)] -union Foo { - a: i32, - b: u32, -} - -fn main() { - let _x: u32 = unsafe { - Foo { a: 0_i32 }.b // Now defined behavior, this is just an i32 -> u32 transmute - }; -} -``` \ No newline at end of file diff --git a/src/docs/deprecated_cfg_attr.txt b/src/docs/deprecated_cfg_attr.txt deleted file mode 100644 index 9f264887a057e..0000000000000 --- a/src/docs/deprecated_cfg_attr.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks for `#[cfg_attr(rustfmt, rustfmt_skip)]` and suggests to replace it -with `#[rustfmt::skip]`. - -### Why is this bad? -Since tool_attributes ([rust-lang/rust#44690](https://github.com/rust-lang/rust/issues/44690)) -are stable now, they should be used instead of the old `cfg_attr(rustfmt)` attributes. - -### Known problems -This lint doesn't detect crate level inner attributes, because they get -processed before the PreExpansionPass lints get executed. See -[#3123](https://github.com/rust-lang/rust-clippy/pull/3123#issuecomment-422321765) - -### Example -``` -#[cfg_attr(rustfmt, rustfmt_skip)] -fn main() { } -``` - -Use instead: -``` -#[rustfmt::skip] -fn main() { } -``` \ No newline at end of file diff --git a/src/docs/deprecated_semver.txt b/src/docs/deprecated_semver.txt deleted file mode 100644 index c9574a99b2bec..0000000000000 --- a/src/docs/deprecated_semver.txt +++ /dev/null @@ -1,13 +0,0 @@ -### What it does -Checks for `#[deprecated]` annotations with a `since` -field that is not a valid semantic version. - -### Why is this bad? -For checking the version of the deprecation, it must be -a valid semver. Failing that, the contained information is useless. - -### Example -``` -#[deprecated(since = "forever")] -fn something_else() { /* ... */ } -``` \ No newline at end of file diff --git a/src/docs/deref_addrof.txt b/src/docs/deref_addrof.txt deleted file mode 100644 index fa711b924d480..0000000000000 --- a/src/docs/deref_addrof.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for usage of `*&` and `*&mut` in expressions. - -### Why is this bad? -Immediately dereferencing a reference is no-op and -makes the code less clear. - -### Known problems -Multiple dereference/addrof pairs are not handled so -the suggested fix for `x = **&&y` is `x = *&y`, which is still incorrect. - -### Example -``` -let a = f(*&mut b); -let c = *&d; -``` - -Use instead: -``` -let a = f(b); -let c = d; -``` \ No newline at end of file diff --git a/src/docs/deref_by_slicing.txt b/src/docs/deref_by_slicing.txt deleted file mode 100644 index 4dad24ac00cad..0000000000000 --- a/src/docs/deref_by_slicing.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for slicing expressions which are equivalent to dereferencing the -value. - -### Why is this bad? -Some people may prefer to dereference rather than slice. - -### Example -``` -let vec = vec![1, 2, 3]; -let slice = &vec[..]; -``` -Use instead: -``` -let vec = vec![1, 2, 3]; -let slice = &*vec; -``` \ No newline at end of file diff --git a/src/docs/derivable_impls.txt b/src/docs/derivable_impls.txt deleted file mode 100644 index 5cee43956cc36..0000000000000 --- a/src/docs/derivable_impls.txt +++ /dev/null @@ -1,35 +0,0 @@ -### What it does -Detects manual `std::default::Default` implementations that are identical to a derived implementation. - -### Why is this bad? -It is less concise. - -### Example -``` -struct Foo { - bar: bool -} - -impl Default for Foo { - fn default() -> Self { - Self { - bar: false - } - } -} -``` - -Use instead: -``` -#[derive(Default)] -struct Foo { - bar: bool -} -``` - -### Known problems -Derive macros [sometimes use incorrect bounds](https://github.com/rust-lang/rust/issues/26925) -in generic types and the user defined `impl` may be more generalized or -specialized than what derive will produce. This lint can't detect the manual `impl` -has exactly equal bounds, and therefore this lint is disabled for types with -generic parameters. \ No newline at end of file diff --git a/src/docs/derive_hash_xor_eq.txt b/src/docs/derive_hash_xor_eq.txt deleted file mode 100644 index fbf623d5adbc1..0000000000000 --- a/src/docs/derive_hash_xor_eq.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for deriving `Hash` but implementing `PartialEq` -explicitly or vice versa. - -### Why is this bad? -The implementation of these traits must agree (for -example for use with `HashMap`) so it’s probably a bad idea to use a -default-generated `Hash` implementation with an explicitly defined -`PartialEq`. In particular, the following must hold for any type: - -``` -k1 == k2 ⇒ hash(k1) == hash(k2) -``` - -### Example -``` -#[derive(Hash)] -struct Foo; - -impl PartialEq for Foo { - ... -} -``` \ No newline at end of file diff --git a/src/docs/derive_ord_xor_partial_ord.txt b/src/docs/derive_ord_xor_partial_ord.txt deleted file mode 100644 index f2107a5f69eea..0000000000000 --- a/src/docs/derive_ord_xor_partial_ord.txt +++ /dev/null @@ -1,44 +0,0 @@ -### What it does -Checks for deriving `Ord` but implementing `PartialOrd` -explicitly or vice versa. - -### Why is this bad? -The implementation of these traits must agree (for -example for use with `sort`) so it’s probably a bad idea to use a -default-generated `Ord` implementation with an explicitly defined -`PartialOrd`. In particular, the following must hold for any type -implementing `Ord`: - -``` -k1.cmp(&k2) == k1.partial_cmp(&k2).unwrap() -``` - -### Example -``` -#[derive(Ord, PartialEq, Eq)] -struct Foo; - -impl PartialOrd for Foo { - ... -} -``` -Use instead: -``` -#[derive(PartialEq, Eq)] -struct Foo; - -impl PartialOrd for Foo { - fn partial_cmp(&self, other: &Foo) -> Option { - Some(self.cmp(other)) - } -} - -impl Ord for Foo { - ... -} -``` -or, if you don't need a custom ordering: -``` -#[derive(Ord, PartialOrd, PartialEq, Eq)] -struct Foo; -``` \ No newline at end of file diff --git a/src/docs/derive_partial_eq_without_eq.txt b/src/docs/derive_partial_eq_without_eq.txt deleted file mode 100644 index 932fabad666c3..0000000000000 --- a/src/docs/derive_partial_eq_without_eq.txt +++ /dev/null @@ -1,25 +0,0 @@ -### What it does -Checks for types that derive `PartialEq` and could implement `Eq`. - -### Why is this bad? -If a type `T` derives `PartialEq` and all of its members implement `Eq`, -then `T` can always implement `Eq`. Implementing `Eq` allows `T` to be used -in APIs that require `Eq` types. It also allows structs containing `T` to derive -`Eq` themselves. - -### Example -``` -#[derive(PartialEq)] -struct Foo { - i_am_eq: i32, - i_am_eq_too: Vec, -} -``` -Use instead: -``` -#[derive(PartialEq, Eq)] -struct Foo { - i_am_eq: i32, - i_am_eq_too: Vec, -} -``` \ No newline at end of file diff --git a/src/docs/disallowed_macros.txt b/src/docs/disallowed_macros.txt deleted file mode 100644 index 96fa15afabfd2..0000000000000 --- a/src/docs/disallowed_macros.txt +++ /dev/null @@ -1,36 +0,0 @@ -### What it does -Denies the configured macros in clippy.toml - -Note: Even though this lint is warn-by-default, it will only trigger if -macros are defined in the clippy.toml file. - -### Why is this bad? -Some macros are undesirable in certain contexts, and it's beneficial to -lint for them as needed. - -### Example -An example clippy.toml configuration: -``` -disallowed-macros = [ - # Can use a string as the path of the disallowed macro. - "std::print", - # Can also use an inline table with a `path` key. - { path = "std::println" }, - # When using an inline table, can add a `reason` for why the macro - # is disallowed. - { path = "serde::Serialize", reason = "no serializing" }, -] -``` -``` -use serde::Serialize; - -// Example code where clippy issues a warning -println!("warns"); - -// The diagnostic will contain the message "no serializing" -#[derive(Serialize)] -struct Data { - name: String, - value: usize, -} -``` \ No newline at end of file diff --git a/src/docs/disallowed_methods.txt b/src/docs/disallowed_methods.txt deleted file mode 100644 index d8ad5b6a6674c..0000000000000 --- a/src/docs/disallowed_methods.txt +++ /dev/null @@ -1,41 +0,0 @@ -### What it does -Denies the configured methods and functions in clippy.toml - -Note: Even though this lint is warn-by-default, it will only trigger if -methods are defined in the clippy.toml file. - -### Why is this bad? -Some methods are undesirable in certain contexts, and it's beneficial to -lint for them as needed. - -### Example -An example clippy.toml configuration: -``` -disallowed-methods = [ - # Can use a string as the path of the disallowed method. - "std::boxed::Box::new", - # Can also use an inline table with a `path` key. - { path = "std::time::Instant::now" }, - # When using an inline table, can add a `reason` for why the method - # is disallowed. - { path = "std::vec::Vec::leak", reason = "no leaking memory" }, -] -``` - -``` -// Example code where clippy issues a warning -let xs = vec![1, 2, 3, 4]; -xs.leak(); // Vec::leak is disallowed in the config. -// The diagnostic contains the message "no leaking memory". - -let _now = Instant::now(); // Instant::now is disallowed in the config. - -let _box = Box::new(3); // Box::new is disallowed in the config. -``` - -Use instead: -``` -// Example code which does not raise clippy warning -let mut xs = Vec::new(); // Vec::new is _not_ disallowed in the config. -xs.push(123); // Vec::push is _not_ disallowed in the config. -``` \ No newline at end of file diff --git a/src/docs/disallowed_names.txt b/src/docs/disallowed_names.txt deleted file mode 100644 index f4aaee9c77b7b..0000000000000 --- a/src/docs/disallowed_names.txt +++ /dev/null @@ -1,12 +0,0 @@ -### What it does -Checks for usage of disallowed names for variables, such -as `foo`. - -### Why is this bad? -These names are usually placeholder names and should be -avoided. - -### Example -``` -let foo = 3.14; -``` \ No newline at end of file diff --git a/src/docs/disallowed_script_idents.txt b/src/docs/disallowed_script_idents.txt deleted file mode 100644 index 2151b7a20dedc..0000000000000 --- a/src/docs/disallowed_script_idents.txt +++ /dev/null @@ -1,32 +0,0 @@ -### What it does -Checks for usage of unicode scripts other than those explicitly allowed -by the lint config. - -This lint doesn't take into account non-text scripts such as `Unknown` and `Linear_A`. -It also ignores the `Common` script type. -While configuring, be sure to use official script name [aliases] from -[the list of supported scripts][supported_scripts]. - -See also: [`non_ascii_idents`]. - -[aliases]: http://www.unicode.org/reports/tr24/tr24-31.html#Script_Value_Aliases -[supported_scripts]: https://www.unicode.org/iso15924/iso15924-codes.html - -### Why is this bad? -It may be not desired to have many different scripts for -identifiers in the codebase. - -Note that if you only want to allow plain English, you might want to use -built-in [`non_ascii_idents`] lint instead. - -[`non_ascii_idents`]: https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#non-ascii-idents - -### Example -``` -// Assuming that `clippy.toml` contains the following line: -// allowed-locales = ["Latin", "Cyrillic"] -let counter = 10; // OK, latin is allowed. -let счётчик = 10; // OK, cyrillic is allowed. -let zähler = 10; // OK, it's still latin. -let カウンタ = 10; // Will spawn the lint. -``` \ No newline at end of file diff --git a/src/docs/disallowed_types.txt b/src/docs/disallowed_types.txt deleted file mode 100644 index 2bcbcddee5666..0000000000000 --- a/src/docs/disallowed_types.txt +++ /dev/null @@ -1,33 +0,0 @@ -### What it does -Denies the configured types in clippy.toml. - -Note: Even though this lint is warn-by-default, it will only trigger if -types are defined in the clippy.toml file. - -### Why is this bad? -Some types are undesirable in certain contexts. - -### Example: -An example clippy.toml configuration: -``` -disallowed-types = [ - # Can use a string as the path of the disallowed type. - "std::collections::BTreeMap", - # Can also use an inline table with a `path` key. - { path = "std::net::TcpListener" }, - # When using an inline table, can add a `reason` for why the type - # is disallowed. - { path = "std::net::Ipv4Addr", reason = "no IPv4 allowed" }, -] -``` - -``` -use std::collections::BTreeMap; -// or its use -let x = std::collections::BTreeMap::new(); -``` -Use instead: -``` -// A similar type that is allowed by the config -use std::collections::HashMap; -``` \ No newline at end of file diff --git a/src/docs/diverging_sub_expression.txt b/src/docs/diverging_sub_expression.txt deleted file mode 100644 index 1943622180251..0000000000000 --- a/src/docs/diverging_sub_expression.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for diverging calls that are not match arms or -statements. - -### Why is this bad? -It is often confusing to read. In addition, the -sub-expression evaluation order for Rust is not well documented. - -### Known problems -Someone might want to use `some_bool || panic!()` as a -shorthand. - -### Example -``` -let a = b() || panic!() || c(); -// `c()` is dead, `panic!()` is only called if `b()` returns `false` -let x = (a, b, c, panic!()); -// can simply be replaced by `panic!()` -``` \ No newline at end of file diff --git a/src/docs/doc_link_with_quotes.txt b/src/docs/doc_link_with_quotes.txt deleted file mode 100644 index 107c8ac116d93..0000000000000 --- a/src/docs/doc_link_with_quotes.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Detects the syntax `['foo']` in documentation comments (notice quotes instead of backticks) -outside of code blocks -### Why is this bad? -It is likely a typo when defining an intra-doc link - -### Example -``` -/// See also: ['foo'] -fn bar() {} -``` -Use instead: -``` -/// See also: [`foo`] -fn bar() {} -``` \ No newline at end of file diff --git a/src/docs/doc_markdown.txt b/src/docs/doc_markdown.txt deleted file mode 100644 index 94f54c587e302..0000000000000 --- a/src/docs/doc_markdown.txt +++ /dev/null @@ -1,35 +0,0 @@ -### What it does -Checks for the presence of `_`, `::` or camel-case words -outside ticks in documentation. - -### Why is this bad? -*Rustdoc* supports markdown formatting, `_`, `::` and -camel-case probably indicates some code which should be included between -ticks. `_` can also be used for emphasis in markdown, this lint tries to -consider that. - -### Known problems -Lots of bad docs won’t be fixed, what the lint checks -for is limited, and there are still false positives. HTML elements and their -content are not linted. - -In addition, when writing documentation comments, including `[]` brackets -inside a link text would trip the parser. Therefore, documenting link with -`[`SmallVec<[T; INLINE_CAPACITY]>`]` and then [`SmallVec<[T; INLINE_CAPACITY]>`]: SmallVec -would fail. - -### Examples -``` -/// Do something with the foo_bar parameter. See also -/// that::other::module::foo. -// ^ `foo_bar` and `that::other::module::foo` should be ticked. -fn doit(foo_bar: usize) {} -``` - -``` -// Link text with `[]` brackets should be written as following: -/// Consume the array and return the inner -/// [`SmallVec<[T; INLINE_CAPACITY]>`][SmallVec]. -/// [SmallVec]: SmallVec -fn main() {} -``` \ No newline at end of file diff --git a/src/docs/double_comparisons.txt b/src/docs/double_comparisons.txt deleted file mode 100644 index 7dc6818779f4b..0000000000000 --- a/src/docs/double_comparisons.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for double comparisons that could be simplified to a single expression. - - -### Why is this bad? -Readability. - -### Example -``` -if x == y || x < y {} -``` - -Use instead: - -``` -if x <= y {} -``` \ No newline at end of file diff --git a/src/docs/double_must_use.txt b/src/docs/double_must_use.txt deleted file mode 100644 index 0017d10d40d33..0000000000000 --- a/src/docs/double_must_use.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for a `#[must_use]` attribute without -further information on functions and methods that return a type already -marked as `#[must_use]`. - -### Why is this bad? -The attribute isn't needed. Not using the result -will already be reported. Alternatively, one can add some text to the -attribute to improve the lint message. - -### Examples -``` -#[must_use] -fn double_must_use() -> Result<(), ()> { - unimplemented!(); -} -``` \ No newline at end of file diff --git a/src/docs/double_neg.txt b/src/docs/double_neg.txt deleted file mode 100644 index a07f67496d7cc..0000000000000 --- a/src/docs/double_neg.txt +++ /dev/null @@ -1,12 +0,0 @@ -### What it does -Detects expressions of the form `--x`. - -### Why is this bad? -It can mislead C/C++ programmers to think `x` was -decremented. - -### Example -``` -let mut x = 3; ---x; -``` \ No newline at end of file diff --git a/src/docs/double_parens.txt b/src/docs/double_parens.txt deleted file mode 100644 index 260d7dd575e55..0000000000000 --- a/src/docs/double_parens.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks for unnecessary double parentheses. - -### Why is this bad? -This makes code harder to read and might indicate a -mistake. - -### Example -``` -fn simple_double_parens() -> i32 { - ((0)) -} - -foo((0)); -``` - -Use instead: -``` -fn simple_no_parens() -> i32 { - 0 -} - -foo(0); -``` \ No newline at end of file diff --git a/src/docs/drop_copy.txt b/src/docs/drop_copy.txt deleted file mode 100644 index f917ca8ed21a6..0000000000000 --- a/src/docs/drop_copy.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for calls to `std::mem::drop` with a value -that derives the Copy trait - -### Why is this bad? -Calling `std::mem::drop` [does nothing for types that -implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html), since the -value will be copied and moved into the function on invocation. - -### Example -``` -let x: i32 = 42; // i32 implements Copy -std::mem::drop(x) // A copy of x is passed to the function, leaving the - // original unaffected -``` \ No newline at end of file diff --git a/src/docs/drop_non_drop.txt b/src/docs/drop_non_drop.txt deleted file mode 100644 index ee1e3a6c216ef..0000000000000 --- a/src/docs/drop_non_drop.txt +++ /dev/null @@ -1,13 +0,0 @@ -### What it does -Checks for calls to `std::mem::drop` with a value that does not implement `Drop`. - -### Why is this bad? -Calling `std::mem::drop` is no different than dropping such a type. A different value may -have been intended. - -### Example -``` -struct Foo; -let x = Foo; -std::mem::drop(x); -``` \ No newline at end of file diff --git a/src/docs/drop_ref.txt b/src/docs/drop_ref.txt deleted file mode 100644 index c4f7adf0cfa33..0000000000000 --- a/src/docs/drop_ref.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for calls to `std::mem::drop` with a reference -instead of an owned value. - -### Why is this bad? -Calling `drop` on a reference will only drop the -reference itself, which is a no-op. It will not call the `drop` method (from -the `Drop` trait implementation) on the underlying referenced value, which -is likely what was intended. - -### Example -``` -let mut lock_guard = mutex.lock(); -std::mem::drop(&lock_guard) // Should have been drop(lock_guard), mutex -// still locked -operation_that_requires_mutex_to_be_unlocked(); -``` \ No newline at end of file diff --git a/src/docs/duplicate_mod.txt b/src/docs/duplicate_mod.txt deleted file mode 100644 index 709a9aba03ad2..0000000000000 --- a/src/docs/duplicate_mod.txt +++ /dev/null @@ -1,31 +0,0 @@ -### What it does -Checks for files that are included as modules multiple times. - -### Why is this bad? -Loading a file as a module more than once causes it to be compiled -multiple times, taking longer and putting duplicate content into the -module tree. - -### Example -``` -// lib.rs -mod a; -mod b; -``` -``` -// a.rs -#[path = "./b.rs"] -mod b; -``` - -Use instead: - -``` -// lib.rs -mod a; -mod b; -``` -``` -// a.rs -use crate::b; -``` \ No newline at end of file diff --git a/src/docs/duplicate_underscore_argument.txt b/src/docs/duplicate_underscore_argument.txt deleted file mode 100644 index a8fcd6a9fbe68..0000000000000 --- a/src/docs/duplicate_underscore_argument.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for function arguments having the similar names -differing by an underscore. - -### Why is this bad? -It affects code readability. - -### Example -``` -fn foo(a: i32, _a: i32) {} -``` - -Use instead: -``` -fn bar(a: i32, _b: i32) {} -``` \ No newline at end of file diff --git a/src/docs/duration_subsec.txt b/src/docs/duration_subsec.txt deleted file mode 100644 index e7e0ca88745ee..0000000000000 --- a/src/docs/duration_subsec.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for calculation of subsecond microseconds or milliseconds -from other `Duration` methods. - -### Why is this bad? -It's more concise to call `Duration::subsec_micros()` or -`Duration::subsec_millis()` than to calculate them. - -### Example -``` -let micros = duration.subsec_nanos() / 1_000; -let millis = duration.subsec_nanos() / 1_000_000; -``` - -Use instead: -``` -let micros = duration.subsec_micros(); -let millis = duration.subsec_millis(); -``` \ No newline at end of file diff --git a/src/docs/else_if_without_else.txt b/src/docs/else_if_without_else.txt deleted file mode 100644 index 33f5d0f918592..0000000000000 --- a/src/docs/else_if_without_else.txt +++ /dev/null @@ -1,27 +0,0 @@ -### What it does -Checks for usage of if expressions with an `else if` branch, -but without a final `else` branch. - -### Why is this bad? -Some coding guidelines require this (e.g., MISRA-C:2004 Rule 14.10). - -### Example -``` -if x.is_positive() { - a(); -} else if x.is_negative() { - b(); -} -``` - -Use instead: - -``` -if x.is_positive() { - a(); -} else if x.is_negative() { - b(); -} else { - // We don't care about zero. -} -``` \ No newline at end of file diff --git a/src/docs/empty_drop.txt b/src/docs/empty_drop.txt deleted file mode 100644 index d0c0c24a9c880..0000000000000 --- a/src/docs/empty_drop.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Checks for empty `Drop` implementations. - -### Why is this bad? -Empty `Drop` implementations have no effect when dropping an instance of the type. They are -most likely useless. However, an empty `Drop` implementation prevents a type from being -destructured, which might be the intention behind adding the implementation as a marker. - -### Example -``` -struct S; - -impl Drop for S { - fn drop(&mut self) {} -} -``` -Use instead: -``` -struct S; -``` \ No newline at end of file diff --git a/src/docs/empty_enum.txt b/src/docs/empty_enum.txt deleted file mode 100644 index f7b41c41ee5a1..0000000000000 --- a/src/docs/empty_enum.txt +++ /dev/null @@ -1,27 +0,0 @@ -### What it does -Checks for `enum`s with no variants. - -As of this writing, the `never_type` is still a -nightly-only experimental API. Therefore, this lint is only triggered -if the `never_type` is enabled. - -### Why is this bad? -If you want to introduce a type which -can't be instantiated, you should use `!` (the primitive type "never"), -or a wrapper around it, because `!` has more extensive -compiler support (type inference, etc...) and wrappers -around it are the conventional way to define an uninhabited type. -For further information visit [never type documentation](https://doc.rust-lang.org/std/primitive.never.html) - - -### Example -``` -enum Test {} -``` - -Use instead: -``` -#![feature(never_type)] - -struct Test(!); -``` \ No newline at end of file diff --git a/src/docs/empty_line_after_outer_attr.txt b/src/docs/empty_line_after_outer_attr.txt deleted file mode 100644 index c85242bbee0ec..0000000000000 --- a/src/docs/empty_line_after_outer_attr.txt +++ /dev/null @@ -1,35 +0,0 @@ -### What it does -Checks for empty lines after outer attributes - -### Why is this bad? -Most likely the attribute was meant to be an inner attribute using a '!'. -If it was meant to be an outer attribute, then the following item -should not be separated by empty lines. - -### Known problems -Can cause false positives. - -From the clippy side it's difficult to detect empty lines between an attributes and the -following item because empty lines and comments are not part of the AST. The parsing -currently works for basic cases but is not perfect. - -### Example -``` -#[allow(dead_code)] - -fn not_quite_good_code() { } -``` - -Use instead: -``` -// Good (as inner attribute) -#![allow(dead_code)] - -fn this_is_fine() { } - -// or - -// Good (as outer attribute) -#[allow(dead_code)] -fn this_is_fine_too() { } -``` \ No newline at end of file diff --git a/src/docs/empty_loop.txt b/src/docs/empty_loop.txt deleted file mode 100644 index fea49a74d04e0..0000000000000 --- a/src/docs/empty_loop.txt +++ /dev/null @@ -1,27 +0,0 @@ -### What it does -Checks for empty `loop` expressions. - -### Why is this bad? -These busy loops burn CPU cycles without doing -anything. It is _almost always_ a better idea to `panic!` than to have -a busy loop. - -If panicking isn't possible, think of the environment and either: - - block on something - - sleep the thread for some microseconds - - yield or pause the thread - -For `std` targets, this can be done with -[`std::thread::sleep`](https://doc.rust-lang.org/std/thread/fn.sleep.html) -or [`std::thread::yield_now`](https://doc.rust-lang.org/std/thread/fn.yield_now.html). - -For `no_std` targets, doing this is more complicated, especially because -`#[panic_handler]`s can't panic. To stop/pause the thread, you will -probably need to invoke some target-specific intrinsic. Examples include: - - [`x86_64::instructions::hlt`](https://docs.rs/x86_64/0.12.2/x86_64/instructions/fn.hlt.html) - - [`cortex_m::asm::wfi`](https://docs.rs/cortex-m/0.6.3/cortex_m/asm/fn.wfi.html) - -### Example -``` -loop {} -``` \ No newline at end of file diff --git a/src/docs/empty_structs_with_brackets.txt b/src/docs/empty_structs_with_brackets.txt deleted file mode 100644 index ab5e35ae2ada8..0000000000000 --- a/src/docs/empty_structs_with_brackets.txt +++ /dev/null @@ -1,14 +0,0 @@ -### What it does -Finds structs without fields (a so-called "empty struct") that are declared with brackets. - -### Why is this bad? -Empty brackets after a struct declaration can be omitted. - -### Example -``` -struct Cookie {} -``` -Use instead: -``` -struct Cookie; -``` \ No newline at end of file diff --git a/src/docs/enum_clike_unportable_variant.txt b/src/docs/enum_clike_unportable_variant.txt deleted file mode 100644 index d30a973a5a135..0000000000000 --- a/src/docs/enum_clike_unportable_variant.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for C-like enumerations that are -`repr(isize/usize)` and have values that don't fit into an `i32`. - -### Why is this bad? -This will truncate the variant value on 32 bit -architectures, but works fine on 64 bit. - -### Example -``` -#[repr(usize)] -enum NonPortable { - X = 0x1_0000_0000, - Y = 0, -} -``` \ No newline at end of file diff --git a/src/docs/enum_glob_use.txt b/src/docs/enum_glob_use.txt deleted file mode 100644 index 3776822c35b0c..0000000000000 --- a/src/docs/enum_glob_use.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks for `use Enum::*`. - -### Why is this bad? -It is usually better style to use the prefixed name of -an enumeration variant, rather than importing variants. - -### Known problems -Old-style enumerations that prefix the variants are -still around. - -### Example -``` -use std::cmp::Ordering::*; - -foo(Less); -``` - -Use instead: -``` -use std::cmp::Ordering; - -foo(Ordering::Less) -``` \ No newline at end of file diff --git a/src/docs/enum_variant_names.txt b/src/docs/enum_variant_names.txt deleted file mode 100644 index e726925edda82..0000000000000 --- a/src/docs/enum_variant_names.txt +++ /dev/null @@ -1,30 +0,0 @@ -### What it does -Detects enumeration variants that are prefixed or suffixed -by the same characters. - -### Why is this bad? -Enumeration variant names should specify their variant, -not repeat the enumeration name. - -### Limitations -Characters with no casing will be considered when comparing prefixes/suffixes -This applies to numbers and non-ascii characters without casing -e.g. `Foo1` and `Foo2` is considered to have different prefixes -(the prefixes are `Foo1` and `Foo2` respectively), as also `Bar螃`, `Bar蟹` - -### Example -``` -enum Cake { - BlackForestCake, - HummingbirdCake, - BattenbergCake, -} -``` -Use instead: -``` -enum Cake { - BlackForest, - Hummingbird, - Battenberg, -} -``` \ No newline at end of file diff --git a/src/docs/eq_op.txt b/src/docs/eq_op.txt deleted file mode 100644 index 2d75a0ec546e7..0000000000000 --- a/src/docs/eq_op.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for equal operands to comparison, logical and -bitwise, difference and division binary operators (`==`, `>`, etc., `&&`, -`||`, `&`, `|`, `^`, `-` and `/`). - -### Why is this bad? -This is usually just a typo or a copy and paste error. - -### Known problems -False negatives: We had some false positives regarding -calls (notably [racer](https://github.com/phildawes/racer) had one instance -of `x.pop() && x.pop()`), so we removed matching any function or method -calls. We may introduce a list of known pure functions in the future. - -### Example -``` -if x + 1 == x + 1 {} - -// or - -assert_eq!(a, a); -``` \ No newline at end of file diff --git a/src/docs/equatable_if_let.txt b/src/docs/equatable_if_let.txt deleted file mode 100644 index 9997046954c20..0000000000000 --- a/src/docs/equatable_if_let.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for pattern matchings that can be expressed using equality. - -### Why is this bad? - -* It reads better and has less cognitive load because equality won't cause binding. -* It is a [Yoda condition](https://en.wikipedia.org/wiki/Yoda_conditions). Yoda conditions are widely -criticized for increasing the cognitive load of reading the code. -* Equality is a simple bool expression and can be merged with `&&` and `||` and -reuse if blocks - -### Example -``` -if let Some(2) = x { - do_thing(); -} -``` -Use instead: -``` -if x == Some(2) { - do_thing(); -} -``` \ No newline at end of file diff --git a/src/docs/erasing_op.txt b/src/docs/erasing_op.txt deleted file mode 100644 index 3d285a6d86e48..0000000000000 --- a/src/docs/erasing_op.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for erasing operations, e.g., `x * 0`. - -### Why is this bad? -The whole expression can be replaced by zero. -This is most likely not the intended outcome and should probably be -corrected - -### Example -``` -let x = 1; -0 / x; -0 * x; -x & 0; -``` \ No newline at end of file diff --git a/src/docs/err_expect.txt b/src/docs/err_expect.txt deleted file mode 100644 index 1dc83c5ce0ee0..0000000000000 --- a/src/docs/err_expect.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for `.err().expect()` calls on the `Result` type. - -### Why is this bad? -`.expect_err()` can be called directly to avoid the extra type conversion from `err()`. - -### Example -``` -let x: Result = Ok(10); -x.err().expect("Testing err().expect()"); -``` -Use instead: -``` -let x: Result = Ok(10); -x.expect_err("Testing expect_err"); -``` \ No newline at end of file diff --git a/src/docs/excessive_precision.txt b/src/docs/excessive_precision.txt deleted file mode 100644 index 517879c47152b..0000000000000 --- a/src/docs/excessive_precision.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for float literals with a precision greater -than that supported by the underlying type. - -### Why is this bad? -Rust will truncate the literal silently. - -### Example -``` -let v: f32 = 0.123_456_789_9; -println!("{}", v); // 0.123_456_789 -``` - -Use instead: -``` -let v: f64 = 0.123_456_789_9; -println!("{}", v); // 0.123_456_789_9 -``` \ No newline at end of file diff --git a/src/docs/exhaustive_enums.txt b/src/docs/exhaustive_enums.txt deleted file mode 100644 index d1032a7a29aa0..0000000000000 --- a/src/docs/exhaustive_enums.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Warns on any exported `enum`s that are not tagged `#[non_exhaustive]` - -### Why is this bad? -Exhaustive enums are typically fine, but a project which does -not wish to make a stability commitment around exported enums may wish to -disable them by default. - -### Example -``` -enum Foo { - Bar, - Baz -} -``` -Use instead: -``` -#[non_exhaustive] -enum Foo { - Bar, - Baz -} -``` \ No newline at end of file diff --git a/src/docs/exhaustive_structs.txt b/src/docs/exhaustive_structs.txt deleted file mode 100644 index fd6e4f5caf1f2..0000000000000 --- a/src/docs/exhaustive_structs.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Warns on any exported `structs`s that are not tagged `#[non_exhaustive]` - -### Why is this bad? -Exhaustive structs are typically fine, but a project which does -not wish to make a stability commitment around exported structs may wish to -disable them by default. - -### Example -``` -struct Foo { - bar: u8, - baz: String, -} -``` -Use instead: -``` -#[non_exhaustive] -struct Foo { - bar: u8, - baz: String, -} -``` \ No newline at end of file diff --git a/src/docs/exit.txt b/src/docs/exit.txt deleted file mode 100644 index 1e6154d43e053..0000000000000 --- a/src/docs/exit.txt +++ /dev/null @@ -1,12 +0,0 @@ -### What it does -`exit()` terminates the program and doesn't provide a -stack trace. - -### Why is this bad? -Ideally a program is terminated by finishing -the main function. - -### Example -``` -std::process::exit(0) -``` \ No newline at end of file diff --git a/src/docs/expect_fun_call.txt b/src/docs/expect_fun_call.txt deleted file mode 100644 index d82d9aa9baff2..0000000000000 --- a/src/docs/expect_fun_call.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks for calls to `.expect(&format!(...))`, `.expect(foo(..))`, -etc., and suggests to use `unwrap_or_else` instead - -### Why is this bad? -The function will always be called. - -### Known problems -If the function has side-effects, not calling it will -change the semantics of the program, but you shouldn't rely on that anyway. - -### Example -``` -foo.expect(&format!("Err {}: {}", err_code, err_msg)); - -// or - -foo.expect(format!("Err {}: {}", err_code, err_msg).as_str()); -``` - -Use instead: -``` -foo.unwrap_or_else(|| panic!("Err {}: {}", err_code, err_msg)); -``` \ No newline at end of file diff --git a/src/docs/expect_used.txt b/src/docs/expect_used.txt deleted file mode 100644 index 4a6981e334fd3..0000000000000 --- a/src/docs/expect_used.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Checks for `.expect()` or `.expect_err()` calls on `Result`s and `.expect()` call on `Option`s. - -### Why is this bad? -Usually it is better to handle the `None` or `Err` case. -Still, for a lot of quick-and-dirty code, `expect` is a good choice, which is why -this lint is `Allow` by default. - -`result.expect()` will let the thread panic on `Err` -values. Normally, you want to implement more sophisticated error handling, -and propagate errors upwards with `?` operator. - -### Examples -``` -option.expect("one"); -result.expect("one"); -``` - -Use instead: -``` -option?; - -// or - -result?; -``` \ No newline at end of file diff --git a/src/docs/expl_impl_clone_on_copy.txt b/src/docs/expl_impl_clone_on_copy.txt deleted file mode 100644 index 391d93b6713cc..0000000000000 --- a/src/docs/expl_impl_clone_on_copy.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Checks for explicit `Clone` implementations for `Copy` -types. - -### Why is this bad? -To avoid surprising behavior, these traits should -agree and the behavior of `Copy` cannot be overridden. In almost all -situations a `Copy` type should have a `Clone` implementation that does -nothing more than copy the object, which is what `#[derive(Copy, Clone)]` -gets you. - -### Example -``` -#[derive(Copy)] -struct Foo; - -impl Clone for Foo { - // .. -} -``` \ No newline at end of file diff --git a/src/docs/explicit_auto_deref.txt b/src/docs/explicit_auto_deref.txt deleted file mode 100644 index 65b2563177252..0000000000000 --- a/src/docs/explicit_auto_deref.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for dereferencing expressions which would be covered by auto-deref. - -### Why is this bad? -This unnecessarily complicates the code. - -### Example -``` -let x = String::new(); -let y: &str = &*x; -``` -Use instead: -``` -let x = String::new(); -let y: &str = &x; -``` \ No newline at end of file diff --git a/src/docs/explicit_counter_loop.txt b/src/docs/explicit_counter_loop.txt deleted file mode 100644 index 2661a43e10341..0000000000000 --- a/src/docs/explicit_counter_loop.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks `for` loops over slices with an explicit counter -and suggests the use of `.enumerate()`. - -### Why is this bad? -Using `.enumerate()` makes the intent more clear, -declutters the code and may be faster in some instances. - -### Example -``` -let mut i = 0; -for item in &v { - bar(i, *item); - i += 1; -} -``` - -Use instead: -``` -for (i, item) in v.iter().enumerate() { bar(i, *item); } -``` \ No newline at end of file diff --git a/src/docs/explicit_deref_methods.txt b/src/docs/explicit_deref_methods.txt deleted file mode 100644 index e14e981c7073b..0000000000000 --- a/src/docs/explicit_deref_methods.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks for explicit `deref()` or `deref_mut()` method calls. - -### Why is this bad? -Dereferencing by `&*x` or `&mut *x` is clearer and more concise, -when not part of a method chain. - -### Example -``` -use std::ops::Deref; -let a: &mut String = &mut String::from("foo"); -let b: &str = a.deref(); -``` - -Use instead: -``` -let a: &mut String = &mut String::from("foo"); -let b = &*a; -``` - -This lint excludes: -``` -let _ = d.unwrap().deref(); -``` \ No newline at end of file diff --git a/src/docs/explicit_into_iter_loop.txt b/src/docs/explicit_into_iter_loop.txt deleted file mode 100644 index 3931dfd69a318..0000000000000 --- a/src/docs/explicit_into_iter_loop.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Checks for loops on `y.into_iter()` where `y` will do, and -suggests the latter. - -### Why is this bad? -Readability. - -### Example -``` -// with `y` a `Vec` or slice: -for x in y.into_iter() { - // .. -} -``` -can be rewritten to -``` -for x in y { - // .. -} -``` \ No newline at end of file diff --git a/src/docs/explicit_iter_loop.txt b/src/docs/explicit_iter_loop.txt deleted file mode 100644 index cabe72e91d04f..0000000000000 --- a/src/docs/explicit_iter_loop.txt +++ /dev/null @@ -1,25 +0,0 @@ -### What it does -Checks for loops on `x.iter()` where `&x` will do, and -suggests the latter. - -### Why is this bad? -Readability. - -### Known problems -False negatives. We currently only warn on some known -types. - -### Example -``` -// with `y` a `Vec` or slice: -for x in y.iter() { - // .. -} -``` - -Use instead: -``` -for x in &y { - // .. -} -``` \ No newline at end of file diff --git a/src/docs/explicit_write.txt b/src/docs/explicit_write.txt deleted file mode 100644 index eafed5d39e5c6..0000000000000 --- a/src/docs/explicit_write.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for usage of `write!()` / `writeln()!` which can be -replaced with `(e)print!()` / `(e)println!()` - -### Why is this bad? -Using `(e)println! is clearer and more concise - -### Example -``` -writeln!(&mut std::io::stderr(), "foo: {:?}", bar).unwrap(); -writeln!(&mut std::io::stdout(), "foo: {:?}", bar).unwrap(); -``` - -Use instead: -``` -eprintln!("foo: {:?}", bar); -println!("foo: {:?}", bar); -``` \ No newline at end of file diff --git a/src/docs/extend_with_drain.txt b/src/docs/extend_with_drain.txt deleted file mode 100644 index 2f31dcf5f7401..0000000000000 --- a/src/docs/extend_with_drain.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for occurrences where one vector gets extended instead of append - -### Why is this bad? -Using `append` instead of `extend` is more concise and faster - -### Example -``` -let mut a = vec![1, 2, 3]; -let mut b = vec![4, 5, 6]; - -a.extend(b.drain(..)); -``` - -Use instead: -``` -let mut a = vec![1, 2, 3]; -let mut b = vec![4, 5, 6]; - -a.append(&mut b); -``` \ No newline at end of file diff --git a/src/docs/extra_unused_lifetimes.txt b/src/docs/extra_unused_lifetimes.txt deleted file mode 100644 index bc1814aa4752d..0000000000000 --- a/src/docs/extra_unused_lifetimes.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for lifetimes in generics that are never used -anywhere else. - -### Why is this bad? -The additional lifetimes make the code look more -complicated, while there is nothing out of the ordinary going on. Removing -them leads to more readable code. - -### Example -``` -// unnecessary lifetimes -fn unused_lifetime<'a>(x: u8) { - // .. -} -``` - -Use instead: -``` -fn no_lifetime(x: u8) { - // ... -} -``` \ No newline at end of file diff --git a/src/docs/fallible_impl_from.txt b/src/docs/fallible_impl_from.txt deleted file mode 100644 index 588a5bb103d46..0000000000000 --- a/src/docs/fallible_impl_from.txt +++ /dev/null @@ -1,32 +0,0 @@ -### What it does -Checks for impls of `From<..>` that contain `panic!()` or `unwrap()` - -### Why is this bad? -`TryFrom` should be used if there's a possibility of failure. - -### Example -``` -struct Foo(i32); - -impl From for Foo { - fn from(s: String) -> Self { - Foo(s.parse().unwrap()) - } -} -``` - -Use instead: -``` -struct Foo(i32); - -impl TryFrom for Foo { - type Error = (); - fn try_from(s: String) -> Result { - if let Ok(parsed) = s.parse() { - Ok(Foo(parsed)) - } else { - Err(()) - } - } -} -``` \ No newline at end of file diff --git a/src/docs/field_reassign_with_default.txt b/src/docs/field_reassign_with_default.txt deleted file mode 100644 index e58b7239fde9e..0000000000000 --- a/src/docs/field_reassign_with_default.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for immediate reassignment of fields initialized -with Default::default(). - -### Why is this bad? -It's more idiomatic to use the [functional update syntax](https://doc.rust-lang.org/reference/expressions/struct-expr.html#functional-update-syntax). - -### Known problems -Assignments to patterns that are of tuple type are not linted. - -### Example -``` -let mut a: A = Default::default(); -a.i = 42; -``` - -Use instead: -``` -let a = A { - i: 42, - .. Default::default() -}; -``` \ No newline at end of file diff --git a/src/docs/filetype_is_file.txt b/src/docs/filetype_is_file.txt deleted file mode 100644 index ad14bd62c4de4..0000000000000 --- a/src/docs/filetype_is_file.txt +++ /dev/null @@ -1,29 +0,0 @@ -### What it does -Checks for `FileType::is_file()`. - -### Why is this bad? -When people testing a file type with `FileType::is_file` -they are testing whether a path is something they can get bytes from. But -`is_file` doesn't cover special file types in unix-like systems, and doesn't cover -symlink in windows. Using `!FileType::is_dir()` is a better way to that intention. - -### Example -``` -let metadata = std::fs::metadata("foo.txt")?; -let filetype = metadata.file_type(); - -if filetype.is_file() { - // read file -} -``` - -should be written as: - -``` -let metadata = std::fs::metadata("foo.txt")?; -let filetype = metadata.file_type(); - -if !filetype.is_dir() { - // read file -} -``` \ No newline at end of file diff --git a/src/docs/filter_map_identity.txt b/src/docs/filter_map_identity.txt deleted file mode 100644 index 83b666f2e2782..0000000000000 --- a/src/docs/filter_map_identity.txt +++ /dev/null @@ -1,14 +0,0 @@ -### What it does -Checks for usage of `filter_map(|x| x)`. - -### Why is this bad? -Readability, this can be written more concisely by using `flatten`. - -### Example -``` -iter.filter_map(|x| x); -``` -Use instead: -``` -iter.flatten(); -``` \ No newline at end of file diff --git a/src/docs/filter_map_next.txt b/src/docs/filter_map_next.txt deleted file mode 100644 index b38620b56a50f..0000000000000 --- a/src/docs/filter_map_next.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for usage of `_.filter_map(_).next()`. - -### Why is this bad? -Readability, this can be written more concisely as -`_.find_map(_)`. - -### Example -``` - (0..3).filter_map(|x| if x == 2 { Some(x) } else { None }).next(); -``` -Can be written as - -``` - (0..3).find_map(|x| if x == 2 { Some(x) } else { None }); -``` \ No newline at end of file diff --git a/src/docs/filter_next.txt b/src/docs/filter_next.txt deleted file mode 100644 index 898a74166dc14..0000000000000 --- a/src/docs/filter_next.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for usage of `_.filter(_).next()`. - -### Why is this bad? -Readability, this can be written more concisely as -`_.find(_)`. - -### Example -``` -vec.iter().filter(|x| **x == 0).next(); -``` - -Use instead: -``` -vec.iter().find(|x| **x == 0); -``` \ No newline at end of file diff --git a/src/docs/flat_map_identity.txt b/src/docs/flat_map_identity.txt deleted file mode 100644 index a5ee79b4982fb..0000000000000 --- a/src/docs/flat_map_identity.txt +++ /dev/null @@ -1,14 +0,0 @@ -### What it does -Checks for usage of `flat_map(|x| x)`. - -### Why is this bad? -Readability, this can be written more concisely by using `flatten`. - -### Example -``` -iter.flat_map(|x| x); -``` -Can be written as -``` -iter.flatten(); -``` \ No newline at end of file diff --git a/src/docs/flat_map_option.txt b/src/docs/flat_map_option.txt deleted file mode 100644 index d50b9156d3654..0000000000000 --- a/src/docs/flat_map_option.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for usages of `Iterator::flat_map()` where `filter_map()` could be -used instead. - -### Why is this bad? -When applicable, `filter_map()` is more clear since it shows that -`Option` is used to produce 0 or 1 items. - -### Example -``` -let nums: Vec = ["1", "2", "whee!"].iter().flat_map(|x| x.parse().ok()).collect(); -``` -Use instead: -``` -let nums: Vec = ["1", "2", "whee!"].iter().filter_map(|x| x.parse().ok()).collect(); -``` \ No newline at end of file diff --git a/src/docs/float_arithmetic.txt b/src/docs/float_arithmetic.txt deleted file mode 100644 index 1f9bce5abd59f..0000000000000 --- a/src/docs/float_arithmetic.txt +++ /dev/null @@ -1,11 +0,0 @@ -### What it does -Checks for float arithmetic. - -### Why is this bad? -For some embedded systems or kernel development, it -can be useful to rule out floating-point numbers. - -### Example -``` -a + 1.0; -``` \ No newline at end of file diff --git a/src/docs/float_cmp.txt b/src/docs/float_cmp.txt deleted file mode 100644 index c19907c903e9d..0000000000000 --- a/src/docs/float_cmp.txt +++ /dev/null @@ -1,28 +0,0 @@ -### What it does -Checks for (in-)equality comparisons on floating-point -values (apart from zero), except in functions called `*eq*` (which probably -implement equality for a type involving floats). - -### Why is this bad? -Floating point calculations are usually imprecise, so -asking if two values are *exactly* equal is asking for trouble. For a good -guide on what to do, see [the floating point -guide](http://www.floating-point-gui.de/errors/comparison). - -### Example -``` -let x = 1.2331f64; -let y = 1.2332f64; - -if y == 1.23f64 { } -if y != x {} // where both are floats -``` - -Use instead: -``` -let error_margin = f64::EPSILON; // Use an epsilon for comparison -// Or, if Rust <= 1.42, use `std::f64::EPSILON` constant instead. -// let error_margin = std::f64::EPSILON; -if (y - 1.23f64).abs() < error_margin { } -if (y - x).abs() > error_margin { } -``` \ No newline at end of file diff --git a/src/docs/float_cmp_const.txt b/src/docs/float_cmp_const.txt deleted file mode 100644 index 9208feaacd810..0000000000000 --- a/src/docs/float_cmp_const.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Checks for (in-)equality comparisons on floating-point -value and constant, except in functions called `*eq*` (which probably -implement equality for a type involving floats). - -### Why is this bad? -Floating point calculations are usually imprecise, so -asking if two values are *exactly* equal is asking for trouble. For a good -guide on what to do, see [the floating point -guide](http://www.floating-point-gui.de/errors/comparison). - -### Example -``` -let x: f64 = 1.0; -const ONE: f64 = 1.00; - -if x == ONE { } // where both are floats -``` - -Use instead: -``` -let error_margin = f64::EPSILON; // Use an epsilon for comparison -// Or, if Rust <= 1.42, use `std::f64::EPSILON` constant instead. -// let error_margin = std::f64::EPSILON; -if (x - ONE).abs() < error_margin { } -``` \ No newline at end of file diff --git a/src/docs/float_equality_without_abs.txt b/src/docs/float_equality_without_abs.txt deleted file mode 100644 index 556b574e15d30..0000000000000 --- a/src/docs/float_equality_without_abs.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Checks for statements of the form `(a - b) < f32::EPSILON` or -`(a - b) < f64::EPSILON`. Notes the missing `.abs()`. - -### Why is this bad? -The code without `.abs()` is more likely to have a bug. - -### Known problems -If the user can ensure that b is larger than a, the `.abs()` is -technically unnecessary. However, it will make the code more robust and doesn't have any -large performance implications. If the abs call was deliberately left out for performance -reasons, it is probably better to state this explicitly in the code, which then can be done -with an allow. - -### Example -``` -pub fn is_roughly_equal(a: f32, b: f32) -> bool { - (a - b) < f32::EPSILON -} -``` -Use instead: -``` -pub fn is_roughly_equal(a: f32, b: f32) -> bool { - (a - b).abs() < f32::EPSILON -} -``` \ No newline at end of file diff --git a/src/docs/fn_address_comparisons.txt b/src/docs/fn_address_comparisons.txt deleted file mode 100644 index 7d2b7b681deb1..0000000000000 --- a/src/docs/fn_address_comparisons.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for comparisons with an address of a function item. - -### Why is this bad? -Function item address is not guaranteed to be unique and could vary -between different code generation units. Furthermore different function items could have -the same address after being merged together. - -### Example -``` -type F = fn(); -fn a() {} -let f: F = a; -if f == a { - // ... -} -``` \ No newline at end of file diff --git a/src/docs/fn_params_excessive_bools.txt b/src/docs/fn_params_excessive_bools.txt deleted file mode 100644 index 2eae0563368c1..0000000000000 --- a/src/docs/fn_params_excessive_bools.txt +++ /dev/null @@ -1,31 +0,0 @@ -### What it does -Checks for excessive use of -bools in function definitions. - -### Why is this bad? -Calls to such functions -are confusing and error prone, because it's -hard to remember argument order and you have -no type system support to back you up. Using -two-variant enums instead of bools often makes -API easier to use. - -### Example -``` -fn f(is_round: bool, is_hot: bool) { ... } -``` - -Use instead: -``` -enum Shape { - Round, - Spiky, -} - -enum Temperature { - Hot, - IceCold, -} - -fn f(shape: Shape, temperature: Temperature) { ... } -``` \ No newline at end of file diff --git a/src/docs/fn_to_numeric_cast.txt b/src/docs/fn_to_numeric_cast.txt deleted file mode 100644 index 1f587f6d71768..0000000000000 --- a/src/docs/fn_to_numeric_cast.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for casts of function pointers to something other than usize - -### Why is this bad? -Casting a function pointer to anything other than usize/isize is not portable across -architectures, because you end up losing bits if the target type is too small or end up with a -bunch of extra bits that waste space and add more instructions to the final binary than -strictly necessary for the problem - -Casting to isize also doesn't make sense since there are no signed addresses. - -### Example -``` -fn fun() -> i32 { 1 } -let _ = fun as i64; -``` - -Use instead: -``` -let _ = fun as usize; -``` \ No newline at end of file diff --git a/src/docs/fn_to_numeric_cast_any.txt b/src/docs/fn_to_numeric_cast_any.txt deleted file mode 100644 index ee3c33d237255..0000000000000 --- a/src/docs/fn_to_numeric_cast_any.txt +++ /dev/null @@ -1,35 +0,0 @@ -### What it does -Checks for casts of a function pointer to any integer type. - -### Why is this bad? -Casting a function pointer to an integer can have surprising results and can occur -accidentally if parentheses are omitted from a function call. If you aren't doing anything -low-level with function pointers then you can opt-out of casting functions to integers in -order to avoid mistakes. Alternatively, you can use this lint to audit all uses of function -pointer casts in your code. - -### Example -``` -// fn1 is cast as `usize` -fn fn1() -> u16 { - 1 -}; -let _ = fn1 as usize; -``` - -Use instead: -``` -// maybe you intended to call the function? -fn fn2() -> u16 { - 1 -}; -let _ = fn2() as usize; - -// or - -// maybe you intended to cast it to a function type? -fn fn3() -> u16 { - 1 -} -let _ = fn3 as fn() -> u16; -``` \ No newline at end of file diff --git a/src/docs/fn_to_numeric_cast_with_truncation.txt b/src/docs/fn_to_numeric_cast_with_truncation.txt deleted file mode 100644 index 69f12fa319f18..0000000000000 --- a/src/docs/fn_to_numeric_cast_with_truncation.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Checks for casts of a function pointer to a numeric type not wide enough to -store address. - -### Why is this bad? -Such a cast discards some bits of the function's address. If this is intended, it would be more -clearly expressed by casting to usize first, then casting the usize to the intended type (with -a comment) to perform the truncation. - -### Example -``` -fn fn1() -> i16 { - 1 -}; -let _ = fn1 as i32; -``` - -Use instead: -``` -// Cast to usize first, then comment with the reason for the truncation -fn fn1() -> i16 { - 1 -}; -let fn_ptr = fn1 as usize; -let fn_ptr_truncated = fn_ptr as i32; -``` \ No newline at end of file diff --git a/src/docs/for_kv_map.txt b/src/docs/for_kv_map.txt deleted file mode 100644 index a9a2ffee9c743..0000000000000 --- a/src/docs/for_kv_map.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for iterating a map (`HashMap` or `BTreeMap`) and -ignoring either the keys or values. - -### Why is this bad? -Readability. There are `keys` and `values` methods that -can be used to express that don't need the values or keys. - -### Example -``` -for (k, _) in &map { - .. -} -``` - -could be replaced by - -``` -for k in map.keys() { - .. -} -``` \ No newline at end of file diff --git a/src/docs/forget_copy.txt b/src/docs/forget_copy.txt deleted file mode 100644 index 1d100912e9a48..0000000000000 --- a/src/docs/forget_copy.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for calls to `std::mem::forget` with a value that -derives the Copy trait - -### Why is this bad? -Calling `std::mem::forget` [does nothing for types that -implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html) since the -value will be copied and moved into the function on invocation. - -An alternative, but also valid, explanation is that Copy types do not -implement -the Drop trait, which means they have no destructors. Without a destructor, -there -is nothing for `std::mem::forget` to ignore. - -### Example -``` -let x: i32 = 42; // i32 implements Copy -std::mem::forget(x) // A copy of x is passed to the function, leaving the - // original unaffected -``` \ No newline at end of file diff --git a/src/docs/forget_non_drop.txt b/src/docs/forget_non_drop.txt deleted file mode 100644 index 3307d654c17f2..0000000000000 --- a/src/docs/forget_non_drop.txt +++ /dev/null @@ -1,13 +0,0 @@ -### What it does -Checks for calls to `std::mem::forget` with a value that does not implement `Drop`. - -### Why is this bad? -Calling `std::mem::forget` is no different than dropping such a type. A different value may -have been intended. - -### Example -``` -struct Foo; -let x = Foo; -std::mem::forget(x); -``` \ No newline at end of file diff --git a/src/docs/forget_ref.txt b/src/docs/forget_ref.txt deleted file mode 100644 index 874fb8786068d..0000000000000 --- a/src/docs/forget_ref.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for calls to `std::mem::forget` with a reference -instead of an owned value. - -### Why is this bad? -Calling `forget` on a reference will only forget the -reference itself, which is a no-op. It will not forget the underlying -referenced -value, which is likely what was intended. - -### Example -``` -let x = Box::new(1); -std::mem::forget(&x) // Should have been forget(x), x will still be dropped -``` \ No newline at end of file diff --git a/src/docs/format_in_format_args.txt b/src/docs/format_in_format_args.txt deleted file mode 100644 index ac498472f017f..0000000000000 --- a/src/docs/format_in_format_args.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Detects `format!` within the arguments of another macro that does -formatting such as `format!` itself, `write!` or `println!`. Suggests -inlining the `format!` call. - -### Why is this bad? -The recommended code is both shorter and avoids a temporary allocation. - -### Example -``` -println!("error: {}", format!("something failed at {}", Location::caller())); -``` -Use instead: -``` -println!("error: something failed at {}", Location::caller()); -``` \ No newline at end of file diff --git a/src/docs/format_push_string.txt b/src/docs/format_push_string.txt deleted file mode 100644 index ca409ebc7ec26..0000000000000 --- a/src/docs/format_push_string.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Detects cases where the result of a `format!` call is -appended to an existing `String`. - -### Why is this bad? -Introduces an extra, avoidable heap allocation. - -### Known problems -`format!` returns a `String` but `write!` returns a `Result`. -Thus you are forced to ignore the `Err` variant to achieve the same API. - -While using `write!` in the suggested way should never fail, this isn't necessarily clear to the programmer. - -### Example -``` -let mut s = String::new(); -s += &format!("0x{:X}", 1024); -s.push_str(&format!("0x{:X}", 1024)); -``` -Use instead: -``` -use std::fmt::Write as _; // import without risk of name clashing - -let mut s = String::new(); -let _ = write!(s, "0x{:X}", 1024); -``` \ No newline at end of file diff --git a/src/docs/from_iter_instead_of_collect.txt b/src/docs/from_iter_instead_of_collect.txt deleted file mode 100644 index f3fd275972645..0000000000000 --- a/src/docs/from_iter_instead_of_collect.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks for `from_iter()` function calls on types that implement the `FromIterator` -trait. - -### Why is this bad? -It is recommended style to use collect. See -[FromIterator documentation](https://doc.rust-lang.org/std/iter/trait.FromIterator.html) - -### Example -``` -let five_fives = std::iter::repeat(5).take(5); - -let v = Vec::from_iter(five_fives); - -assert_eq!(v, vec![5, 5, 5, 5, 5]); -``` -Use instead: -``` -let five_fives = std::iter::repeat(5).take(5); - -let v: Vec = five_fives.collect(); - -assert_eq!(v, vec![5, 5, 5, 5, 5]); -``` \ No newline at end of file diff --git a/src/docs/from_over_into.txt b/src/docs/from_over_into.txt deleted file mode 100644 index 0770bcc42c270..0000000000000 --- a/src/docs/from_over_into.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Searches for implementations of the `Into<..>` trait and suggests to implement `From<..>` instead. - -### Why is this bad? -According the std docs implementing `From<..>` is preferred since it gives you `Into<..>` for free where the reverse isn't true. - -### Example -``` -struct StringWrapper(String); - -impl Into for String { - fn into(self) -> StringWrapper { - StringWrapper(self) - } -} -``` -Use instead: -``` -struct StringWrapper(String); - -impl From for StringWrapper { - fn from(s: String) -> StringWrapper { - StringWrapper(s) - } -} -``` \ No newline at end of file diff --git a/src/docs/from_str_radix_10.txt b/src/docs/from_str_radix_10.txt deleted file mode 100644 index f6f319d3eaa18..0000000000000 --- a/src/docs/from_str_radix_10.txt +++ /dev/null @@ -1,25 +0,0 @@ -### What it does - -Checks for function invocations of the form `primitive::from_str_radix(s, 10)` - -### Why is this bad? - -This specific common use case can be rewritten as `s.parse::()` -(and in most cases, the turbofish can be removed), which reduces code length -and complexity. - -### Known problems - -This lint may suggest using (&).parse() instead of .parse() directly -in some cases, which is correct but adds unnecessary complexity to the code. - -### Example -``` -let input: &str = get_input(); -let num = u16::from_str_radix(input, 10)?; -``` -Use instead: -``` -let input: &str = get_input(); -let num: u16 = input.parse()?; -``` \ No newline at end of file diff --git a/src/docs/future_not_send.txt b/src/docs/future_not_send.txt deleted file mode 100644 index 0aa048d273551..0000000000000 --- a/src/docs/future_not_send.txt +++ /dev/null @@ -1,29 +0,0 @@ -### What it does -This lint requires Future implementations returned from -functions and methods to implement the `Send` marker trait. It is mostly -used by library authors (public and internal) that target an audience where -multithreaded executors are likely to be used for running these Futures. - -### Why is this bad? -A Future implementation captures some state that it -needs to eventually produce its final value. When targeting a multithreaded -executor (which is the norm on non-embedded devices) this means that this -state may need to be transported to other threads, in other words the -whole Future needs to implement the `Send` marker trait. If it does not, -then the resulting Future cannot be submitted to a thread pool in the -end user’s code. - -Especially for generic functions it can be confusing to leave the -discovery of this problem to the end user: the reported error location -will be far from its cause and can in many cases not even be fixed without -modifying the library where the offending Future implementation is -produced. - -### Example -``` -async fn not_send(bytes: std::rc::Rc<[u8]>) {} -``` -Use instead: -``` -async fn is_send(bytes: std::sync::Arc<[u8]>) {} -``` \ No newline at end of file diff --git a/src/docs/get_first.txt b/src/docs/get_first.txt deleted file mode 100644 index c905a737ddf3f..0000000000000 --- a/src/docs/get_first.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for using `x.get(0)` instead of -`x.first()`. - -### Why is this bad? -Using `x.first()` is easier to read and has the same -result. - -### Example -``` -let x = vec![2, 3, 5]; -let first_element = x.get(0); -``` - -Use instead: -``` -let x = vec![2, 3, 5]; -let first_element = x.first(); -``` \ No newline at end of file diff --git a/src/docs/get_last_with_len.txt b/src/docs/get_last_with_len.txt deleted file mode 100644 index 31c7f269586ab..0000000000000 --- a/src/docs/get_last_with_len.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Checks for using `x.get(x.len() - 1)` instead of -`x.last()`. - -### Why is this bad? -Using `x.last()` is easier to read and has the same -result. - -Note that using `x[x.len() - 1]` is semantically different from -`x.last()`. Indexing into the array will panic on out-of-bounds -accesses, while `x.get()` and `x.last()` will return `None`. - -There is another lint (get_unwrap) that covers the case of using -`x.get(index).unwrap()` instead of `x[index]`. - -### Example -``` -let x = vec![2, 3, 5]; -let last_element = x.get(x.len() - 1); -``` - -Use instead: -``` -let x = vec![2, 3, 5]; -let last_element = x.last(); -``` \ No newline at end of file diff --git a/src/docs/get_unwrap.txt b/src/docs/get_unwrap.txt deleted file mode 100644 index 8defc22244169..0000000000000 --- a/src/docs/get_unwrap.txt +++ /dev/null @@ -1,30 +0,0 @@ -### What it does -Checks for use of `.get().unwrap()` (or -`.get_mut().unwrap`) on a standard library type which implements `Index` - -### Why is this bad? -Using the Index trait (`[]`) is more clear and more -concise. - -### Known problems -Not a replacement for error handling: Using either -`.unwrap()` or the Index trait (`[]`) carries the risk of causing a `panic` -if the value being accessed is `None`. If the use of `.get().unwrap()` is a -temporary placeholder for dealing with the `Option` type, then this does -not mitigate the need for error handling. If there is a chance that `.get()` -will be `None` in your program, then it is advisable that the `None` case -is handled in a future refactor instead of using `.unwrap()` or the Index -trait. - -### Example -``` -let mut some_vec = vec![0, 1, 2, 3]; -let last = some_vec.get(3).unwrap(); -*some_vec.get_mut(0).unwrap() = 1; -``` -The correct use would be: -``` -let mut some_vec = vec![0, 1, 2, 3]; -let last = some_vec[3]; -some_vec[0] = 1; -``` \ No newline at end of file diff --git a/src/docs/identity_op.txt b/src/docs/identity_op.txt deleted file mode 100644 index a8e40bb43e9d5..0000000000000 --- a/src/docs/identity_op.txt +++ /dev/null @@ -1,11 +0,0 @@ -### What it does -Checks for identity operations, e.g., `x + 0`. - -### Why is this bad? -This code can be removed without changing the -meaning. So it just obscures what's going on. Delete it mercilessly. - -### Example -``` -x / 1 + 0 * 1 - 0 | 0; -``` \ No newline at end of file diff --git a/src/docs/if_let_mutex.txt b/src/docs/if_let_mutex.txt deleted file mode 100644 index 4d873ade9ace3..0000000000000 --- a/src/docs/if_let_mutex.txt +++ /dev/null @@ -1,25 +0,0 @@ -### What it does -Checks for `Mutex::lock` calls in `if let` expression -with lock calls in any of the else blocks. - -### Why is this bad? -The Mutex lock remains held for the whole -`if let ... else` block and deadlocks. - -### Example -``` -if let Ok(thing) = mutex.lock() { - do_thing(); -} else { - mutex.lock(); -} -``` -Should be written -``` -let locked = mutex.lock(); -if let Ok(thing) = locked { - do_thing(thing); -} else { - use_locked(locked); -} -``` \ No newline at end of file diff --git a/src/docs/if_not_else.txt b/src/docs/if_not_else.txt deleted file mode 100644 index 0e5ac4ce6bb80..0000000000000 --- a/src/docs/if_not_else.txt +++ /dev/null @@ -1,25 +0,0 @@ -### What it does -Checks for usage of `!` or `!=` in an if condition with an -else branch. - -### Why is this bad? -Negations reduce the readability of statements. - -### Example -``` -if !v.is_empty() { - a() -} else { - b() -} -``` - -Could be written: - -``` -if v.is_empty() { - b() -} else { - a() -} -``` \ No newline at end of file diff --git a/src/docs/if_same_then_else.txt b/src/docs/if_same_then_else.txt deleted file mode 100644 index 75127016bb8c1..0000000000000 --- a/src/docs/if_same_then_else.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for `if/else` with the same body as the *then* part -and the *else* part. - -### Why is this bad? -This is probably a copy & paste error. - -### Example -``` -let foo = if … { - 42 -} else { - 42 -}; -``` \ No newline at end of file diff --git a/src/docs/if_then_some_else_none.txt b/src/docs/if_then_some_else_none.txt deleted file mode 100644 index 13744f920e362..0000000000000 --- a/src/docs/if_then_some_else_none.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Checks for if-else that could be written using either `bool::then` or `bool::then_some`. - -### Why is this bad? -Looks a little redundant. Using `bool::then` is more concise and incurs no loss of clarity. -For simple calculations and known values, use `bool::then_some`, which is eagerly evaluated -in comparison to `bool::then`. - -### Example -``` -let a = if v.is_empty() { - println!("true!"); - Some(42) -} else { - None -}; -``` - -Could be written: - -``` -let a = v.is_empty().then(|| { - println!("true!"); - 42 -}); -``` \ No newline at end of file diff --git a/src/docs/ifs_same_cond.txt b/src/docs/ifs_same_cond.txt deleted file mode 100644 index 024ba5df93a63..0000000000000 --- a/src/docs/ifs_same_cond.txt +++ /dev/null @@ -1,25 +0,0 @@ -### What it does -Checks for consecutive `if`s with the same condition. - -### Why is this bad? -This is probably a copy & paste error. - -### Example -``` -if a == b { - … -} else if a == b { - … -} -``` - -Note that this lint ignores all conditions with a function call as it could -have side effects: - -``` -if foo() { - … -} else if foo() { // not linted - … -} -``` \ No newline at end of file diff --git a/src/docs/implicit_clone.txt b/src/docs/implicit_clone.txt deleted file mode 100644 index f5aa112c52c36..0000000000000 --- a/src/docs/implicit_clone.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for the usage of `_.to_owned()`, `vec.to_vec()`, or similar when calling `_.clone()` would be clearer. - -### Why is this bad? -These methods do the same thing as `_.clone()` but may be confusing as -to why we are calling `to_vec` on something that is already a `Vec` or calling `to_owned` on something that is already owned. - -### Example -``` -let a = vec![1, 2, 3]; -let b = a.to_vec(); -let c = a.to_owned(); -``` -Use instead: -``` -let a = vec![1, 2, 3]; -let b = a.clone(); -let c = a.clone(); -``` \ No newline at end of file diff --git a/src/docs/implicit_hasher.txt b/src/docs/implicit_hasher.txt deleted file mode 100644 index 0c1f76620f51d..0000000000000 --- a/src/docs/implicit_hasher.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Checks for public `impl` or `fn` missing generalization -over different hashers and implicitly defaulting to the default hashing -algorithm (`SipHash`). - -### Why is this bad? -`HashMap` or `HashSet` with custom hashers cannot be -used with them. - -### Known problems -Suggestions for replacing constructors can contain -false-positives. Also applying suggestions can require modification of other -pieces of code, possibly including external crates. - -### Example -``` -impl Serialize for HashMap { } - -pub fn foo(map: &mut HashMap) { } -``` -could be rewritten as -``` -impl Serialize for HashMap { } - -pub fn foo(map: &mut HashMap) { } -``` \ No newline at end of file diff --git a/src/docs/implicit_return.txt b/src/docs/implicit_return.txt deleted file mode 100644 index ee65a636b38c5..0000000000000 --- a/src/docs/implicit_return.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for missing return statements at the end of a block. - -### Why is this bad? -Actually omitting the return keyword is idiomatic Rust code. Programmers -coming from other languages might prefer the expressiveness of `return`. It's possible to miss -the last returning statement because the only difference is a missing `;`. Especially in bigger -code with multiple return paths having a `return` keyword makes it easier to find the -corresponding statements. - -### Example -``` -fn foo(x: usize) -> usize { - x -} -``` -add return -``` -fn foo(x: usize) -> usize { - return x; -} -``` \ No newline at end of file diff --git a/src/docs/implicit_saturating_add.txt b/src/docs/implicit_saturating_add.txt deleted file mode 100644 index 5883a5363e2b6..0000000000000 --- a/src/docs/implicit_saturating_add.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Checks for implicit saturating addition. - -### Why is this bad? -The built-in function is more readable and may be faster. - -### Example -``` -let mut u:u32 = 7000; - -if u != u32::MAX { - u += 1; -} -``` -Use instead: -``` -let mut u:u32 = 7000; - -u = u.saturating_add(1); -``` \ No newline at end of file diff --git a/src/docs/implicit_saturating_sub.txt b/src/docs/implicit_saturating_sub.txt deleted file mode 100644 index 03b47905a2118..0000000000000 --- a/src/docs/implicit_saturating_sub.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for implicit saturating subtraction. - -### Why is this bad? -Simplicity and readability. Instead we can easily use an builtin function. - -### Example -``` -let mut i: u32 = end - start; - -if i != 0 { - i -= 1; -} -``` - -Use instead: -``` -let mut i: u32 = end - start; - -i = i.saturating_sub(1); -``` \ No newline at end of file diff --git a/src/docs/imprecise_flops.txt b/src/docs/imprecise_flops.txt deleted file mode 100644 index e84d81cea98e0..0000000000000 --- a/src/docs/imprecise_flops.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Looks for floating-point expressions that -can be expressed using built-in methods to improve accuracy -at the cost of performance. - -### Why is this bad? -Negatively impacts accuracy. - -### Example -``` -let a = 3f32; -let _ = a.powf(1.0 / 3.0); -let _ = (1.0 + a).ln(); -let _ = a.exp() - 1.0; -``` - -Use instead: -``` -let a = 3f32; -let _ = a.cbrt(); -let _ = a.ln_1p(); -let _ = a.exp_m1(); -``` \ No newline at end of file diff --git a/src/docs/inconsistent_digit_grouping.txt b/src/docs/inconsistent_digit_grouping.txt deleted file mode 100644 index aa0b072de1c40..0000000000000 --- a/src/docs/inconsistent_digit_grouping.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Warns if an integral or floating-point constant is -grouped inconsistently with underscores. - -### Why is this bad? -Readers may incorrectly interpret inconsistently -grouped digits. - -### Example -``` -618_64_9189_73_511 -``` - -Use instead: -``` -61_864_918_973_511 -``` \ No newline at end of file diff --git a/src/docs/inconsistent_struct_constructor.txt b/src/docs/inconsistent_struct_constructor.txt deleted file mode 100644 index eb682109a54ee..0000000000000 --- a/src/docs/inconsistent_struct_constructor.txt +++ /dev/null @@ -1,40 +0,0 @@ -### What it does -Checks for struct constructors where all fields are shorthand and -the order of the field init shorthand in the constructor is inconsistent -with the order in the struct definition. - -### Why is this bad? -Since the order of fields in a constructor doesn't affect the -resulted instance as the below example indicates, - -``` -#[derive(Debug, PartialEq, Eq)] -struct Foo { - x: i32, - y: i32, -} -let x = 1; -let y = 2; - -// This assertion never fails: -assert_eq!(Foo { x, y }, Foo { y, x }); -``` - -inconsistent order can be confusing and decreases readability and consistency. - -### Example -``` -struct Foo { - x: i32, - y: i32, -} -let x = 1; -let y = 2; - -Foo { y, x }; -``` - -Use instead: -``` -Foo { x, y }; -``` \ No newline at end of file diff --git a/src/docs/index_refutable_slice.txt b/src/docs/index_refutable_slice.txt deleted file mode 100644 index 8a7d52761af81..0000000000000 --- a/src/docs/index_refutable_slice.txt +++ /dev/null @@ -1,29 +0,0 @@ -### What it does -The lint checks for slice bindings in patterns that are only used to -access individual slice values. - -### Why is this bad? -Accessing slice values using indices can lead to panics. Using refutable -patterns can avoid these. Binding to individual values also improves the -readability as they can be named. - -### Limitations -This lint currently only checks for immutable access inside `if let` -patterns. - -### Example -``` -let slice: Option<&[u32]> = Some(&[1, 2, 3]); - -if let Some(slice) = slice { - println!("{}", slice[0]); -} -``` -Use instead: -``` -let slice: Option<&[u32]> = Some(&[1, 2, 3]); - -if let Some(&[first, ..]) = slice { - println!("{}", first); -} -``` \ No newline at end of file diff --git a/src/docs/indexing_slicing.txt b/src/docs/indexing_slicing.txt deleted file mode 100644 index 76ca6ed318b38..0000000000000 --- a/src/docs/indexing_slicing.txt +++ /dev/null @@ -1,33 +0,0 @@ -### What it does -Checks for usage of indexing or slicing. Arrays are special cases, this lint -does report on arrays if we can tell that slicing operations are in bounds and does not -lint on constant `usize` indexing on arrays because that is handled by rustc's `const_err` lint. - -### Why is this bad? -Indexing and slicing can panic at runtime and there are -safe alternatives. - -### Example -``` -// Vector -let x = vec![0; 5]; - -x[2]; -&x[2..100]; - -// Array -let y = [0, 1, 2, 3]; - -&y[10..100]; -&y[10..]; -``` - -Use instead: -``` - -x.get(2); -x.get(2..100); - -y.get(10); -y.get(10..100); -``` \ No newline at end of file diff --git a/src/docs/ineffective_bit_mask.txt b/src/docs/ineffective_bit_mask.txt deleted file mode 100644 index f6e7ef556215b..0000000000000 --- a/src/docs/ineffective_bit_mask.txt +++ /dev/null @@ -1,25 +0,0 @@ -### What it does -Checks for bit masks in comparisons which can be removed -without changing the outcome. The basic structure can be seen in the -following table: - -|Comparison| Bit Op |Example |equals | -|----------|----------|------------|-------| -|`>` / `<=`|`\|` / `^`|`x \| 2 > 3`|`x > 3`| -|`<` / `>=`|`\|` / `^`|`x ^ 1 < 4` |`x < 4`| - -### Why is this bad? -Not equally evil as [`bad_bit_mask`](#bad_bit_mask), -but still a bit misleading, because the bit mask is ineffective. - -### Known problems -False negatives: This lint will only match instances -where we have figured out the math (which is for a power-of-two compared -value). This means things like `x | 1 >= 7` (which would be better written -as `x >= 6`) will not be reported (but bit masks like this are fairly -uncommon). - -### Example -``` -if (x | 1 > 3) { } -``` \ No newline at end of file diff --git a/src/docs/inefficient_to_string.txt b/src/docs/inefficient_to_string.txt deleted file mode 100644 index f7061d1ce7b08..0000000000000 --- a/src/docs/inefficient_to_string.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for usage of `.to_string()` on an `&&T` where -`T` implements `ToString` directly (like `&&str` or `&&String`). - -### Why is this bad? -This bypasses the specialized implementation of -`ToString` and instead goes through the more expensive string formatting -facilities. - -### Example -``` -// Generic implementation for `T: Display` is used (slow) -["foo", "bar"].iter().map(|s| s.to_string()); - -// OK, the specialized impl is used -["foo", "bar"].iter().map(|&s| s.to_string()); -``` \ No newline at end of file diff --git a/src/docs/infallible_destructuring_match.txt b/src/docs/infallible_destructuring_match.txt deleted file mode 100644 index 4b5d3c4ba6c40..0000000000000 --- a/src/docs/infallible_destructuring_match.txt +++ /dev/null @@ -1,29 +0,0 @@ -### What it does -Checks for matches being used to destructure a single-variant enum -or tuple struct where a `let` will suffice. - -### Why is this bad? -Just readability – `let` doesn't nest, whereas a `match` does. - -### Example -``` -enum Wrapper { - Data(i32), -} - -let wrapper = Wrapper::Data(42); - -let data = match wrapper { - Wrapper::Data(i) => i, -}; -``` - -The correct use would be: -``` -enum Wrapper { - Data(i32), -} - -let wrapper = Wrapper::Data(42); -let Wrapper::Data(data) = wrapper; -``` \ No newline at end of file diff --git a/src/docs/infinite_iter.txt b/src/docs/infinite_iter.txt deleted file mode 100644 index 8a22fabc5492c..0000000000000 --- a/src/docs/infinite_iter.txt +++ /dev/null @@ -1,13 +0,0 @@ -### What it does -Checks for iteration that is guaranteed to be infinite. - -### Why is this bad? -While there may be places where this is acceptable -(e.g., in event streams), in most cases this is simply an error. - -### Example -``` -use std::iter; - -iter::repeat(1_u8).collect::>(); -``` \ No newline at end of file diff --git a/src/docs/inherent_to_string.txt b/src/docs/inherent_to_string.txt deleted file mode 100644 index b18e600e9e67b..0000000000000 --- a/src/docs/inherent_to_string.txt +++ /dev/null @@ -1,29 +0,0 @@ -### What it does -Checks for the definition of inherent methods with a signature of `to_string(&self) -> String`. - -### Why is this bad? -This method is also implicitly defined if a type implements the `Display` trait. As the functionality of `Display` is much more versatile, it should be preferred. - -### Example -``` -pub struct A; - -impl A { - pub fn to_string(&self) -> String { - "I am A".to_string() - } -} -``` - -Use instead: -``` -use std::fmt; - -pub struct A; - -impl fmt::Display for A { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "I am A") - } -} -``` \ No newline at end of file diff --git a/src/docs/inherent_to_string_shadow_display.txt b/src/docs/inherent_to_string_shadow_display.txt deleted file mode 100644 index a4bd0b622c4f5..0000000000000 --- a/src/docs/inherent_to_string_shadow_display.txt +++ /dev/null @@ -1,37 +0,0 @@ -### What it does -Checks for the definition of inherent methods with a signature of `to_string(&self) -> String` and if the type implementing this method also implements the `Display` trait. - -### Why is this bad? -This method is also implicitly defined if a type implements the `Display` trait. The less versatile inherent method will then shadow the implementation introduced by `Display`. - -### Example -``` -use std::fmt; - -pub struct A; - -impl A { - pub fn to_string(&self) -> String { - "I am A".to_string() - } -} - -impl fmt::Display for A { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "I am A, too") - } -} -``` - -Use instead: -``` -use std::fmt; - -pub struct A; - -impl fmt::Display for A { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "I am A") - } -} -``` \ No newline at end of file diff --git a/src/docs/init_numbered_fields.txt b/src/docs/init_numbered_fields.txt deleted file mode 100644 index ba40af6a5fa55..0000000000000 --- a/src/docs/init_numbered_fields.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks for tuple structs initialized with field syntax. -It will however not lint if a base initializer is present. -The lint will also ignore code in macros. - -### Why is this bad? -This may be confusing to the uninitiated and adds no -benefit as opposed to tuple initializers - -### Example -``` -struct TupleStruct(u8, u16); - -let _ = TupleStruct { - 0: 1, - 1: 23, -}; - -// should be written as -let base = TupleStruct(1, 23); - -// This is OK however -let _ = TupleStruct { 0: 42, ..base }; -``` \ No newline at end of file diff --git a/src/docs/inline_always.txt b/src/docs/inline_always.txt deleted file mode 100644 index 7721da4c4cc71..0000000000000 --- a/src/docs/inline_always.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for items annotated with `#[inline(always)]`, -unless the annotated function is empty or simply panics. - -### Why is this bad? -While there are valid uses of this annotation (and once -you know when to use it, by all means `allow` this lint), it's a common -newbie-mistake to pepper one's code with it. - -As a rule of thumb, before slapping `#[inline(always)]` on a function, -measure if that additional function call really affects your runtime profile -sufficiently to make up for the increase in compile time. - -### Known problems -False positives, big time. This lint is meant to be -deactivated by everyone doing serious performance work. This means having -done the measurement. - -### Example -``` -#[inline(always)] -fn not_quite_hot_code(..) { ... } -``` \ No newline at end of file diff --git a/src/docs/inline_asm_x86_att_syntax.txt b/src/docs/inline_asm_x86_att_syntax.txt deleted file mode 100644 index 8eb49d122d899..0000000000000 --- a/src/docs/inline_asm_x86_att_syntax.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for usage of AT&T x86 assembly syntax. - -### Why is this bad? -The lint has been enabled to indicate a preference -for Intel x86 assembly syntax. - -### Example - -``` -asm!("lea ({}), {}", in(reg) ptr, lateout(reg) _, options(att_syntax)); -``` -Use instead: -``` -asm!("lea {}, [{}]", lateout(reg) _, in(reg) ptr); -``` \ No newline at end of file diff --git a/src/docs/inline_asm_x86_intel_syntax.txt b/src/docs/inline_asm_x86_intel_syntax.txt deleted file mode 100644 index 5aa22c8ed2357..0000000000000 --- a/src/docs/inline_asm_x86_intel_syntax.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for usage of Intel x86 assembly syntax. - -### Why is this bad? -The lint has been enabled to indicate a preference -for AT&T x86 assembly syntax. - -### Example - -``` -asm!("lea {}, [{}]", lateout(reg) _, in(reg) ptr); -``` -Use instead: -``` -asm!("lea ({}), {}", in(reg) ptr, lateout(reg) _, options(att_syntax)); -``` \ No newline at end of file diff --git a/src/docs/inline_fn_without_body.txt b/src/docs/inline_fn_without_body.txt deleted file mode 100644 index 127c161aaa250..0000000000000 --- a/src/docs/inline_fn_without_body.txt +++ /dev/null @@ -1,14 +0,0 @@ -### What it does -Checks for `#[inline]` on trait methods without bodies - -### Why is this bad? -Only implementations of trait methods may be inlined. -The inline attribute is ignored for trait methods without bodies. - -### Example -``` -trait Animal { - #[inline] - fn name(&self) -> &'static str; -} -``` \ No newline at end of file diff --git a/src/docs/inspect_for_each.txt b/src/docs/inspect_for_each.txt deleted file mode 100644 index 01a46d6c451f4..0000000000000 --- a/src/docs/inspect_for_each.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for usage of `inspect().for_each()`. - -### Why is this bad? -It is the same as performing the computation -inside `inspect` at the beginning of the closure in `for_each`. - -### Example -``` -[1,2,3,4,5].iter() -.inspect(|&x| println!("inspect the number: {}", x)) -.for_each(|&x| { - assert!(x >= 0); -}); -``` -Can be written as -``` -[1,2,3,4,5].iter() -.for_each(|&x| { - println!("inspect the number: {}", x); - assert!(x >= 0); -}); -``` \ No newline at end of file diff --git a/src/docs/int_plus_one.txt b/src/docs/int_plus_one.txt deleted file mode 100644 index 1b68f3eeb64b4..0000000000000 --- a/src/docs/int_plus_one.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block - -### Why is this bad? -Readability -- better to use `> y` instead of `>= y + 1`. - -### Example -``` -if x >= y + 1 {} -``` - -Use instead: -``` -if x > y {} -``` \ No newline at end of file diff --git a/src/docs/integer_arithmetic.txt b/src/docs/integer_arithmetic.txt deleted file mode 100644 index ea57a2ef97bf5..0000000000000 --- a/src/docs/integer_arithmetic.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for integer arithmetic operations which could overflow or panic. - -Specifically, checks for any operators (`+`, `-`, `*`, `<<`, etc) which are capable -of overflowing according to the [Rust -Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow), -or which can panic (`/`, `%`). No bounds analysis or sophisticated reasoning is -attempted. - -### Why is this bad? -Integer overflow will trigger a panic in debug builds or will wrap in -release mode. Division by zero will cause a panic in either mode. In some applications one -wants explicitly checked, wrapping or saturating arithmetic. - -### Example -``` -a + 1; -``` \ No newline at end of file diff --git a/src/docs/integer_division.txt b/src/docs/integer_division.txt deleted file mode 100644 index f6d3349810ed8..0000000000000 --- a/src/docs/integer_division.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for division of integers - -### Why is this bad? -When outside of some very specific algorithms, -integer division is very often a mistake because it discards the -remainder. - -### Example -``` -let x = 3 / 2; -println!("{}", x); -``` - -Use instead: -``` -let x = 3f32 / 2f32; -println!("{}", x); -``` \ No newline at end of file diff --git a/src/docs/into_iter_on_ref.txt b/src/docs/into_iter_on_ref.txt deleted file mode 100644 index acb6bd474ebf3..0000000000000 --- a/src/docs/into_iter_on_ref.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for `into_iter` calls on references which should be replaced by `iter` -or `iter_mut`. - -### Why is this bad? -Readability. Calling `into_iter` on a reference will not move out its -content into the resulting iterator, which is confusing. It is better just call `iter` or -`iter_mut` directly. - -### Example -``` -(&vec).into_iter(); -``` - -Use instead: -``` -(&vec).iter(); -``` \ No newline at end of file diff --git a/src/docs/invalid_null_ptr_usage.txt b/src/docs/invalid_null_ptr_usage.txt deleted file mode 100644 index 6fb3fa3f83d66..0000000000000 --- a/src/docs/invalid_null_ptr_usage.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -This lint checks for invalid usages of `ptr::null`. - -### Why is this bad? -This causes undefined behavior. - -### Example -``` -// Undefined behavior -unsafe { std::slice::from_raw_parts(ptr::null(), 0); } -``` - -Use instead: -``` -unsafe { std::slice::from_raw_parts(NonNull::dangling().as_ptr(), 0); } -``` \ No newline at end of file diff --git a/src/docs/invalid_regex.txt b/src/docs/invalid_regex.txt deleted file mode 100644 index 6c9969b6e1a33..0000000000000 --- a/src/docs/invalid_regex.txt +++ /dev/null @@ -1,12 +0,0 @@ -### What it does -Checks [regex](https://crates.io/crates/regex) creation -(with `Regex::new`, `RegexBuilder::new`, or `RegexSet::new`) for correct -regex syntax. - -### Why is this bad? -This will lead to a runtime panic. - -### Example -``` -Regex::new("(") -``` \ No newline at end of file diff --git a/src/docs/invalid_upcast_comparisons.txt b/src/docs/invalid_upcast_comparisons.txt deleted file mode 100644 index 77cb033080372..0000000000000 --- a/src/docs/invalid_upcast_comparisons.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for comparisons where the relation is always either -true or false, but where one side has been upcast so that the comparison is -necessary. Only integer types are checked. - -### Why is this bad? -An expression like `let x : u8 = ...; (x as u32) > 300` -will mistakenly imply that it is possible for `x` to be outside the range of -`u8`. - -### Known problems -https://github.com/rust-lang/rust-clippy/issues/886 - -### Example -``` -let x: u8 = 1; -(x as u32) > 300; -``` \ No newline at end of file diff --git a/src/docs/invalid_utf8_in_unchecked.txt b/src/docs/invalid_utf8_in_unchecked.txt deleted file mode 100644 index afb5acbe9c51c..0000000000000 --- a/src/docs/invalid_utf8_in_unchecked.txt +++ /dev/null @@ -1,12 +0,0 @@ -### What it does -Checks for `std::str::from_utf8_unchecked` with an invalid UTF-8 literal - -### Why is this bad? -Creating such a `str` would result in undefined behavior - -### Example -``` -unsafe { - std::str::from_utf8_unchecked(b"cl\x82ippy"); -} -``` \ No newline at end of file diff --git a/src/docs/invisible_characters.txt b/src/docs/invisible_characters.txt deleted file mode 100644 index 3dda380911f91..0000000000000 --- a/src/docs/invisible_characters.txt +++ /dev/null @@ -1,10 +0,0 @@ -### What it does -Checks for invisible Unicode characters in the code. - -### Why is this bad? -Having an invisible character in the code makes for all -sorts of April fools, but otherwise is very much frowned upon. - -### Example -You don't see it, but there may be a zero-width space or soft hyphen -some­where in this text. \ No newline at end of file diff --git a/src/docs/is_digit_ascii_radix.txt b/src/docs/is_digit_ascii_radix.txt deleted file mode 100644 index 9f11cf43054fe..0000000000000 --- a/src/docs/is_digit_ascii_radix.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Finds usages of [`char::is_digit`](https://doc.rust-lang.org/stable/std/primitive.char.html#method.is_digit) that -can be replaced with [`is_ascii_digit`](https://doc.rust-lang.org/stable/std/primitive.char.html#method.is_ascii_digit) or -[`is_ascii_hexdigit`](https://doc.rust-lang.org/stable/std/primitive.char.html#method.is_ascii_hexdigit). - -### Why is this bad? -`is_digit(..)` is slower and requires specifying the radix. - -### Example -``` -let c: char = '6'; -c.is_digit(10); -c.is_digit(16); -``` -Use instead: -``` -let c: char = '6'; -c.is_ascii_digit(); -c.is_ascii_hexdigit(); -``` \ No newline at end of file diff --git a/src/docs/items_after_statements.txt b/src/docs/items_after_statements.txt deleted file mode 100644 index 6fdfff50d20e4..0000000000000 --- a/src/docs/items_after_statements.txt +++ /dev/null @@ -1,37 +0,0 @@ -### What it does -Checks for items declared after some statement in a block. - -### Why is this bad? -Items live for the entire scope they are declared -in. But statements are processed in order. This might cause confusion as -it's hard to figure out which item is meant in a statement. - -### Example -``` -fn foo() { - println!("cake"); -} - -fn main() { - foo(); // prints "foo" - fn foo() { - println!("foo"); - } - foo(); // prints "foo" -} -``` - -Use instead: -``` -fn foo() { - println!("cake"); -} - -fn main() { - fn foo() { - println!("foo"); - } - foo(); // prints "foo" - foo(); // prints "foo" -} -``` \ No newline at end of file diff --git a/src/docs/iter_cloned_collect.txt b/src/docs/iter_cloned_collect.txt deleted file mode 100644 index 90dc9ebb40f0b..0000000000000 --- a/src/docs/iter_cloned_collect.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for the use of `.cloned().collect()` on slice to -create a `Vec`. - -### Why is this bad? -`.to_vec()` is clearer - -### Example -``` -let s = [1, 2, 3, 4, 5]; -let s2: Vec = s[..].iter().cloned().collect(); -``` -The better use would be: -``` -let s = [1, 2, 3, 4, 5]; -let s2: Vec = s.to_vec(); -``` \ No newline at end of file diff --git a/src/docs/iter_count.txt b/src/docs/iter_count.txt deleted file mode 100644 index f3db4a26c2997..0000000000000 --- a/src/docs/iter_count.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for the use of `.iter().count()`. - -### Why is this bad? -`.len()` is more efficient and more -readable. - -### Example -``` -let some_vec = vec![0, 1, 2, 3]; - -some_vec.iter().count(); -&some_vec[..].iter().count(); -``` - -Use instead: -``` -let some_vec = vec![0, 1, 2, 3]; - -some_vec.len(); -&some_vec[..].len(); -``` \ No newline at end of file diff --git a/src/docs/iter_kv_map.txt b/src/docs/iter_kv_map.txt deleted file mode 100644 index a063c8195ef57..0000000000000 --- a/src/docs/iter_kv_map.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does - -Checks for iterating a map (`HashMap` or `BTreeMap`) and -ignoring either the keys or values. - -### Why is this bad? - -Readability. There are `keys` and `values` methods that -can be used to express that we only need the keys or the values. - -### Example - -``` -let map: HashMap = HashMap::new(); -let values = map.iter().map(|(_, value)| value).collect::>(); -``` - -Use instead: -``` -let map: HashMap = HashMap::new(); -let values = map.values().collect::>(); -``` \ No newline at end of file diff --git a/src/docs/iter_next_loop.txt b/src/docs/iter_next_loop.txt deleted file mode 100644 index b33eb39d6e1d3..0000000000000 --- a/src/docs/iter_next_loop.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for loops on `x.next()`. - -### Why is this bad? -`next()` returns either `Some(value)` if there was a -value, or `None` otherwise. The insidious thing is that `Option<_>` -implements `IntoIterator`, so that possibly one value will be iterated, -leading to some hard to find bugs. No one will want to write such code -[except to win an Underhanded Rust -Contest](https://www.reddit.com/r/rust/comments/3hb0wm/underhanded_rust_contest/cu5yuhr). - -### Example -``` -for x in y.next() { - .. -} -``` \ No newline at end of file diff --git a/src/docs/iter_next_slice.txt b/src/docs/iter_next_slice.txt deleted file mode 100644 index 1cea25eaf3017..0000000000000 --- a/src/docs/iter_next_slice.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for usage of `iter().next()` on a Slice or an Array - -### Why is this bad? -These can be shortened into `.get()` - -### Example -``` -a[2..].iter().next(); -b.iter().next(); -``` -should be written as: -``` -a.get(2); -b.get(0); -``` \ No newline at end of file diff --git a/src/docs/iter_not_returning_iterator.txt b/src/docs/iter_not_returning_iterator.txt deleted file mode 100644 index 0ca862910a6f0..0000000000000 --- a/src/docs/iter_not_returning_iterator.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Detects methods named `iter` or `iter_mut` that do not have a return type that implements `Iterator`. - -### Why is this bad? -Methods named `iter` or `iter_mut` conventionally return an `Iterator`. - -### Example -``` -// `String` does not implement `Iterator` -struct Data {} -impl Data { - fn iter(&self) -> String { - todo!() - } -} -``` -Use instead: -``` -use std::str::Chars; -struct Data {} -impl Data { - fn iter(&self) -> Chars<'static> { - todo!() - } -} -``` \ No newline at end of file diff --git a/src/docs/iter_nth.txt b/src/docs/iter_nth.txt deleted file mode 100644 index 3d67d583ffde3..0000000000000 --- a/src/docs/iter_nth.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Checks for use of `.iter().nth()` (and the related -`.iter_mut().nth()`) on standard library types with *O*(1) element access. - -### Why is this bad? -`.get()` and `.get_mut()` are more efficient and more -readable. - -### Example -``` -let some_vec = vec![0, 1, 2, 3]; -let bad_vec = some_vec.iter().nth(3); -let bad_slice = &some_vec[..].iter().nth(3); -``` -The correct use would be: -``` -let some_vec = vec![0, 1, 2, 3]; -let bad_vec = some_vec.get(3); -let bad_slice = &some_vec[..].get(3); -``` \ No newline at end of file diff --git a/src/docs/iter_nth_zero.txt b/src/docs/iter_nth_zero.txt deleted file mode 100644 index 8efe47a16a100..0000000000000 --- a/src/docs/iter_nth_zero.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for the use of `iter.nth(0)`. - -### Why is this bad? -`iter.next()` is equivalent to -`iter.nth(0)`, as they both consume the next element, - but is more readable. - -### Example -``` -let x = s.iter().nth(0); -``` - -Use instead: -``` -let x = s.iter().next(); -``` \ No newline at end of file diff --git a/src/docs/iter_on_empty_collections.txt b/src/docs/iter_on_empty_collections.txt deleted file mode 100644 index 87c4ec12afae7..0000000000000 --- a/src/docs/iter_on_empty_collections.txt +++ /dev/null @@ -1,25 +0,0 @@ -### What it does - -Checks for calls to `iter`, `iter_mut` or `into_iter` on empty collections - -### Why is this bad? - -It is simpler to use the empty function from the standard library: - -### Example - -``` -use std::{slice, option}; -let a: slice::Iter = [].iter(); -let f: option::IntoIter = None.into_iter(); -``` -Use instead: -``` -use std::iter; -let a: iter::Empty = iter::empty(); -let b: iter::Empty = iter::empty(); -``` - -### Known problems - -The type of the resulting iterator might become incompatible with its usage \ No newline at end of file diff --git a/src/docs/iter_on_single_items.txt b/src/docs/iter_on_single_items.txt deleted file mode 100644 index d0388f25d045e..0000000000000 --- a/src/docs/iter_on_single_items.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does - -Checks for calls to `iter`, `iter_mut` or `into_iter` on collections containing a single item - -### Why is this bad? - -It is simpler to use the once function from the standard library: - -### Example - -``` -let a = [123].iter(); -let b = Some(123).into_iter(); -``` -Use instead: -``` -use std::iter; -let a = iter::once(&123); -let b = iter::once(123); -``` - -### Known problems - -The type of the resulting iterator might become incompatible with its usage \ No newline at end of file diff --git a/src/docs/iter_overeager_cloned.txt b/src/docs/iter_overeager_cloned.txt deleted file mode 100644 index 2f902a0c2db4d..0000000000000 --- a/src/docs/iter_overeager_cloned.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for usage of `_.cloned().()` where call to `.cloned()` can be postponed. - -### Why is this bad? -It's often inefficient to clone all elements of an iterator, when eventually, only some -of them will be consumed. - -### Known Problems -This `lint` removes the side of effect of cloning items in the iterator. -A code that relies on that side-effect could fail. - -### Examples -``` -vec.iter().cloned().take(10); -vec.iter().cloned().last(); -``` - -Use instead: -``` -vec.iter().take(10).cloned(); -vec.iter().last().cloned(); -``` \ No newline at end of file diff --git a/src/docs/iter_skip_next.txt b/src/docs/iter_skip_next.txt deleted file mode 100644 index da226b041cf23..0000000000000 --- a/src/docs/iter_skip_next.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for use of `.skip(x).next()` on iterators. - -### Why is this bad? -`.nth(x)` is cleaner - -### Example -``` -let some_vec = vec![0, 1, 2, 3]; -let bad_vec = some_vec.iter().skip(3).next(); -let bad_slice = &some_vec[..].iter().skip(3).next(); -``` -The correct use would be: -``` -let some_vec = vec![0, 1, 2, 3]; -let bad_vec = some_vec.iter().nth(3); -let bad_slice = &some_vec[..].iter().nth(3); -``` \ No newline at end of file diff --git a/src/docs/iter_with_drain.txt b/src/docs/iter_with_drain.txt deleted file mode 100644 index 2c52b99f7a5c5..0000000000000 --- a/src/docs/iter_with_drain.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for use of `.drain(..)` on `Vec` and `VecDeque` for iteration. - -### Why is this bad? -`.into_iter()` is simpler with better performance. - -### Example -``` -let mut foo = vec![0, 1, 2, 3]; -let bar: HashSet = foo.drain(..).collect(); -``` -Use instead: -``` -let foo = vec![0, 1, 2, 3]; -let bar: HashSet = foo.into_iter().collect(); -``` \ No newline at end of file diff --git a/src/docs/iterator_step_by_zero.txt b/src/docs/iterator_step_by_zero.txt deleted file mode 100644 index 73ecc99acfcbc..0000000000000 --- a/src/docs/iterator_step_by_zero.txt +++ /dev/null @@ -1,13 +0,0 @@ -### What it does -Checks for calling `.step_by(0)` on iterators which panics. - -### Why is this bad? -This very much looks like an oversight. Use `panic!()` instead if you -actually intend to panic. - -### Example -``` -for x in (0..100).step_by(0) { - //.. -} -``` \ No newline at end of file diff --git a/src/docs/just_underscores_and_digits.txt b/src/docs/just_underscores_and_digits.txt deleted file mode 100644 index a8790bcf25be6..0000000000000 --- a/src/docs/just_underscores_and_digits.txt +++ /dev/null @@ -1,14 +0,0 @@ -### What it does -Checks if you have variables whose name consists of just -underscores and digits. - -### Why is this bad? -It's hard to memorize what a variable means without a -descriptive name. - -### Example -``` -let _1 = 1; -let ___1 = 1; -let __1___2 = 11; -``` \ No newline at end of file diff --git a/src/docs/large_const_arrays.txt b/src/docs/large_const_arrays.txt deleted file mode 100644 index 71f67854f2a1a..0000000000000 --- a/src/docs/large_const_arrays.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for large `const` arrays that should -be defined as `static` instead. - -### Why is this bad? -Performance: const variables are inlined upon use. -Static items result in only one instance and has a fixed location in memory. - -### Example -``` -pub const a = [0u32; 1_000_000]; -``` - -Use instead: -``` -pub static a = [0u32; 1_000_000]; -``` \ No newline at end of file diff --git a/src/docs/large_digit_groups.txt b/src/docs/large_digit_groups.txt deleted file mode 100644 index f60b19345af44..0000000000000 --- a/src/docs/large_digit_groups.txt +++ /dev/null @@ -1,12 +0,0 @@ -### What it does -Warns if the digits of an integral or floating-point -constant are grouped into groups that -are too large. - -### Why is this bad? -Negatively impacts readability. - -### Example -``` -let x: u64 = 6186491_8973511; -``` \ No newline at end of file diff --git a/src/docs/large_enum_variant.txt b/src/docs/large_enum_variant.txt deleted file mode 100644 index 1f95430790d29..0000000000000 --- a/src/docs/large_enum_variant.txt +++ /dev/null @@ -1,41 +0,0 @@ -### What it does -Checks for large size differences between variants on -`enum`s. - -### Why is this bad? -Enum size is bounded by the largest variant. Having one -large variant can penalize the memory layout of that enum. - -### Known problems -This lint obviously cannot take the distribution of -variants in your running program into account. It is possible that the -smaller variants make up less than 1% of all instances, in which case -the overhead is negligible and the boxing is counter-productive. Always -measure the change this lint suggests. - -For types that implement `Copy`, the suggestion to `Box` a variant's -data would require removing the trait impl. The types can of course -still be `Clone`, but that is worse ergonomically. Depending on the -use case it may be possible to store the large data in an auxiliary -structure (e.g. Arena or ECS). - -The lint will ignore the impact of generic types to the type layout by -assuming every type parameter is zero-sized. Depending on your use case, -this may lead to a false positive. - -### Example -``` -enum Test { - A(i32), - B([i32; 8000]), -} -``` - -Use instead: -``` -// Possibly better -enum Test2 { - A(i32), - B(Box<[i32; 8000]>), -} -``` \ No newline at end of file diff --git a/src/docs/large_include_file.txt b/src/docs/large_include_file.txt deleted file mode 100644 index b2a54bd2eb5ca..0000000000000 --- a/src/docs/large_include_file.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for the inclusion of large files via `include_bytes!()` -and `include_str!()` - -### Why is this bad? -Including large files can increase the size of the binary - -### Example -``` -let included_str = include_str!("very_large_file.txt"); -let included_bytes = include_bytes!("very_large_file.txt"); -``` - -Use instead: -``` -use std::fs; - -// You can load the file at runtime -let string = fs::read_to_string("very_large_file.txt")?; -let bytes = fs::read("very_large_file.txt")?; -``` \ No newline at end of file diff --git a/src/docs/large_stack_arrays.txt b/src/docs/large_stack_arrays.txt deleted file mode 100644 index 4a6f34785b0ef..0000000000000 --- a/src/docs/large_stack_arrays.txt +++ /dev/null @@ -1,10 +0,0 @@ -### What it does -Checks for local arrays that may be too large. - -### Why is this bad? -Large local arrays may cause stack overflow. - -### Example -``` -let a = [0u32; 1_000_000]; -``` \ No newline at end of file diff --git a/src/docs/large_types_passed_by_value.txt b/src/docs/large_types_passed_by_value.txt deleted file mode 100644 index bca07f3ac61bb..0000000000000 --- a/src/docs/large_types_passed_by_value.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks for functions taking arguments by value, where -the argument type is `Copy` and large enough to be worth considering -passing by reference. Does not trigger if the function is being exported, -because that might induce API breakage, if the parameter is declared as mutable, -or if the argument is a `self`. - -### Why is this bad? -Arguments passed by value might result in an unnecessary -shallow copy, taking up more space in the stack and requiring a call to -`memcpy`, which can be expensive. - -### Example -``` -#[derive(Clone, Copy)] -struct TooLarge([u8; 2048]); - -fn foo(v: TooLarge) {} -``` - -Use instead: -``` -fn foo(v: &TooLarge) {} -``` \ No newline at end of file diff --git a/src/docs/len_without_is_empty.txt b/src/docs/len_without_is_empty.txt deleted file mode 100644 index 47a2e85752285..0000000000000 --- a/src/docs/len_without_is_empty.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for items that implement `.len()` but not -`.is_empty()`. - -### Why is this bad? -It is good custom to have both methods, because for -some data structures, asking about the length will be a costly operation, -whereas `.is_empty()` can usually answer in constant time. Also it used to -lead to false positives on the [`len_zero`](#len_zero) lint – currently that -lint will ignore such entities. - -### Example -``` -impl X { - pub fn len(&self) -> usize { - .. - } -} -``` \ No newline at end of file diff --git a/src/docs/len_zero.txt b/src/docs/len_zero.txt deleted file mode 100644 index 664124bd391df..0000000000000 --- a/src/docs/len_zero.txt +++ /dev/null @@ -1,28 +0,0 @@ -### What it does -Checks for getting the length of something via `.len()` -just to compare to zero, and suggests using `.is_empty()` where applicable. - -### Why is this bad? -Some structures can answer `.is_empty()` much faster -than calculating their length. So it is good to get into the habit of using -`.is_empty()`, and having it is cheap. -Besides, it makes the intent clearer than a manual comparison in some contexts. - -### Example -``` -if x.len() == 0 { - .. -} -if y.len() != 0 { - .. -} -``` -instead use -``` -if x.is_empty() { - .. -} -if !y.is_empty() { - .. -} -``` \ No newline at end of file diff --git a/src/docs/let_and_return.txt b/src/docs/let_and_return.txt deleted file mode 100644 index eba5a90ddd66c..0000000000000 --- a/src/docs/let_and_return.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for `let`-bindings, which are subsequently -returned. - -### Why is this bad? -It is just extraneous code. Remove it to make your code -more rusty. - -### Example -``` -fn foo() -> String { - let x = String::new(); - x -} -``` -instead, use -``` -fn foo() -> String { - String::new() -} -``` \ No newline at end of file diff --git a/src/docs/let_underscore_drop.txt b/src/docs/let_underscore_drop.txt deleted file mode 100644 index 29ce9bf50ce6a..0000000000000 --- a/src/docs/let_underscore_drop.txt +++ /dev/null @@ -1,29 +0,0 @@ -### What it does -Checks for `let _ = ` -where expr has a type that implements `Drop` - -### Why is this bad? -This statement immediately drops the initializer -expression instead of extending its lifetime to the end of the scope, which -is often not intended. To extend the expression's lifetime to the end of the -scope, use an underscore-prefixed name instead (i.e. _var). If you want to -explicitly drop the expression, `std::mem::drop` conveys your intention -better and is less error-prone. - -### Example -``` -{ - let _ = DroppableItem; - // ^ dropped here - /* more code */ -} -``` - -Use instead: -``` -{ - let _droppable = DroppableItem; - /* more code */ - // dropped at end of scope -} -``` \ No newline at end of file diff --git a/src/docs/let_underscore_lock.txt b/src/docs/let_underscore_lock.txt deleted file mode 100644 index bd8217fb58b3c..0000000000000 --- a/src/docs/let_underscore_lock.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Checks for `let _ = sync_lock`. -This supports `mutex` and `rwlock` in `std::sync` and `parking_lot`. - -### Why is this bad? -This statement immediately drops the lock instead of -extending its lifetime to the end of the scope, which is often not intended. -To extend lock lifetime to the end of the scope, use an underscore-prefixed -name instead (i.e. _lock). If you want to explicitly drop the lock, -`std::mem::drop` conveys your intention better and is less error-prone. - -### Example -``` -let _ = mutex.lock(); -``` - -Use instead: -``` -let _lock = mutex.lock(); -``` \ No newline at end of file diff --git a/src/docs/let_underscore_must_use.txt b/src/docs/let_underscore_must_use.txt deleted file mode 100644 index 270b81d9a4c41..0000000000000 --- a/src/docs/let_underscore_must_use.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for `let _ = ` where expr is `#[must_use]` - -### Why is this bad? -It's better to explicitly handle the value of a `#[must_use]` -expr - -### Example -``` -fn f() -> Result { - Ok(0) -} - -let _ = f(); -// is_ok() is marked #[must_use] -let _ = f().is_ok(); -``` \ No newline at end of file diff --git a/src/docs/let_unit_value.txt b/src/docs/let_unit_value.txt deleted file mode 100644 index bc16d5b3d81be..0000000000000 --- a/src/docs/let_unit_value.txt +++ /dev/null @@ -1,13 +0,0 @@ -### What it does -Checks for binding a unit value. - -### Why is this bad? -A unit value cannot usefully be used anywhere. So -binding one is kind of pointless. - -### Example -``` -let x = { - 1; -}; -``` \ No newline at end of file diff --git a/src/docs/linkedlist.txt b/src/docs/linkedlist.txt deleted file mode 100644 index 986ff1369e3c1..0000000000000 --- a/src/docs/linkedlist.txt +++ /dev/null @@ -1,32 +0,0 @@ -### What it does -Checks for usage of any `LinkedList`, suggesting to use a -`Vec` or a `VecDeque` (formerly called `RingBuf`). - -### Why is this bad? -Gankro says: - -> The TL;DR of `LinkedList` is that it's built on a massive amount of -pointers and indirection. -> It wastes memory, it has terrible cache locality, and is all-around slow. -`RingBuf`, while -> "only" amortized for push/pop, should be faster in the general case for -almost every possible -> workload, and isn't even amortized at all if you can predict the capacity -you need. -> -> `LinkedList`s are only really good if you're doing a lot of merging or -splitting of lists. -> This is because they can just mangle some pointers instead of actually -copying the data. Even -> if you're doing a lot of insertion in the middle of the list, `RingBuf` -can still be better -> because of how expensive it is to seek to the middle of a `LinkedList`. - -### Known problems -False positives – the instances where using a -`LinkedList` makes sense are few and far between, but they can still happen. - -### Example -``` -let x: LinkedList = LinkedList::new(); -``` \ No newline at end of file diff --git a/src/docs/lossy_float_literal.txt b/src/docs/lossy_float_literal.txt deleted file mode 100644 index bbcb9115ea62a..0000000000000 --- a/src/docs/lossy_float_literal.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for whole number float literals that -cannot be represented as the underlying type without loss. - -### Why is this bad? -Rust will silently lose precision during -conversion to a float. - -### Example -``` -let _: f32 = 16_777_217.0; // 16_777_216.0 -``` - -Use instead: -``` -let _: f32 = 16_777_216.0; -let _: f64 = 16_777_217.0; -``` \ No newline at end of file diff --git a/src/docs/macro_use_imports.txt b/src/docs/macro_use_imports.txt deleted file mode 100644 index 6a8180a60bc6f..0000000000000 --- a/src/docs/macro_use_imports.txt +++ /dev/null @@ -1,12 +0,0 @@ -### What it does -Checks for `#[macro_use] use...`. - -### Why is this bad? -Since the Rust 2018 edition you can import -macro's directly, this is considered idiomatic. - -### Example -``` -#[macro_use] -use some_macro; -``` \ No newline at end of file diff --git a/src/docs/main_recursion.txt b/src/docs/main_recursion.txt deleted file mode 100644 index e49becd15bbdb..0000000000000 --- a/src/docs/main_recursion.txt +++ /dev/null @@ -1,13 +0,0 @@ -### What it does -Checks for recursion using the entrypoint. - -### Why is this bad? -Apart from special setups (which we could detect following attributes like #![no_std]), -recursing into main() seems like an unintuitive anti-pattern we should be able to detect. - -### Example -``` -fn main() { - main(); -} -``` \ No newline at end of file diff --git a/src/docs/manual_assert.txt b/src/docs/manual_assert.txt deleted file mode 100644 index 93653081a2ce0..0000000000000 --- a/src/docs/manual_assert.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Detects `if`-then-`panic!` that can be replaced with `assert!`. - -### Why is this bad? -`assert!` is simpler than `if`-then-`panic!`. - -### Example -``` -let sad_people: Vec<&str> = vec![]; -if !sad_people.is_empty() { - panic!("there are sad people: {:?}", sad_people); -} -``` -Use instead: -``` -let sad_people: Vec<&str> = vec![]; -assert!(sad_people.is_empty(), "there are sad people: {:?}", sad_people); -``` \ No newline at end of file diff --git a/src/docs/manual_async_fn.txt b/src/docs/manual_async_fn.txt deleted file mode 100644 index d01ac402e0d21..0000000000000 --- a/src/docs/manual_async_fn.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -It checks for manual implementations of `async` functions. - -### Why is this bad? -It's more idiomatic to use the dedicated syntax. - -### Example -``` -use std::future::Future; - -fn foo() -> impl Future { async { 42 } } -``` -Use instead: -``` -async fn foo() -> i32 { 42 } -``` \ No newline at end of file diff --git a/src/docs/manual_bits.txt b/src/docs/manual_bits.txt deleted file mode 100644 index b96c2eb151d41..0000000000000 --- a/src/docs/manual_bits.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for uses of `std::mem::size_of::() * 8` when -`T::BITS` is available. - -### Why is this bad? -Can be written as the shorter `T::BITS`. - -### Example -``` -std::mem::size_of::() * 8; -``` -Use instead: -``` -usize::BITS as usize; -``` \ No newline at end of file diff --git a/src/docs/manual_clamp.txt b/src/docs/manual_clamp.txt deleted file mode 100644 index 8993f6683adfe..0000000000000 --- a/src/docs/manual_clamp.txt +++ /dev/null @@ -1,46 +0,0 @@ -### What it does -Identifies good opportunities for a clamp function from std or core, and suggests using it. - -### Why is this bad? -clamp is much shorter, easier to read, and doesn't use any control flow. - -### Known issue(s) -If the clamped variable is NaN this suggestion will cause the code to propagate NaN -rather than returning either `max` or `min`. - -`clamp` functions will panic if `max < min`, `max.is_nan()`, or `min.is_nan()`. -Some may consider panicking in these situations to be desirable, but it also may -introduce panicking where there wasn't any before. - -### Examples -``` -if input > max { - max -} else if input < min { - min -} else { - input -} -``` - -``` -input.max(min).min(max) -``` - -``` -match input { - x if x > max => max, - x if x < min => min, - x => x, -} -``` - -``` -let mut x = input; -if x < min { x = min; } -if x > max { x = max; } -``` -Use instead: -``` -input.clamp(min, max) -``` \ No newline at end of file diff --git a/src/docs/manual_filter.txt b/src/docs/manual_filter.txt deleted file mode 100644 index 19a4d9319d94b..0000000000000 --- a/src/docs/manual_filter.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for usages of `match` which could be implemented using `filter` - -### Why is this bad? -Using the `filter` method is clearer and more concise. - -### Example -``` -match Some(0) { - Some(x) => if x % 2 == 0 { - Some(x) - } else { - None - }, - None => None, -}; -``` -Use instead: -``` -Some(0).filter(|&x| x % 2 == 0); -``` \ No newline at end of file diff --git a/src/docs/manual_filter_map.txt b/src/docs/manual_filter_map.txt deleted file mode 100644 index 3b6860798ff58..0000000000000 --- a/src/docs/manual_filter_map.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for usage of `_.filter(_).map(_)` that can be written more simply -as `filter_map(_)`. - -### Why is this bad? -Redundant code in the `filter` and `map` operations is poor style and -less performant. - -### Example -``` -(0_i32..10) - .filter(|n| n.checked_add(1).is_some()) - .map(|n| n.checked_add(1).unwrap()); -``` - -Use instead: -``` -(0_i32..10).filter_map(|n| n.checked_add(1)); -``` \ No newline at end of file diff --git a/src/docs/manual_find.txt b/src/docs/manual_find.txt deleted file mode 100644 index e3e07a2771f95..0000000000000 --- a/src/docs/manual_find.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Check for manual implementations of Iterator::find - -### Why is this bad? -It doesn't affect performance, but using `find` is shorter and easier to read. - -### Example - -``` -fn example(arr: Vec) -> Option { - for el in arr { - if el == 1 { - return Some(el); - } - } - None -} -``` -Use instead: -``` -fn example(arr: Vec) -> Option { - arr.into_iter().find(|&el| el == 1) -} -``` \ No newline at end of file diff --git a/src/docs/manual_find_map.txt b/src/docs/manual_find_map.txt deleted file mode 100644 index 83b22060c0e19..0000000000000 --- a/src/docs/manual_find_map.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for usage of `_.find(_).map(_)` that can be written more simply -as `find_map(_)`. - -### Why is this bad? -Redundant code in the `find` and `map` operations is poor style and -less performant. - -### Example -``` -(0_i32..10) - .find(|n| n.checked_add(1).is_some()) - .map(|n| n.checked_add(1).unwrap()); -``` - -Use instead: -``` -(0_i32..10).find_map(|n| n.checked_add(1)); -``` \ No newline at end of file diff --git a/src/docs/manual_flatten.txt b/src/docs/manual_flatten.txt deleted file mode 100644 index 62d5f3ec93576..0000000000000 --- a/src/docs/manual_flatten.txt +++ /dev/null @@ -1,25 +0,0 @@ -### What it does -Check for unnecessary `if let` usage in a for loop -where only the `Some` or `Ok` variant of the iterator element is used. - -### Why is this bad? -It is verbose and can be simplified -by first calling the `flatten` method on the `Iterator`. - -### Example - -``` -let x = vec![Some(1), Some(2), Some(3)]; -for n in x { - if let Some(n) = n { - println!("{}", n); - } -} -``` -Use instead: -``` -let x = vec![Some(1), Some(2), Some(3)]; -for n in x.into_iter().flatten() { - println!("{}", n); -} -``` \ No newline at end of file diff --git a/src/docs/manual_instant_elapsed.txt b/src/docs/manual_instant_elapsed.txt deleted file mode 100644 index dde3d493c70d3..0000000000000 --- a/src/docs/manual_instant_elapsed.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Lints subtraction between `Instant::now()` and another `Instant`. - -### Why is this bad? -It is easy to accidentally write `prev_instant - Instant::now()`, which will always be 0ns -as `Instant` subtraction saturates. - -`prev_instant.elapsed()` also more clearly signals intention. - -### Example -``` -use std::time::Instant; -let prev_instant = Instant::now(); -let duration = Instant::now() - prev_instant; -``` -Use instead: -``` -use std::time::Instant; -let prev_instant = Instant::now(); -let duration = prev_instant.elapsed(); -``` \ No newline at end of file diff --git a/src/docs/manual_map.txt b/src/docs/manual_map.txt deleted file mode 100644 index 7f68ccd1037e3..0000000000000 --- a/src/docs/manual_map.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for usages of `match` which could be implemented using `map` - -### Why is this bad? -Using the `map` method is clearer and more concise. - -### Example -``` -match Some(0) { - Some(x) => Some(x + 1), - None => None, -}; -``` -Use instead: -``` -Some(0).map(|x| x + 1); -``` \ No newline at end of file diff --git a/src/docs/manual_memcpy.txt b/src/docs/manual_memcpy.txt deleted file mode 100644 index d7690bf258669..0000000000000 --- a/src/docs/manual_memcpy.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for for-loops that manually copy items between -slices that could be optimized by having a memcpy. - -### Why is this bad? -It is not as fast as a memcpy. - -### Example -``` -for i in 0..src.len() { - dst[i + 64] = src[i]; -} -``` - -Use instead: -``` -dst[64..(src.len() + 64)].clone_from_slice(&src[..]); -``` \ No newline at end of file diff --git a/src/docs/manual_non_exhaustive.txt b/src/docs/manual_non_exhaustive.txt deleted file mode 100644 index fb021393bd7c9..0000000000000 --- a/src/docs/manual_non_exhaustive.txt +++ /dev/null @@ -1,41 +0,0 @@ -### What it does -Checks for manual implementations of the non-exhaustive pattern. - -### Why is this bad? -Using the #[non_exhaustive] attribute expresses better the intent -and allows possible optimizations when applied to enums. - -### Example -``` -struct S { - pub a: i32, - pub b: i32, - _c: (), -} - -enum E { - A, - B, - #[doc(hidden)] - _C, -} - -struct T(pub i32, pub i32, ()); -``` -Use instead: -``` -#[non_exhaustive] -struct S { - pub a: i32, - pub b: i32, -} - -#[non_exhaustive] -enum E { - A, - B, -} - -#[non_exhaustive] -struct T(pub i32, pub i32); -``` \ No newline at end of file diff --git a/src/docs/manual_ok_or.txt b/src/docs/manual_ok_or.txt deleted file mode 100644 index 5accdf25965ab..0000000000000 --- a/src/docs/manual_ok_or.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does - -Finds patterns that reimplement `Option::ok_or`. - -### Why is this bad? - -Concise code helps focusing on behavior instead of boilerplate. - -### Examples -``` -let foo: Option = None; -foo.map_or(Err("error"), |v| Ok(v)); -``` - -Use instead: -``` -let foo: Option = None; -foo.ok_or("error"); -``` \ No newline at end of file diff --git a/src/docs/manual_range_contains.txt b/src/docs/manual_range_contains.txt deleted file mode 100644 index 0ade26951d385..0000000000000 --- a/src/docs/manual_range_contains.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for expressions like `x >= 3 && x < 8` that could -be more readably expressed as `(3..8).contains(x)`. - -### Why is this bad? -`contains` expresses the intent better and has less -failure modes (such as fencepost errors or using `||` instead of `&&`). - -### Example -``` -// given -let x = 6; - -assert!(x >= 3 && x < 8); -``` -Use instead: -``` -assert!((3..8).contains(&x)); -``` \ No newline at end of file diff --git a/src/docs/manual_rem_euclid.txt b/src/docs/manual_rem_euclid.txt deleted file mode 100644 index d3bb8c61304e1..0000000000000 --- a/src/docs/manual_rem_euclid.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for an expression like `((x % 4) + 4) % 4` which is a common manual reimplementation -of `x.rem_euclid(4)`. - -### Why is this bad? -It's simpler and more readable. - -### Example -``` -let x: i32 = 24; -let rem = ((x % 4) + 4) % 4; -``` -Use instead: -``` -let x: i32 = 24; -let rem = x.rem_euclid(4); -``` \ No newline at end of file diff --git a/src/docs/manual_retain.txt b/src/docs/manual_retain.txt deleted file mode 100644 index cd4f65a93fc33..0000000000000 --- a/src/docs/manual_retain.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for code to be replaced by `.retain()`. -### Why is this bad? -`.retain()` is simpler and avoids needless allocation. -### Example -``` -let mut vec = vec![0, 1, 2]; -vec = vec.iter().filter(|&x| x % 2 == 0).copied().collect(); -vec = vec.into_iter().filter(|x| x % 2 == 0).collect(); -``` -Use instead: -``` -let mut vec = vec![0, 1, 2]; -vec.retain(|x| x % 2 == 0); -``` \ No newline at end of file diff --git a/src/docs/manual_saturating_arithmetic.txt b/src/docs/manual_saturating_arithmetic.txt deleted file mode 100644 index d9f5d3d118712..0000000000000 --- a/src/docs/manual_saturating_arithmetic.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for `.checked_add/sub(x).unwrap_or(MAX/MIN)`. - -### Why is this bad? -These can be written simply with `saturating_add/sub` methods. - -### Example -``` -let add = x.checked_add(y).unwrap_or(u32::MAX); -let sub = x.checked_sub(y).unwrap_or(u32::MIN); -``` - -can be written using dedicated methods for saturating addition/subtraction as: - -``` -let add = x.saturating_add(y); -let sub = x.saturating_sub(y); -``` \ No newline at end of file diff --git a/src/docs/manual_split_once.txt b/src/docs/manual_split_once.txt deleted file mode 100644 index 291ae447de08a..0000000000000 --- a/src/docs/manual_split_once.txt +++ /dev/null @@ -1,29 +0,0 @@ -### What it does -Checks for usages of `str::splitn(2, _)` - -### Why is this bad? -`split_once` is both clearer in intent and slightly more efficient. - -### Example -``` -let s = "key=value=add"; -let (key, value) = s.splitn(2, '=').next_tuple()?; -let value = s.splitn(2, '=').nth(1)?; - -let mut parts = s.splitn(2, '='); -let key = parts.next()?; -let value = parts.next()?; -``` - -Use instead: -``` -let s = "key=value=add"; -let (key, value) = s.split_once('=')?; -let value = s.split_once('=')?.1; - -let (key, value) = s.split_once('=')?; -``` - -### Limitations -The multiple statement variant currently only detects `iter.next()?`/`iter.next().unwrap()` -in two separate `let` statements that immediately follow the `splitn()` \ No newline at end of file diff --git a/src/docs/manual_str_repeat.txt b/src/docs/manual_str_repeat.txt deleted file mode 100644 index 1d4a7a48e2033..0000000000000 --- a/src/docs/manual_str_repeat.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for manual implementations of `str::repeat` - -### Why is this bad? -These are both harder to read, as well as less performant. - -### Example -``` -let x: String = std::iter::repeat('x').take(10).collect(); -``` - -Use instead: -``` -let x: String = "x".repeat(10); -``` \ No newline at end of file diff --git a/src/docs/manual_string_new.txt b/src/docs/manual_string_new.txt deleted file mode 100644 index 4cbc43f8f8439..0000000000000 --- a/src/docs/manual_string_new.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does - -Checks for usage of `""` to create a `String`, such as `"".to_string()`, `"".to_owned()`, -`String::from("")` and others. - -### Why is this bad? - -Different ways of creating an empty string makes your code less standardized, which can -be confusing. - -### Example -``` -let a = "".to_string(); -let b: String = "".into(); -``` -Use instead: -``` -let a = String::new(); -let b = String::new(); -``` \ No newline at end of file diff --git a/src/docs/manual_strip.txt b/src/docs/manual_strip.txt deleted file mode 100644 index f32d8e7a09bb3..0000000000000 --- a/src/docs/manual_strip.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Suggests using `strip_{prefix,suffix}` over `str::{starts,ends}_with` and slicing using -the pattern's length. - -### Why is this bad? -Using `str:strip_{prefix,suffix}` is safer and may have better performance as there is no -slicing which may panic and the compiler does not need to insert this panic code. It is -also sometimes more readable as it removes the need for duplicating or storing the pattern -used by `str::{starts,ends}_with` and in the slicing. - -### Example -``` -let s = "hello, world!"; -if s.starts_with("hello, ") { - assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!"); -} -``` -Use instead: -``` -let s = "hello, world!"; -if let Some(end) = s.strip_prefix("hello, ") { - assert_eq!(end.to_uppercase(), "WORLD!"); -} -``` \ No newline at end of file diff --git a/src/docs/manual_swap.txt b/src/docs/manual_swap.txt deleted file mode 100644 index bd9526288e35b..0000000000000 --- a/src/docs/manual_swap.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for manual swapping. - -### Why is this bad? -The `std::mem::swap` function exposes the intent better -without deinitializing or copying either variable. - -### Example -``` -let mut a = 42; -let mut b = 1337; - -let t = b; -b = a; -a = t; -``` -Use std::mem::swap(): -``` -let mut a = 1; -let mut b = 2; -std::mem::swap(&mut a, &mut b); -``` \ No newline at end of file diff --git a/src/docs/manual_unwrap_or.txt b/src/docs/manual_unwrap_or.txt deleted file mode 100644 index 1fd7d831bfca2..0000000000000 --- a/src/docs/manual_unwrap_or.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Finds patterns that reimplement `Option::unwrap_or` or `Result::unwrap_or`. - -### Why is this bad? -Concise code helps focusing on behavior instead of boilerplate. - -### Example -``` -let foo: Option = None; -match foo { - Some(v) => v, - None => 1, -}; -``` - -Use instead: -``` -let foo: Option = None; -foo.unwrap_or(1); -``` \ No newline at end of file diff --git a/src/docs/many_single_char_names.txt b/src/docs/many_single_char_names.txt deleted file mode 100644 index 55ee5da555744..0000000000000 --- a/src/docs/many_single_char_names.txt +++ /dev/null @@ -1,12 +0,0 @@ -### What it does -Checks for too many variables whose name consists of a -single character. - -### Why is this bad? -It's hard to memorize what a variable means without a -descriptive name. - -### Example -``` -let (a, b, c, d, e, f, g) = (...); -``` \ No newline at end of file diff --git a/src/docs/map_clone.txt b/src/docs/map_clone.txt deleted file mode 100644 index 3ee27f072ef3d..0000000000000 --- a/src/docs/map_clone.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for usage of `map(|x| x.clone())` or -dereferencing closures for `Copy` types, on `Iterator` or `Option`, -and suggests `cloned()` or `copied()` instead - -### Why is this bad? -Readability, this can be written more concisely - -### Example -``` -let x = vec![42, 43]; -let y = x.iter(); -let z = y.map(|i| *i); -``` - -The correct use would be: - -``` -let x = vec![42, 43]; -let y = x.iter(); -let z = y.cloned(); -``` \ No newline at end of file diff --git a/src/docs/map_collect_result_unit.txt b/src/docs/map_collect_result_unit.txt deleted file mode 100644 index 9b720612495ce..0000000000000 --- a/src/docs/map_collect_result_unit.txt +++ /dev/null @@ -1,14 +0,0 @@ -### What it does -Checks for usage of `_.map(_).collect::()`. - -### Why is this bad? -Using `try_for_each` instead is more readable and idiomatic. - -### Example -``` -(0..3).map(|t| Err(t)).collect::>(); -``` -Use instead: -``` -(0..3).try_for_each(|t| Err(t)); -``` \ No newline at end of file diff --git a/src/docs/map_entry.txt b/src/docs/map_entry.txt deleted file mode 100644 index 20dba1798d0d5..0000000000000 --- a/src/docs/map_entry.txt +++ /dev/null @@ -1,28 +0,0 @@ -### What it does -Checks for uses of `contains_key` + `insert` on `HashMap` -or `BTreeMap`. - -### Why is this bad? -Using `entry` is more efficient. - -### Known problems -The suggestion may have type inference errors in some cases. e.g. -``` -let mut map = std::collections::HashMap::new(); -let _ = if !map.contains_key(&0) { - map.insert(0, 0) -} else { - None -}; -``` - -### Example -``` -if !map.contains_key(&k) { - map.insert(k, v); -} -``` -Use instead: -``` -map.entry(k).or_insert(v); -``` \ No newline at end of file diff --git a/src/docs/map_err_ignore.txt b/src/docs/map_err_ignore.txt deleted file mode 100644 index 2606c13a7afd9..0000000000000 --- a/src/docs/map_err_ignore.txt +++ /dev/null @@ -1,93 +0,0 @@ -### What it does -Checks for instances of `map_err(|_| Some::Enum)` - -### Why is this bad? -This `map_err` throws away the original error rather than allowing the enum to contain and report the cause of the error - -### Example -Before: -``` -use std::fmt; - -#[derive(Debug)] -enum Error { - Indivisible, - Remainder(u8), -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Error::Indivisible => write!(f, "could not divide input by three"), - Error::Remainder(remainder) => write!( - f, - "input is not divisible by three, remainder = {}", - remainder - ), - } - } -} - -impl std::error::Error for Error {} - -fn divisible_by_3(input: &str) -> Result<(), Error> { - input - .parse::() - .map_err(|_| Error::Indivisible) - .map(|v| v % 3) - .and_then(|remainder| { - if remainder == 0 { - Ok(()) - } else { - Err(Error::Remainder(remainder as u8)) - } - }) -} - ``` - - After: - ```rust -use std::{fmt, num::ParseIntError}; - -#[derive(Debug)] -enum Error { - Indivisible(ParseIntError), - Remainder(u8), -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Error::Indivisible(_) => write!(f, "could not divide input by three"), - Error::Remainder(remainder) => write!( - f, - "input is not divisible by three, remainder = {}", - remainder - ), - } - } -} - -impl std::error::Error for Error { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - match self { - Error::Indivisible(source) => Some(source), - _ => None, - } - } -} - -fn divisible_by_3(input: &str) -> Result<(), Error> { - input - .parse::() - .map_err(Error::Indivisible) - .map(|v| v % 3) - .and_then(|remainder| { - if remainder == 0 { - Ok(()) - } else { - Err(Error::Remainder(remainder as u8)) - } - }) -} -``` \ No newline at end of file diff --git a/src/docs/map_flatten.txt b/src/docs/map_flatten.txt deleted file mode 100644 index 73c0e51407f28..0000000000000 --- a/src/docs/map_flatten.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for usage of `_.map(_).flatten(_)` on `Iterator` and `Option` - -### Why is this bad? -Readability, this can be written more concisely as -`_.flat_map(_)` for `Iterator` or `_.and_then(_)` for `Option` - -### Example -``` -let vec = vec![vec![1]]; -let opt = Some(5); - -vec.iter().map(|x| x.iter()).flatten(); -opt.map(|x| Some(x * 2)).flatten(); -``` - -Use instead: -``` -vec.iter().flat_map(|x| x.iter()); -opt.and_then(|x| Some(x * 2)); -``` \ No newline at end of file diff --git a/src/docs/map_identity.txt b/src/docs/map_identity.txt deleted file mode 100644 index e2e7af0bed9b1..0000000000000 --- a/src/docs/map_identity.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for instances of `map(f)` where `f` is the identity function. - -### Why is this bad? -It can be written more concisely without the call to `map`. - -### Example -``` -let x = [1, 2, 3]; -let y: Vec<_> = x.iter().map(|x| x).map(|x| 2*x).collect(); -``` -Use instead: -``` -let x = [1, 2, 3]; -let y: Vec<_> = x.iter().map(|x| 2*x).collect(); -``` \ No newline at end of file diff --git a/src/docs/map_unwrap_or.txt b/src/docs/map_unwrap_or.txt deleted file mode 100644 index 485b29f01b103..0000000000000 --- a/src/docs/map_unwrap_or.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for usage of `option.map(_).unwrap_or(_)` or `option.map(_).unwrap_or_else(_)` or -`result.map(_).unwrap_or_else(_)`. - -### Why is this bad? -Readability, these can be written more concisely (resp.) as -`option.map_or(_, _)`, `option.map_or_else(_, _)` and `result.map_or_else(_, _)`. - -### Known problems -The order of the arguments is not in execution order - -### Examples -``` -option.map(|a| a + 1).unwrap_or(0); -result.map(|a| a + 1).unwrap_or_else(some_function); -``` - -Use instead: -``` -option.map_or(0, |a| a + 1); -result.map_or_else(some_function, |a| a + 1); -``` \ No newline at end of file diff --git a/src/docs/match_as_ref.txt b/src/docs/match_as_ref.txt deleted file mode 100644 index 5e5f3d645a4e4..0000000000000 --- a/src/docs/match_as_ref.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for match which is used to add a reference to an -`Option` value. - -### Why is this bad? -Using `as_ref()` or `as_mut()` instead is shorter. - -### Example -``` -let x: Option<()> = None; - -let r: Option<&()> = match x { - None => None, - Some(ref v) => Some(v), -}; -``` - -Use instead: -``` -let x: Option<()> = None; - -let r: Option<&()> = x.as_ref(); -``` \ No newline at end of file diff --git a/src/docs/match_bool.txt b/src/docs/match_bool.txt deleted file mode 100644 index 96f9e1f8b7d1b..0000000000000 --- a/src/docs/match_bool.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks for matches where match expression is a `bool`. It -suggests to replace the expression with an `if...else` block. - -### Why is this bad? -It makes the code less readable. - -### Example -``` -let condition: bool = true; -match condition { - true => foo(), - false => bar(), -} -``` -Use if/else instead: -``` -let condition: bool = true; -if condition { - foo(); -} else { - bar(); -} -``` \ No newline at end of file diff --git a/src/docs/match_like_matches_macro.txt b/src/docs/match_like_matches_macro.txt deleted file mode 100644 index 643e2ddc97ba0..0000000000000 --- a/src/docs/match_like_matches_macro.txt +++ /dev/null @@ -1,32 +0,0 @@ -### What it does -Checks for `match` or `if let` expressions producing a -`bool` that could be written using `matches!` - -### Why is this bad? -Readability and needless complexity. - -### Known problems -This lint falsely triggers, if there are arms with -`cfg` attributes that remove an arm evaluating to `false`. - -### Example -``` -let x = Some(5); - -let a = match x { - Some(0) => true, - _ => false, -}; - -let a = if let Some(0) = x { - true -} else { - false -}; -``` - -Use instead: -``` -let x = Some(5); -let a = matches!(x, Some(0)); -``` \ No newline at end of file diff --git a/src/docs/match_on_vec_items.txt b/src/docs/match_on_vec_items.txt deleted file mode 100644 index 981d18d0f9ed5..0000000000000 --- a/src/docs/match_on_vec_items.txt +++ /dev/null @@ -1,29 +0,0 @@ -### What it does -Checks for `match vec[idx]` or `match vec[n..m]`. - -### Why is this bad? -This can panic at runtime. - -### Example -``` -let arr = vec![0, 1, 2, 3]; -let idx = 1; - -match arr[idx] { - 0 => println!("{}", 0), - 1 => println!("{}", 3), - _ => {}, -} -``` - -Use instead: -``` -let arr = vec![0, 1, 2, 3]; -let idx = 1; - -match arr.get(idx) { - Some(0) => println!("{}", 0), - Some(1) => println!("{}", 3), - _ => {}, -} -``` \ No newline at end of file diff --git a/src/docs/match_overlapping_arm.txt b/src/docs/match_overlapping_arm.txt deleted file mode 100644 index 841c091bd5cad..0000000000000 --- a/src/docs/match_overlapping_arm.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for overlapping match arms. - -### Why is this bad? -It is likely to be an error and if not, makes the code -less obvious. - -### Example -``` -let x = 5; -match x { - 1..=10 => println!("1 ... 10"), - 5..=15 => println!("5 ... 15"), - _ => (), -} -``` \ No newline at end of file diff --git a/src/docs/match_ref_pats.txt b/src/docs/match_ref_pats.txt deleted file mode 100644 index b1d9029950972..0000000000000 --- a/src/docs/match_ref_pats.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Checks for matches where all arms match a reference, -suggesting to remove the reference and deref the matched expression -instead. It also checks for `if let &foo = bar` blocks. - -### Why is this bad? -It just makes the code less readable. That reference -destructuring adds nothing to the code. - -### Example -``` -match x { - &A(ref y) => foo(y), - &B => bar(), - _ => frob(&x), -} -``` - -Use instead: -``` -match *x { - A(ref y) => foo(y), - B => bar(), - _ => frob(x), -} -``` \ No newline at end of file diff --git a/src/docs/match_result_ok.txt b/src/docs/match_result_ok.txt deleted file mode 100644 index eea7c8e00f1bb..0000000000000 --- a/src/docs/match_result_ok.txt +++ /dev/null @@ -1,27 +0,0 @@ -### What it does -Checks for unnecessary `ok()` in `while let`. - -### Why is this bad? -Calling `ok()` in `while let` is unnecessary, instead match -on `Ok(pat)` - -### Example -``` -while let Some(value) = iter.next().ok() { - vec.push(value) -} - -if let Some(value) = iter.next().ok() { - vec.push(value) -} -``` -Use instead: -``` -while let Ok(value) = iter.next() { - vec.push(value) -} - -if let Ok(value) = iter.next() { - vec.push(value) -} -``` \ No newline at end of file diff --git a/src/docs/match_same_arms.txt b/src/docs/match_same_arms.txt deleted file mode 100644 index 14edf12032e0d..0000000000000 --- a/src/docs/match_same_arms.txt +++ /dev/null @@ -1,38 +0,0 @@ -### What it does -Checks for `match` with identical arm bodies. - -### Why is this bad? -This is probably a copy & paste error. If arm bodies -are the same on purpose, you can factor them -[using `|`](https://doc.rust-lang.org/book/patterns.html#multiple-patterns). - -### Known problems -False positive possible with order dependent `match` -(see issue -[#860](https://github.com/rust-lang/rust-clippy/issues/860)). - -### Example -``` -match foo { - Bar => bar(), - Quz => quz(), - Baz => bar(), // <= oops -} -``` - -This should probably be -``` -match foo { - Bar => bar(), - Quz => quz(), - Baz => baz(), // <= fixed -} -``` - -or if the original code was not a typo: -``` -match foo { - Bar | Baz => bar(), // <= shows the intent better - Quz => quz(), -} -``` \ No newline at end of file diff --git a/src/docs/match_single_binding.txt b/src/docs/match_single_binding.txt deleted file mode 100644 index 67ded0bbd5534..0000000000000 --- a/src/docs/match_single_binding.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for useless match that binds to only one value. - -### Why is this bad? -Readability and needless complexity. - -### Known problems - Suggested replacements may be incorrect when `match` -is actually binding temporary value, bringing a 'dropped while borrowed' error. - -### Example -``` -match (a, b) { - (c, d) => { - // useless match - } -} -``` - -Use instead: -``` -let (c, d) = (a, b); -``` \ No newline at end of file diff --git a/src/docs/match_str_case_mismatch.txt b/src/docs/match_str_case_mismatch.txt deleted file mode 100644 index 19e74c2084eae..0000000000000 --- a/src/docs/match_str_case_mismatch.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for `match` expressions modifying the case of a string with non-compliant arms - -### Why is this bad? -The arm is unreachable, which is likely a mistake - -### Example -``` -match &*text.to_ascii_lowercase() { - "foo" => {}, - "Bar" => {}, - _ => {}, -} -``` -Use instead: -``` -match &*text.to_ascii_lowercase() { - "foo" => {}, - "bar" => {}, - _ => {}, -} -``` \ No newline at end of file diff --git a/src/docs/match_wild_err_arm.txt b/src/docs/match_wild_err_arm.txt deleted file mode 100644 index f89b3a23a1ca2..0000000000000 --- a/src/docs/match_wild_err_arm.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for arm which matches all errors with `Err(_)` -and take drastic actions like `panic!`. - -### Why is this bad? -It is generally a bad practice, similar to -catching all exceptions in java with `catch(Exception)` - -### Example -``` -let x: Result = Ok(3); -match x { - Ok(_) => println!("ok"), - Err(_) => panic!("err"), -} -``` \ No newline at end of file diff --git a/src/docs/match_wildcard_for_single_variants.txt b/src/docs/match_wildcard_for_single_variants.txt deleted file mode 100644 index 25559b9ecdcff..0000000000000 --- a/src/docs/match_wildcard_for_single_variants.txt +++ /dev/null @@ -1,27 +0,0 @@ -### What it does -Checks for wildcard enum matches for a single variant. - -### Why is this bad? -New enum variants added by library updates can be missed. - -### Known problems -Suggested replacements may not use correct path to enum -if it's not present in the current scope. - -### Example -``` -match x { - Foo::A => {}, - Foo::B => {}, - _ => {}, -} -``` - -Use instead: -``` -match x { - Foo::A => {}, - Foo::B => {}, - Foo::C => {}, -} -``` \ No newline at end of file diff --git a/src/docs/maybe_infinite_iter.txt b/src/docs/maybe_infinite_iter.txt deleted file mode 100644 index 1204a49b46681..0000000000000 --- a/src/docs/maybe_infinite_iter.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for iteration that may be infinite. - -### Why is this bad? -While there may be places where this is acceptable -(e.g., in event streams), in most cases this is simply an error. - -### Known problems -The code may have a condition to stop iteration, but -this lint is not clever enough to analyze it. - -### Example -``` -let infinite_iter = 0..; -[0..].iter().zip(infinite_iter.take_while(|x| *x > 5)); -``` \ No newline at end of file diff --git a/src/docs/mem_forget.txt b/src/docs/mem_forget.txt deleted file mode 100644 index a6888c48fc33b..0000000000000 --- a/src/docs/mem_forget.txt +++ /dev/null @@ -1,12 +0,0 @@ -### What it does -Checks for usage of `std::mem::forget(t)` where `t` is -`Drop`. - -### Why is this bad? -`std::mem::forget(t)` prevents `t` from running its -destructor, possibly causing leaks. - -### Example -``` -mem::forget(Rc::new(55)) -``` \ No newline at end of file diff --git a/src/docs/mem_replace_option_with_none.txt b/src/docs/mem_replace_option_with_none.txt deleted file mode 100644 index 7f243d1c16565..0000000000000 --- a/src/docs/mem_replace_option_with_none.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for `mem::replace()` on an `Option` with -`None`. - -### Why is this bad? -`Option` already has the method `take()` for -taking its current value (Some(..) or None) and replacing it with -`None`. - -### Example -``` -use std::mem; - -let mut an_option = Some(0); -let replaced = mem::replace(&mut an_option, None); -``` -Is better expressed with: -``` -let mut an_option = Some(0); -let taken = an_option.take(); -``` \ No newline at end of file diff --git a/src/docs/mem_replace_with_default.txt b/src/docs/mem_replace_with_default.txt deleted file mode 100644 index 24e0913a30c92..0000000000000 --- a/src/docs/mem_replace_with_default.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for `std::mem::replace` on a value of type -`T` with `T::default()`. - -### Why is this bad? -`std::mem` module already has the method `take` to -take the current value and replace it with the default value of that type. - -### Example -``` -let mut text = String::from("foo"); -let replaced = std::mem::replace(&mut text, String::default()); -``` -Is better expressed with: -``` -let mut text = String::from("foo"); -let taken = std::mem::take(&mut text); -``` \ No newline at end of file diff --git a/src/docs/mem_replace_with_uninit.txt b/src/docs/mem_replace_with_uninit.txt deleted file mode 100644 index 0bb483668abc1..0000000000000 --- a/src/docs/mem_replace_with_uninit.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks for `mem::replace(&mut _, mem::uninitialized())` -and `mem::replace(&mut _, mem::zeroed())`. - -### Why is this bad? -This will lead to undefined behavior even if the -value is overwritten later, because the uninitialized value may be -observed in the case of a panic. - -### Example -``` -use std::mem; - -#[allow(deprecated, invalid_value)] -fn myfunc (v: &mut Vec) { - let taken_v = unsafe { mem::replace(v, mem::uninitialized()) }; - let new_v = may_panic(taken_v); // undefined behavior on panic - mem::forget(mem::replace(v, new_v)); -} -``` - -The [take_mut](https://docs.rs/take_mut) crate offers a sound solution, -at the cost of either lazily creating a replacement value or aborting -on panic, to ensure that the uninitialized value cannot be observed. \ No newline at end of file diff --git a/src/docs/min_max.txt b/src/docs/min_max.txt deleted file mode 100644 index 6acf0f932e986..0000000000000 --- a/src/docs/min_max.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for expressions where `std::cmp::min` and `max` are -used to clamp values, but switched so that the result is constant. - -### Why is this bad? -This is in all probability not the intended outcome. At -the least it hurts readability of the code. - -### Example -``` -min(0, max(100, x)) - -// or - -x.max(100).min(0) -``` -It will always be equal to `0`. Probably the author meant to clamp the value -between 0 and 100, but has erroneously swapped `min` and `max`. \ No newline at end of file diff --git a/src/docs/mismatched_target_os.txt b/src/docs/mismatched_target_os.txt deleted file mode 100644 index 51e5ec6e7c5c4..0000000000000 --- a/src/docs/mismatched_target_os.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks for cfg attributes having operating systems used in target family position. - -### Why is this bad? -The configuration option will not be recognised and the related item will not be included -by the conditional compilation engine. - -### Example -``` -#[cfg(linux)] -fn conditional() { } -``` - -Use instead: -``` -#[cfg(target_os = "linux")] -fn conditional() { } - -// or - -#[cfg(unix)] -fn conditional() { } -``` -Check the [Rust Reference](https://doc.rust-lang.org/reference/conditional-compilation.html#target_os) for more details. \ No newline at end of file diff --git a/src/docs/mismatching_type_param_order.txt b/src/docs/mismatching_type_param_order.txt deleted file mode 100644 index ffc7f32d0aad8..0000000000000 --- a/src/docs/mismatching_type_param_order.txt +++ /dev/null @@ -1,33 +0,0 @@ -### What it does -Checks for type parameters which are positioned inconsistently between -a type definition and impl block. Specifically, a parameter in an impl -block which has the same name as a parameter in the type def, but is in -a different place. - -### Why is this bad? -Type parameters are determined by their position rather than name. -Naming type parameters inconsistently may cause you to refer to the -wrong type parameter. - -### Limitations -This lint only applies to impl blocks with simple generic params, e.g. -`A`. If there is anything more complicated, such as a tuple, it will be -ignored. - -### Example -``` -struct Foo { - x: A, - y: B, -} -// inside the impl, B refers to Foo::A -impl Foo {} -``` -Use instead: -``` -struct Foo { - x: A, - y: B, -} -impl Foo {} -``` \ No newline at end of file diff --git a/src/docs/misrefactored_assign_op.txt b/src/docs/misrefactored_assign_op.txt deleted file mode 100644 index 3d691fe4178b5..0000000000000 --- a/src/docs/misrefactored_assign_op.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Checks for `a op= a op b` or `a op= b op a` patterns. - -### Why is this bad? -Most likely these are bugs where one meant to write `a -op= b`. - -### Known problems -Clippy cannot know for sure if `a op= a op b` should have -been `a = a op a op b` or `a = a op b`/`a op= b`. Therefore, it suggests both. -If `a op= a op b` is really the correct behavior it should be -written as `a = a op a op b` as it's less confusing. - -### Example -``` -let mut a = 5; -let b = 2; -// ... -a += a + b; -``` \ No newline at end of file diff --git a/src/docs/missing_const_for_fn.txt b/src/docs/missing_const_for_fn.txt deleted file mode 100644 index 067614d4c46bf..0000000000000 --- a/src/docs/missing_const_for_fn.txt +++ /dev/null @@ -1,40 +0,0 @@ -### What it does -Suggests the use of `const` in functions and methods where possible. - -### Why is this bad? -Not having the function const prevents callers of the function from being const as well. - -### Known problems -Const functions are currently still being worked on, with some features only being available -on nightly. This lint does not consider all edge cases currently and the suggestions may be -incorrect if you are using this lint on stable. - -Also, the lint only runs one pass over the code. Consider these two non-const functions: - -``` -fn a() -> i32 { - 0 -} -fn b() -> i32 { - a() -} -``` - -When running Clippy, the lint will only suggest to make `a` const, because `b` at this time -can't be const as it calls a non-const function. Making `a` const and running Clippy again, -will suggest to make `b` const, too. - -### Example -``` -fn new() -> Self { - Self { random_number: 42 } -} -``` - -Could be a const fn: - -``` -const fn new() -> Self { - Self { random_number: 42 } -} -``` \ No newline at end of file diff --git a/src/docs/missing_docs_in_private_items.txt b/src/docs/missing_docs_in_private_items.txt deleted file mode 100644 index 5d37505bb1716..0000000000000 --- a/src/docs/missing_docs_in_private_items.txt +++ /dev/null @@ -1,9 +0,0 @@ -### What it does -Warns if there is missing doc for any documentable item -(public or private). - -### Why is this bad? -Doc is good. *rustc* has a `MISSING_DOCS` -allowed-by-default lint for -public members, but has no way to enforce documentation of private items. -This lint fixes that. \ No newline at end of file diff --git a/src/docs/missing_enforced_import_renames.txt b/src/docs/missing_enforced_import_renames.txt deleted file mode 100644 index 8f4649bd5923d..0000000000000 --- a/src/docs/missing_enforced_import_renames.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for imports that do not rename the item as specified -in the `enforce-import-renames` config option. - -### Why is this bad? -Consistency is important, if a project has defined import -renames they should be followed. More practically, some item names are too -vague outside of their defining scope this can enforce a more meaningful naming. - -### Example -An example clippy.toml configuration: -``` -enforced-import-renames = [ { path = "serde_json::Value", rename = "JsonValue" }] -``` - -``` -use serde_json::Value; -``` -Use instead: -``` -use serde_json::Value as JsonValue; -``` \ No newline at end of file diff --git a/src/docs/missing_errors_doc.txt b/src/docs/missing_errors_doc.txt deleted file mode 100644 index 028778d85aeba..0000000000000 --- a/src/docs/missing_errors_doc.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks the doc comments of publicly visible functions that -return a `Result` type and warns if there is no `# Errors` section. - -### Why is this bad? -Documenting the type of errors that can be returned from a -function can help callers write code to handle the errors appropriately. - -### Examples -Since the following function returns a `Result` it has an `# Errors` section in -its doc comment: - -``` -/// # Errors -/// -/// Will return `Err` if `filename` does not exist or the user does not have -/// permission to read it. -pub fn read(filename: String) -> io::Result { - unimplemented!(); -} -``` \ No newline at end of file diff --git a/src/docs/missing_inline_in_public_items.txt b/src/docs/missing_inline_in_public_items.txt deleted file mode 100644 index d90c50fe7f9e8..0000000000000 --- a/src/docs/missing_inline_in_public_items.txt +++ /dev/null @@ -1,45 +0,0 @@ -### What it does -It lints if an exported function, method, trait method with default impl, -or trait method impl is not `#[inline]`. - -### Why is this bad? -In general, it is not. Functions can be inlined across -crates when that's profitable as long as any form of LTO is used. When LTO is disabled, -functions that are not `#[inline]` cannot be inlined across crates. Certain types of crates -might intend for most of the methods in their public API to be able to be inlined across -crates even when LTO is disabled. For these types of crates, enabling this lint might make -sense. It allows the crate to require all exported methods to be `#[inline]` by default, and -then opt out for specific methods where this might not make sense. - -### Example -``` -pub fn foo() {} // missing #[inline] -fn ok() {} // ok -#[inline] pub fn bar() {} // ok -#[inline(always)] pub fn baz() {} // ok - -pub trait Bar { - fn bar(); // ok - fn def_bar() {} // missing #[inline] -} - -struct Baz; -impl Baz { - fn private() {} // ok -} - -impl Bar for Baz { - fn bar() {} // ok - Baz is not exported -} - -pub struct PubBaz; -impl PubBaz { - fn private() {} // ok - pub fn not_private() {} // missing #[inline] -} - -impl Bar for PubBaz { - fn bar() {} // missing #[inline] - fn def_bar() {} // missing #[inline] -} -``` \ No newline at end of file diff --git a/src/docs/missing_panics_doc.txt b/src/docs/missing_panics_doc.txt deleted file mode 100644 index e5e39a824519b..0000000000000 --- a/src/docs/missing_panics_doc.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks the doc comments of publicly visible functions that -may panic and warns if there is no `# Panics` section. - -### Why is this bad? -Documenting the scenarios in which panicking occurs -can help callers who do not want to panic to avoid those situations. - -### Examples -Since the following function may panic it has a `# Panics` section in -its doc comment: - -``` -/// # Panics -/// -/// Will panic if y is 0 -pub fn divide_by(x: i32, y: i32) -> i32 { - if y == 0 { - panic!("Cannot divide by 0") - } else { - x / y - } -} -``` \ No newline at end of file diff --git a/src/docs/missing_safety_doc.txt b/src/docs/missing_safety_doc.txt deleted file mode 100644 index 6492eb84f63bf..0000000000000 --- a/src/docs/missing_safety_doc.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Checks for the doc comments of publicly visible -unsafe functions and warns if there is no `# Safety` section. - -### Why is this bad? -Unsafe functions should document their safety -preconditions, so that users can be sure they are using them safely. - -### Examples -``` -/// This function should really be documented -pub unsafe fn start_apocalypse(u: &mut Universe) { - unimplemented!(); -} -``` - -At least write a line about safety: - -``` -/// # Safety -/// -/// This function should not be called before the horsemen are ready. -pub unsafe fn start_apocalypse(u: &mut Universe) { - unimplemented!(); -} -``` \ No newline at end of file diff --git a/src/docs/missing_spin_loop.txt b/src/docs/missing_spin_loop.txt deleted file mode 100644 index 3a06a91d71835..0000000000000 --- a/src/docs/missing_spin_loop.txt +++ /dev/null @@ -1,27 +0,0 @@ -### What it does -Check for empty spin loops - -### Why is this bad? -The loop body should have something like `thread::park()` or at least -`std::hint::spin_loop()` to avoid needlessly burning cycles and conserve -energy. Perhaps even better use an actual lock, if possible. - -### Known problems -This lint doesn't currently trigger on `while let` or -`loop { match .. { .. } }` loops, which would be considered idiomatic in -combination with e.g. `AtomicBool::compare_exchange_weak`. - -### Example - -``` -use core::sync::atomic::{AtomicBool, Ordering}; -let b = AtomicBool::new(true); -// give a ref to `b` to another thread,wait for it to become false -while b.load(Ordering::Acquire) {}; -``` -Use instead: -``` -while b.load(Ordering::Acquire) { - std::hint::spin_loop() -} -``` \ No newline at end of file diff --git a/src/docs/missing_trait_methods.txt b/src/docs/missing_trait_methods.txt deleted file mode 100644 index 788ad764f8c39..0000000000000 --- a/src/docs/missing_trait_methods.txt +++ /dev/null @@ -1,40 +0,0 @@ -### What it does -Checks if a provided method is used implicitly by a trait -implementation. A usage example would be a wrapper where every method -should perform some operation before delegating to the inner type's -implemenation. - -This lint should typically be enabled on a specific trait `impl` item -rather than globally. - -### Why is this bad? -Indicates that a method is missing. - -### Example -``` -trait Trait { - fn required(); - - fn provided() {} -} - -#[warn(clippy::missing_trait_methods)] -impl Trait for Type { - fn required() { /* ... */ } -} -``` -Use instead: -``` -trait Trait { - fn required(); - - fn provided() {} -} - -#[warn(clippy::missing_trait_methods)] -impl Trait for Type { - fn required() { /* ... */ } - - fn provided() { /* ... */ } -} -``` \ No newline at end of file diff --git a/src/docs/mistyped_literal_suffixes.txt b/src/docs/mistyped_literal_suffixes.txt deleted file mode 100644 index 1760fcbfeacc4..0000000000000 --- a/src/docs/mistyped_literal_suffixes.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Warns for mistyped suffix in literals - -### Why is this bad? -This is most probably a typo - -### Known problems -- Does not match on integers too large to fit in the corresponding unsigned type -- Does not match on `_127` since that is a valid grouping for decimal and octal numbers - -### Example -``` -`2_32` => `2_i32` -`250_8 => `250_u8` -``` \ No newline at end of file diff --git a/src/docs/mixed_case_hex_literals.txt b/src/docs/mixed_case_hex_literals.txt deleted file mode 100644 index d2d01e0c98eb4..0000000000000 --- a/src/docs/mixed_case_hex_literals.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Warns on hexadecimal literals with mixed-case letter -digits. - -### Why is this bad? -It looks confusing. - -### Example -``` -0x1a9BAcD -``` - -Use instead: -``` -0x1A9BACD -``` \ No newline at end of file diff --git a/src/docs/mixed_read_write_in_expression.txt b/src/docs/mixed_read_write_in_expression.txt deleted file mode 100644 index 02d1c5d052524..0000000000000 --- a/src/docs/mixed_read_write_in_expression.txt +++ /dev/null @@ -1,32 +0,0 @@ -### What it does -Checks for a read and a write to the same variable where -whether the read occurs before or after the write depends on the evaluation -order of sub-expressions. - -### Why is this bad? -It is often confusing to read. As described [here](https://doc.rust-lang.org/reference/expressions.html?highlight=subexpression#evaluation-order-of-operands), -the operands of these expressions are evaluated before applying the effects of the expression. - -### Known problems -Code which intentionally depends on the evaluation -order, or which is correct for any evaluation order. - -### Example -``` -let mut x = 0; - -let a = { - x = 1; - 1 -} + x; -// Unclear whether a is 1 or 2. -``` - -Use instead: -``` -let tmp = { - x = 1; - 1 -}; -let a = tmp + x; -``` \ No newline at end of file diff --git a/src/docs/mod_module_files.txt b/src/docs/mod_module_files.txt deleted file mode 100644 index 95bca583afd3c..0000000000000 --- a/src/docs/mod_module_files.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks that module layout uses only self named module files, bans `mod.rs` files. - -### Why is this bad? -Having multiple module layout styles in a project can be confusing. - -### Example -``` -src/ - stuff/ - stuff_files.rs - mod.rs - lib.rs -``` -Use instead: -``` -src/ - stuff/ - stuff_files.rs - stuff.rs - lib.rs -``` \ No newline at end of file diff --git a/src/docs/module_inception.txt b/src/docs/module_inception.txt deleted file mode 100644 index d80a1b8d8fe57..0000000000000 --- a/src/docs/module_inception.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks for modules that have the same name as their -parent module - -### Why is this bad? -A typical beginner mistake is to have `mod foo;` and -again `mod foo { .. -}` in `foo.rs`. -The expectation is that items inside the inner `mod foo { .. }` are then -available -through `foo::x`, but they are only available through -`foo::foo::x`. -If this is done on purpose, it would be better to choose a more -representative module name. - -### Example -``` -// lib.rs -mod foo; -// foo.rs -mod foo { - ... -} -``` \ No newline at end of file diff --git a/src/docs/module_name_repetitions.txt b/src/docs/module_name_repetitions.txt deleted file mode 100644 index 3bc05d02780ca..0000000000000 --- a/src/docs/module_name_repetitions.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Detects type names that are prefixed or suffixed by the -containing module's name. - -### Why is this bad? -It requires the user to type the module name twice. - -### Example -``` -mod cake { - struct BlackForestCake; -} -``` - -Use instead: -``` -mod cake { - struct BlackForest; -} -``` \ No newline at end of file diff --git a/src/docs/modulo_arithmetic.txt b/src/docs/modulo_arithmetic.txt deleted file mode 100644 index ff7296f3c5b8d..0000000000000 --- a/src/docs/modulo_arithmetic.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for modulo arithmetic. - -### Why is this bad? -The results of modulo (%) operation might differ -depending on the language, when negative numbers are involved. -If you interop with different languages it might be beneficial -to double check all places that use modulo arithmetic. - -For example, in Rust `17 % -3 = 2`, but in Python `17 % -3 = -1`. - -### Example -``` -let x = -17 % 3; -``` \ No newline at end of file diff --git a/src/docs/modulo_one.txt b/src/docs/modulo_one.txt deleted file mode 100644 index bc8f95b0be693..0000000000000 --- a/src/docs/modulo_one.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for getting the remainder of a division by one or minus -one. - -### Why is this bad? -The result for a divisor of one can only ever be zero; for -minus one it can cause panic/overflow (if the left operand is the minimal value of -the respective integer type) or results in zero. No one will write such code -deliberately, unless trying to win an Underhanded Rust Contest. Even for that -contest, it's probably a bad idea. Use something more underhanded. - -### Example -``` -let a = x % 1; -let a = x % -1; -``` \ No newline at end of file diff --git a/src/docs/multi_assignments.txt b/src/docs/multi_assignments.txt deleted file mode 100644 index ed1f1b420cbd3..0000000000000 --- a/src/docs/multi_assignments.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for nested assignments. - -### Why is this bad? -While this is in most cases already a type mismatch, -the result of an assignment being `()` can throw off people coming from languages like python or C, -where such assignments return a copy of the assigned value. - -### Example -``` -a = b = 42; -``` -Use instead: -``` -b = 42; -a = b; -``` \ No newline at end of file diff --git a/src/docs/multiple_crate_versions.txt b/src/docs/multiple_crate_versions.txt deleted file mode 100644 index cf2d2c6abee3e..0000000000000 --- a/src/docs/multiple_crate_versions.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks to see if multiple versions of a crate are being -used. - -### Why is this bad? -This bloats the size of targets, and can lead to -confusing error messages when structs or traits are used interchangeably -between different versions of a crate. - -### Known problems -Because this can be caused purely by the dependencies -themselves, it's not always possible to fix this issue. - -### Example -``` -[dependencies] -ctrlc = "=3.1.0" -ansi_term = "=0.11.0" -``` \ No newline at end of file diff --git a/src/docs/multiple_inherent_impl.txt b/src/docs/multiple_inherent_impl.txt deleted file mode 100644 index 9d42286560cef..0000000000000 --- a/src/docs/multiple_inherent_impl.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Checks for multiple inherent implementations of a struct - -### Why is this bad? -Splitting the implementation of a type makes the code harder to navigate. - -### Example -``` -struct X; -impl X { - fn one() {} -} -impl X { - fn other() {} -} -``` - -Could be written: - -``` -struct X; -impl X { - fn one() {} - fn other() {} -} -``` \ No newline at end of file diff --git a/src/docs/must_use_candidate.txt b/src/docs/must_use_candidate.txt deleted file mode 100644 index 70890346fe6b8..0000000000000 --- a/src/docs/must_use_candidate.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for public functions that have no -`#[must_use]` attribute, but return something not already marked -must-use, have no mutable arg and mutate no statics. - -### Why is this bad? -Not bad at all, this lint just shows places where -you could add the attribute. - -### Known problems -The lint only checks the arguments for mutable -types without looking if they are actually changed. On the other hand, -it also ignores a broad range of potentially interesting side effects, -because we cannot decide whether the programmer intends the function to -be called for the side effect or the result. Expect many false -positives. At least we don't lint if the result type is unit or already -`#[must_use]`. - -### Examples -``` -// this could be annotated with `#[must_use]`. -fn id(t: T) -> T { t } -``` \ No newline at end of file diff --git a/src/docs/must_use_unit.txt b/src/docs/must_use_unit.txt deleted file mode 100644 index cabbb23f8651e..0000000000000 --- a/src/docs/must_use_unit.txt +++ /dev/null @@ -1,13 +0,0 @@ -### What it does -Checks for a `#[must_use]` attribute on -unit-returning functions and methods. - -### Why is this bad? -Unit values are useless. The attribute is likely -a remnant of a refactoring that removed the return type. - -### Examples -``` -#[must_use] -fn useless() { } -``` \ No newline at end of file diff --git a/src/docs/mut_from_ref.txt b/src/docs/mut_from_ref.txt deleted file mode 100644 index cc1da12549a5d..0000000000000 --- a/src/docs/mut_from_ref.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -This lint checks for functions that take immutable references and return -mutable ones. This will not trigger if no unsafe code exists as there -are multiple safe functions which will do this transformation - -To be on the conservative side, if there's at least one mutable -reference with the output lifetime, this lint will not trigger. - -### Why is this bad? -Creating a mutable reference which can be repeatably derived from an -immutable reference is unsound as it allows creating multiple live -mutable references to the same object. - -This [error](https://github.com/rust-lang/rust/issues/39465) actually -lead to an interim Rust release 1.15.1. - -### Known problems -This pattern is used by memory allocators to allow allocating multiple -objects while returning mutable references to each one. So long as -different mutable references are returned each time such a function may -be safe. - -### Example -``` -fn foo(&Foo) -> &mut Bar { .. } -``` \ No newline at end of file diff --git a/src/docs/mut_mut.txt b/src/docs/mut_mut.txt deleted file mode 100644 index 0bd34dd24b26a..0000000000000 --- a/src/docs/mut_mut.txt +++ /dev/null @@ -1,12 +0,0 @@ -### What it does -Checks for instances of `mut mut` references. - -### Why is this bad? -Multiple `mut`s don't add anything meaningful to the -source. This is either a copy'n'paste error, or it shows a fundamental -misunderstanding of references. - -### Example -``` -let x = &mut &mut y; -``` \ No newline at end of file diff --git a/src/docs/mut_mutex_lock.txt b/src/docs/mut_mutex_lock.txt deleted file mode 100644 index 5e9ad8a3f176a..0000000000000 --- a/src/docs/mut_mutex_lock.txt +++ /dev/null @@ -1,29 +0,0 @@ -### What it does -Checks for `&mut Mutex::lock` calls - -### Why is this bad? -`Mutex::lock` is less efficient than -calling `Mutex::get_mut`. In addition you also have a statically -guarantee that the mutex isn't locked, instead of just a runtime -guarantee. - -### Example -``` -use std::sync::{Arc, Mutex}; - -let mut value_rc = Arc::new(Mutex::new(42_u8)); -let value_mutex = Arc::get_mut(&mut value_rc).unwrap(); - -let mut value = value_mutex.lock().unwrap(); -*value += 1; -``` -Use instead: -``` -use std::sync::{Arc, Mutex}; - -let mut value_rc = Arc::new(Mutex::new(42_u8)); -let value_mutex = Arc::get_mut(&mut value_rc).unwrap(); - -let value = value_mutex.get_mut().unwrap(); -*value += 1; -``` \ No newline at end of file diff --git a/src/docs/mut_range_bound.txt b/src/docs/mut_range_bound.txt deleted file mode 100644 index e9c38a543b146..0000000000000 --- a/src/docs/mut_range_bound.txt +++ /dev/null @@ -1,29 +0,0 @@ -### What it does -Checks for loops which have a range bound that is a mutable variable - -### Why is this bad? -One might think that modifying the mutable variable changes the loop bounds - -### Known problems -False positive when mutation is followed by a `break`, but the `break` is not immediately -after the mutation: - -``` -let mut x = 5; -for _ in 0..x { - x += 1; // x is a range bound that is mutated - ..; // some other expression - break; // leaves the loop, so mutation is not an issue -} -``` - -False positive on nested loops ([#6072](https://github.com/rust-lang/rust-clippy/issues/6072)) - -### Example -``` -let mut foo = 42; -for i in 0..foo { - foo -= 1; - println!("{}", i); // prints numbers from 0 to 42, not 0 to 21 -} -``` \ No newline at end of file diff --git a/src/docs/mutable_key_type.txt b/src/docs/mutable_key_type.txt deleted file mode 100644 index 15fe34f2bb5ec..0000000000000 --- a/src/docs/mutable_key_type.txt +++ /dev/null @@ -1,61 +0,0 @@ -### What it does -Checks for sets/maps with mutable key types. - -### Why is this bad? -All of `HashMap`, `HashSet`, `BTreeMap` and -`BtreeSet` rely on either the hash or the order of keys be unchanging, -so having types with interior mutability is a bad idea. - -### Known problems - -#### False Positives -It's correct to use a struct that contains interior mutability as a key, when its -implementation of `Hash` or `Ord` doesn't access any of the interior mutable types. -However, this lint is unable to recognize this, so it will often cause false positives in -theses cases. The `bytes` crate is a great example of this. - -#### False Negatives -For custom `struct`s/`enum`s, this lint is unable to check for interior mutability behind -indirection. For example, `struct BadKey<'a>(&'a Cell)` will be seen as immutable -and cause a false negative if its implementation of `Hash`/`Ord` accesses the `Cell`. - -This lint does check a few cases for indirection. Firstly, using some standard library -types (`Option`, `Result`, `Box`, `Rc`, `Arc`, `Vec`, `VecDeque`, `BTreeMap` and -`BTreeSet`) directly as keys (e.g. in `HashMap>, ()>`) **will** trigger the -lint, because the impls of `Hash`/`Ord` for these types directly call `Hash`/`Ord` on their -contained type. - -Secondly, the implementations of `Hash` and `Ord` for raw pointers (`*const T` or `*mut T`) -apply only to the **address** of the contained value. Therefore, interior mutability -behind raw pointers (e.g. in `HashSet<*mut Cell>`) can't impact the value of `Hash` -or `Ord`, and therefore will not trigger this link. For more info, see issue -[#6745](https://github.com/rust-lang/rust-clippy/issues/6745). - -### Example -``` -use std::cmp::{PartialEq, Eq}; -use std::collections::HashSet; -use std::hash::{Hash, Hasher}; -use std::sync::atomic::AtomicUsize; - -struct Bad(AtomicUsize); -impl PartialEq for Bad { - fn eq(&self, rhs: &Self) -> bool { - .. -; unimplemented!(); - } -} - -impl Eq for Bad {} - -impl Hash for Bad { - fn hash(&self, h: &mut H) { - .. -; unimplemented!(); - } -} - -fn main() { - let _: HashSet = HashSet::new(); -} -``` \ No newline at end of file diff --git a/src/docs/mutex_atomic.txt b/src/docs/mutex_atomic.txt deleted file mode 100644 index 062ac8b323b74..0000000000000 --- a/src/docs/mutex_atomic.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for usages of `Mutex` where an atomic will do. - -### Why is this bad? -Using a mutex just to make access to a plain bool or -reference sequential is shooting flies with cannons. -`std::sync::atomic::AtomicBool` and `std::sync::atomic::AtomicPtr` are leaner and -faster. - -### Known problems -This lint cannot detect if the mutex is actually used -for waiting before a critical section. - -### Example -``` -let x = Mutex::new(&y); -``` - -Use instead: -``` -let x = AtomicBool::new(y); -``` \ No newline at end of file diff --git a/src/docs/mutex_integer.txt b/src/docs/mutex_integer.txt deleted file mode 100644 index f9dbdfb904c9a..0000000000000 --- a/src/docs/mutex_integer.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for usages of `Mutex` where `X` is an integral -type. - -### Why is this bad? -Using a mutex just to make access to a plain integer -sequential is -shooting flies with cannons. `std::sync::atomic::AtomicUsize` is leaner and faster. - -### Known problems -This lint cannot detect if the mutex is actually used -for waiting before a critical section. - -### Example -``` -let x = Mutex::new(0usize); -``` - -Use instead: -``` -let x = AtomicUsize::new(0usize); -``` \ No newline at end of file diff --git a/src/docs/naive_bytecount.txt b/src/docs/naive_bytecount.txt deleted file mode 100644 index 24659dc79ae71..0000000000000 --- a/src/docs/naive_bytecount.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for naive byte counts - -### Why is this bad? -The [`bytecount`](https://crates.io/crates/bytecount) -crate has methods to count your bytes faster, especially for large slices. - -### Known problems -If you have predominantly small slices, the -`bytecount::count(..)` method may actually be slower. However, if you can -ensure that less than 2³²-1 matches arise, the `naive_count_32(..)` can be -faster in those cases. - -### Example -``` -let count = vec.iter().filter(|x| **x == 0u8).count(); -``` - -Use instead: -``` -let count = bytecount::count(&vec, 0u8); -``` \ No newline at end of file diff --git a/src/docs/needless_arbitrary_self_type.txt b/src/docs/needless_arbitrary_self_type.txt deleted file mode 100644 index 8216a3a3fb6e5..0000000000000 --- a/src/docs/needless_arbitrary_self_type.txt +++ /dev/null @@ -1,44 +0,0 @@ -### What it does -The lint checks for `self` in fn parameters that -specify the `Self`-type explicitly -### Why is this bad? -Increases the amount and decreases the readability of code - -### Example -``` -enum ValType { - I32, - I64, - F32, - F64, -} - -impl ValType { - pub fn bytes(self: Self) -> usize { - match self { - Self::I32 | Self::F32 => 4, - Self::I64 | Self::F64 => 8, - } - } -} -``` - -Could be rewritten as - -``` -enum ValType { - I32, - I64, - F32, - F64, -} - -impl ValType { - pub fn bytes(self) -> usize { - match self { - Self::I32 | Self::F32 => 4, - Self::I64 | Self::F64 => 8, - } - } -} -``` \ No newline at end of file diff --git a/src/docs/needless_bitwise_bool.txt b/src/docs/needless_bitwise_bool.txt deleted file mode 100644 index fcd7b730aaae0..0000000000000 --- a/src/docs/needless_bitwise_bool.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for uses of bitwise and/or operators between booleans, where performance may be improved by using -a lazy and. - -### Why is this bad? -The bitwise operators do not support short-circuiting, so it may hinder code performance. -Additionally, boolean logic "masked" as bitwise logic is not caught by lints like `unnecessary_fold` - -### Known problems -This lint evaluates only when the right side is determined to have no side effects. At this time, that -determination is quite conservative. - -### Example -``` -let (x,y) = (true, false); -if x & !y {} // where both x and y are booleans -``` -Use instead: -``` -let (x,y) = (true, false); -if x && !y {} -``` \ No newline at end of file diff --git a/src/docs/needless_bool.txt b/src/docs/needless_bool.txt deleted file mode 100644 index b5c78871f14f6..0000000000000 --- a/src/docs/needless_bool.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Checks for expressions of the form `if c { true } else { -false }` (or vice versa) and suggests using the condition directly. - -### Why is this bad? -Redundant code. - -### Known problems -Maybe false positives: Sometimes, the two branches are -painstakingly documented (which we, of course, do not detect), so they *may* -have some value. Even then, the documentation can be rewritten to match the -shorter code. - -### Example -``` -if x { - false -} else { - true -} -``` - -Use instead: -``` -!x -``` \ No newline at end of file diff --git a/src/docs/needless_borrow.txt b/src/docs/needless_borrow.txt deleted file mode 100644 index 4debcf473723a..0000000000000 --- a/src/docs/needless_borrow.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for address of operations (`&`) that are going to -be dereferenced immediately by the compiler. - -### Why is this bad? -Suggests that the receiver of the expression borrows -the expression. - -### Example -``` -fn fun(_a: &i32) {} - -let x: &i32 = &&&&&&5; -fun(&x); -``` - -Use instead: -``` -let x: &i32 = &5; -fun(x); -``` \ No newline at end of file diff --git a/src/docs/needless_borrowed_reference.txt b/src/docs/needless_borrowed_reference.txt deleted file mode 100644 index 152459ba1c9d7..0000000000000 --- a/src/docs/needless_borrowed_reference.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for bindings that needlessly destructure a reference and borrow the inner -value with `&ref`. - -### Why is this bad? -This pattern has no effect in almost all cases. - -### Example -``` -let mut v = Vec::::new(); -v.iter_mut().filter(|&ref a| a.is_empty()); - -if let &[ref first, ref second] = v.as_slice() {} -``` - -Use instead: -``` -let mut v = Vec::::new(); -v.iter_mut().filter(|a| a.is_empty()); - -if let [first, second] = v.as_slice() {} -``` \ No newline at end of file diff --git a/src/docs/needless_collect.txt b/src/docs/needless_collect.txt deleted file mode 100644 index 275c39afc9df1..0000000000000 --- a/src/docs/needless_collect.txt +++ /dev/null @@ -1,14 +0,0 @@ -### What it does -Checks for functions collecting an iterator when collect -is not needed. - -### Why is this bad? -`collect` causes the allocation of a new data structure, -when this allocation may not be needed. - -### Example -``` -let len = iterator.clone().collect::>().len(); -// should be -let len = iterator.count(); -``` \ No newline at end of file diff --git a/src/docs/needless_continue.txt b/src/docs/needless_continue.txt deleted file mode 100644 index 2cee621c1af03..0000000000000 --- a/src/docs/needless_continue.txt +++ /dev/null @@ -1,61 +0,0 @@ -### What it does -The lint checks for `if`-statements appearing in loops -that contain a `continue` statement in either their main blocks or their -`else`-blocks, when omitting the `else`-block possibly with some -rearrangement of code can make the code easier to understand. - -### Why is this bad? -Having explicit `else` blocks for `if` statements -containing `continue` in their THEN branch adds unnecessary branching and -nesting to the code. Having an else block containing just `continue` can -also be better written by grouping the statements following the whole `if` -statement within the THEN block and omitting the else block completely. - -### Example -``` -while condition() { - update_condition(); - if x { - // ... - } else { - continue; - } - println!("Hello, world"); -} -``` - -Could be rewritten as - -``` -while condition() { - update_condition(); - if x { - // ... - println!("Hello, world"); - } -} -``` - -As another example, the following code - -``` -loop { - if waiting() { - continue; - } else { - // Do something useful - } - # break; -} -``` -Could be rewritten as - -``` -loop { - if waiting() { - continue; - } - // Do something useful - # break; -} -``` \ No newline at end of file diff --git a/src/docs/needless_doctest_main.txt b/src/docs/needless_doctest_main.txt deleted file mode 100644 index 8f91a7baa7151..0000000000000 --- a/src/docs/needless_doctest_main.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for `fn main() { .. }` in doctests - -### Why is this bad? -The test can be shorter (and likely more readable) -if the `fn main()` is left implicit. - -### Examples -``` -/// An example of a doctest with a `main()` function -/// -/// # Examples -/// -/// ``` -/// fn main() { -/// // this needs not be in an `fn` -/// } -/// ``` -fn needless_main() { - unimplemented!(); -} -``` \ No newline at end of file diff --git a/src/docs/needless_for_each.txt b/src/docs/needless_for_each.txt deleted file mode 100644 index 9ae6dd360c8f7..0000000000000 --- a/src/docs/needless_for_each.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks for usage of `for_each` that would be more simply written as a -`for` loop. - -### Why is this bad? -`for_each` may be used after applying iterator transformers like -`filter` for better readability and performance. It may also be used to fit a simple -operation on one line. -But when none of these apply, a simple `for` loop is more idiomatic. - -### Example -``` -let v = vec![0, 1, 2]; -v.iter().for_each(|elem| { - println!("{}", elem); -}) -``` -Use instead: -``` -let v = vec![0, 1, 2]; -for elem in v.iter() { - println!("{}", elem); -} -``` \ No newline at end of file diff --git a/src/docs/needless_late_init.txt b/src/docs/needless_late_init.txt deleted file mode 100644 index 9e7bbcea99825..0000000000000 --- a/src/docs/needless_late_init.txt +++ /dev/null @@ -1,42 +0,0 @@ -### What it does -Checks for late initializations that can be replaced by a `let` statement -with an initializer. - -### Why is this bad? -Assigning in the `let` statement is less repetitive. - -### Example -``` -let a; -a = 1; - -let b; -match 3 { - 0 => b = "zero", - 1 => b = "one", - _ => b = "many", -} - -let c; -if true { - c = 1; -} else { - c = -1; -} -``` -Use instead: -``` -let a = 1; - -let b = match 3 { - 0 => "zero", - 1 => "one", - _ => "many", -}; - -let c = if true { - 1 -} else { - -1 -}; -``` \ No newline at end of file diff --git a/src/docs/needless_lifetimes.txt b/src/docs/needless_lifetimes.txt deleted file mode 100644 index b280caa66b583..0000000000000 --- a/src/docs/needless_lifetimes.txt +++ /dev/null @@ -1,29 +0,0 @@ -### What it does -Checks for lifetime annotations which can be removed by -relying on lifetime elision. - -### Why is this bad? -The additional lifetimes make the code look more -complicated, while there is nothing out of the ordinary going on. Removing -them leads to more readable code. - -### Known problems -- We bail out if the function has a `where` clause where lifetimes -are mentioned due to potential false positives. -- Lifetime bounds such as `impl Foo + 'a` and `T: 'a` must be elided with the -placeholder notation `'_` because the fully elided notation leaves the type bound to `'static`. - -### Example -``` -// Unnecessary lifetime annotations -fn in_and_out<'a>(x: &'a u8, y: u8) -> &'a u8 { - x -} -``` - -Use instead: -``` -fn elided(x: &u8, y: u8) -> &u8 { - x -} -``` \ No newline at end of file diff --git a/src/docs/needless_match.txt b/src/docs/needless_match.txt deleted file mode 100644 index 92b40a5df6488..0000000000000 --- a/src/docs/needless_match.txt +++ /dev/null @@ -1,36 +0,0 @@ -### What it does -Checks for unnecessary `match` or match-like `if let` returns for `Option` and `Result` -when function signatures are the same. - -### Why is this bad? -This `match` block does nothing and might not be what the coder intended. - -### Example -``` -fn foo() -> Result<(), i32> { - match result { - Ok(val) => Ok(val), - Err(err) => Err(err), - } -} - -fn bar() -> Option { - if let Some(val) = option { - Some(val) - } else { - None - } -} -``` - -Could be replaced as - -``` -fn foo() -> Result<(), i32> { - result -} - -fn bar() -> Option { - option -} -``` \ No newline at end of file diff --git a/src/docs/needless_option_as_deref.txt b/src/docs/needless_option_as_deref.txt deleted file mode 100644 index 226396c97ac49..0000000000000 --- a/src/docs/needless_option_as_deref.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for no-op uses of `Option::{as_deref, as_deref_mut}`, -for example, `Option<&T>::as_deref()` returns the same type. - -### Why is this bad? -Redundant code and improving readability. - -### Example -``` -let a = Some(&1); -let b = a.as_deref(); // goes from Option<&i32> to Option<&i32> -``` - -Use instead: -``` -let a = Some(&1); -let b = a; -``` \ No newline at end of file diff --git a/src/docs/needless_option_take.txt b/src/docs/needless_option_take.txt deleted file mode 100644 index 6bac65a13b5b8..0000000000000 --- a/src/docs/needless_option_take.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for calling `take` function after `as_ref`. - -### Why is this bad? -Redundant code. `take` writes `None` to its argument. -In this case the modification is useless as it's a temporary that cannot be read from afterwards. - -### Example -``` -let x = Some(3); -x.as_ref().take(); -``` -Use instead: -``` -let x = Some(3); -x.as_ref(); -``` \ No newline at end of file diff --git a/src/docs/needless_parens_on_range_literals.txt b/src/docs/needless_parens_on_range_literals.txt deleted file mode 100644 index 85fab10cb5f65..0000000000000 --- a/src/docs/needless_parens_on_range_literals.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -The lint checks for parenthesis on literals in range statements that are -superfluous. - -### Why is this bad? -Having superfluous parenthesis makes the code less readable -overhead when reading. - -### Example - -``` -for i in (0)..10 { - println!("{i}"); -} -``` - -Use instead: - -``` -for i in 0..10 { - println!("{i}"); -} -``` \ No newline at end of file diff --git a/src/docs/needless_pass_by_value.txt b/src/docs/needless_pass_by_value.txt deleted file mode 100644 index 58c420b19f628..0000000000000 --- a/src/docs/needless_pass_by_value.txt +++ /dev/null @@ -1,27 +0,0 @@ -### What it does -Checks for functions taking arguments by value, but not -consuming them in its -body. - -### Why is this bad? -Taking arguments by reference is more flexible and can -sometimes avoid -unnecessary allocations. - -### Known problems -* This lint suggests taking an argument by reference, -however sometimes it is better to let users decide the argument type -(by using `Borrow` trait, for example), depending on how the function is used. - -### Example -``` -fn foo(v: Vec) { - assert_eq!(v.len(), 42); -} -``` -should be -``` -fn foo(v: &[i32]) { - assert_eq!(v.len(), 42); -} -``` \ No newline at end of file diff --git a/src/docs/needless_question_mark.txt b/src/docs/needless_question_mark.txt deleted file mode 100644 index 540739fd45fae..0000000000000 --- a/src/docs/needless_question_mark.txt +++ /dev/null @@ -1,43 +0,0 @@ -### What it does -Suggests alternatives for useless applications of `?` in terminating expressions - -### Why is this bad? -There's no reason to use `?` to short-circuit when execution of the body will end there anyway. - -### Example -``` -struct TO { - magic: Option, -} - -fn f(to: TO) -> Option { - Some(to.magic?) -} - -struct TR { - magic: Result, -} - -fn g(tr: Result) -> Result { - tr.and_then(|t| Ok(t.magic?)) -} - -``` -Use instead: -``` -struct TO { - magic: Option, -} - -fn f(to: TO) -> Option { - to.magic -} - -struct TR { - magic: Result, -} - -fn g(tr: Result) -> Result { - tr.and_then(|t| t.magic) -} -``` \ No newline at end of file diff --git a/src/docs/needless_range_loop.txt b/src/docs/needless_range_loop.txt deleted file mode 100644 index 583c09b284976..0000000000000 --- a/src/docs/needless_range_loop.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for looping over the range of `0..len` of some -collection just to get the values by index. - -### Why is this bad? -Just iterating the collection itself makes the intent -more clear and is probably faster. - -### Example -``` -let vec = vec!['a', 'b', 'c']; -for i in 0..vec.len() { - println!("{}", vec[i]); -} -``` - -Use instead: -``` -let vec = vec!['a', 'b', 'c']; -for i in vec { - println!("{}", i); -} -``` \ No newline at end of file diff --git a/src/docs/needless_return.txt b/src/docs/needless_return.txt deleted file mode 100644 index 48782cb0ca84d..0000000000000 --- a/src/docs/needless_return.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for return statements at the end of a block. - -### Why is this bad? -Removing the `return` and semicolon will make the code -more rusty. - -### Example -``` -fn foo(x: usize) -> usize { - return x; -} -``` -simplify to -``` -fn foo(x: usize) -> usize { - x -} -``` \ No newline at end of file diff --git a/src/docs/needless_splitn.txt b/src/docs/needless_splitn.txt deleted file mode 100644 index b10a84fbc42d4..0000000000000 --- a/src/docs/needless_splitn.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for usages of `str::splitn` (or `str::rsplitn`) where using `str::split` would be the same. -### Why is this bad? -The function `split` is simpler and there is no performance difference in these cases, considering -that both functions return a lazy iterator. -### Example -``` -let str = "key=value=add"; -let _ = str.splitn(3, '=').next().unwrap(); -``` - -Use instead: -``` -let str = "key=value=add"; -let _ = str.split('=').next().unwrap(); -``` \ No newline at end of file diff --git a/src/docs/needless_update.txt b/src/docs/needless_update.txt deleted file mode 100644 index 82adabf6482c3..0000000000000 --- a/src/docs/needless_update.txt +++ /dev/null @@ -1,30 +0,0 @@ -### What it does -Checks for needlessly including a base struct on update -when all fields are changed anyway. - -This lint is not applied to structs marked with -[non_exhaustive](https://doc.rust-lang.org/reference/attributes/type_system.html). - -### Why is this bad? -This will cost resources (because the base has to be -somewhere), and make the code less readable. - -### Example -``` -Point { - x: 1, - y: 1, - z: 1, - ..zero_point -}; -``` - -Use instead: -``` -// Missing field `z` -Point { - x: 1, - y: 1, - ..zero_point -}; -``` \ No newline at end of file diff --git a/src/docs/neg_cmp_op_on_partial_ord.txt b/src/docs/neg_cmp_op_on_partial_ord.txt deleted file mode 100644 index fa55c6cfd74b3..0000000000000 --- a/src/docs/neg_cmp_op_on_partial_ord.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Checks for the usage of negated comparison operators on types which only implement -`PartialOrd` (e.g., `f64`). - -### Why is this bad? -These operators make it easy to forget that the underlying types actually allow not only three -potential Orderings (Less, Equal, Greater) but also a fourth one (Uncomparable). This is -especially easy to miss if the operator based comparison result is negated. - -### Example -``` -let a = 1.0; -let b = f64::NAN; - -let not_less_or_equal = !(a <= b); -``` - -Use instead: -``` -use std::cmp::Ordering; - -let _not_less_or_equal = match a.partial_cmp(&b) { - None | Some(Ordering::Greater) => true, - _ => false, -}; -``` \ No newline at end of file diff --git a/src/docs/neg_multiply.txt b/src/docs/neg_multiply.txt deleted file mode 100644 index 4e8b096eb9cc7..0000000000000 --- a/src/docs/neg_multiply.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for multiplication by -1 as a form of negation. - -### Why is this bad? -It's more readable to just negate. - -### Known problems -This only catches integers (for now). - -### Example -``` -let a = x * -1; -``` - -Use instead: -``` -let a = -x; -``` \ No newline at end of file diff --git a/src/docs/negative_feature_names.txt b/src/docs/negative_feature_names.txt deleted file mode 100644 index 01ee9efb318be..0000000000000 --- a/src/docs/negative_feature_names.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for negative feature names with prefix `no-` or `not-` - -### Why is this bad? -Features are supposed to be additive, and negatively-named features violate it. - -### Example -``` -[features] -default = [] -no-abc = [] -not-def = [] - -``` -Use instead: -``` -[features] -default = ["abc", "def"] -abc = [] -def = [] - -``` \ No newline at end of file diff --git a/src/docs/never_loop.txt b/src/docs/never_loop.txt deleted file mode 100644 index 737ccf415cb98..0000000000000 --- a/src/docs/never_loop.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for loops that will always `break`, `return` or -`continue` an outer loop. - -### Why is this bad? -This loop never loops, all it does is obfuscating the -code. - -### Example -``` -loop { - ..; - break; -} -``` \ No newline at end of file diff --git a/src/docs/new_ret_no_self.txt b/src/docs/new_ret_no_self.txt deleted file mode 100644 index 291bad24a643b..0000000000000 --- a/src/docs/new_ret_no_self.txt +++ /dev/null @@ -1,47 +0,0 @@ -### What it does -Checks for `new` not returning a type that contains `Self`. - -### Why is this bad? -As a convention, `new` methods are used to make a new -instance of a type. - -### Example -In an impl block: -``` -impl Foo { - fn new() -> NotAFoo { - } -} -``` - -``` -struct Bar(Foo); -impl Foo { - // Bad. The type name must contain `Self` - fn new() -> Bar { - } -} -``` - -``` -impl Foo { - // Good. Return type contains `Self` - fn new() -> Result { - } -} -``` - -Or in a trait definition: -``` -pub trait Trait { - // Bad. The type name must contain `Self` - fn new(); -} -``` - -``` -pub trait Trait { - // Good. Return type contains `Self` - fn new() -> Self; -} -``` \ No newline at end of file diff --git a/src/docs/new_without_default.txt b/src/docs/new_without_default.txt deleted file mode 100644 index 662d39c8efdd2..0000000000000 --- a/src/docs/new_without_default.txt +++ /dev/null @@ -1,32 +0,0 @@ -### What it does -Checks for public types with a `pub fn new() -> Self` method and no -implementation of -[`Default`](https://doc.rust-lang.org/std/default/trait.Default.html). - -### Why is this bad? -The user might expect to be able to use -[`Default`](https://doc.rust-lang.org/std/default/trait.Default.html) as the -type can be constructed without arguments. - -### Example -``` -pub struct Foo(Bar); - -impl Foo { - pub fn new() -> Self { - Foo(Bar::new()) - } -} -``` - -To fix the lint, add a `Default` implementation that delegates to `new`: - -``` -pub struct Foo(Bar); - -impl Default for Foo { - fn default() -> Self { - Foo::new() - } -} -``` \ No newline at end of file diff --git a/src/docs/no_effect.txt b/src/docs/no_effect.txt deleted file mode 100644 index d4cc08fa8a7d8..0000000000000 --- a/src/docs/no_effect.txt +++ /dev/null @@ -1,12 +0,0 @@ -### What it does -Checks for statements which have no effect. - -### Why is this bad? -Unlike dead code, these statements are actually -executed. However, as they have no effect, all they do is make the code less -readable. - -### Example -``` -0; -``` \ No newline at end of file diff --git a/src/docs/no_effect_replace.txt b/src/docs/no_effect_replace.txt deleted file mode 100644 index 646d45287ef50..0000000000000 --- a/src/docs/no_effect_replace.txt +++ /dev/null @@ -1,11 +0,0 @@ -### What it does -Checks for `replace` statements which have no effect. - -### Why is this bad? -It's either a mistake or confusing. - -### Example -``` -"1234".replace("12", "12"); -"1234".replacen("12", "12", 1); -``` \ No newline at end of file diff --git a/src/docs/no_effect_underscore_binding.txt b/src/docs/no_effect_underscore_binding.txt deleted file mode 100644 index 972f60dd01e92..0000000000000 --- a/src/docs/no_effect_underscore_binding.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for binding to underscore prefixed variable without side-effects. - -### Why is this bad? -Unlike dead code, these bindings are actually -executed. However, as they have no effect and shouldn't be used further on, all they -do is make the code less readable. - -### Known problems -Further usage of this variable is not checked, which can lead to false positives if it is -used later in the code. - -### Example -``` -let _i_serve_no_purpose = 1; -``` \ No newline at end of file diff --git a/src/docs/non_ascii_literal.txt b/src/docs/non_ascii_literal.txt deleted file mode 100644 index 164902b4726e1..0000000000000 --- a/src/docs/non_ascii_literal.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for non-ASCII characters in string and char literals. - -### Why is this bad? -Yeah, we know, the 90's called and wanted their charset -back. Even so, there still are editors and other programs out there that -don't work well with Unicode. So if the code is meant to be used -internationally, on multiple operating systems, or has other portability -requirements, activating this lint could be useful. - -### Example -``` -let x = String::from("€"); -``` - -Use instead: -``` -let x = String::from("\u{20ac}"); -``` \ No newline at end of file diff --git a/src/docs/non_octal_unix_permissions.txt b/src/docs/non_octal_unix_permissions.txt deleted file mode 100644 index 4a468e94db164..0000000000000 --- a/src/docs/non_octal_unix_permissions.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for non-octal values used to set Unix file permissions. - -### Why is this bad? -They will be converted into octal, creating potentially -unintended file permissions. - -### Example -``` -use std::fs::OpenOptions; -use std::os::unix::fs::OpenOptionsExt; - -let mut options = OpenOptions::new(); -options.mode(644); -``` -Use instead: -``` -use std::fs::OpenOptions; -use std::os::unix::fs::OpenOptionsExt; - -let mut options = OpenOptions::new(); -options.mode(0o644); -``` \ No newline at end of file diff --git a/src/docs/non_send_fields_in_send_ty.txt b/src/docs/non_send_fields_in_send_ty.txt deleted file mode 100644 index 11e6f6e162cf0..0000000000000 --- a/src/docs/non_send_fields_in_send_ty.txt +++ /dev/null @@ -1,36 +0,0 @@ -### What it does -This lint warns about a `Send` implementation for a type that -contains fields that are not safe to be sent across threads. -It tries to detect fields that can cause a soundness issue -when sent to another thread (e.g., `Rc`) while allowing `!Send` fields -that are expected to exist in a `Send` type, such as raw pointers. - -### Why is this bad? -Sending the struct to another thread effectively sends all of its fields, -and the fields that do not implement `Send` can lead to soundness bugs -such as data races when accessed in a thread -that is different from the thread that created it. - -See: -* [*The Rustonomicon* about *Send and Sync*](https://doc.rust-lang.org/nomicon/send-and-sync.html) -* [The documentation of `Send`](https://doc.rust-lang.org/std/marker/trait.Send.html) - -### Known Problems -This lint relies on heuristics to distinguish types that are actually -unsafe to be sent across threads and `!Send` types that are expected to -exist in `Send` type. Its rule can filter out basic cases such as -`Vec<*const T>`, but it's not perfect. Feel free to create an issue if -you have a suggestion on how this heuristic can be improved. - -### Example -``` -struct ExampleStruct { - rc_is_not_send: Rc, - unbounded_generic_field: T, -} - -// This impl is unsound because it allows sending `!Send` types through `ExampleStruct` -unsafe impl Send for ExampleStruct {} -``` -Use thread-safe types like [`std::sync::Arc`](https://doc.rust-lang.org/std/sync/struct.Arc.html) -or specify correct bounds on generic type parameters (`T: Send`). \ No newline at end of file diff --git a/src/docs/nonminimal_bool.txt b/src/docs/nonminimal_bool.txt deleted file mode 100644 index 488980ddf0239..0000000000000 --- a/src/docs/nonminimal_bool.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for boolean expressions that can be written more -concisely. - -### Why is this bad? -Readability of boolean expressions suffers from -unnecessary duplication. - -### Known problems -Ignores short circuiting behavior of `||` and -`&&`. Ignores `|`, `&` and `^`. - -### Example -``` -if a && true {} -if !(a == b) {} -``` - -Use instead: -``` -if a {} -if a != b {} -``` \ No newline at end of file diff --git a/src/docs/nonsensical_open_options.txt b/src/docs/nonsensical_open_options.txt deleted file mode 100644 index 7a95443b51a50..0000000000000 --- a/src/docs/nonsensical_open_options.txt +++ /dev/null @@ -1,14 +0,0 @@ -### What it does -Checks for duplicate open options as well as combinations -that make no sense. - -### Why is this bad? -In the best case, the code will be harder to read than -necessary. I don't know the worst case. - -### Example -``` -use std::fs::OpenOptions; - -OpenOptions::new().read(true).truncate(true); -``` \ No newline at end of file diff --git a/src/docs/nonstandard_macro_braces.txt b/src/docs/nonstandard_macro_braces.txt deleted file mode 100644 index 7e8d0d2d33bf1..0000000000000 --- a/src/docs/nonstandard_macro_braces.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks that common macros are used with consistent bracing. - -### Why is this bad? -This is mostly a consistency lint although using () or [] -doesn't give you a semicolon in item position, which can be unexpected. - -### Example -``` -vec!{1, 2, 3}; -``` -Use instead: -``` -vec![1, 2, 3]; -``` \ No newline at end of file diff --git a/src/docs/not_unsafe_ptr_arg_deref.txt b/src/docs/not_unsafe_ptr_arg_deref.txt deleted file mode 100644 index 31355fbb7b666..0000000000000 --- a/src/docs/not_unsafe_ptr_arg_deref.txt +++ /dev/null @@ -1,30 +0,0 @@ -### What it does -Checks for public functions that dereference raw pointer -arguments but are not marked `unsafe`. - -### Why is this bad? -The function should probably be marked `unsafe`, since -for an arbitrary raw pointer, there is no way of telling for sure if it is -valid. - -### Known problems -* It does not check functions recursively so if the pointer is passed to a -private non-`unsafe` function which does the dereferencing, the lint won't -trigger. -* It only checks for arguments whose type are raw pointers, not raw pointers -got from an argument in some other way (`fn foo(bar: &[*const u8])` or -`some_argument.get_raw_ptr()`). - -### Example -``` -pub fn foo(x: *const u8) { - println!("{}", unsafe { *x }); -} -``` - -Use instead: -``` -pub unsafe fn foo(x: *const u8) { - println!("{}", unsafe { *x }); -} -``` \ No newline at end of file diff --git a/src/docs/obfuscated_if_else.txt b/src/docs/obfuscated_if_else.txt deleted file mode 100644 index 638f63b0db5e4..0000000000000 --- a/src/docs/obfuscated_if_else.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for usages of `.then_some(..).unwrap_or(..)` - -### Why is this bad? -This can be written more clearly with `if .. else ..` - -### Limitations -This lint currently only looks for usages of -`.then_some(..).unwrap_or(..)`, but will be expanded -to account for similar patterns. - -### Example -``` -let x = true; -x.then_some("a").unwrap_or("b"); -``` -Use instead: -``` -let x = true; -if x { "a" } else { "b" }; -``` \ No newline at end of file diff --git a/src/docs/octal_escapes.txt b/src/docs/octal_escapes.txt deleted file mode 100644 index eee820587158c..0000000000000 --- a/src/docs/octal_escapes.txt +++ /dev/null @@ -1,33 +0,0 @@ -### What it does -Checks for `\0` escapes in string and byte literals that look like octal -character escapes in C. - -### Why is this bad? - -C and other languages support octal character escapes in strings, where -a backslash is followed by up to three octal digits. For example, `\033` -stands for the ASCII character 27 (ESC). Rust does not support this -notation, but has the escape code `\0` which stands for a null -byte/character, and any following digits do not form part of the escape -sequence. Therefore, `\033` is not a compiler error but the result may -be surprising. - -### Known problems -The actual meaning can be the intended one. `\x00` can be used in these -cases to be unambiguous. - -The lint does not trigger for format strings in `print!()`, `write!()` -and friends since the string is already preprocessed when Clippy lints -can see it. - -### Example -``` -let one = "\033[1m Bold? \033[0m"; // \033 intended as escape -let two = "\033\0"; // \033 intended as null-3-3 -``` - -Use instead: -``` -let one = "\x1b[1mWill this be bold?\x1b[0m"; -let two = "\x0033\x00"; -``` \ No newline at end of file diff --git a/src/docs/ok_expect.txt b/src/docs/ok_expect.txt deleted file mode 100644 index fd5205d49dc0e..0000000000000 --- a/src/docs/ok_expect.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for usage of `ok().expect(..)`. - -### Why is this bad? -Because you usually call `expect()` on the `Result` -directly to get a better error message. - -### Known problems -The error type needs to implement `Debug` - -### Example -``` -x.ok().expect("why did I do this again?"); -``` - -Use instead: -``` -x.expect("why did I do this again?"); -``` \ No newline at end of file diff --git a/src/docs/only_used_in_recursion.txt b/src/docs/only_used_in_recursion.txt deleted file mode 100644 index f19f47ff9eb50..0000000000000 --- a/src/docs/only_used_in_recursion.txt +++ /dev/null @@ -1,58 +0,0 @@ -### What it does -Checks for arguments that are only used in recursion with no side-effects. - -### Why is this bad? -It could contain a useless calculation and can make function simpler. - -The arguments can be involved in calculations and assignments but as long as -the calculations have no side-effects (function calls or mutating dereference) -and the assigned variables are also only in recursion, it is useless. - -### Known problems -Too many code paths in the linting code are currently untested and prone to produce false -positives or are prone to have performance implications. - -In some cases, this would not catch all useless arguments. - -``` -fn foo(a: usize, b: usize) -> usize { - let f = |x| x + 1; - - if a == 0 { - 1 - } else { - foo(a - 1, f(b)) - } -} -``` - -For example, the argument `b` is only used in recursion, but the lint would not catch it. - -List of some examples that can not be caught: -- binary operation of non-primitive types -- closure usage -- some `break` relative operations -- struct pattern binding - -Also, when you recurse the function name with path segments, it is not possible to detect. - -### Example -``` -fn f(a: usize, b: usize) -> usize { - if a == 0 { - 1 - } else { - f(a - 1, b + 1) - } -} -``` -Use instead: -``` -fn f(a: usize) -> usize { - if a == 0 { - 1 - } else { - f(a - 1) - } -} -``` \ No newline at end of file diff --git a/src/docs/op_ref.txt b/src/docs/op_ref.txt deleted file mode 100644 index 7a7ed1bc9badc..0000000000000 --- a/src/docs/op_ref.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for arguments to `==` which have their address -taken to satisfy a bound -and suggests to dereference the other argument instead - -### Why is this bad? -It is more idiomatic to dereference the other argument. - -### Example -``` -&x == y -``` - -Use instead: -``` -x == *y -``` \ No newline at end of file diff --git a/src/docs/option_as_ref_deref.txt b/src/docs/option_as_ref_deref.txt deleted file mode 100644 index ad7411d3d4bd9..0000000000000 --- a/src/docs/option_as_ref_deref.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for usage of `_.as_ref().map(Deref::deref)` or it's aliases (such as String::as_str). - -### Why is this bad? -Readability, this can be written more concisely as -`_.as_deref()`. - -### Example -``` -opt.as_ref().map(String::as_str) -``` -Can be written as -``` -opt.as_deref() -``` \ No newline at end of file diff --git a/src/docs/option_env_unwrap.txt b/src/docs/option_env_unwrap.txt deleted file mode 100644 index c952cba8e2615..0000000000000 --- a/src/docs/option_env_unwrap.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for usage of `option_env!(...).unwrap()` and -suggests usage of the `env!` macro. - -### Why is this bad? -Unwrapping the result of `option_env!` will panic -at run-time if the environment variable doesn't exist, whereas `env!` -catches it at compile-time. - -### Example -``` -let _ = option_env!("HOME").unwrap(); -``` - -Is better expressed as: - -``` -let _ = env!("HOME"); -``` \ No newline at end of file diff --git a/src/docs/option_filter_map.txt b/src/docs/option_filter_map.txt deleted file mode 100644 index 25f7bde7b4d04..0000000000000 --- a/src/docs/option_filter_map.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for indirect collection of populated `Option` - -### Why is this bad? -`Option` is like a collection of 0-1 things, so `flatten` -automatically does this without suspicious-looking `unwrap` calls. - -### Example -``` -let _ = std::iter::empty::>().filter(Option::is_some).map(Option::unwrap); -``` -Use instead: -``` -let _ = std::iter::empty::>().flatten(); -``` \ No newline at end of file diff --git a/src/docs/option_if_let_else.txt b/src/docs/option_if_let_else.txt deleted file mode 100644 index 43652db513b4c..0000000000000 --- a/src/docs/option_if_let_else.txt +++ /dev/null @@ -1,46 +0,0 @@ -### What it does -Lints usage of `if let Some(v) = ... { y } else { x }` and -`match .. { Some(v) => y, None/_ => x }` which are more -idiomatically done with `Option::map_or` (if the else bit is a pure -expression) or `Option::map_or_else` (if the else bit is an impure -expression). - -### Why is this bad? -Using the dedicated functions of the `Option` type is clearer and -more concise than an `if let` expression. - -### Known problems -This lint uses a deliberately conservative metric for checking -if the inside of either body contains breaks or continues which will -cause it to not suggest a fix if either block contains a loop with -continues or breaks contained within the loop. - -### Example -``` -let _ = if let Some(foo) = optional { - foo -} else { - 5 -}; -let _ = match optional { - Some(val) => val + 1, - None => 5 -}; -let _ = if let Some(foo) = optional { - foo -} else { - let y = do_complicated_function(); - y*y -}; -``` - -should be - -``` -let _ = optional.map_or(5, |foo| foo); -let _ = optional.map_or(5, |val| val + 1); -let _ = optional.map_or_else(||{ - let y = do_complicated_function(); - y*y -}, |foo| foo); -``` \ No newline at end of file diff --git a/src/docs/option_map_or_none.txt b/src/docs/option_map_or_none.txt deleted file mode 100644 index c86c65215f01c..0000000000000 --- a/src/docs/option_map_or_none.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for usage of `_.map_or(None, _)`. - -### Why is this bad? -Readability, this can be written more concisely as -`_.and_then(_)`. - -### Known problems -The order of the arguments is not in execution order. - -### Example -``` -opt.map_or(None, |a| Some(a + 1)); -``` - -Use instead: -``` -opt.and_then(|a| Some(a + 1)); -``` \ No newline at end of file diff --git a/src/docs/option_map_unit_fn.txt b/src/docs/option_map_unit_fn.txt deleted file mode 100644 index fc4b528f0923e..0000000000000 --- a/src/docs/option_map_unit_fn.txt +++ /dev/null @@ -1,27 +0,0 @@ -### What it does -Checks for usage of `option.map(f)` where f is a function -or closure that returns the unit type `()`. - -### Why is this bad? -Readability, this can be written more clearly with -an if let statement - -### Example -``` -let x: Option = do_stuff(); -x.map(log_err_msg); -x.map(|msg| log_err_msg(format_msg(msg))); -``` - -The correct use would be: - -``` -let x: Option = do_stuff(); -if let Some(msg) = x { - log_err_msg(msg); -} - -if let Some(msg) = x { - log_err_msg(format_msg(msg)); -} -``` \ No newline at end of file diff --git a/src/docs/option_option.txt b/src/docs/option_option.txt deleted file mode 100644 index b4324bd83990a..0000000000000 --- a/src/docs/option_option.txt +++ /dev/null @@ -1,32 +0,0 @@ -### What it does -Checks for use of `Option>` in function signatures and type -definitions - -### Why is this bad? -`Option<_>` represents an optional value. `Option>` -represents an optional optional value which is logically the same thing as an optional -value but has an unneeded extra level of wrapping. - -If you have a case where `Some(Some(_))`, `Some(None)` and `None` are distinct cases, -consider a custom `enum` instead, with clear names for each case. - -### Example -``` -fn get_data() -> Option> { - None -} -``` - -Better: - -``` -pub enum Contents { - Data(Vec), // Was Some(Some(Vec)) - NotYetFetched, // Was Some(None) - None, // Was None -} - -fn get_data() -> Contents { - Contents::None -} -``` \ No newline at end of file diff --git a/src/docs/or_fun_call.txt b/src/docs/or_fun_call.txt deleted file mode 100644 index 6ce77cc268c51..0000000000000 --- a/src/docs/or_fun_call.txt +++ /dev/null @@ -1,27 +0,0 @@ -### What it does -Checks for calls to `.or(foo(..))`, `.unwrap_or(foo(..))`, -`.or_insert(foo(..))` etc., and suggests to use `.or_else(|| foo(..))`, -`.unwrap_or_else(|| foo(..))`, `.unwrap_or_default()` or `.or_default()` -etc. instead. - -### Why is this bad? -The function will always be called and potentially -allocate an object acting as the default. - -### Known problems -If the function has side-effects, not calling it will -change the semantic of the program, but you shouldn't rely on that anyway. - -### Example -``` -foo.unwrap_or(String::new()); -``` - -Use instead: -``` -foo.unwrap_or_else(String::new); - -// or - -foo.unwrap_or_default(); -``` \ No newline at end of file diff --git a/src/docs/or_then_unwrap.txt b/src/docs/or_then_unwrap.txt deleted file mode 100644 index 64ac53749e8ad..0000000000000 --- a/src/docs/or_then_unwrap.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for `.or(…).unwrap()` calls to Options and Results. - -### Why is this bad? -You should use `.unwrap_or(…)` instead for clarity. - -### Example -``` -// Result -let value = result.or::(Ok(fallback)).unwrap(); - -// Option -let value = option.or(Some(fallback)).unwrap(); -``` -Use instead: -``` -// Result -let value = result.unwrap_or(fallback); - -// Option -let value = option.unwrap_or(fallback); -``` \ No newline at end of file diff --git a/src/docs/out_of_bounds_indexing.txt b/src/docs/out_of_bounds_indexing.txt deleted file mode 100644 index 5802eea29966f..0000000000000 --- a/src/docs/out_of_bounds_indexing.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for out of bounds array indexing with a constant -index. - -### Why is this bad? -This will always panic at runtime. - -### Example -``` -let x = [1, 2, 3, 4]; - -x[9]; -&x[2..9]; -``` - -Use instead: -``` -// Index within bounds - -x[0]; -x[3]; -``` \ No newline at end of file diff --git a/src/docs/overflow_check_conditional.txt b/src/docs/overflow_check_conditional.txt deleted file mode 100644 index a09cc18a0bcc5..0000000000000 --- a/src/docs/overflow_check_conditional.txt +++ /dev/null @@ -1,11 +0,0 @@ -### What it does -Detects classic underflow/overflow checks. - -### Why is this bad? -Most classic C underflow/overflow checks will fail in -Rust. Users can use functions like `overflowing_*` and `wrapping_*` instead. - -### Example -``` -a + b < a; -``` \ No newline at end of file diff --git a/src/docs/overly_complex_bool_expr.txt b/src/docs/overly_complex_bool_expr.txt deleted file mode 100644 index 65ca18392e756..0000000000000 --- a/src/docs/overly_complex_bool_expr.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Checks for boolean expressions that contain terminals that -can be eliminated. - -### Why is this bad? -This is most likely a logic bug. - -### Known problems -Ignores short circuiting behavior. - -### Example -``` -// The `b` is unnecessary, the expression is equivalent to `if a`. -if a && b || a { ... } -``` - -Use instead: -``` -if a {} -``` \ No newline at end of file diff --git a/src/docs/panic.txt b/src/docs/panic.txt deleted file mode 100644 index f9bdc6e87ccf9..0000000000000 --- a/src/docs/panic.txt +++ /dev/null @@ -1,10 +0,0 @@ -### What it does -Checks for usage of `panic!`. - -### Why is this bad? -`panic!` will stop the execution of the executable - -### Example -``` -panic!("even with a good reason"); -``` \ No newline at end of file diff --git a/src/docs/panic_in_result_fn.txt b/src/docs/panic_in_result_fn.txt deleted file mode 100644 index 51c2f8ae5a30e..0000000000000 --- a/src/docs/panic_in_result_fn.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for usage of `panic!`, `unimplemented!`, `todo!`, `unreachable!` or assertions in a function of type result. - -### Why is this bad? -For some codebases, it is desirable for functions of type result to return an error instead of crashing. Hence panicking macros should be avoided. - -### Known problems -Functions called from a function returning a `Result` may invoke a panicking macro. This is not checked. - -### Example -``` -fn result_with_panic() -> Result -{ - panic!("error"); -} -``` -Use instead: -``` -fn result_without_panic() -> Result { - Err(String::from("error")) -} -``` \ No newline at end of file diff --git a/src/docs/panicking_unwrap.txt b/src/docs/panicking_unwrap.txt deleted file mode 100644 index 1fbc245c8ec38..0000000000000 --- a/src/docs/panicking_unwrap.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for calls of `unwrap[_err]()` that will always fail. - -### Why is this bad? -If panicking is desired, an explicit `panic!()` should be used. - -### Known problems -This lint only checks `if` conditions not assignments. -So something like `let x: Option<()> = None; x.unwrap();` will not be recognized. - -### Example -``` -if option.is_none() { - do_something_with(option.unwrap()) -} -``` - -This code will always panic. The if condition should probably be inverted. \ No newline at end of file diff --git a/src/docs/partial_pub_fields.txt b/src/docs/partial_pub_fields.txt deleted file mode 100644 index b529adf1547de..0000000000000 --- a/src/docs/partial_pub_fields.txt +++ /dev/null @@ -1,27 +0,0 @@ -### What it does -Checks whether partial fields of a struct are public. - -Either make all fields of a type public, or make none of them public - -### Why is this bad? -Most types should either be: -* Abstract data types: complex objects with opaque implementation which guard -interior invariants and expose intentionally limited API to the outside world. -* Data: relatively simple objects which group a bunch of related attributes together. - -### Example -``` -pub struct Color { - pub r: u8, - pub g: u8, - b: u8, -} -``` -Use instead: -``` -pub struct Color { - pub r: u8, - pub g: u8, - pub b: u8, -} -``` \ No newline at end of file diff --git a/src/docs/partialeq_ne_impl.txt b/src/docs/partialeq_ne_impl.txt deleted file mode 100644 index 78f55188bab81..0000000000000 --- a/src/docs/partialeq_ne_impl.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for manual re-implementations of `PartialEq::ne`. - -### Why is this bad? -`PartialEq::ne` is required to always return the -negated result of `PartialEq::eq`, which is exactly what the default -implementation does. Therefore, there should never be any need to -re-implement it. - -### Example -``` -struct Foo; - -impl PartialEq for Foo { - fn eq(&self, other: &Foo) -> bool { true } - fn ne(&self, other: &Foo) -> bool { !(self == other) } -} -``` \ No newline at end of file diff --git a/src/docs/partialeq_to_none.txt b/src/docs/partialeq_to_none.txt deleted file mode 100644 index 5cc07bf88435c..0000000000000 --- a/src/docs/partialeq_to_none.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does - -Checks for binary comparisons to a literal `Option::None`. - -### Why is this bad? - -A programmer checking if some `foo` is `None` via a comparison `foo == None` -is usually inspired from other programming languages (e.g. `foo is None` -in Python). -Checking if a value of type `Option` is (not) equal to `None` in that -way relies on `T: PartialEq` to do the comparison, which is unneeded. - -### Example -``` -fn foo(f: Option) -> &'static str { - if f != None { "yay" } else { "nay" } -} -``` -Use instead: -``` -fn foo(f: Option) -> &'static str { - if f.is_some() { "yay" } else { "nay" } -} -``` \ No newline at end of file diff --git a/src/docs/path_buf_push_overwrite.txt b/src/docs/path_buf_push_overwrite.txt deleted file mode 100644 index 34f8901da2396..0000000000000 --- a/src/docs/path_buf_push_overwrite.txt +++ /dev/null @@ -1,25 +0,0 @@ -### What it does -* Checks for [push](https://doc.rust-lang.org/std/path/struct.PathBuf.html#method.push) -calls on `PathBuf` that can cause overwrites. - -### Why is this bad? -Calling `push` with a root path at the start can overwrite the -previous defined path. - -### Example -``` -use std::path::PathBuf; - -let mut x = PathBuf::from("/foo"); -x.push("/bar"); -assert_eq!(x, PathBuf::from("/bar")); -``` -Could be written: - -``` -use std::path::PathBuf; - -let mut x = PathBuf::from("/foo"); -x.push("bar"); -assert_eq!(x, PathBuf::from("/foo/bar")); -``` \ No newline at end of file diff --git a/src/docs/pattern_type_mismatch.txt b/src/docs/pattern_type_mismatch.txt deleted file mode 100644 index 64da881d592a8..0000000000000 --- a/src/docs/pattern_type_mismatch.txt +++ /dev/null @@ -1,64 +0,0 @@ -### What it does -Checks for patterns that aren't exact representations of the types -they are applied to. - -To satisfy this lint, you will have to adjust either the expression that is matched -against or the pattern itself, as well as the bindings that are introduced by the -adjusted patterns. For matching you will have to either dereference the expression -with the `*` operator, or amend the patterns to explicitly match against `&` -or `&mut ` depending on the reference mutability. For the bindings you need -to use the inverse. You can leave them as plain bindings if you wish for the value -to be copied, but you must use `ref mut ` or `ref ` to construct -a reference into the matched structure. - -If you are looking for a way to learn about ownership semantics in more detail, it -is recommended to look at IDE options available to you to highlight types, lifetimes -and reference semantics in your code. The available tooling would expose these things -in a general way even outside of the various pattern matching mechanics. Of course -this lint can still be used to highlight areas of interest and ensure a good understanding -of ownership semantics. - -### Why is this bad? -It isn't bad in general. But in some contexts it can be desirable -because it increases ownership hints in the code, and will guard against some changes -in ownership. - -### Example -This example shows the basic adjustments necessary to satisfy the lint. Note how -the matched expression is explicitly dereferenced with `*` and the `inner` variable -is bound to a shared borrow via `ref inner`. - -``` -// Bad -let value = &Some(Box::new(23)); -match value { - Some(inner) => println!("{}", inner), - None => println!("none"), -} - -// Good -let value = &Some(Box::new(23)); -match *value { - Some(ref inner) => println!("{}", inner), - None => println!("none"), -} -``` - -The following example demonstrates one of the advantages of the more verbose style. -Note how the second version uses `ref mut a` to explicitly declare `a` a shared mutable -borrow, while `b` is simply taken by value. This ensures that the loop body cannot -accidentally modify the wrong part of the structure. - -``` -// Bad -let mut values = vec![(2, 3), (3, 4)]; -for (a, b) in &mut values { - *a += *b; -} - -// Good -let mut values = vec![(2, 3), (3, 4)]; -for &mut (ref mut a, b) in &mut values { - *a += b; -} -``` \ No newline at end of file diff --git a/src/docs/possible_missing_comma.txt b/src/docs/possible_missing_comma.txt deleted file mode 100644 index 5d92f4cae91e0..0000000000000 --- a/src/docs/possible_missing_comma.txt +++ /dev/null @@ -1,14 +0,0 @@ -### What it does -Checks for possible missing comma in an array. It lints if -an array element is a binary operator expression and it lies on two lines. - -### Why is this bad? -This could lead to unexpected results. - -### Example -``` -let a = &[ - -1, -2, -3 // <= no comma here - -4, -5, -6 -]; -``` \ No newline at end of file diff --git a/src/docs/precedence.txt b/src/docs/precedence.txt deleted file mode 100644 index fda0b831f335f..0000000000000 --- a/src/docs/precedence.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for operations where precedence may be unclear -and suggests to add parentheses. Currently it catches the following: -* mixed usage of arithmetic and bit shifting/combining operators without -parentheses -* a "negative" numeric literal (which is really a unary `-` followed by a -numeric literal) - followed by a method call - -### Why is this bad? -Not everyone knows the precedence of those operators by -heart, so expressions like these may trip others trying to reason about the -code. - -### Example -* `1 << 2 + 3` equals 32, while `(1 << 2) + 3` equals 7 -* `-1i32.abs()` equals -1, while `(-1i32).abs()` equals 1 \ No newline at end of file diff --git a/src/docs/print_in_format_impl.txt b/src/docs/print_in_format_impl.txt deleted file mode 100644 index 140d23d6faab4..0000000000000 --- a/src/docs/print_in_format_impl.txt +++ /dev/null @@ -1,34 +0,0 @@ -### What it does -Checks for use of `println`, `print`, `eprintln` or `eprint` in an -implementation of a formatting trait. - -### Why is this bad? -Using a print macro is likely unintentional since formatting traits -should write to the `Formatter`, not stdout/stderr. - -### Example -``` -use std::fmt::{Display, Error, Formatter}; - -struct S; -impl Display for S { - fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { - println!("S"); - - Ok(()) - } -} -``` -Use instead: -``` -use std::fmt::{Display, Error, Formatter}; - -struct S; -impl Display for S { - fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { - writeln!(f, "S"); - - Ok(()) - } -} -``` \ No newline at end of file diff --git a/src/docs/print_literal.txt b/src/docs/print_literal.txt deleted file mode 100644 index a6252a68780bb..0000000000000 --- a/src/docs/print_literal.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -This lint warns about the use of literals as `print!`/`println!` args. - -### Why is this bad? -Using literals as `println!` args is inefficient -(c.f., https://github.com/matthiaskrgr/rust-str-bench) and unnecessary -(i.e., just put the literal in the format string) - -### Example -``` -println!("{}", "foo"); -``` -use the literal without formatting: -``` -println!("foo"); -``` \ No newline at end of file diff --git a/src/docs/print_stderr.txt b/src/docs/print_stderr.txt deleted file mode 100644 index 9c6edeeef125d..0000000000000 --- a/src/docs/print_stderr.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for printing on *stderr*. The purpose of this lint -is to catch debugging remnants. - -### Why is this bad? -People often print on *stderr* while debugging an -application and might forget to remove those prints afterward. - -### Known problems -Only catches `eprint!` and `eprintln!` calls. - -### Example -``` -eprintln!("Hello world!"); -``` \ No newline at end of file diff --git a/src/docs/print_stdout.txt b/src/docs/print_stdout.txt deleted file mode 100644 index d2cbd811d1b2a..0000000000000 --- a/src/docs/print_stdout.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for printing on *stdout*. The purpose of this lint -is to catch debugging remnants. - -### Why is this bad? -People often print on *stdout* while debugging an -application and might forget to remove those prints afterward. - -### Known problems -Only catches `print!` and `println!` calls. - -### Example -``` -println!("Hello world!"); -``` \ No newline at end of file diff --git a/src/docs/print_with_newline.txt b/src/docs/print_with_newline.txt deleted file mode 100644 index 640323e822dbf..0000000000000 --- a/src/docs/print_with_newline.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -This lint warns when you use `print!()` with a format -string that ends in a newline. - -### Why is this bad? -You should use `println!()` instead, which appends the -newline. - -### Example -``` -print!("Hello {}!\n", name); -``` -use println!() instead -``` -println!("Hello {}!", name); -``` \ No newline at end of file diff --git a/src/docs/println_empty_string.txt b/src/docs/println_empty_string.txt deleted file mode 100644 index b980413022cab..0000000000000 --- a/src/docs/println_empty_string.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -This lint warns when you use `println!("")` to -print a newline. - -### Why is this bad? -You should use `println!()`, which is simpler. - -### Example -``` -println!(""); -``` - -Use instead: -``` -println!(); -``` \ No newline at end of file diff --git a/src/docs/ptr_arg.txt b/src/docs/ptr_arg.txt deleted file mode 100644 index 796b0a65b717f..0000000000000 --- a/src/docs/ptr_arg.txt +++ /dev/null @@ -1,29 +0,0 @@ -### What it does -This lint checks for function arguments of type `&String`, `&Vec`, -`&PathBuf`, and `Cow<_>`. It will also suggest you replace `.clone()` calls -with the appropriate `.to_owned()`/`to_string()` calls. - -### Why is this bad? -Requiring the argument to be of the specific size -makes the function less useful for no benefit; slices in the form of `&[T]` -or `&str` usually suffice and can be obtained from other types, too. - -### Known problems -There may be `fn(&Vec)`-typed references pointing to your function. -If you have them, you will get a compiler error after applying this lint's -suggestions. You then have the choice to undo your changes or change the -type of the reference. - -Note that if the function is part of your public interface, there may be -other crates referencing it, of which you may not be aware. Carefully -deprecate the function before applying the lint suggestions in this case. - -### Example -``` -fn foo(&Vec) { .. } -``` - -Use instead: -``` -fn foo(&[u32]) { .. } -``` \ No newline at end of file diff --git a/src/docs/ptr_as_ptr.txt b/src/docs/ptr_as_ptr.txt deleted file mode 100644 index 8fb35c4aae8f1..0000000000000 --- a/src/docs/ptr_as_ptr.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for `as` casts between raw pointers without changing its mutability, -namely `*const T` to `*const U` and `*mut T` to `*mut U`. - -### Why is this bad? -Though `as` casts between raw pointers is not terrible, `pointer::cast` is safer because -it cannot accidentally change the pointer's mutability nor cast the pointer to other types like `usize`. - -### Example -``` -let ptr: *const u32 = &42_u32; -let mut_ptr: *mut u32 = &mut 42_u32; -let _ = ptr as *const i32; -let _ = mut_ptr as *mut i32; -``` -Use instead: -``` -let ptr: *const u32 = &42_u32; -let mut_ptr: *mut u32 = &mut 42_u32; -let _ = ptr.cast::(); -let _ = mut_ptr.cast::(); -``` \ No newline at end of file diff --git a/src/docs/ptr_eq.txt b/src/docs/ptr_eq.txt deleted file mode 100644 index 06b36ca55e691..0000000000000 --- a/src/docs/ptr_eq.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Use `std::ptr::eq` when applicable - -### Why is this bad? -`ptr::eq` can be used to compare `&T` references -(which coerce to `*const T` implicitly) by their address rather than -comparing the values they point to. - -### Example -``` -let a = &[1, 2, 3]; -let b = &[1, 2, 3]; - -assert!(a as *const _ as usize == b as *const _ as usize); -``` -Use instead: -``` -let a = &[1, 2, 3]; -let b = &[1, 2, 3]; - -assert!(std::ptr::eq(a, b)); -``` \ No newline at end of file diff --git a/src/docs/ptr_offset_with_cast.txt b/src/docs/ptr_offset_with_cast.txt deleted file mode 100644 index f204e769bf4b8..0000000000000 --- a/src/docs/ptr_offset_with_cast.txt +++ /dev/null @@ -1,30 +0,0 @@ -### What it does -Checks for usage of the `offset` pointer method with a `usize` casted to an -`isize`. - -### Why is this bad? -If we’re always increasing the pointer address, we can avoid the numeric -cast by using the `add` method instead. - -### Example -``` -let vec = vec![b'a', b'b', b'c']; -let ptr = vec.as_ptr(); -let offset = 1_usize; - -unsafe { - ptr.offset(offset as isize); -} -``` - -Could be written: - -``` -let vec = vec![b'a', b'b', b'c']; -let ptr = vec.as_ptr(); -let offset = 1_usize; - -unsafe { - ptr.add(offset); -} -``` \ No newline at end of file diff --git a/src/docs/pub_use.txt b/src/docs/pub_use.txt deleted file mode 100644 index 407cafa01903f..0000000000000 --- a/src/docs/pub_use.txt +++ /dev/null @@ -1,28 +0,0 @@ -### What it does - -Restricts the usage of `pub use ...` - -### Why is this bad? - -`pub use` is usually fine, but a project may wish to limit `pub use` instances to prevent -unintentional exports or to encourage placing exported items directly in public modules - -### Example -``` -pub mod outer { - mod inner { - pub struct Test {} - } - pub use inner::Test; -} - -use outer::Test; -``` -Use instead: -``` -pub mod outer { - pub struct Test {} -} - -use outer::Test; -``` \ No newline at end of file diff --git a/src/docs/question_mark.txt b/src/docs/question_mark.txt deleted file mode 100644 index 4dc987be88132..0000000000000 --- a/src/docs/question_mark.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for expressions that could be replaced by the question mark operator. - -### Why is this bad? -Question mark usage is more idiomatic. - -### Example -``` -if option.is_none() { - return None; -} -``` - -Could be written: - -``` -option?; -``` \ No newline at end of file diff --git a/src/docs/range_minus_one.txt b/src/docs/range_minus_one.txt deleted file mode 100644 index fcb96dcc34edf..0000000000000 --- a/src/docs/range_minus_one.txt +++ /dev/null @@ -1,27 +0,0 @@ -### What it does -Checks for inclusive ranges where 1 is subtracted from -the upper bound, e.g., `x..=(y-1)`. - -### Why is this bad? -The code is more readable with an exclusive range -like `x..y`. - -### Known problems -This will cause a warning that cannot be fixed if -the consumer of the range only accepts a specific range type, instead of -the generic `RangeBounds` trait -([#3307](https://github.com/rust-lang/rust-clippy/issues/3307)). - -### Example -``` -for i in x..=(y-1) { - // .. -} -``` - -Use instead: -``` -for i in x..y { - // .. -} -``` \ No newline at end of file diff --git a/src/docs/range_plus_one.txt b/src/docs/range_plus_one.txt deleted file mode 100644 index 193c85f9cbc72..0000000000000 --- a/src/docs/range_plus_one.txt +++ /dev/null @@ -1,36 +0,0 @@ -### What it does -Checks for exclusive ranges where 1 is added to the -upper bound, e.g., `x..(y+1)`. - -### Why is this bad? -The code is more readable with an inclusive range -like `x..=y`. - -### Known problems -Will add unnecessary pair of parentheses when the -expression is not wrapped in a pair but starts with an opening parenthesis -and ends with a closing one. -I.e., `let _ = (f()+1)..(f()+1)` results in `let _ = ((f()+1)..=f())`. - -Also in many cases, inclusive ranges are still slower to run than -exclusive ranges, because they essentially add an extra branch that -LLVM may fail to hoist out of the loop. - -This will cause a warning that cannot be fixed if the consumer of the -range only accepts a specific range type, instead of the generic -`RangeBounds` trait -([#3307](https://github.com/rust-lang/rust-clippy/issues/3307)). - -### Example -``` -for i in x..(y+1) { - // .. -} -``` - -Use instead: -``` -for i in x..=y { - // .. -} -``` \ No newline at end of file diff --git a/src/docs/range_zip_with_len.txt b/src/docs/range_zip_with_len.txt deleted file mode 100644 index 24c1efec789a9..0000000000000 --- a/src/docs/range_zip_with_len.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for zipping a collection with the range of -`0.._.len()`. - -### Why is this bad? -The code is better expressed with `.enumerate()`. - -### Example -``` -let _ = x.iter().zip(0..x.len()); -``` - -Use instead: -``` -let _ = x.iter().enumerate(); -``` \ No newline at end of file diff --git a/src/docs/rc_buffer.txt b/src/docs/rc_buffer.txt deleted file mode 100644 index 82ac58eeb3081..0000000000000 --- a/src/docs/rc_buffer.txt +++ /dev/null @@ -1,27 +0,0 @@ -### What it does -Checks for `Rc` and `Arc` when `T` is a mutable buffer type such as `String` or `Vec`. - -### Why is this bad? -Expressions such as `Rc` usually have no advantage over `Rc`, since -it is larger and involves an extra level of indirection, and doesn't implement `Borrow`. - -While mutating a buffer type would still be possible with `Rc::get_mut()`, it only -works if there are no additional references yet, which usually defeats the purpose of -enclosing it in a shared ownership type. Instead, additionally wrapping the inner -type with an interior mutable container (such as `RefCell` or `Mutex`) would normally -be used. - -### Known problems -This pattern can be desirable to avoid the overhead of a `RefCell` or `Mutex` for -cases where mutation only happens before there are any additional references. - -### Example -``` -fn foo(interned: Rc) { ... } -``` - -Better: - -``` -fn foo(interned: Rc) { ... } -``` \ No newline at end of file diff --git a/src/docs/rc_clone_in_vec_init.txt b/src/docs/rc_clone_in_vec_init.txt deleted file mode 100644 index 6fc08aaf9ab57..0000000000000 --- a/src/docs/rc_clone_in_vec_init.txt +++ /dev/null @@ -1,29 +0,0 @@ -### What it does -Checks for reference-counted pointers (`Arc`, `Rc`, `rc::Weak`, and `sync::Weak`) -in `vec![elem; len]` - -### Why is this bad? -This will create `elem` once and clone it `len` times - doing so with `Arc`/`Rc`/`Weak` -is a bit misleading, as it will create references to the same pointer, rather -than different instances. - -### Example -``` -let v = vec![std::sync::Arc::new("some data".to_string()); 100]; -// or -let v = vec![std::rc::Rc::new("some data".to_string()); 100]; -``` -Use instead: -``` -// Initialize each value separately: -let mut data = Vec::with_capacity(100); -for _ in 0..100 { - data.push(std::rc::Rc::new("some data".to_string())); -} - -// Or if you want clones of the same reference, -// Create the reference beforehand to clarify that -// it should be cloned for each value -let data = std::rc::Rc::new("some data".to_string()); -let v = vec![data; 100]; -``` \ No newline at end of file diff --git a/src/docs/rc_mutex.txt b/src/docs/rc_mutex.txt deleted file mode 100644 index ed7a1e344d086..0000000000000 --- a/src/docs/rc_mutex.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Checks for `Rc>`. - -### Why is this bad? -`Rc` is used in single thread and `Mutex` is used in multi thread. -Consider using `Rc>` in single thread or `Arc>` in multi thread. - -### Known problems -Sometimes combining generic types can lead to the requirement that a -type use Rc in conjunction with Mutex. We must consider those cases false positives, but -alas they are quite hard to rule out. Luckily they are also rare. - -### Example -``` -use std::rc::Rc; -use std::sync::Mutex; -fn foo(interned: Rc>) { ... } -``` - -Better: - -``` -use std::rc::Rc; -use std::cell::RefCell -fn foo(interned: Rc>) { ... } -``` \ No newline at end of file diff --git a/src/docs/read_zero_byte_vec.txt b/src/docs/read_zero_byte_vec.txt deleted file mode 100644 index cef5604e01c08..0000000000000 --- a/src/docs/read_zero_byte_vec.txt +++ /dev/null @@ -1,30 +0,0 @@ -### What it does -This lint catches reads into a zero-length `Vec`. -Especially in the case of a call to `with_capacity`, this lint warns that read -gets the number of bytes from the `Vec`'s length, not its capacity. - -### Why is this bad? -Reading zero bytes is almost certainly not the intended behavior. - -### Known problems -In theory, a very unusual read implementation could assign some semantic meaning -to zero-byte reads. But it seems exceptionally unlikely that code intending to do -a zero-byte read would allocate a `Vec` for it. - -### Example -``` -use std::io; -fn foo(mut f: F) { - let mut data = Vec::with_capacity(100); - f.read(&mut data).unwrap(); -} -``` -Use instead: -``` -use std::io; -fn foo(mut f: F) { - let mut data = Vec::with_capacity(100); - data.resize(100, 0); - f.read(&mut data).unwrap(); -} -``` \ No newline at end of file diff --git a/src/docs/recursive_format_impl.txt b/src/docs/recursive_format_impl.txt deleted file mode 100644 index 32fffd84cf432..0000000000000 --- a/src/docs/recursive_format_impl.txt +++ /dev/null @@ -1,32 +0,0 @@ -### What it does -Checks for format trait implementations (e.g. `Display`) with a recursive call to itself -which uses `self` as a parameter. -This is typically done indirectly with the `write!` macro or with `to_string()`. - -### Why is this bad? -This will lead to infinite recursion and a stack overflow. - -### Example - -``` -use std::fmt; - -struct Structure(i32); -impl fmt::Display for Structure { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.to_string()) - } -} - -``` -Use instead: -``` -use std::fmt; - -struct Structure(i32); -impl fmt::Display for Structure { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.0) - } -} -``` \ No newline at end of file diff --git a/src/docs/redundant_allocation.txt b/src/docs/redundant_allocation.txt deleted file mode 100644 index 86bf51e8dfec7..0000000000000 --- a/src/docs/redundant_allocation.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for use of redundant allocations anywhere in the code. - -### Why is this bad? -Expressions such as `Rc<&T>`, `Rc>`, `Rc>`, `Rc>`, `Arc<&T>`, `Arc>`, -`Arc>`, `Arc>`, `Box<&T>`, `Box>`, `Box>`, `Box>`, add an unnecessary level of indirection. - -### Example -``` -fn foo(bar: Rc<&usize>) {} -``` - -Better: - -``` -fn foo(bar: &usize) {} -``` \ No newline at end of file diff --git a/src/docs/redundant_clone.txt b/src/docs/redundant_clone.txt deleted file mode 100644 index b29aed0b5e779..0000000000000 --- a/src/docs/redundant_clone.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for a redundant `clone()` (and its relatives) which clones an owned -value that is going to be dropped without further use. - -### Why is this bad? -It is not always possible for the compiler to eliminate useless -allocations and deallocations generated by redundant `clone()`s. - -### Known problems -False-negatives: analysis performed by this lint is conservative and limited. - -### Example -``` -{ - let x = Foo::new(); - call(x.clone()); - call(x.clone()); // this can just pass `x` -} - -["lorem", "ipsum"].join(" ").to_string(); - -Path::new("/a/b").join("c").to_path_buf(); -``` \ No newline at end of file diff --git a/src/docs/redundant_closure.txt b/src/docs/redundant_closure.txt deleted file mode 100644 index 0faa9513f97f2..0000000000000 --- a/src/docs/redundant_closure.txt +++ /dev/null @@ -1,25 +0,0 @@ -### What it does -Checks for closures which just call another function where -the function can be called directly. `unsafe` functions or calls where types -get adjusted are ignored. - -### Why is this bad? -Needlessly creating a closure adds code for no benefit -and gives the optimizer more work. - -### Known problems -If creating the closure inside the closure has a side- -effect then moving the closure creation out will change when that side- -effect runs. -See [#1439](https://github.com/rust-lang/rust-clippy/issues/1439) for more details. - -### Example -``` -xs.map(|x| foo(x)) -``` - -Use instead: -``` -// where `foo(_)` is a plain function that takes the exact argument type of `x`. -xs.map(foo) -``` \ No newline at end of file diff --git a/src/docs/redundant_closure_call.txt b/src/docs/redundant_closure_call.txt deleted file mode 100644 index 913d1a705e294..0000000000000 --- a/src/docs/redundant_closure_call.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Detects closures called in the same expression where they -are defined. - -### Why is this bad? -It is unnecessarily adding to the expression's -complexity. - -### Example -``` -let a = (|| 42)(); -``` - -Use instead: -``` -let a = 42; -``` \ No newline at end of file diff --git a/src/docs/redundant_closure_for_method_calls.txt b/src/docs/redundant_closure_for_method_calls.txt deleted file mode 100644 index 865510e14755a..0000000000000 --- a/src/docs/redundant_closure_for_method_calls.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for closures which only invoke a method on the closure -argument and can be replaced by referencing the method directly. - -### Why is this bad? -It's unnecessary to create the closure. - -### Example -``` -Some('a').map(|s| s.to_uppercase()); -``` -may be rewritten as -``` -Some('a').map(char::to_uppercase); -``` \ No newline at end of file diff --git a/src/docs/redundant_else.txt b/src/docs/redundant_else.txt deleted file mode 100644 index 3f4e86917603c..0000000000000 --- a/src/docs/redundant_else.txt +++ /dev/null @@ -1,30 +0,0 @@ -### What it does -Checks for `else` blocks that can be removed without changing semantics. - -### Why is this bad? -The `else` block adds unnecessary indentation and verbosity. - -### Known problems -Some may prefer to keep the `else` block for clarity. - -### Example -``` -fn my_func(count: u32) { - if count == 0 { - print!("Nothing to do"); - return; - } else { - print!("Moving on..."); - } -} -``` -Use instead: -``` -fn my_func(count: u32) { - if count == 0 { - print!("Nothing to do"); - return; - } - print!("Moving on..."); -} -``` \ No newline at end of file diff --git a/src/docs/redundant_feature_names.txt b/src/docs/redundant_feature_names.txt deleted file mode 100644 index 5bd6925ed47d0..0000000000000 --- a/src/docs/redundant_feature_names.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for feature names with prefix `use-`, `with-` or suffix `-support` - -### Why is this bad? -These prefixes and suffixes have no significant meaning. - -### Example -``` -[features] -default = ["use-abc", "with-def", "ghi-support"] -use-abc = [] // redundant -with-def = [] // redundant -ghi-support = [] // redundant -``` - -Use instead: -``` -[features] -default = ["abc", "def", "ghi"] -abc = [] -def = [] -ghi = [] -``` diff --git a/src/docs/redundant_field_names.txt b/src/docs/redundant_field_names.txt deleted file mode 100644 index 35f20a466b378..0000000000000 --- a/src/docs/redundant_field_names.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for fields in struct literals where shorthands -could be used. - -### Why is this bad? -If the field and variable names are the same, -the field name is redundant. - -### Example -``` -let bar: u8 = 123; - -struct Foo { - bar: u8, -} - -let foo = Foo { bar: bar }; -``` -the last line can be simplified to -``` -let foo = Foo { bar }; -``` \ No newline at end of file diff --git a/src/docs/redundant_pattern.txt b/src/docs/redundant_pattern.txt deleted file mode 100644 index 45f6cfc86702c..0000000000000 --- a/src/docs/redundant_pattern.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for patterns in the form `name @ _`. - -### Why is this bad? -It's almost always more readable to just use direct -bindings. - -### Example -``` -match v { - Some(x) => (), - y @ _ => (), -} -``` - -Use instead: -``` -match v { - Some(x) => (), - y => (), -} -``` \ No newline at end of file diff --git a/src/docs/redundant_pattern_matching.txt b/src/docs/redundant_pattern_matching.txt deleted file mode 100644 index 77b1021e0db3b..0000000000000 --- a/src/docs/redundant_pattern_matching.txt +++ /dev/null @@ -1,45 +0,0 @@ -### What it does -Lint for redundant pattern matching over `Result`, `Option`, -`std::task::Poll` or `std::net::IpAddr` - -### Why is this bad? -It's more concise and clear to just use the proper -utility function - -### Known problems -This will change the drop order for the matched type. Both `if let` and -`while let` will drop the value at the end of the block, both `if` and `while` will drop the -value before entering the block. For most types this change will not matter, but for a few -types this will not be an acceptable change (e.g. locks). See the -[reference](https://doc.rust-lang.org/reference/destructors.html#drop-scopes) for more about -drop order. - -### Example -``` -if let Ok(_) = Ok::(42) {} -if let Err(_) = Err::(42) {} -if let None = None::<()> {} -if let Some(_) = Some(42) {} -if let Poll::Pending = Poll::Pending::<()> {} -if let Poll::Ready(_) = Poll::Ready(42) {} -if let IpAddr::V4(_) = IpAddr::V4(Ipv4Addr::LOCALHOST) {} -if let IpAddr::V6(_) = IpAddr::V6(Ipv6Addr::LOCALHOST) {} -match Ok::(42) { - Ok(_) => true, - Err(_) => false, -}; -``` - -The more idiomatic use would be: - -``` -if Ok::(42).is_ok() {} -if Err::(42).is_err() {} -if None::<()>.is_none() {} -if Some(42).is_some() {} -if Poll::Pending::<()>.is_pending() {} -if Poll::Ready(42).is_ready() {} -if IpAddr::V4(Ipv4Addr::LOCALHOST).is_ipv4() {} -if IpAddr::V6(Ipv6Addr::LOCALHOST).is_ipv6() {} -Ok::(42).is_ok(); -``` \ No newline at end of file diff --git a/src/docs/redundant_pub_crate.txt b/src/docs/redundant_pub_crate.txt deleted file mode 100644 index a527bb5acda1e..0000000000000 --- a/src/docs/redundant_pub_crate.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for items declared `pub(crate)` that are not crate visible because they -are inside a private module. - -### Why is this bad? -Writing `pub(crate)` is misleading when it's redundant due to the parent -module's visibility. - -### Example -``` -mod internal { - pub(crate) fn internal_fn() { } -} -``` -This function is not visible outside the module and it can be declared with `pub` or -private visibility -``` -mod internal { - pub fn internal_fn() { } -} -``` \ No newline at end of file diff --git a/src/docs/redundant_slicing.txt b/src/docs/redundant_slicing.txt deleted file mode 100644 index 6798911ed76c0..0000000000000 --- a/src/docs/redundant_slicing.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks for redundant slicing expressions which use the full range, and -do not change the type. - -### Why is this bad? -It unnecessarily adds complexity to the expression. - -### Known problems -If the type being sliced has an implementation of `Index` -that actually changes anything then it can't be removed. However, this would be surprising -to people reading the code and should have a note with it. - -### Example -``` -fn get_slice(x: &[u32]) -> &[u32] { - &x[..] -} -``` -Use instead: -``` -fn get_slice(x: &[u32]) -> &[u32] { - x -} -``` \ No newline at end of file diff --git a/src/docs/redundant_static_lifetimes.txt b/src/docs/redundant_static_lifetimes.txt deleted file mode 100644 index edb8e7b5c62bf..0000000000000 --- a/src/docs/redundant_static_lifetimes.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for constants and statics with an explicit `'static` lifetime. - -### Why is this bad? -Adding `'static` to every reference can create very -complicated types. - -### Example -``` -const FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] = -&[...] -static FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] = -&[...] -``` -This code can be rewritten as -``` - const FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...] - static FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...] -``` \ No newline at end of file diff --git a/src/docs/ref_binding_to_reference.txt b/src/docs/ref_binding_to_reference.txt deleted file mode 100644 index dc391cd988e58..0000000000000 --- a/src/docs/ref_binding_to_reference.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for `ref` bindings which create a reference to a reference. - -### Why is this bad? -The address-of operator at the use site is clearer about the need for a reference. - -### Example -``` -let x = Some(""); -if let Some(ref x) = x { - // use `x` here -} -``` - -Use instead: -``` -let x = Some(""); -if let Some(x) = x { - // use `&x` here -} -``` \ No newline at end of file diff --git a/src/docs/ref_option_ref.txt b/src/docs/ref_option_ref.txt deleted file mode 100644 index 951c7bd7f7ec6..0000000000000 --- a/src/docs/ref_option_ref.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for usage of `&Option<&T>`. - -### Why is this bad? -Since `&` is Copy, it's useless to have a -reference on `Option<&T>`. - -### Known problems -It may be irrelevant to use this lint on -public API code as it will make a breaking change to apply it. - -### Example -``` -let x: &Option<&u32> = &Some(&0u32); -``` -Use instead: -``` -let x: Option<&u32> = Some(&0u32); -``` \ No newline at end of file diff --git a/src/docs/repeat_once.txt b/src/docs/repeat_once.txt deleted file mode 100644 index 3ba189c1945d7..0000000000000 --- a/src/docs/repeat_once.txt +++ /dev/null @@ -1,25 +0,0 @@ -### What it does -Checks for usage of `.repeat(1)` and suggest the following method for each types. -- `.to_string()` for `str` -- `.clone()` for `String` -- `.to_vec()` for `slice` - -The lint will evaluate constant expressions and values as arguments of `.repeat(..)` and emit a message if -they are equivalent to `1`. (Related discussion in [rust-clippy#7306](https://github.com/rust-lang/rust-clippy/issues/7306)) - -### Why is this bad? -For example, `String.repeat(1)` is equivalent to `.clone()`. If cloning -the string is the intention behind this, `clone()` should be used. - -### Example -``` -fn main() { - let x = String::from("hello world").repeat(1); -} -``` -Use instead: -``` -fn main() { - let x = String::from("hello world").clone(); -} -``` \ No newline at end of file diff --git a/src/docs/rest_pat_in_fully_bound_structs.txt b/src/docs/rest_pat_in_fully_bound_structs.txt deleted file mode 100644 index 40ebbe754a372..0000000000000 --- a/src/docs/rest_pat_in_fully_bound_structs.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks for unnecessary '..' pattern binding on struct when all fields are explicitly matched. - -### Why is this bad? -Correctness and readability. It's like having a wildcard pattern after -matching all enum variants explicitly. - -### Example -``` -let a = A { a: 5 }; - -match a { - A { a: 5, .. } => {}, - _ => {}, -} -``` - -Use instead: -``` -match a { - A { a: 5 } => {}, - _ => {}, -} -``` \ No newline at end of file diff --git a/src/docs/result_large_err.txt b/src/docs/result_large_err.txt deleted file mode 100644 index e5fab3c5cfcfa..0000000000000 --- a/src/docs/result_large_err.txt +++ /dev/null @@ -1,36 +0,0 @@ -### What it does -Checks for functions that return `Result` with an unusually large -`Err`-variant. - -### Why is this bad? -A `Result` is at least as large as the `Err`-variant. While we -expect that variant to be seldomly used, the compiler needs to reserve -and move that much memory every single time. - -### Known problems -The size determined by Clippy is platform-dependent. - -### Examples -``` -pub enum ParseError { - UnparsedBytes([u8; 512]), - UnexpectedEof, -} - -// The `Result` has at least 512 bytes, even in the `Ok`-case -pub fn parse() -> Result<(), ParseError> { - Ok(()) -} -``` -should be -``` -pub enum ParseError { - UnparsedBytes(Box<[u8; 512]>), - UnexpectedEof, -} - -// The `Result` is slightly larger than a pointer -pub fn parse() -> Result<(), ParseError> { - Ok(()) -} -``` \ No newline at end of file diff --git a/src/docs/result_map_or_into_option.txt b/src/docs/result_map_or_into_option.txt deleted file mode 100644 index 899d98c307c8f..0000000000000 --- a/src/docs/result_map_or_into_option.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for usage of `_.map_or(None, Some)`. - -### Why is this bad? -Readability, this can be written more concisely as -`_.ok()`. - -### Example -``` -assert_eq!(Some(1), r.map_or(None, Some)); -``` - -Use instead: -``` -assert_eq!(Some(1), r.ok()); -``` \ No newline at end of file diff --git a/src/docs/result_map_unit_fn.txt b/src/docs/result_map_unit_fn.txt deleted file mode 100644 index 3455c5c1f9b8a..0000000000000 --- a/src/docs/result_map_unit_fn.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Checks for usage of `result.map(f)` where f is a function -or closure that returns the unit type `()`. - -### Why is this bad? -Readability, this can be written more clearly with -an if let statement - -### Example -``` -let x: Result = do_stuff(); -x.map(log_err_msg); -x.map(|msg| log_err_msg(format_msg(msg))); -``` - -The correct use would be: - -``` -let x: Result = do_stuff(); -if let Ok(msg) = x { - log_err_msg(msg); -}; -if let Ok(msg) = x { - log_err_msg(format_msg(msg)); -}; -``` \ No newline at end of file diff --git a/src/docs/result_unit_err.txt b/src/docs/result_unit_err.txt deleted file mode 100644 index 7c8ec2ffcf948..0000000000000 --- a/src/docs/result_unit_err.txt +++ /dev/null @@ -1,40 +0,0 @@ -### What it does -Checks for public functions that return a `Result` -with an `Err` type of `()`. It suggests using a custom type that -implements `std::error::Error`. - -### Why is this bad? -Unit does not implement `Error` and carries no -further information about what went wrong. - -### Known problems -Of course, this lint assumes that `Result` is used -for a fallible operation (which is after all the intended use). However -code may opt to (mis)use it as a basic two-variant-enum. In that case, -the suggestion is misguided, and the code should use a custom enum -instead. - -### Examples -``` -pub fn read_u8() -> Result { Err(()) } -``` -should become -``` -use std::fmt; - -#[derive(Debug)] -pub struct EndOfStream; - -impl fmt::Display for EndOfStream { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "End of Stream") - } -} - -impl std::error::Error for EndOfStream { } - -pub fn read_u8() -> Result { Err(EndOfStream) } -``` - -Note that there are crates that simplify creating the error type, e.g. -[`thiserror`](https://docs.rs/thiserror). \ No newline at end of file diff --git a/src/docs/return_self_not_must_use.txt b/src/docs/return_self_not_must_use.txt deleted file mode 100644 index 4a4fd2c6e517b..0000000000000 --- a/src/docs/return_self_not_must_use.txt +++ /dev/null @@ -1,46 +0,0 @@ -### What it does -This lint warns when a method returning `Self` doesn't have the `#[must_use]` attribute. - -### Why is this bad? -Methods returning `Self` often create new values, having the `#[must_use]` attribute -prevents users from "forgetting" to use the newly created value. - -The `#[must_use]` attribute can be added to the type itself to ensure that instances -are never forgotten. Functions returning a type marked with `#[must_use]` will not be -linted, as the usage is already enforced by the type attribute. - -### Limitations -This lint is only applied on methods taking a `self` argument. It would be mostly noise -if it was added on constructors for example. - -### Example -``` -pub struct Bar; -impl Bar { - // Missing attribute - pub fn bar(&self) -> Self { - Self - } -} -``` - -Use instead: -``` -// It's better to have the `#[must_use]` attribute on the method like this: -pub struct Bar; -impl Bar { - #[must_use] - pub fn bar(&self) -> Self { - Self - } -} - -// Or on the type definition like this: -#[must_use] -pub struct Bar; -impl Bar { - pub fn bar(&self) -> Self { - Self - } -} -``` \ No newline at end of file diff --git a/src/docs/reversed_empty_ranges.txt b/src/docs/reversed_empty_ranges.txt deleted file mode 100644 index 39f4811938668..0000000000000 --- a/src/docs/reversed_empty_ranges.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Checks for range expressions `x..y` where both `x` and `y` -are constant and `x` is greater or equal to `y`. - -### Why is this bad? -Empty ranges yield no values so iterating them is a no-op. -Moreover, trying to use a reversed range to index a slice will panic at run-time. - -### Example -``` -fn main() { - (10..=0).for_each(|x| println!("{}", x)); - - let arr = [1, 2, 3, 4, 5]; - let sub = &arr[3..1]; -} -``` -Use instead: -``` -fn main() { - (0..=10).rev().for_each(|x| println!("{}", x)); - - let arr = [1, 2, 3, 4, 5]; - let sub = &arr[1..3]; -} -``` \ No newline at end of file diff --git a/src/docs/same_functions_in_if_condition.txt b/src/docs/same_functions_in_if_condition.txt deleted file mode 100644 index a0a90eec681e6..0000000000000 --- a/src/docs/same_functions_in_if_condition.txt +++ /dev/null @@ -1,41 +0,0 @@ -### What it does -Checks for consecutive `if`s with the same function call. - -### Why is this bad? -This is probably a copy & paste error. -Despite the fact that function can have side effects and `if` works as -intended, such an approach is implicit and can be considered a "code smell". - -### Example -``` -if foo() == bar { - … -} else if foo() == bar { - … -} -``` - -This probably should be: -``` -if foo() == bar { - … -} else if foo() == baz { - … -} -``` - -or if the original code was not a typo and called function mutates a state, -consider move the mutation out of the `if` condition to avoid similarity to -a copy & paste error: - -``` -let first = foo(); -if first == bar { - … -} else { - let second = foo(); - if second == bar { - … - } -} -``` \ No newline at end of file diff --git a/src/docs/same_item_push.txt b/src/docs/same_item_push.txt deleted file mode 100644 index 7e724073f8aa7..0000000000000 --- a/src/docs/same_item_push.txt +++ /dev/null @@ -1,29 +0,0 @@ -### What it does -Checks whether a for loop is being used to push a constant -value into a Vec. - -### Why is this bad? -This kind of operation can be expressed more succinctly with -`vec![item; SIZE]` or `vec.resize(NEW_SIZE, item)` and using these alternatives may also -have better performance. - -### Example -``` -let item1 = 2; -let item2 = 3; -let mut vec: Vec = Vec::new(); -for _ in 0..20 { - vec.push(item1); -} -for _ in 0..30 { - vec.push(item2); -} -``` - -Use instead: -``` -let item1 = 2; -let item2 = 3; -let mut vec: Vec = vec![item1; 20]; -vec.resize(20 + 30, item2); -``` \ No newline at end of file diff --git a/src/docs/same_name_method.txt b/src/docs/same_name_method.txt deleted file mode 100644 index 792dd717fc645..0000000000000 --- a/src/docs/same_name_method.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -It lints if a struct has two methods with the same name: -one from a trait, another not from trait. - -### Why is this bad? -Confusing. - -### Example -``` -trait T { - fn foo(&self) {} -} - -struct S; - -impl T for S { - fn foo(&self) {} -} - -impl S { - fn foo(&self) {} -} -``` \ No newline at end of file diff --git a/src/docs/search_is_some.txt b/src/docs/search_is_some.txt deleted file mode 100644 index 67292d54585f2..0000000000000 --- a/src/docs/search_is_some.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks for an iterator or string search (such as `find()`, -`position()`, or `rposition()`) followed by a call to `is_some()` or `is_none()`. - -### Why is this bad? -Readability, this can be written more concisely as: -* `_.any(_)`, or `_.contains(_)` for `is_some()`, -* `!_.any(_)`, or `!_.contains(_)` for `is_none()`. - -### Example -``` -let vec = vec![1]; -vec.iter().find(|x| **x == 0).is_some(); - -"hello world".find("world").is_none(); -``` - -Use instead: -``` -let vec = vec![1]; -vec.iter().any(|x| *x == 0); - -!"hello world".contains("world"); -``` \ No newline at end of file diff --git a/src/docs/self_assignment.txt b/src/docs/self_assignment.txt deleted file mode 100644 index ea60ea077ab5f..0000000000000 --- a/src/docs/self_assignment.txt +++ /dev/null @@ -1,32 +0,0 @@ -### What it does -Checks for explicit self-assignments. - -### Why is this bad? -Self-assignments are redundant and unlikely to be -intentional. - -### Known problems -If expression contains any deref coercions or -indexing operations they are assumed not to have any side effects. - -### Example -``` -struct Event { - x: i32, -} - -fn copy_position(a: &mut Event, b: &Event) { - a.x = a.x; -} -``` - -Should be: -``` -struct Event { - x: i32, -} - -fn copy_position(a: &mut Event, b: &Event) { - a.x = b.x; -} -``` \ No newline at end of file diff --git a/src/docs/self_named_constructors.txt b/src/docs/self_named_constructors.txt deleted file mode 100644 index a01669a84542d..0000000000000 --- a/src/docs/self_named_constructors.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Warns when constructors have the same name as their types. - -### Why is this bad? -Repeating the name of the type is redundant. - -### Example -``` -struct Foo {} - -impl Foo { - pub fn foo() -> Foo { - Foo {} - } -} -``` -Use instead: -``` -struct Foo {} - -impl Foo { - pub fn new() -> Foo { - Foo {} - } -} -``` \ No newline at end of file diff --git a/src/docs/self_named_module_files.txt b/src/docs/self_named_module_files.txt deleted file mode 100644 index 73e8051362883..0000000000000 --- a/src/docs/self_named_module_files.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks that module layout uses only `mod.rs` files. - -### Why is this bad? -Having multiple module layout styles in a project can be confusing. - -### Example -``` -src/ - stuff/ - stuff_files.rs - stuff.rs - lib.rs -``` -Use instead: -``` -src/ - stuff/ - stuff_files.rs - mod.rs - lib.rs -``` \ No newline at end of file diff --git a/src/docs/semicolon_if_nothing_returned.txt b/src/docs/semicolon_if_nothing_returned.txt deleted file mode 100644 index 30c963ca211b3..0000000000000 --- a/src/docs/semicolon_if_nothing_returned.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Looks for blocks of expressions and fires if the last expression returns -`()` but is not followed by a semicolon. - -### Why is this bad? -The semicolon might be optional but when extending the block with new -code, it doesn't require a change in previous last line. - -### Example -``` -fn main() { - println!("Hello world") -} -``` -Use instead: -``` -fn main() { - println!("Hello world"); -} -``` \ No newline at end of file diff --git a/src/docs/separated_literal_suffix.txt b/src/docs/separated_literal_suffix.txt deleted file mode 100644 index 226a6b8a98766..0000000000000 --- a/src/docs/separated_literal_suffix.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Warns if literal suffixes are separated by an underscore. -To enforce separated literal suffix style, -see the `unseparated_literal_suffix` lint. - -### Why is this bad? -Suffix style should be consistent. - -### Example -``` -123832_i32 -``` - -Use instead: -``` -123832i32 -``` \ No newline at end of file diff --git a/src/docs/serde_api_misuse.txt b/src/docs/serde_api_misuse.txt deleted file mode 100644 index 8a3c89ac11adc..0000000000000 --- a/src/docs/serde_api_misuse.txt +++ /dev/null @@ -1,10 +0,0 @@ -### What it does -Checks for mis-uses of the serde API. - -### Why is this bad? -Serde is very finnicky about how its API should be -used, but the type system can't be used to enforce it (yet?). - -### Example -Implementing `Visitor::visit_string` but not -`Visitor::visit_str`. \ No newline at end of file diff --git a/src/docs/shadow_reuse.txt b/src/docs/shadow_reuse.txt deleted file mode 100644 index 9eb8e7ad164bb..0000000000000 --- a/src/docs/shadow_reuse.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Checks for bindings that shadow other bindings already in -scope, while reusing the original value. - -### Why is this bad? -Not too much, in fact it's a common pattern in Rust -code. Still, some argue that name shadowing like this hurts readability, -because a value may be bound to different things depending on position in -the code. - -### Example -``` -let x = 2; -let x = x + 1; -``` -use different variable name: -``` -let x = 2; -let y = x + 1; -``` \ No newline at end of file diff --git a/src/docs/shadow_same.txt b/src/docs/shadow_same.txt deleted file mode 100644 index 3cd96f560a5e2..0000000000000 --- a/src/docs/shadow_same.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for bindings that shadow other bindings already in -scope, while just changing reference level or mutability. - -### Why is this bad? -Not much, in fact it's a very common pattern in Rust -code. Still, some may opt to avoid it in their code base, they can set this -lint to `Warn`. - -### Example -``` -let x = &x; -``` - -Use instead: -``` -let y = &x; // use different variable name -``` \ No newline at end of file diff --git a/src/docs/shadow_unrelated.txt b/src/docs/shadow_unrelated.txt deleted file mode 100644 index 436251c520a98..0000000000000 --- a/src/docs/shadow_unrelated.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for bindings that shadow other bindings already in -scope, either without an initialization or with one that does not even use -the original value. - -### Why is this bad? -Name shadowing can hurt readability, especially in -large code bases, because it is easy to lose track of the active binding at -any place in the code. This can be alleviated by either giving more specific -names to bindings or introducing more scopes to contain the bindings. - -### Example -``` -let x = y; -let x = z; // shadows the earlier binding -``` - -Use instead: -``` -let x = y; -let w = z; // use different variable name -``` \ No newline at end of file diff --git a/src/docs/short_circuit_statement.txt b/src/docs/short_circuit_statement.txt deleted file mode 100644 index 31492bed03d4b..0000000000000 --- a/src/docs/short_circuit_statement.txt +++ /dev/null @@ -1,14 +0,0 @@ -### What it does -Checks for the use of short circuit boolean conditions as -a -statement. - -### Why is this bad? -Using a short circuit boolean condition as a statement -may hide the fact that the second part is executed or not depending on the -outcome of the first part. - -### Example -``` -f() && g(); // We should write `if f() { g(); }`. -``` \ No newline at end of file diff --git a/src/docs/should_implement_trait.txt b/src/docs/should_implement_trait.txt deleted file mode 100644 index 02e74751ae034..0000000000000 --- a/src/docs/should_implement_trait.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for methods that should live in a trait -implementation of a `std` trait (see [llogiq's blog -post](http://llogiq.github.io/2015/07/30/traits.html) for further -information) instead of an inherent implementation. - -### Why is this bad? -Implementing the traits improve ergonomics for users of -the code, often with very little cost. Also people seeing a `mul(...)` -method -may expect `*` to work equally, so you should have good reason to disappoint -them. - -### Example -``` -struct X; -impl X { - fn add(&self, other: &X) -> X { - // .. - } -} -``` \ No newline at end of file diff --git a/src/docs/significant_drop_in_scrutinee.txt b/src/docs/significant_drop_in_scrutinee.txt deleted file mode 100644 index f869def0ddb7c..0000000000000 --- a/src/docs/significant_drop_in_scrutinee.txt +++ /dev/null @@ -1,43 +0,0 @@ -### What it does -Check for temporaries returned from function calls in a match scrutinee that have the -`clippy::has_significant_drop` attribute. - -### Why is this bad? -The `clippy::has_significant_drop` attribute can be added to types whose Drop impls have -an important side-effect, such as unlocking a mutex, making it important for users to be -able to accurately understand their lifetimes. When a temporary is returned in a function -call in a match scrutinee, its lifetime lasts until the end of the match block, which may -be surprising. - -For `Mutex`es this can lead to a deadlock. This happens when the match scrutinee uses a -function call that returns a `MutexGuard` and then tries to lock again in one of the match -arms. In that case the `MutexGuard` in the scrutinee will not be dropped until the end of -the match block and thus will not unlock. - -### Example -``` -let mutex = Mutex::new(State {}); - -match mutex.lock().unwrap().foo() { - true => { - mutex.lock().unwrap().bar(); // Deadlock! - } - false => {} -}; - -println!("All done!"); -``` -Use instead: -``` -let mutex = Mutex::new(State {}); - -let is_foo = mutex.lock().unwrap().foo(); -match is_foo { - true => { - mutex.lock().unwrap().bar(); - } - false => {} -}; - -println!("All done!"); -``` \ No newline at end of file diff --git a/src/docs/similar_names.txt b/src/docs/similar_names.txt deleted file mode 100644 index f9eff21b67935..0000000000000 --- a/src/docs/similar_names.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for names that are very similar and thus confusing. - -Note: this lint looks for similar names throughout each -scope. To allow it, you need to allow it on the scope -level, not on the name that is reported. - -### Why is this bad? -It's hard to distinguish between names that differ only -by a single character. - -### Example -``` -let checked_exp = something; -let checked_expr = something_else; -``` \ No newline at end of file diff --git a/src/docs/single_char_add_str.txt b/src/docs/single_char_add_str.txt deleted file mode 100644 index cf23dc0c89bbb..0000000000000 --- a/src/docs/single_char_add_str.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Warns when using `push_str`/`insert_str` with a single-character string literal -where `push`/`insert` with a `char` would work fine. - -### Why is this bad? -It's less clear that we are pushing a single character. - -### Example -``` -string.insert_str(0, "R"); -string.push_str("R"); -``` - -Use instead: -``` -string.insert(0, 'R'); -string.push('R'); -``` \ No newline at end of file diff --git a/src/docs/single_char_lifetime_names.txt b/src/docs/single_char_lifetime_names.txt deleted file mode 100644 index 92dd24bf247ec..0000000000000 --- a/src/docs/single_char_lifetime_names.txt +++ /dev/null @@ -1,28 +0,0 @@ -### What it does -Checks for lifetimes with names which are one character -long. - -### Why is this bad? -A single character is likely not enough to express the -purpose of a lifetime. Using a longer name can make code -easier to understand, especially for those who are new to -Rust. - -### Known problems -Rust programmers and learning resources tend to use single -character lifetimes, so this lint is at odds with the -ecosystem at large. In addition, the lifetime's purpose may -be obvious or, rarely, expressible in one character. - -### Example -``` -struct DiagnosticCtx<'a> { - source: &'a str, -} -``` -Use instead: -``` -struct DiagnosticCtx<'src> { - source: &'src str, -} -``` \ No newline at end of file diff --git a/src/docs/single_char_pattern.txt b/src/docs/single_char_pattern.txt deleted file mode 100644 index 9e5ad1e7d6395..0000000000000 --- a/src/docs/single_char_pattern.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Checks for string methods that receive a single-character -`str` as an argument, e.g., `_.split("x")`. - -### Why is this bad? -Performing these methods using a `char` is faster than -using a `str`. - -### Known problems -Does not catch multi-byte unicode characters. - -### Example -``` -_.split("x"); -``` - -Use instead: -``` -_.split('x'); -``` \ No newline at end of file diff --git a/src/docs/single_component_path_imports.txt b/src/docs/single_component_path_imports.txt deleted file mode 100644 index 3a026388183e1..0000000000000 --- a/src/docs/single_component_path_imports.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checking for imports with single component use path. - -### Why is this bad? -Import with single component use path such as `use cratename;` -is not necessary, and thus should be removed. - -### Example -``` -use regex; - -fn main() { - regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap(); -} -``` -Better as -``` -fn main() { - regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap(); -} -``` \ No newline at end of file diff --git a/src/docs/single_element_loop.txt b/src/docs/single_element_loop.txt deleted file mode 100644 index 6f0c15a85f774..0000000000000 --- a/src/docs/single_element_loop.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks whether a for loop has a single element. - -### Why is this bad? -There is no reason to have a loop of a -single element. - -### Example -``` -let item1 = 2; -for item in &[item1] { - println!("{}", item); -} -``` - -Use instead: -``` -let item1 = 2; -let item = &item1; -println!("{}", item); -``` \ No newline at end of file diff --git a/src/docs/single_match.txt b/src/docs/single_match.txt deleted file mode 100644 index 31dde4da8489c..0000000000000 --- a/src/docs/single_match.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for matches with a single arm where an `if let` -will usually suffice. - -### Why is this bad? -Just readability – `if let` nests less than a `match`. - -### Example -``` -match x { - Some(ref foo) => bar(foo), - _ => (), -} -``` - -Use instead: -``` -if let Some(ref foo) = x { - bar(foo); -} -``` \ No newline at end of file diff --git a/src/docs/single_match_else.txt b/src/docs/single_match_else.txt deleted file mode 100644 index 29a447af09b86..0000000000000 --- a/src/docs/single_match_else.txt +++ /dev/null @@ -1,29 +0,0 @@ -### What it does -Checks for matches with two arms where an `if let else` will -usually suffice. - -### Why is this bad? -Just readability – `if let` nests less than a `match`. - -### Known problems -Personal style preferences may differ. - -### Example -Using `match`: - -``` -match x { - Some(ref foo) => bar(foo), - _ => bar(&other_ref), -} -``` - -Using `if let` with `else`: - -``` -if let Some(ref foo) = x { - bar(foo); -} else { - bar(&other_ref); -} -``` \ No newline at end of file diff --git a/src/docs/size_of_in_element_count.txt b/src/docs/size_of_in_element_count.txt deleted file mode 100644 index d893ec6a2a014..0000000000000 --- a/src/docs/size_of_in_element_count.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Detects expressions where -`size_of::` or `size_of_val::` is used as a -count of elements of type `T` - -### Why is this bad? -These functions expect a count -of `T` and not a number of bytes - -### Example -``` -const SIZE: usize = 128; -let x = [2u8; SIZE]; -let mut y = [2u8; SIZE]; -unsafe { copy_nonoverlapping(x.as_ptr(), y.as_mut_ptr(), size_of::() * SIZE) }; -``` \ No newline at end of file diff --git a/src/docs/skip_while_next.txt b/src/docs/skip_while_next.txt deleted file mode 100644 index 1ec8a3a28d5b0..0000000000000 --- a/src/docs/skip_while_next.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for usage of `_.skip_while(condition).next()`. - -### Why is this bad? -Readability, this can be written more concisely as -`_.find(!condition)`. - -### Example -``` -vec.iter().skip_while(|x| **x == 0).next(); -``` - -Use instead: -``` -vec.iter().find(|x| **x != 0); -``` \ No newline at end of file diff --git a/src/docs/slow_vector_initialization.txt b/src/docs/slow_vector_initialization.txt deleted file mode 100644 index 53442e1796541..0000000000000 --- a/src/docs/slow_vector_initialization.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks slow zero-filled vector initialization - -### Why is this bad? -These structures are non-idiomatic and less efficient than simply using -`vec![0; len]`. - -### Example -``` -let mut vec1 = Vec::with_capacity(len); -vec1.resize(len, 0); - -let mut vec1 = Vec::with_capacity(len); -vec1.resize(vec1.capacity(), 0); - -let mut vec2 = Vec::with_capacity(len); -vec2.extend(repeat(0).take(len)); -``` - -Use instead: -``` -let mut vec1 = vec![0; len]; -let mut vec2 = vec![0; len]; -``` \ No newline at end of file diff --git a/src/docs/stable_sort_primitive.txt b/src/docs/stable_sort_primitive.txt deleted file mode 100644 index 6465dbee46b10..0000000000000 --- a/src/docs/stable_sort_primitive.txt +++ /dev/null @@ -1,31 +0,0 @@ -### What it does -When sorting primitive values (integers, bools, chars, as well -as arrays, slices, and tuples of such items), it is typically better to -use an unstable sort than a stable sort. - -### Why is this bad? -Typically, using a stable sort consumes more memory and cpu cycles. -Because values which compare equal are identical, preserving their -relative order (the guarantee that a stable sort provides) means -nothing, while the extra costs still apply. - -### Known problems - -As pointed out in -[issue #8241](https://github.com/rust-lang/rust-clippy/issues/8241), -a stable sort can instead be significantly faster for certain scenarios -(eg. when a sorted vector is extended with new data and resorted). - -For more information and benchmarking results, please refer to the -issue linked above. - -### Example -``` -let mut vec = vec![2, 1, 3]; -vec.sort(); -``` -Use instead: -``` -let mut vec = vec![2, 1, 3]; -vec.sort_unstable(); -``` \ No newline at end of file diff --git a/src/docs/std_instead_of_alloc.txt b/src/docs/std_instead_of_alloc.txt deleted file mode 100644 index c2d32704e5055..0000000000000 --- a/src/docs/std_instead_of_alloc.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does - -Finds items imported through `std` when available through `alloc`. - -### Why is this bad? - -Crates which have `no_std` compatibility and require alloc may wish to ensure types are imported from -alloc to ensure disabling `std` does not cause the crate to fail to compile. This lint is also useful -for crates migrating to become `no_std` compatible. - -### Example -``` -use std::vec::Vec; -``` -Use instead: -``` -use alloc::vec::Vec; -``` \ No newline at end of file diff --git a/src/docs/std_instead_of_core.txt b/src/docs/std_instead_of_core.txt deleted file mode 100644 index f1e1518c6a62a..0000000000000 --- a/src/docs/std_instead_of_core.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does - -Finds items imported through `std` when available through `core`. - -### Why is this bad? - -Crates which have `no_std` compatibility may wish to ensure types are imported from core to ensure -disabling `std` does not cause the crate to fail to compile. This lint is also useful for crates -migrating to become `no_std` compatible. - -### Example -``` -use std::hash::Hasher; -``` -Use instead: -``` -use core::hash::Hasher; -``` \ No newline at end of file diff --git a/src/docs/str_to_string.txt b/src/docs/str_to_string.txt deleted file mode 100644 index a24755223747b..0000000000000 --- a/src/docs/str_to_string.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -This lint checks for `.to_string()` method calls on values of type `&str`. - -### Why is this bad? -The `to_string` method is also used on other types to convert them to a string. -When called on a `&str` it turns the `&str` into the owned variant `String`, which can be better -expressed with `.to_owned()`. - -### Example -``` -// example code where clippy issues a warning -let _ = "str".to_string(); -``` -Use instead: -``` -// example code which does not raise clippy warning -let _ = "str".to_owned(); -``` \ No newline at end of file diff --git a/src/docs/string_add.txt b/src/docs/string_add.txt deleted file mode 100644 index 23dafd0d0333d..0000000000000 --- a/src/docs/string_add.txt +++ /dev/null @@ -1,27 +0,0 @@ -### What it does -Checks for all instances of `x + _` where `x` is of type -`String`, but only if [`string_add_assign`](#string_add_assign) does *not* -match. - -### Why is this bad? -It's not bad in and of itself. However, this particular -`Add` implementation is asymmetric (the other operand need not be `String`, -but `x` does), while addition as mathematically defined is symmetric, also -the `String::push_str(_)` function is a perfectly good replacement. -Therefore, some dislike it and wish not to have it in their code. - -That said, other people think that string addition, having a long tradition -in other languages is actually fine, which is why we decided to make this -particular lint `allow` by default. - -### Example -``` -let x = "Hello".to_owned(); -x + ", World"; -``` - -Use instead: -``` -let mut x = "Hello".to_owned(); -x.push_str(", World"); -``` \ No newline at end of file diff --git a/src/docs/string_add_assign.txt b/src/docs/string_add_assign.txt deleted file mode 100644 index 7438be855db8c..0000000000000 --- a/src/docs/string_add_assign.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for string appends of the form `x = x + y` (without -`let`!). - -### Why is this bad? -It's not really bad, but some people think that the -`.push_str(_)` method is more readable. - -### Example -``` -let mut x = "Hello".to_owned(); -x = x + ", World"; - -// More readable -x += ", World"; -x.push_str(", World"); -``` \ No newline at end of file diff --git a/src/docs/string_extend_chars.txt b/src/docs/string_extend_chars.txt deleted file mode 100644 index 619ea3e118673..0000000000000 --- a/src/docs/string_extend_chars.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for the use of `.extend(s.chars())` where s is a -`&str` or `String`. - -### Why is this bad? -`.push_str(s)` is clearer - -### Example -``` -let abc = "abc"; -let def = String::from("def"); -let mut s = String::new(); -s.extend(abc.chars()); -s.extend(def.chars()); -``` -The correct use would be: -``` -let abc = "abc"; -let def = String::from("def"); -let mut s = String::new(); -s.push_str(abc); -s.push_str(&def); -``` \ No newline at end of file diff --git a/src/docs/string_from_utf8_as_bytes.txt b/src/docs/string_from_utf8_as_bytes.txt deleted file mode 100644 index 9102d73471cb2..0000000000000 --- a/src/docs/string_from_utf8_as_bytes.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Check if the string is transformed to byte array and casted back to string. - -### Why is this bad? -It's unnecessary, the string can be used directly. - -### Example -``` -std::str::from_utf8(&"Hello World!".as_bytes()[6..11]).unwrap(); -``` - -Use instead: -``` -&"Hello World!"[6..11]; -``` \ No newline at end of file diff --git a/src/docs/string_lit_as_bytes.txt b/src/docs/string_lit_as_bytes.txt deleted file mode 100644 index a125b97ed6567..0000000000000 --- a/src/docs/string_lit_as_bytes.txt +++ /dev/null @@ -1,39 +0,0 @@ -### What it does -Checks for the `as_bytes` method called on string literals -that contain only ASCII characters. - -### Why is this bad? -Byte string literals (e.g., `b"foo"`) can be used -instead. They are shorter but less discoverable than `as_bytes()`. - -### Known problems -`"str".as_bytes()` and the suggested replacement of `b"str"` are not -equivalent because they have different types. The former is `&[u8]` -while the latter is `&[u8; 3]`. That means in general they will have a -different set of methods and different trait implementations. - -``` -fn f(v: Vec) {} - -f("...".as_bytes().to_owned()); // works -f(b"...".to_owned()); // does not work, because arg is [u8; 3] not Vec - -fn g(r: impl std::io::Read) {} - -g("...".as_bytes()); // works -g(b"..."); // does not work -``` - -The actual equivalent of `"str".as_bytes()` with the same type is not -`b"str"` but `&b"str"[..]`, which is a great deal of punctuation and not -more readable than a function call. - -### Example -``` -let bstr = "a byte string".as_bytes(); -``` - -Use instead: -``` -let bstr = b"a byte string"; -``` \ No newline at end of file diff --git a/src/docs/string_slice.txt b/src/docs/string_slice.txt deleted file mode 100644 index 3d9e49dd39eb7..0000000000000 --- a/src/docs/string_slice.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for slice operations on strings - -### Why is this bad? -UTF-8 characters span multiple bytes, and it is easy to inadvertently confuse character -counts and string indices. This may lead to panics, and should warrant some test cases -containing wide UTF-8 characters. This lint is most useful in code that should avoid -panics at all costs. - -### Known problems -Probably lots of false positives. If an index comes from a known valid position (e.g. -obtained via `char_indices` over the same string), it is totally OK. - -# Example -``` -&"Ölkanne"[1..]; -``` \ No newline at end of file diff --git a/src/docs/string_to_string.txt b/src/docs/string_to_string.txt deleted file mode 100644 index deb7eebe7842c..0000000000000 --- a/src/docs/string_to_string.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -This lint checks for `.to_string()` method calls on values of type `String`. - -### Why is this bad? -The `to_string` method is also used on other types to convert them to a string. -When called on a `String` it only clones the `String`, which can be better expressed with `.clone()`. - -### Example -``` -// example code where clippy issues a warning -let msg = String::from("Hello World"); -let _ = msg.to_string(); -``` -Use instead: -``` -// example code which does not raise clippy warning -let msg = String::from("Hello World"); -let _ = msg.clone(); -``` \ No newline at end of file diff --git a/src/docs/strlen_on_c_strings.txt b/src/docs/strlen_on_c_strings.txt deleted file mode 100644 index 0454abf55a204..0000000000000 --- a/src/docs/strlen_on_c_strings.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Checks for usage of `libc::strlen` on a `CString` or `CStr` value, -and suggest calling `as_bytes().len()` or `to_bytes().len()` respectively instead. - -### Why is this bad? -This avoids calling an unsafe `libc` function. -Currently, it also avoids calculating the length. - -### Example -``` -use std::ffi::CString; -let cstring = CString::new("foo").expect("CString::new failed"); -let len = unsafe { libc::strlen(cstring.as_ptr()) }; -``` -Use instead: -``` -use std::ffi::CString; -let cstring = CString::new("foo").expect("CString::new failed"); -let len = cstring.as_bytes().len(); -``` \ No newline at end of file diff --git a/src/docs/struct_excessive_bools.txt b/src/docs/struct_excessive_bools.txt deleted file mode 100644 index 9e197c786201d..0000000000000 --- a/src/docs/struct_excessive_bools.txt +++ /dev/null @@ -1,29 +0,0 @@ -### What it does -Checks for excessive -use of bools in structs. - -### Why is this bad? -Excessive bools in a struct -is often a sign that it's used as a state machine, -which is much better implemented as an enum. -If it's not the case, excessive bools usually benefit -from refactoring into two-variant enums for better -readability and API. - -### Example -``` -struct S { - is_pending: bool, - is_processing: bool, - is_finished: bool, -} -``` - -Use instead: -``` -enum S { - Pending, - Processing, - Finished, -} -``` \ No newline at end of file diff --git a/src/docs/suboptimal_flops.txt b/src/docs/suboptimal_flops.txt deleted file mode 100644 index f1c9c665b085d..0000000000000 --- a/src/docs/suboptimal_flops.txt +++ /dev/null @@ -1,50 +0,0 @@ -### What it does -Looks for floating-point expressions that -can be expressed using built-in methods to improve both -accuracy and performance. - -### Why is this bad? -Negatively impacts accuracy and performance. - -### Example -``` -use std::f32::consts::E; - -let a = 3f32; -let _ = (2f32).powf(a); -let _ = E.powf(a); -let _ = a.powf(1.0 / 2.0); -let _ = a.log(2.0); -let _ = a.log(10.0); -let _ = a.log(E); -let _ = a.powf(2.0); -let _ = a * 2.0 + 4.0; -let _ = if a < 0.0 { - -a -} else { - a -}; -let _ = if a < 0.0 { - a -} else { - -a -}; -``` - -is better expressed as - -``` -use std::f32::consts::E; - -let a = 3f32; -let _ = a.exp2(); -let _ = a.exp(); -let _ = a.sqrt(); -let _ = a.log2(); -let _ = a.log10(); -let _ = a.ln(); -let _ = a.powi(2); -let _ = a.mul_add(2.0, 4.0); -let _ = a.abs(); -let _ = -a.abs(); -``` \ No newline at end of file diff --git a/src/docs/suspicious_arithmetic_impl.txt b/src/docs/suspicious_arithmetic_impl.txt deleted file mode 100644 index d67ff279346dc..0000000000000 --- a/src/docs/suspicious_arithmetic_impl.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Lints for suspicious operations in impls of arithmetic operators, e.g. -subtracting elements in an Add impl. - -### Why is this bad? -This is probably a typo or copy-and-paste error and not intended. - -### Example -``` -impl Add for Foo { - type Output = Foo; - - fn add(self, other: Foo) -> Foo { - Foo(self.0 - other.0) - } -} -``` \ No newline at end of file diff --git a/src/docs/suspicious_assignment_formatting.txt b/src/docs/suspicious_assignment_formatting.txt deleted file mode 100644 index b889827cdf27f..0000000000000 --- a/src/docs/suspicious_assignment_formatting.txt +++ /dev/null @@ -1,12 +0,0 @@ -### What it does -Checks for use of the non-existent `=*`, `=!` and `=-` -operators. - -### Why is this bad? -This is either a typo of `*=`, `!=` or `-=` or -confusing. - -### Example -``` -a =- 42; // confusing, should it be `a -= 42` or `a = -42`? -``` \ No newline at end of file diff --git a/src/docs/suspicious_else_formatting.txt b/src/docs/suspicious_else_formatting.txt deleted file mode 100644 index 3cf2f74868ebf..0000000000000 --- a/src/docs/suspicious_else_formatting.txt +++ /dev/null @@ -1,30 +0,0 @@ -### What it does -Checks for formatting of `else`. It lints if the `else` -is followed immediately by a newline or the `else` seems to be missing. - -### Why is this bad? -This is probably some refactoring remnant, even if the -code is correct, it might look confusing. - -### Example -``` -if foo { -} { // looks like an `else` is missing here -} - -if foo { -} if bar { // looks like an `else` is missing here -} - -if foo { -} else - -{ // this is the `else` block of the previous `if`, but should it be? -} - -if foo { -} else - -if bar { // this is the `else` block of the previous `if`, but should it be? -} -``` \ No newline at end of file diff --git a/src/docs/suspicious_map.txt b/src/docs/suspicious_map.txt deleted file mode 100644 index d8fa52c43fb0a..0000000000000 --- a/src/docs/suspicious_map.txt +++ /dev/null @@ -1,13 +0,0 @@ -### What it does -Checks for calls to `map` followed by a `count`. - -### Why is this bad? -It looks suspicious. Maybe `map` was confused with `filter`. -If the `map` call is intentional, this should be rewritten -using `inspect`. Or, if you intend to drive the iterator to -completion, you can just use `for_each` instead. - -### Example -``` -let _ = (0..3).map(|x| x + 2).count(); -``` \ No newline at end of file diff --git a/src/docs/suspicious_op_assign_impl.txt b/src/docs/suspicious_op_assign_impl.txt deleted file mode 100644 index 81abfbecae073..0000000000000 --- a/src/docs/suspicious_op_assign_impl.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Lints for suspicious operations in impls of OpAssign, e.g. -subtracting elements in an AddAssign impl. - -### Why is this bad? -This is probably a typo or copy-and-paste error and not intended. - -### Example -``` -impl AddAssign for Foo { - fn add_assign(&mut self, other: Foo) { - *self = *self - other; - } -} -``` \ No newline at end of file diff --git a/src/docs/suspicious_operation_groupings.txt b/src/docs/suspicious_operation_groupings.txt deleted file mode 100644 index 81ede5d3da575..0000000000000 --- a/src/docs/suspicious_operation_groupings.txt +++ /dev/null @@ -1,41 +0,0 @@ -### What it does -Checks for unlikely usages of binary operators that are almost -certainly typos and/or copy/paste errors, given the other usages -of binary operators nearby. - -### Why is this bad? -They are probably bugs and if they aren't then they look like bugs -and you should add a comment explaining why you are doing such an -odd set of operations. - -### Known problems -There may be some false positives if you are trying to do something -unusual that happens to look like a typo. - -### Example -``` -struct Vec3 { - x: f64, - y: f64, - z: f64, -} - -impl Eq for Vec3 {} - -impl PartialEq for Vec3 { - fn eq(&self, other: &Self) -> bool { - // This should trigger the lint because `self.x` is compared to `other.y` - self.x == other.y && self.y == other.y && self.z == other.z - } -} -``` -Use instead: -``` -// same as above except: -impl PartialEq for Vec3 { - fn eq(&self, other: &Self) -> bool { - // Note we now compare other.x to self.x - self.x == other.x && self.y == other.y && self.z == other.z - } -} -``` \ No newline at end of file diff --git a/src/docs/suspicious_splitn.txt b/src/docs/suspicious_splitn.txt deleted file mode 100644 index 79a3dbfa6f4a7..0000000000000 --- a/src/docs/suspicious_splitn.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for calls to [`splitn`] -(https://doc.rust-lang.org/std/primitive.str.html#method.splitn) and -related functions with either zero or one splits. - -### Why is this bad? -These calls don't actually split the value and are -likely to be intended as a different number. - -### Example -``` -for x in s.splitn(1, ":") { - // .. -} -``` - -Use instead: -``` -for x in s.splitn(2, ":") { - // .. -} -``` \ No newline at end of file diff --git a/src/docs/suspicious_to_owned.txt b/src/docs/suspicious_to_owned.txt deleted file mode 100644 index 8cbf61dc7175b..0000000000000 --- a/src/docs/suspicious_to_owned.txt +++ /dev/null @@ -1,39 +0,0 @@ -### What it does -Checks for the usage of `_.to_owned()`, on a `Cow<'_, _>`. - -### Why is this bad? -Calling `to_owned()` on a `Cow` creates a clone of the `Cow` -itself, without taking ownership of the `Cow` contents (i.e. -it's equivalent to calling `Cow::clone`). -The similarly named `into_owned` method, on the other hand, -clones the `Cow` contents, effectively turning any `Cow::Borrowed` -into a `Cow::Owned`. - -Given the potential ambiguity, consider replacing `to_owned` -with `clone` for better readability or, if getting a `Cow::Owned` -was the original intent, using `into_owned` instead. - -### Example -``` -let s = "Hello world!"; -let cow = Cow::Borrowed(s); - -let data = cow.to_owned(); -assert!(matches!(data, Cow::Borrowed(_))) -``` -Use instead: -``` -let s = "Hello world!"; -let cow = Cow::Borrowed(s); - -let data = cow.clone(); -assert!(matches!(data, Cow::Borrowed(_))) -``` -or -``` -let s = "Hello world!"; -let cow = Cow::Borrowed(s); - -let data = cow.into_owned(); -assert!(matches!(data, String)) -``` \ No newline at end of file diff --git a/src/docs/suspicious_unary_op_formatting.txt b/src/docs/suspicious_unary_op_formatting.txt deleted file mode 100644 index 06fb09db76d0c..0000000000000 --- a/src/docs/suspicious_unary_op_formatting.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks the formatting of a unary operator on the right hand side -of a binary operator. It lints if there is no space between the binary and unary operators, -but there is a space between the unary and its operand. - -### Why is this bad? -This is either a typo in the binary operator or confusing. - -### Example -``` -// &&! looks like a different operator -if foo &&! bar {} -``` - -Use instead: -``` -if foo && !bar {} -``` \ No newline at end of file diff --git a/src/docs/swap_ptr_to_ref.txt b/src/docs/swap_ptr_to_ref.txt deleted file mode 100644 index 0215d1e8a4215..0000000000000 --- a/src/docs/swap_ptr_to_ref.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for calls to `core::mem::swap` where either parameter is derived from a pointer - -### Why is this bad? -When at least one parameter to `swap` is derived from a pointer it may overlap with the -other. This would then lead to undefined behavior. - -### Example -``` -unsafe fn swap(x: &[*mut u32], y: &[*mut u32]) { - for (&x, &y) in x.iter().zip(y) { - core::mem::swap(&mut *x, &mut *y); - } -} -``` -Use instead: -``` -unsafe fn swap(x: &[*mut u32], y: &[*mut u32]) { - for (&x, &y) in x.iter().zip(y) { - core::ptr::swap(x, y); - } -} -``` \ No newline at end of file diff --git a/src/docs/tabs_in_doc_comments.txt b/src/docs/tabs_in_doc_comments.txt deleted file mode 100644 index f83dbe2b73cb7..0000000000000 --- a/src/docs/tabs_in_doc_comments.txt +++ /dev/null @@ -1,44 +0,0 @@ -### What it does -Checks doc comments for usage of tab characters. - -### Why is this bad? -The rust style-guide promotes spaces instead of tabs for indentation. -To keep a consistent view on the source, also doc comments should not have tabs. -Also, explaining ascii-diagrams containing tabs can get displayed incorrectly when the -display settings of the author and reader differ. - -### Example -``` -/// -/// Struct to hold two strings: -/// - first one -/// - second one -pub struct DoubleString { - /// - /// - First String: - /// - needs to be inside here - first_string: String, - /// - /// - Second String: - /// - needs to be inside here - second_string: String, -} -``` - -Will be converted to: -``` -/// -/// Struct to hold two strings: -/// - first one -/// - second one -pub struct DoubleString { - /// - /// - First String: - /// - needs to be inside here - first_string: String, - /// - /// - Second String: - /// - needs to be inside here - second_string: String, -} -``` \ No newline at end of file diff --git a/src/docs/temporary_assignment.txt b/src/docs/temporary_assignment.txt deleted file mode 100644 index 195b42cf0d492..0000000000000 --- a/src/docs/temporary_assignment.txt +++ /dev/null @@ -1,12 +0,0 @@ -### What it does -Checks for construction of a structure or tuple just to -assign a value in it. - -### Why is this bad? -Readability. If the structure is only created to be -updated, why not write the structure you want in the first place? - -### Example -``` -(0, 0).0 = 1 -``` \ No newline at end of file diff --git a/src/docs/to_digit_is_some.txt b/src/docs/to_digit_is_some.txt deleted file mode 100644 index eee8375adf7ae..0000000000000 --- a/src/docs/to_digit_is_some.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for `.to_digit(..).is_some()` on `char`s. - -### Why is this bad? -This is a convoluted way of checking if a `char` is a digit. It's -more straight forward to use the dedicated `is_digit` method. - -### Example -``` -let is_digit = c.to_digit(radix).is_some(); -``` -can be written as: -``` -let is_digit = c.is_digit(radix); -``` \ No newline at end of file diff --git a/src/docs/to_string_in_format_args.txt b/src/docs/to_string_in_format_args.txt deleted file mode 100644 index 34b20583585ad..0000000000000 --- a/src/docs/to_string_in_format_args.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for [`ToString::to_string`](https://doc.rust-lang.org/std/string/trait.ToString.html#tymethod.to_string) -applied to a type that implements [`Display`](https://doc.rust-lang.org/std/fmt/trait.Display.html) -in a macro that does formatting. - -### Why is this bad? -Since the type implements `Display`, the use of `to_string` is -unnecessary. - -### Example -``` -println!("error: something failed at {}", Location::caller().to_string()); -``` -Use instead: -``` -println!("error: something failed at {}", Location::caller()); -``` \ No newline at end of file diff --git a/src/docs/todo.txt b/src/docs/todo.txt deleted file mode 100644 index 661eb1ac5cff4..0000000000000 --- a/src/docs/todo.txt +++ /dev/null @@ -1,10 +0,0 @@ -### What it does -Checks for usage of `todo!`. - -### Why is this bad? -This macro should not be present in production code - -### Example -``` -todo!(); -``` \ No newline at end of file diff --git a/src/docs/too_many_arguments.txt b/src/docs/too_many_arguments.txt deleted file mode 100644 index 4669f9f82e661..0000000000000 --- a/src/docs/too_many_arguments.txt +++ /dev/null @@ -1,14 +0,0 @@ -### What it does -Checks for functions with too many parameters. - -### Why is this bad? -Functions with lots of parameters are considered bad -style and reduce readability (“what does the 5th parameter mean?”). Consider -grouping some parameters into a new type. - -### Example -``` -fn foo(x: u32, y: u32, name: &str, c: Color, w: f32, h: f32, a: f32, b: f32) { - // .. -} -``` \ No newline at end of file diff --git a/src/docs/too_many_lines.txt b/src/docs/too_many_lines.txt deleted file mode 100644 index 425db348bbd75..0000000000000 --- a/src/docs/too_many_lines.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for functions with a large amount of lines. - -### Why is this bad? -Functions with a lot of lines are harder to understand -due to having to look at a larger amount of code to understand what the -function is doing. Consider splitting the body of the function into -multiple functions. - -### Example -``` -fn im_too_long() { - println!(""); - // ... 100 more LoC - println!(""); -} -``` \ No newline at end of file diff --git a/src/docs/toplevel_ref_arg.txt b/src/docs/toplevel_ref_arg.txt deleted file mode 100644 index 96a9e2db8b7e7..0000000000000 --- a/src/docs/toplevel_ref_arg.txt +++ /dev/null @@ -1,28 +0,0 @@ -### What it does -Checks for function arguments and let bindings denoted as -`ref`. - -### Why is this bad? -The `ref` declaration makes the function take an owned -value, but turns the argument into a reference (which means that the value -is destroyed when exiting the function). This adds not much value: either -take a reference type, or take an owned value and create references in the -body. - -For let bindings, `let x = &foo;` is preferred over `let ref x = foo`. The -type of `x` is more obvious with the former. - -### Known problems -If the argument is dereferenced within the function, -removing the `ref` will lead to errors. This can be fixed by removing the -dereferences, e.g., changing `*x` to `x` within the function. - -### Example -``` -fn foo(ref _x: u8) {} -``` - -Use instead: -``` -fn foo(_x: &u8) {} -``` \ No newline at end of file diff --git a/src/docs/trailing_empty_array.txt b/src/docs/trailing_empty_array.txt deleted file mode 100644 index db1908cc96d03..0000000000000 --- a/src/docs/trailing_empty_array.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Displays a warning when a struct with a trailing zero-sized array is declared without a `repr` attribute. - -### Why is this bad? -Zero-sized arrays aren't very useful in Rust itself, so such a struct is likely being created to pass to C code or in some other situation where control over memory layout matters (for example, in conjunction with manual allocation to make it easy to compute the offset of the array). Either way, `#[repr(C)]` (or another `repr` attribute) is needed. - -### Example -``` -struct RarelyUseful { - some_field: u32, - last: [u32; 0], -} -``` - -Use instead: -``` -#[repr(C)] -struct MoreOftenUseful { - some_field: usize, - last: [u32; 0], -} -``` \ No newline at end of file diff --git a/src/docs/trait_duplication_in_bounds.txt b/src/docs/trait_duplication_in_bounds.txt deleted file mode 100644 index 509736bb36444..0000000000000 --- a/src/docs/trait_duplication_in_bounds.txt +++ /dev/null @@ -1,37 +0,0 @@ -### What it does -Checks for cases where generics are being used and multiple -syntax specifications for trait bounds are used simultaneously. - -### Why is this bad? -Duplicate bounds makes the code -less readable than specifying them only once. - -### Example -``` -fn func(arg: T) where T: Clone + Default {} -``` - -Use instead: -``` -fn func(arg: T) {} - -// or - -fn func(arg: T) where T: Clone + Default {} -``` - -``` -fn foo(bar: T) {} -``` -Use instead: -``` -fn foo(bar: T) {} -``` - -``` -fn foo(bar: T) where T: Default + Default {} -``` -Use instead: -``` -fn foo(bar: T) where T: Default {} -``` \ No newline at end of file diff --git a/src/docs/transmute_bytes_to_str.txt b/src/docs/transmute_bytes_to_str.txt deleted file mode 100644 index 75889b9c73ae3..0000000000000 --- a/src/docs/transmute_bytes_to_str.txt +++ /dev/null @@ -1,27 +0,0 @@ -### What it does -Checks for transmutes from a `&[u8]` to a `&str`. - -### Why is this bad? -Not every byte slice is a valid UTF-8 string. - -### Known problems -- [`from_utf8`] which this lint suggests using is slower than `transmute` -as it needs to validate the input. -If you are certain that the input is always a valid UTF-8, -use [`from_utf8_unchecked`] which is as fast as `transmute` -but has a semantically meaningful name. -- You might want to handle errors returned from [`from_utf8`] instead of calling `unwrap`. - -[`from_utf8`]: https://doc.rust-lang.org/std/str/fn.from_utf8.html -[`from_utf8_unchecked`]: https://doc.rust-lang.org/std/str/fn.from_utf8_unchecked.html - -### Example -``` -let b: &[u8] = &[1_u8, 2_u8]; -unsafe { - let _: &str = std::mem::transmute(b); // where b: &[u8] -} - -// should be: -let _ = std::str::from_utf8(b).unwrap(); -``` \ No newline at end of file diff --git a/src/docs/transmute_float_to_int.txt b/src/docs/transmute_float_to_int.txt deleted file mode 100644 index 1877e5a465a4e..0000000000000 --- a/src/docs/transmute_float_to_int.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for transmutes from a float to an integer. - -### Why is this bad? -Transmutes are dangerous and error-prone, whereas `to_bits` is intuitive -and safe. - -### Example -``` -unsafe { - let _: u32 = std::mem::transmute(1f32); -} - -// should be: -let _: u32 = 1f32.to_bits(); -``` \ No newline at end of file diff --git a/src/docs/transmute_int_to_bool.txt b/src/docs/transmute_int_to_bool.txt deleted file mode 100644 index 07c10f8d0bca7..0000000000000 --- a/src/docs/transmute_int_to_bool.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for transmutes from an integer to a `bool`. - -### Why is this bad? -This might result in an invalid in-memory representation of a `bool`. - -### Example -``` -let x = 1_u8; -unsafe { - let _: bool = std::mem::transmute(x); // where x: u8 -} - -// should be: -let _: bool = x != 0; -``` \ No newline at end of file diff --git a/src/docs/transmute_int_to_char.txt b/src/docs/transmute_int_to_char.txt deleted file mode 100644 index 836d22d3f99c0..0000000000000 --- a/src/docs/transmute_int_to_char.txt +++ /dev/null @@ -1,27 +0,0 @@ -### What it does -Checks for transmutes from an integer to a `char`. - -### Why is this bad? -Not every integer is a Unicode scalar value. - -### Known problems -- [`from_u32`] which this lint suggests using is slower than `transmute` -as it needs to validate the input. -If you are certain that the input is always a valid Unicode scalar value, -use [`from_u32_unchecked`] which is as fast as `transmute` -but has a semantically meaningful name. -- You might want to handle `None` returned from [`from_u32`] instead of calling `unwrap`. - -[`from_u32`]: https://doc.rust-lang.org/std/char/fn.from_u32.html -[`from_u32_unchecked`]: https://doc.rust-lang.org/std/char/fn.from_u32_unchecked.html - -### Example -``` -let x = 1_u32; -unsafe { - let _: char = std::mem::transmute(x); // where x: u32 -} - -// should be: -let _ = std::char::from_u32(x).unwrap(); -``` \ No newline at end of file diff --git a/src/docs/transmute_int_to_float.txt b/src/docs/transmute_int_to_float.txt deleted file mode 100644 index 75cdc62e97275..0000000000000 --- a/src/docs/transmute_int_to_float.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for transmutes from an integer to a float. - -### Why is this bad? -Transmutes are dangerous and error-prone, whereas `from_bits` is intuitive -and safe. - -### Example -``` -unsafe { - let _: f32 = std::mem::transmute(1_u32); // where x: u32 -} - -// should be: -let _: f32 = f32::from_bits(1_u32); -``` \ No newline at end of file diff --git a/src/docs/transmute_num_to_bytes.txt b/src/docs/transmute_num_to_bytes.txt deleted file mode 100644 index a2c39a1b947e3..0000000000000 --- a/src/docs/transmute_num_to_bytes.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for transmutes from a number to an array of `u8` - -### Why this is bad? -Transmutes are dangerous and error-prone, whereas `to_ne_bytes` -is intuitive and safe. - -### Example -``` -unsafe { - let x: [u8; 8] = std::mem::transmute(1i64); -} - -// should be -let x: [u8; 8] = 0i64.to_ne_bytes(); -``` \ No newline at end of file diff --git a/src/docs/transmute_ptr_to_ptr.txt b/src/docs/transmute_ptr_to_ptr.txt deleted file mode 100644 index 65777db986186..0000000000000 --- a/src/docs/transmute_ptr_to_ptr.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for transmutes from a pointer to a pointer, or -from a reference to a reference. - -### Why is this bad? -Transmutes are dangerous, and these can instead be -written as casts. - -### Example -``` -let ptr = &1u32 as *const u32; -unsafe { - // pointer-to-pointer transmute - let _: *const f32 = std::mem::transmute(ptr); - // ref-ref transmute - let _: &f32 = std::mem::transmute(&1u32); -} -// These can be respectively written: -let _ = ptr as *const f32; -let _ = unsafe{ &*(&1u32 as *const u32 as *const f32) }; -``` \ No newline at end of file diff --git a/src/docs/transmute_ptr_to_ref.txt b/src/docs/transmute_ptr_to_ref.txt deleted file mode 100644 index aca550f5036ec..0000000000000 --- a/src/docs/transmute_ptr_to_ref.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Checks for transmutes from a pointer to a reference. - -### Why is this bad? -This can always be rewritten with `&` and `*`. - -### Known problems -- `mem::transmute` in statics and constants is stable from Rust 1.46.0, -while dereferencing raw pointer is not stable yet. -If you need to do this in those places, -you would have to use `transmute` instead. - -### Example -``` -unsafe { - let _: &T = std::mem::transmute(p); // where p: *const T -} - -// can be written: -let _: &T = &*p; -``` \ No newline at end of file diff --git a/src/docs/transmute_undefined_repr.txt b/src/docs/transmute_undefined_repr.txt deleted file mode 100644 index 5ee6aaf4ca92c..0000000000000 --- a/src/docs/transmute_undefined_repr.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for transmutes between types which do not have a representation defined relative to -each other. - -### Why is this bad? -The results of such a transmute are not defined. - -### Known problems -This lint has had multiple problems in the past and was moved to `nursery`. See issue -[#8496](https://github.com/rust-lang/rust-clippy/issues/8496) for more details. - -### Example -``` -struct Foo(u32, T); -let _ = unsafe { core::mem::transmute::, Foo>(Foo(0u32, 0u32)) }; -``` -Use instead: -``` -#[repr(C)] -struct Foo(u32, T); -let _ = unsafe { core::mem::transmute::, Foo>(Foo(0u32, 0u32)) }; -``` \ No newline at end of file diff --git a/src/docs/transmutes_expressible_as_ptr_casts.txt b/src/docs/transmutes_expressible_as_ptr_casts.txt deleted file mode 100644 index b68a8cda9c74b..0000000000000 --- a/src/docs/transmutes_expressible_as_ptr_casts.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Checks for transmutes that could be a pointer cast. - -### Why is this bad? -Readability. The code tricks people into thinking that -something complex is going on. - -### Example - -``` -unsafe { std::mem::transmute::<*const [i32], *const [u16]>(p) }; -``` -Use instead: -``` -p as *const [u16]; -``` \ No newline at end of file diff --git a/src/docs/transmuting_null.txt b/src/docs/transmuting_null.txt deleted file mode 100644 index f8bacfc0b90b4..0000000000000 --- a/src/docs/transmuting_null.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for transmute calls which would receive a null pointer. - -### Why is this bad? -Transmuting a null pointer is undefined behavior. - -### Known problems -Not all cases can be detected at the moment of this writing. -For example, variables which hold a null pointer and are then fed to a `transmute` -call, aren't detectable yet. - -### Example -``` -let null_ref: &u64 = unsafe { std::mem::transmute(0 as *const u64) }; -``` \ No newline at end of file diff --git a/src/docs/trim_split_whitespace.txt b/src/docs/trim_split_whitespace.txt deleted file mode 100644 index f7e3e7858f949..0000000000000 --- a/src/docs/trim_split_whitespace.txt +++ /dev/null @@ -1,14 +0,0 @@ -### What it does -Warns about calling `str::trim` (or variants) before `str::split_whitespace`. - -### Why is this bad? -`split_whitespace` already ignores leading and trailing whitespace. - -### Example -``` -" A B C ".trim().split_whitespace(); -``` -Use instead: -``` -" A B C ".split_whitespace(); -``` \ No newline at end of file diff --git a/src/docs/trivial_regex.txt b/src/docs/trivial_regex.txt deleted file mode 100644 index f71d667fd7711..0000000000000 --- a/src/docs/trivial_regex.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for trivial [regex](https://crates.io/crates/regex) -creation (with `Regex::new`, `RegexBuilder::new`, or `RegexSet::new`). - -### Why is this bad? -Matching the regex can likely be replaced by `==` or -`str::starts_with`, `str::ends_with` or `std::contains` or other `str` -methods. - -### Known problems -If the same regex is going to be applied to multiple -inputs, the precomputations done by `Regex` construction can give -significantly better performance than any of the `str`-based methods. - -### Example -``` -Regex::new("^foobar") -``` \ No newline at end of file diff --git a/src/docs/trivially_copy_pass_by_ref.txt b/src/docs/trivially_copy_pass_by_ref.txt deleted file mode 100644 index f54cce5e2bd76..0000000000000 --- a/src/docs/trivially_copy_pass_by_ref.txt +++ /dev/null @@ -1,43 +0,0 @@ -### What it does -Checks for functions taking arguments by reference, where -the argument type is `Copy` and small enough to be more efficient to always -pass by value. - -### Why is this bad? -In many calling conventions instances of structs will -be passed through registers if they fit into two or less general purpose -registers. - -### Known problems -This lint is target register size dependent, it is -limited to 32-bit to try and reduce portability problems between 32 and -64-bit, but if you are compiling for 8 or 16-bit targets then the limit -will be different. - -The configuration option `trivial_copy_size_limit` can be set to override -this limit for a project. - -This lint attempts to allow passing arguments by reference if a reference -to that argument is returned. This is implemented by comparing the lifetime -of the argument and return value for equality. However, this can cause -false positives in cases involving multiple lifetimes that are bounded by -each other. - -Also, it does not take account of other similar cases where getting memory addresses -matters; namely, returning the pointer to the argument in question, -and passing the argument, as both references and pointers, -to a function that needs the memory address. For further details, refer to -[this issue](https://github.com/rust-lang/rust-clippy/issues/5953) -that explains a real case in which this false positive -led to an **undefined behavior** introduced with unsafe code. - -### Example - -``` -fn foo(v: &u32) {} -``` - -Use instead: -``` -fn foo(v: u32) {} -``` \ No newline at end of file diff --git a/src/docs/try_err.txt b/src/docs/try_err.txt deleted file mode 100644 index e3d4ef3a09df1..0000000000000 --- a/src/docs/try_err.txt +++ /dev/null @@ -1,28 +0,0 @@ -### What it does -Checks for usages of `Err(x)?`. - -### Why is this bad? -The `?` operator is designed to allow calls that -can fail to be easily chained. For example, `foo()?.bar()` or -`foo(bar()?)`. Because `Err(x)?` can't be used that way (it will -always return), it is more clear to write `return Err(x)`. - -### Example -``` -fn foo(fail: bool) -> Result { - if fail { - Err("failed")?; - } - Ok(0) -} -``` -Could be written: - -``` -fn foo(fail: bool) -> Result { - if fail { - return Err("failed".into()); - } - Ok(0) -} -``` \ No newline at end of file diff --git a/src/docs/type_complexity.txt b/src/docs/type_complexity.txt deleted file mode 100644 index 69cd875005043..0000000000000 --- a/src/docs/type_complexity.txt +++ /dev/null @@ -1,14 +0,0 @@ -### What it does -Checks for types used in structs, parameters and `let` -declarations above a certain complexity threshold. - -### Why is this bad? -Too complex types make the code less readable. Consider -using a `type` definition to simplify them. - -### Example -``` -struct Foo { - inner: Rc>>>, -} -``` \ No newline at end of file diff --git a/src/docs/type_repetition_in_bounds.txt b/src/docs/type_repetition_in_bounds.txt deleted file mode 100644 index 18ed372fd13e3..0000000000000 --- a/src/docs/type_repetition_in_bounds.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -This lint warns about unnecessary type repetitions in trait bounds - -### Why is this bad? -Repeating the type for every bound makes the code -less readable than combining the bounds - -### Example -``` -pub fn foo(t: T) where T: Copy, T: Clone {} -``` - -Use instead: -``` -pub fn foo(t: T) where T: Copy + Clone {} -``` \ No newline at end of file diff --git a/src/docs/undocumented_unsafe_blocks.txt b/src/docs/undocumented_unsafe_blocks.txt deleted file mode 100644 index f3af4753c5f75..0000000000000 --- a/src/docs/undocumented_unsafe_blocks.txt +++ /dev/null @@ -1,43 +0,0 @@ -### What it does -Checks for `unsafe` blocks and impls without a `// SAFETY: ` comment -explaining why the unsafe operations performed inside -the block are safe. - -Note the comment must appear on the line(s) preceding the unsafe block -with nothing appearing in between. The following is ok: -``` -foo( - // SAFETY: - // This is a valid safety comment - unsafe { *x } -) -``` -But neither of these are: -``` -// SAFETY: -// This is not a valid safety comment -foo( - /* SAFETY: Neither is this */ unsafe { *x }, -); -``` - -### Why is this bad? -Undocumented unsafe blocks and impls can make it difficult to -read and maintain code, as well as uncover unsoundness -and bugs. - -### Example -``` -use std::ptr::NonNull; -let a = &mut 42; - -let ptr = unsafe { NonNull::new_unchecked(a) }; -``` -Use instead: -``` -use std::ptr::NonNull; -let a = &mut 42; - -// SAFETY: references are guaranteed to be non-null. -let ptr = unsafe { NonNull::new_unchecked(a) }; -``` \ No newline at end of file diff --git a/src/docs/undropped_manually_drops.txt b/src/docs/undropped_manually_drops.txt deleted file mode 100644 index 85e3ec56653c9..0000000000000 --- a/src/docs/undropped_manually_drops.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Prevents the safe `std::mem::drop` function from being called on `std::mem::ManuallyDrop`. - -### Why is this bad? -The safe `drop` function does not drop the inner value of a `ManuallyDrop`. - -### Known problems -Does not catch cases if the user binds `std::mem::drop` -to a different name and calls it that way. - -### Example -``` -struct S; -drop(std::mem::ManuallyDrop::new(S)); -``` -Use instead: -``` -struct S; -unsafe { - std::mem::ManuallyDrop::drop(&mut std::mem::ManuallyDrop::new(S)); -} -``` \ No newline at end of file diff --git a/src/docs/unicode_not_nfc.txt b/src/docs/unicode_not_nfc.txt deleted file mode 100644 index c660c51dadb26..0000000000000 --- a/src/docs/unicode_not_nfc.txt +++ /dev/null @@ -1,12 +0,0 @@ -### What it does -Checks for string literals that contain Unicode in a form -that is not equal to its -[NFC-recomposition](http://www.unicode.org/reports/tr15/#Norm_Forms). - -### Why is this bad? -If such a string is compared to another, the results -may be surprising. - -### Example -You may not see it, but "à"" and "à"" aren't the same string. The -former when escaped is actually `"a\u{300}"` while the latter is `"\u{e0}"`. \ No newline at end of file diff --git a/src/docs/unimplemented.txt b/src/docs/unimplemented.txt deleted file mode 100644 index 7095594fb2e73..0000000000000 --- a/src/docs/unimplemented.txt +++ /dev/null @@ -1,10 +0,0 @@ -### What it does -Checks for usage of `unimplemented!`. - -### Why is this bad? -This macro should not be present in production code - -### Example -``` -unimplemented!(); -``` \ No newline at end of file diff --git a/src/docs/uninit_assumed_init.txt b/src/docs/uninit_assumed_init.txt deleted file mode 100644 index cca24093d40df..0000000000000 --- a/src/docs/uninit_assumed_init.txt +++ /dev/null @@ -1,28 +0,0 @@ -### What it does -Checks for `MaybeUninit::uninit().assume_init()`. - -### Why is this bad? -For most types, this is undefined behavior. - -### Known problems -For now, we accept empty tuples and tuples / arrays -of `MaybeUninit`. There may be other types that allow uninitialized -data, but those are not yet rigorously defined. - -### Example -``` -// Beware the UB -use std::mem::MaybeUninit; - -let _: usize = unsafe { MaybeUninit::uninit().assume_init() }; -``` - -Note that the following is OK: - -``` -use std::mem::MaybeUninit; - -let _: [MaybeUninit; 5] = unsafe { - MaybeUninit::uninit().assume_init() -}; -``` \ No newline at end of file diff --git a/src/docs/uninit_vec.txt b/src/docs/uninit_vec.txt deleted file mode 100644 index cd50afe78f6f2..0000000000000 --- a/src/docs/uninit_vec.txt +++ /dev/null @@ -1,41 +0,0 @@ -### What it does -Checks for `set_len()` call that creates `Vec` with uninitialized elements. -This is commonly caused by calling `set_len()` right after allocating or -reserving a buffer with `new()`, `default()`, `with_capacity()`, or `reserve()`. - -### Why is this bad? -It creates a `Vec` with uninitialized data, which leads to -undefined behavior with most safe operations. Notably, uninitialized -`Vec` must not be used with generic `Read`. - -Moreover, calling `set_len()` on a `Vec` created with `new()` or `default()` -creates out-of-bound values that lead to heap memory corruption when used. - -### Known Problems -This lint only checks directly adjacent statements. - -### Example -``` -let mut vec: Vec = Vec::with_capacity(1000); -unsafe { vec.set_len(1000); } -reader.read(&mut vec); // undefined behavior! -``` - -### How to fix? -1. Use an initialized buffer: - ```rust,ignore - let mut vec: Vec = vec![0; 1000]; - reader.read(&mut vec); - ``` -2. Wrap the content in `MaybeUninit`: - ```rust,ignore - let mut vec: Vec> = Vec::with_capacity(1000); - vec.set_len(1000); // `MaybeUninit` can be uninitialized - ``` -3. If you are on 1.60.0 or later, `Vec::spare_capacity_mut()` is available: - ```rust,ignore - let mut vec: Vec = Vec::with_capacity(1000); - let remaining = vec.spare_capacity_mut(); // `&mut [MaybeUninit]` - // perform initialization with `remaining` - vec.set_len(...); // Safe to call `set_len()` on initialized part - ``` \ No newline at end of file diff --git a/src/docs/uninlined_format_args.txt b/src/docs/uninlined_format_args.txt deleted file mode 100644 index 3d2966c84dbe3..0000000000000 --- a/src/docs/uninlined_format_args.txt +++ /dev/null @@ -1,36 +0,0 @@ -### What it does -Detect when a variable is not inlined in a format string, -and suggests to inline it. - -### Why is this bad? -Non-inlined code is slightly more difficult to read and understand, -as it requires arguments to be matched against the format string. -The inlined syntax, where allowed, is simpler. - -### Example -``` -format!("{}", var); -format!("{v:?}", v = var); -format!("{0} {0}", var); -format!("{0:1$}", var, width); -format!("{:.*}", prec, var); -``` -Use instead: -``` -format!("{var}"); -format!("{var:?}"); -format!("{var} {var}"); -format!("{var:width$}"); -format!("{var:.prec$}"); -``` - -### Known Problems - -There may be a false positive if the format string is expanded from certain proc macros: - -``` -println!(indoc!("{}"), var); -``` - -If a format string contains a numbered argument that cannot be inlined -nothing will be suggested, e.g. `println!("{0}={1}", var, 1+2)`. \ No newline at end of file diff --git a/src/docs/unit_arg.txt b/src/docs/unit_arg.txt deleted file mode 100644 index eb83403bb275c..0000000000000 --- a/src/docs/unit_arg.txt +++ /dev/null @@ -1,14 +0,0 @@ -### What it does -Checks for passing a unit value as an argument to a function without using a -unit literal (`()`). - -### Why is this bad? -This is likely the result of an accidental semicolon. - -### Example -``` -foo({ - let a = bar(); - baz(a); -}) -``` \ No newline at end of file diff --git a/src/docs/unit_cmp.txt b/src/docs/unit_cmp.txt deleted file mode 100644 index 6f3d62010dc51..0000000000000 --- a/src/docs/unit_cmp.txt +++ /dev/null @@ -1,33 +0,0 @@ -### What it does -Checks for comparisons to unit. This includes all binary -comparisons (like `==` and `<`) and asserts. - -### Why is this bad? -Unit is always equal to itself, and thus is just a -clumsily written constant. Mostly this happens when someone accidentally -adds semicolons at the end of the operands. - -### Example -``` -if { - foo(); -} == { - bar(); -} { - baz(); -} -``` -is equal to -``` -{ - foo(); - bar(); - baz(); -} -``` - -For asserts: -``` -assert_eq!({ foo(); }, { bar(); }); -``` -will always succeed \ No newline at end of file diff --git a/src/docs/unit_hash.txt b/src/docs/unit_hash.txt deleted file mode 100644 index a22d2994602ac..0000000000000 --- a/src/docs/unit_hash.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Detects `().hash(_)`. - -### Why is this bad? -Hashing a unit value doesn't do anything as the implementation of `Hash` for `()` is a no-op. - -### Example -``` -match my_enum { - Empty => ().hash(&mut state), - WithValue(x) => x.hash(&mut state), -} -``` -Use instead: -``` -match my_enum { - Empty => 0_u8.hash(&mut state), - WithValue(x) => x.hash(&mut state), -} -``` \ No newline at end of file diff --git a/src/docs/unit_return_expecting_ord.txt b/src/docs/unit_return_expecting_ord.txt deleted file mode 100644 index 781feac5afcfc..0000000000000 --- a/src/docs/unit_return_expecting_ord.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Checks for functions that expect closures of type -Fn(...) -> Ord where the implemented closure returns the unit type. -The lint also suggests to remove the semi-colon at the end of the statement if present. - -### Why is this bad? -Likely, returning the unit type is unintentional, and -could simply be caused by an extra semi-colon. Since () implements Ord -it doesn't cause a compilation error. -This is the same reasoning behind the unit_cmp lint. - -### Known problems -If returning unit is intentional, then there is no -way of specifying this without triggering needless_return lint - -### Example -``` -let mut twins = vec!((1, 1), (2, 2)); -twins.sort_by_key(|x| { x.1; }); -``` \ No newline at end of file diff --git a/src/docs/unnecessary_cast.txt b/src/docs/unnecessary_cast.txt deleted file mode 100644 index 603f2606099c8..0000000000000 --- a/src/docs/unnecessary_cast.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for casts to the same type, casts of int literals to integer types -and casts of float literals to float types. - -### Why is this bad? -It's just unnecessary. - -### Example -``` -let _ = 2i32 as i32; -let _ = 0.5 as f32; -``` - -Better: - -``` -let _ = 2_i32; -let _ = 0.5_f32; -``` \ No newline at end of file diff --git a/src/docs/unnecessary_filter_map.txt b/src/docs/unnecessary_filter_map.txt deleted file mode 100644 index b19341ecf660e..0000000000000 --- a/src/docs/unnecessary_filter_map.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for `filter_map` calls that could be replaced by `filter` or `map`. -More specifically it checks if the closure provided is only performing one of the -filter or map operations and suggests the appropriate option. - -### Why is this bad? -Complexity. The intent is also clearer if only a single -operation is being performed. - -### Example -``` -let _ = (0..3).filter_map(|x| if x > 2 { Some(x) } else { None }); - -// As there is no transformation of the argument this could be written as: -let _ = (0..3).filter(|&x| x > 2); -``` - -``` -let _ = (0..4).filter_map(|x| Some(x + 1)); - -// As there is no conditional check on the argument this could be written as: -let _ = (0..4).map(|x| x + 1); -``` \ No newline at end of file diff --git a/src/docs/unnecessary_find_map.txt b/src/docs/unnecessary_find_map.txt deleted file mode 100644 index f9444dc48ad6a..0000000000000 --- a/src/docs/unnecessary_find_map.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for `find_map` calls that could be replaced by `find` or `map`. More -specifically it checks if the closure provided is only performing one of the -find or map operations and suggests the appropriate option. - -### Why is this bad? -Complexity. The intent is also clearer if only a single -operation is being performed. - -### Example -``` -let _ = (0..3).find_map(|x| if x > 2 { Some(x) } else { None }); - -// As there is no transformation of the argument this could be written as: -let _ = (0..3).find(|&x| x > 2); -``` - -``` -let _ = (0..4).find_map(|x| Some(x + 1)); - -// As there is no conditional check on the argument this could be written as: -let _ = (0..4).map(|x| x + 1).next(); -``` \ No newline at end of file diff --git a/src/docs/unnecessary_fold.txt b/src/docs/unnecessary_fold.txt deleted file mode 100644 index e1b0e65f51971..0000000000000 --- a/src/docs/unnecessary_fold.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for using `fold` when a more succinct alternative exists. -Specifically, this checks for `fold`s which could be replaced by `any`, `all`, -`sum` or `product`. - -### Why is this bad? -Readability. - -### Example -``` -(0..3).fold(false, |acc, x| acc || x > 2); -``` - -Use instead: -``` -(0..3).any(|x| x > 2); -``` \ No newline at end of file diff --git a/src/docs/unnecessary_join.txt b/src/docs/unnecessary_join.txt deleted file mode 100644 index ee4e78601f847..0000000000000 --- a/src/docs/unnecessary_join.txt +++ /dev/null @@ -1,25 +0,0 @@ -### What it does -Checks for use of `.collect::>().join("")` on iterators. - -### Why is this bad? -`.collect::()` is more concise and might be more performant - -### Example -``` -let vector = vec!["hello", "world"]; -let output = vector.iter().map(|item| item.to_uppercase()).collect::>().join(""); -println!("{}", output); -``` -The correct use would be: -``` -let vector = vec!["hello", "world"]; -let output = vector.iter().map(|item| item.to_uppercase()).collect::(); -println!("{}", output); -``` -### Known problems -While `.collect::()` is sometimes more performant, there are cases where -using `.collect::()` over `.collect::>().join("")` -will prevent loop unrolling and will result in a negative performance impact. - -Additionally, differences have been observed between aarch64 and x86_64 assembly output, -with aarch64 tending to producing faster assembly in more cases when using `.collect::()` \ No newline at end of file diff --git a/src/docs/unnecessary_lazy_evaluations.txt b/src/docs/unnecessary_lazy_evaluations.txt deleted file mode 100644 index 208188ce971a5..0000000000000 --- a/src/docs/unnecessary_lazy_evaluations.txt +++ /dev/null @@ -1,32 +0,0 @@ -### What it does -As the counterpart to `or_fun_call`, this lint looks for unnecessary -lazily evaluated closures on `Option` and `Result`. - -This lint suggests changing the following functions, when eager evaluation results in -simpler code: - - `unwrap_or_else` to `unwrap_or` - - `and_then` to `and` - - `or_else` to `or` - - `get_or_insert_with` to `get_or_insert` - - `ok_or_else` to `ok_or` - -### Why is this bad? -Using eager evaluation is shorter and simpler in some cases. - -### Known problems -It is possible, but not recommended for `Deref` and `Index` to have -side effects. Eagerly evaluating them can change the semantics of the program. - -### Example -``` -// example code where clippy issues a warning -let opt: Option = None; - -opt.unwrap_or_else(|| 42); -``` -Use instead: -``` -let opt: Option = None; - -opt.unwrap_or(42); -``` \ No newline at end of file diff --git a/src/docs/unnecessary_mut_passed.txt b/src/docs/unnecessary_mut_passed.txt deleted file mode 100644 index 2f8bdd113dfdb..0000000000000 --- a/src/docs/unnecessary_mut_passed.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Detects passing a mutable reference to a function that only -requires an immutable reference. - -### Why is this bad? -The mutable reference rules out all other references to -the value. Also the code misleads about the intent of the call site. - -### Example -``` -vec.push(&mut value); -``` - -Use instead: -``` -vec.push(&value); -``` \ No newline at end of file diff --git a/src/docs/unnecessary_operation.txt b/src/docs/unnecessary_operation.txt deleted file mode 100644 index 7f455e264cb30..0000000000000 --- a/src/docs/unnecessary_operation.txt +++ /dev/null @@ -1,12 +0,0 @@ -### What it does -Checks for expression statements that can be reduced to a -sub-expression. - -### Why is this bad? -Expressions by themselves often have no side-effects. -Having such expressions reduces readability. - -### Example -``` -compute_array()[0]; -``` \ No newline at end of file diff --git a/src/docs/unnecessary_owned_empty_strings.txt b/src/docs/unnecessary_owned_empty_strings.txt deleted file mode 100644 index 8cd9fba603e06..0000000000000 --- a/src/docs/unnecessary_owned_empty_strings.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does - -Detects cases of owned empty strings being passed as an argument to a function expecting `&str` - -### Why is this bad? - -This results in longer and less readable code - -### Example -``` -vec!["1", "2", "3"].join(&String::new()); -``` -Use instead: -``` -vec!["1", "2", "3"].join(""); -``` \ No newline at end of file diff --git a/src/docs/unnecessary_self_imports.txt b/src/docs/unnecessary_self_imports.txt deleted file mode 100644 index b909cd5a76dae..0000000000000 --- a/src/docs/unnecessary_self_imports.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for imports ending in `::{self}`. - -### Why is this bad? -In most cases, this can be written much more cleanly by omitting `::{self}`. - -### Known problems -Removing `::{self}` will cause any non-module items at the same path to also be imported. -This might cause a naming conflict (https://github.com/rust-lang/rustfmt/issues/3568). This lint makes no attempt -to detect this scenario and that is why it is a restriction lint. - -### Example -``` -use std::io::{self}; -``` -Use instead: -``` -use std::io; -``` \ No newline at end of file diff --git a/src/docs/unnecessary_sort_by.txt b/src/docs/unnecessary_sort_by.txt deleted file mode 100644 index 6913b62c48eb4..0000000000000 --- a/src/docs/unnecessary_sort_by.txt +++ /dev/null @@ -1,21 +0,0 @@ -### What it does -Detects uses of `Vec::sort_by` passing in a closure -which compares the two arguments, either directly or indirectly. - -### Why is this bad? -It is more clear to use `Vec::sort_by_key` (or `Vec::sort` if -possible) than to use `Vec::sort_by` and a more complicated -closure. - -### Known problems -If the suggested `Vec::sort_by_key` uses Reverse and it isn't already -imported by a use statement, then it will need to be added manually. - -### Example -``` -vec.sort_by(|a, b| a.foo().cmp(&b.foo())); -``` -Use instead: -``` -vec.sort_by_key(|a| a.foo()); -``` \ No newline at end of file diff --git a/src/docs/unnecessary_to_owned.txt b/src/docs/unnecessary_to_owned.txt deleted file mode 100644 index 5d4213bdaf899..0000000000000 --- a/src/docs/unnecessary_to_owned.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks for unnecessary calls to [`ToOwned::to_owned`](https://doc.rust-lang.org/std/borrow/trait.ToOwned.html#tymethod.to_owned) -and other `to_owned`-like functions. - -### Why is this bad? -The unnecessary calls result in useless allocations. - -### Known problems -`unnecessary_to_owned` can falsely trigger if `IntoIterator::into_iter` is applied to an -owned copy of a resource and the resource is later used mutably. See -[#8148](https://github.com/rust-lang/rust-clippy/issues/8148). - -### Example -``` -let path = std::path::Path::new("x"); -foo(&path.to_string_lossy().to_string()); -fn foo(s: &str) {} -``` -Use instead: -``` -let path = std::path::Path::new("x"); -foo(&path.to_string_lossy()); -fn foo(s: &str) {} -``` \ No newline at end of file diff --git a/src/docs/unnecessary_unwrap.txt b/src/docs/unnecessary_unwrap.txt deleted file mode 100644 index 50ae845bb44f7..0000000000000 --- a/src/docs/unnecessary_unwrap.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Checks for calls of `unwrap[_err]()` that cannot fail. - -### Why is this bad? -Using `if let` or `match` is more idiomatic. - -### Example -``` -if option.is_some() { - do_something_with(option.unwrap()) -} -``` - -Could be written: - -``` -if let Some(value) = option { - do_something_with(value) -} -``` \ No newline at end of file diff --git a/src/docs/unnecessary_wraps.txt b/src/docs/unnecessary_wraps.txt deleted file mode 100644 index c0a23d492889a..0000000000000 --- a/src/docs/unnecessary_wraps.txt +++ /dev/null @@ -1,36 +0,0 @@ -### What it does -Checks for private functions that only return `Ok` or `Some`. - -### Why is this bad? -It is not meaningful to wrap values when no `None` or `Err` is returned. - -### Known problems -There can be false positives if the function signature is designed to -fit some external requirement. - -### Example -``` -fn get_cool_number(a: bool, b: bool) -> Option { - if a && b { - return Some(50); - } - if a { - Some(0) - } else { - Some(10) - } -} -``` -Use instead: -``` -fn get_cool_number(a: bool, b: bool) -> i32 { - if a && b { - return 50; - } - if a { - 0 - } else { - 10 - } -} -``` \ No newline at end of file diff --git a/src/docs/unneeded_field_pattern.txt b/src/docs/unneeded_field_pattern.txt deleted file mode 100644 index 3cd00a0f3e384..0000000000000 --- a/src/docs/unneeded_field_pattern.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Checks for structure field patterns bound to wildcards. - -### Why is this bad? -Using `..` instead is shorter and leaves the focus on -the fields that are actually bound. - -### Example -``` -let f = Foo { a: 0, b: 0, c: 0 }; - -match f { - Foo { a: _, b: 0, .. } => {}, - Foo { a: _, b: _, c: _ } => {}, -} -``` - -Use instead: -``` -let f = Foo { a: 0, b: 0, c: 0 }; - -match f { - Foo { b: 0, .. } => {}, - Foo { .. } => {}, -} -``` \ No newline at end of file diff --git a/src/docs/unneeded_wildcard_pattern.txt b/src/docs/unneeded_wildcard_pattern.txt deleted file mode 100644 index 817061efd1622..0000000000000 --- a/src/docs/unneeded_wildcard_pattern.txt +++ /dev/null @@ -1,28 +0,0 @@ -### What it does -Checks for tuple patterns with a wildcard -pattern (`_`) is next to a rest pattern (`..`). - -_NOTE_: While `_, ..` means there is at least one element left, `..` -means there are 0 or more elements left. This can make a difference -when refactoring, but shouldn't result in errors in the refactored code, -since the wildcard pattern isn't used anyway. - -### Why is this bad? -The wildcard pattern is unneeded as the rest pattern -can match that element as well. - -### Example -``` -match t { - TupleStruct(0, .., _) => (), - _ => (), -} -``` - -Use instead: -``` -match t { - TupleStruct(0, ..) => (), - _ => (), -} -``` \ No newline at end of file diff --git a/src/docs/unnested_or_patterns.txt b/src/docs/unnested_or_patterns.txt deleted file mode 100644 index 49c45d4ee5e9c..0000000000000 --- a/src/docs/unnested_or_patterns.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for unnested or-patterns, e.g., `Some(0) | Some(2)` and -suggests replacing the pattern with a nested one, `Some(0 | 2)`. - -Another way to think of this is that it rewrites patterns in -*disjunctive normal form (DNF)* into *conjunctive normal form (CNF)*. - -### Why is this bad? -In the example above, `Some` is repeated, which unnecessarily complicates the pattern. - -### Example -``` -fn main() { - if let Some(0) | Some(2) = Some(0) {} -} -``` -Use instead: -``` -fn main() { - if let Some(0 | 2) = Some(0) {} -} -``` \ No newline at end of file diff --git a/src/docs/unreachable.txt b/src/docs/unreachable.txt deleted file mode 100644 index 10469ca77454f..0000000000000 --- a/src/docs/unreachable.txt +++ /dev/null @@ -1,10 +0,0 @@ -### What it does -Checks for usage of `unreachable!`. - -### Why is this bad? -This macro can cause code to panic - -### Example -``` -unreachable!(); -``` \ No newline at end of file diff --git a/src/docs/unreadable_literal.txt b/src/docs/unreadable_literal.txt deleted file mode 100644 index e168f90a84c19..0000000000000 --- a/src/docs/unreadable_literal.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Warns if a long integral or floating-point constant does -not contain underscores. - -### Why is this bad? -Reading long numbers is difficult without separators. - -### Example -``` -61864918973511 -``` - -Use instead: -``` -61_864_918_973_511 -``` \ No newline at end of file diff --git a/src/docs/unsafe_derive_deserialize.txt b/src/docs/unsafe_derive_deserialize.txt deleted file mode 100644 index f56c48044f4fa..0000000000000 --- a/src/docs/unsafe_derive_deserialize.txt +++ /dev/null @@ -1,27 +0,0 @@ -### What it does -Checks for deriving `serde::Deserialize` on a type that -has methods using `unsafe`. - -### Why is this bad? -Deriving `serde::Deserialize` will create a constructor -that may violate invariants hold by another constructor. - -### Example -``` -use serde::Deserialize; - -#[derive(Deserialize)] -pub struct Foo { - // .. -} - -impl Foo { - pub fn new() -> Self { - // setup here .. - } - - pub unsafe fn parts() -> (&str, &str) { - // assumes invariants hold - } -} -``` \ No newline at end of file diff --git a/src/docs/unsafe_removed_from_name.txt b/src/docs/unsafe_removed_from_name.txt deleted file mode 100644 index 6f55c1815dd65..0000000000000 --- a/src/docs/unsafe_removed_from_name.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for imports that remove "unsafe" from an item's -name. - -### Why is this bad? -Renaming makes it less clear which traits and -structures are unsafe. - -### Example -``` -use std::cell::{UnsafeCell as TotallySafeCell}; - -extern crate crossbeam; -use crossbeam::{spawn_unsafe as spawn}; -``` \ No newline at end of file diff --git a/src/docs/unseparated_literal_suffix.txt b/src/docs/unseparated_literal_suffix.txt deleted file mode 100644 index d80248e34d0c7..0000000000000 --- a/src/docs/unseparated_literal_suffix.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Warns if literal suffixes are not separated by an -underscore. -To enforce unseparated literal suffix style, -see the `separated_literal_suffix` lint. - -### Why is this bad? -Suffix style should be consistent. - -### Example -``` -123832i32 -``` - -Use instead: -``` -123832_i32 -``` \ No newline at end of file diff --git a/src/docs/unsound_collection_transmute.txt b/src/docs/unsound_collection_transmute.txt deleted file mode 100644 index 29db9258e83fb..0000000000000 --- a/src/docs/unsound_collection_transmute.txt +++ /dev/null @@ -1,25 +0,0 @@ -### What it does -Checks for transmutes between collections whose -types have different ABI, size or alignment. - -### Why is this bad? -This is undefined behavior. - -### Known problems -Currently, we cannot know whether a type is a -collection, so we just lint the ones that come with `std`. - -### Example -``` -// different size, therefore likely out-of-bounds memory access -// You absolutely do not want this in your code! -unsafe { - std::mem::transmute::<_, Vec>(vec![2_u16]) -}; -``` - -You must always iterate, map and collect the values: - -``` -vec![2_u16].into_iter().map(u32::from).collect::>(); -``` \ No newline at end of file diff --git a/src/docs/unused_async.txt b/src/docs/unused_async.txt deleted file mode 100644 index 26def11aa17a5..0000000000000 --- a/src/docs/unused_async.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for functions that are declared `async` but have no `.await`s inside of them. - -### Why is this bad? -Async functions with no async code create overhead, both mentally and computationally. -Callers of async methods either need to be calling from an async function themselves or run it on an executor, both of which -causes runtime overhead and hassle for the caller. - -### Example -``` -async fn get_random_number() -> i64 { - 4 // Chosen by fair dice roll. Guaranteed to be random. -} -let number_future = get_random_number(); -``` - -Use instead: -``` -fn get_random_number_improved() -> i64 { - 4 // Chosen by fair dice roll. Guaranteed to be random. -} -let number_future = async { get_random_number_improved() }; -``` \ No newline at end of file diff --git a/src/docs/unused_format_specs.txt b/src/docs/unused_format_specs.txt deleted file mode 100644 index 77be3a2fb170d..0000000000000 --- a/src/docs/unused_format_specs.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Detects [formatting parameters] that have no effect on the output of -`format!()`, `println!()` or similar macros. - -### Why is this bad? -Shorter format specifiers are easier to read, it may also indicate that -an expected formatting operation such as adding padding isn't happening. - -### Example -``` -println!("{:.}", 1.0); - -println!("not padded: {:5}", format_args!("...")); -``` -Use instead: -``` -println!("{}", 1.0); - -println!("not padded: {}", format_args!("...")); -// OR -println!("padded: {:5}", format!("...")); -``` - -[formatting parameters]: https://doc.rust-lang.org/std/fmt/index.html#formatting-parameters \ No newline at end of file diff --git a/src/docs/unused_io_amount.txt b/src/docs/unused_io_amount.txt deleted file mode 100644 index fbc4c299c7bb7..0000000000000 --- a/src/docs/unused_io_amount.txt +++ /dev/null @@ -1,31 +0,0 @@ -### What it does -Checks for unused written/read amount. - -### Why is this bad? -`io::Write::write(_vectored)` and -`io::Read::read(_vectored)` are not guaranteed to -process the entire buffer. They return how many bytes were processed, which -might be smaller -than a given buffer's length. If you don't need to deal with -partial-write/read, use -`write_all`/`read_exact` instead. - -When working with asynchronous code (either with the `futures` -crate or with `tokio`), a similar issue exists for -`AsyncWriteExt::write()` and `AsyncReadExt::read()` : these -functions are also not guaranteed to process the entire -buffer. Your code should either handle partial-writes/reads, or -call the `write_all`/`read_exact` methods on those traits instead. - -### Known problems -Detects only common patterns. - -### Examples -``` -use std::io; -fn foo(w: &mut W) -> io::Result<()> { - // must be `w.write_all(b"foo")?;` - w.write(b"foo")?; - Ok(()) -} -``` \ No newline at end of file diff --git a/src/docs/unused_peekable.txt b/src/docs/unused_peekable.txt deleted file mode 100644 index 268de1ce3bec3..0000000000000 --- a/src/docs/unused_peekable.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Checks for the creation of a `peekable` iterator that is never `.peek()`ed - -### Why is this bad? -Creating a peekable iterator without using any of its methods is likely a mistake, -or just a leftover after a refactor. - -### Example -``` -let collection = vec![1, 2, 3]; -let iter = collection.iter().peekable(); - -for item in iter { - // ... -} -``` - -Use instead: -``` -let collection = vec![1, 2, 3]; -let iter = collection.iter(); - -for item in iter { - // ... -} -``` \ No newline at end of file diff --git a/src/docs/unused_rounding.txt b/src/docs/unused_rounding.txt deleted file mode 100644 index 70947aceee77b..0000000000000 --- a/src/docs/unused_rounding.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does - -Detects cases where a whole-number literal float is being rounded, using -the `floor`, `ceil`, or `round` methods. - -### Why is this bad? - -This is unnecessary and confusing to the reader. Doing this is probably a mistake. - -### Example -``` -let x = 1f32.ceil(); -``` -Use instead: -``` -let x = 1f32; -``` \ No newline at end of file diff --git a/src/docs/unused_self.txt b/src/docs/unused_self.txt deleted file mode 100644 index a8d0fc7598954..0000000000000 --- a/src/docs/unused_self.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks methods that contain a `self` argument but don't use it - -### Why is this bad? -It may be clearer to define the method as an associated function instead -of an instance method if it doesn't require `self`. - -### Example -``` -struct A; -impl A { - fn method(&self) {} -} -``` - -Could be written: - -``` -struct A; -impl A { - fn method() {} -} -``` \ No newline at end of file diff --git a/src/docs/unused_unit.txt b/src/docs/unused_unit.txt deleted file mode 100644 index 48d16ca655232..0000000000000 --- a/src/docs/unused_unit.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for unit (`()`) expressions that can be removed. - -### Why is this bad? -Such expressions add no value, but can make the code -less readable. Depending on formatting they can make a `break` or `return` -statement look like a function call. - -### Example -``` -fn return_unit() -> () { - () -} -``` -is equivalent to -``` -fn return_unit() {} -``` \ No newline at end of file diff --git a/src/docs/unusual_byte_groupings.txt b/src/docs/unusual_byte_groupings.txt deleted file mode 100644 index 9a1f132a6112f..0000000000000 --- a/src/docs/unusual_byte_groupings.txt +++ /dev/null @@ -1,12 +0,0 @@ -### What it does -Warns if hexadecimal or binary literals are not grouped -by nibble or byte. - -### Why is this bad? -Negatively impacts readability. - -### Example -``` -let x: u32 = 0xFFF_FFF; -let y: u8 = 0b01_011_101; -``` \ No newline at end of file diff --git a/src/docs/unwrap_in_result.txt b/src/docs/unwrap_in_result.txt deleted file mode 100644 index 7497dd863d354..0000000000000 --- a/src/docs/unwrap_in_result.txt +++ /dev/null @@ -1,39 +0,0 @@ -### What it does -Checks for functions of type `Result` that contain `expect()` or `unwrap()` - -### Why is this bad? -These functions promote recoverable errors to non-recoverable errors which may be undesirable in code bases which wish to avoid panics. - -### Known problems -This can cause false positives in functions that handle both recoverable and non recoverable errors. - -### Example -Before: -``` -fn divisible_by_3(i_str: String) -> Result<(), String> { - let i = i_str - .parse::() - .expect("cannot divide the input by three"); - - if i % 3 != 0 { - Err("Number is not divisible by 3")? - } - - Ok(()) -} -``` - -After: -``` -fn divisible_by_3(i_str: String) -> Result<(), String> { - let i = i_str - .parse::() - .map_err(|e| format!("cannot divide the input by three: {}", e))?; - - if i % 3 != 0 { - Err("Number is not divisible by 3")? - } - - Ok(()) -} -``` \ No newline at end of file diff --git a/src/docs/unwrap_or_else_default.txt b/src/docs/unwrap_or_else_default.txt deleted file mode 100644 index 34b4cf088581f..0000000000000 --- a/src/docs/unwrap_or_else_default.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for usages of `_.unwrap_or_else(Default::default)` on `Option` and -`Result` values. - -### Why is this bad? -Readability, these can be written as `_.unwrap_or_default`, which is -simpler and more concise. - -### Examples -``` -x.unwrap_or_else(Default::default); -x.unwrap_or_else(u32::default); -``` - -Use instead: -``` -x.unwrap_or_default(); -``` \ No newline at end of file diff --git a/src/docs/unwrap_used.txt b/src/docs/unwrap_used.txt deleted file mode 100644 index 9b4713df515d6..0000000000000 --- a/src/docs/unwrap_used.txt +++ /dev/null @@ -1,37 +0,0 @@ -### What it does -Checks for `.unwrap()` or `.unwrap_err()` calls on `Result`s and `.unwrap()` call on `Option`s. - -### Why is this bad? -It is better to handle the `None` or `Err` case, -or at least call `.expect(_)` with a more helpful message. Still, for a lot of -quick-and-dirty code, `unwrap` is a good choice, which is why this lint is -`Allow` by default. - -`result.unwrap()` will let the thread panic on `Err` values. -Normally, you want to implement more sophisticated error handling, -and propagate errors upwards with `?` operator. - -Even if you want to panic on errors, not all `Error`s implement good -messages on display. Therefore, it may be beneficial to look at the places -where they may get displayed. Activate this lint to do just that. - -### Examples -``` -option.unwrap(); -result.unwrap(); -``` - -Use instead: -``` -option.expect("more helpful message"); -result.expect("more helpful message"); -``` - -If [expect_used](#expect_used) is enabled, instead: -``` -option?; - -// or - -result?; -``` \ No newline at end of file diff --git a/src/docs/upper_case_acronyms.txt b/src/docs/upper_case_acronyms.txt deleted file mode 100644 index a1e39c7e10c6e..0000000000000 --- a/src/docs/upper_case_acronyms.txt +++ /dev/null @@ -1,25 +0,0 @@ -### What it does -Checks for fully capitalized names and optionally names containing a capitalized acronym. - -### Why is this bad? -In CamelCase, acronyms count as one word. -See [naming conventions](https://rust-lang.github.io/api-guidelines/naming.html#casing-conforms-to-rfc-430-c-case) -for more. - -By default, the lint only triggers on fully-capitalized names. -You can use the `upper-case-acronyms-aggressive: true` config option to enable linting -on all camel case names - -### Known problems -When two acronyms are contiguous, the lint can't tell where -the first acronym ends and the second starts, so it suggests to lowercase all of -the letters in the second acronym. - -### Example -``` -struct HTTPResponse; -``` -Use instead: -``` -struct HttpResponse; -``` \ No newline at end of file diff --git a/src/docs/use_debug.txt b/src/docs/use_debug.txt deleted file mode 100644 index 94d4a6fd29ae5..0000000000000 --- a/src/docs/use_debug.txt +++ /dev/null @@ -1,12 +0,0 @@ -### What it does -Checks for use of `Debug` formatting. The purpose of this -lint is to catch debugging remnants. - -### Why is this bad? -The purpose of the `Debug` trait is to facilitate -debugging Rust code. It should not be used in user-facing output. - -### Example -``` -println!("{:?}", foo); -``` \ No newline at end of file diff --git a/src/docs/use_self.txt b/src/docs/use_self.txt deleted file mode 100644 index bd37ed1e00256..0000000000000 --- a/src/docs/use_self.txt +++ /dev/null @@ -1,31 +0,0 @@ -### What it does -Checks for unnecessary repetition of structure name when a -replacement with `Self` is applicable. - -### Why is this bad? -Unnecessary repetition. Mixed use of `Self` and struct -name -feels inconsistent. - -### Known problems -- Unaddressed false negative in fn bodies of trait implementations -- False positive with associated types in traits (#4140) - -### Example -``` -struct Foo; -impl Foo { - fn new() -> Foo { - Foo {} - } -} -``` -could be -``` -struct Foo; -impl Foo { - fn new() -> Self { - Self {} - } -} -``` \ No newline at end of file diff --git a/src/docs/used_underscore_binding.txt b/src/docs/used_underscore_binding.txt deleted file mode 100644 index ed67c41eb0dbf..0000000000000 --- a/src/docs/used_underscore_binding.txt +++ /dev/null @@ -1,19 +0,0 @@ -### What it does -Checks for the use of bindings with a single leading -underscore. - -### Why is this bad? -A single leading underscore is usually used to indicate -that a binding will not be used. Using such a binding breaks this -expectation. - -### Known problems -The lint does not work properly with desugaring and -macro, it has been allowed in the mean time. - -### Example -``` -let _x = 0; -let y = _x + 1; // Here we are using `_x`, even though it has a leading - // underscore. We should rename `_x` to `x` -``` \ No newline at end of file diff --git a/src/docs/useless_asref.txt b/src/docs/useless_asref.txt deleted file mode 100644 index f777cd3775edf..0000000000000 --- a/src/docs/useless_asref.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for usage of `.as_ref()` or `.as_mut()` where the -types before and after the call are the same. - -### Why is this bad? -The call is unnecessary. - -### Example -``` -let x: &[i32] = &[1, 2, 3, 4, 5]; -do_stuff(x.as_ref()); -``` -The correct use would be: -``` -let x: &[i32] = &[1, 2, 3, 4, 5]; -do_stuff(x); -``` \ No newline at end of file diff --git a/src/docs/useless_attribute.txt b/src/docs/useless_attribute.txt deleted file mode 100644 index e02d4c9078984..0000000000000 --- a/src/docs/useless_attribute.txt +++ /dev/null @@ -1,36 +0,0 @@ -### What it does -Checks for `extern crate` and `use` items annotated with -lint attributes. - -This lint permits lint attributes for lints emitted on the items themself. -For `use` items these lints are: -* deprecated -* unreachable_pub -* unused_imports -* clippy::enum_glob_use -* clippy::macro_use_imports -* clippy::wildcard_imports - -For `extern crate` items these lints are: -* `unused_imports` on items with `#[macro_use]` - -### Why is this bad? -Lint attributes have no effect on crate imports. Most -likely a `!` was forgotten. - -### Example -``` -#[deny(dead_code)] -extern crate foo; -#[forbid(dead_code)] -use foo::bar; -``` - -Use instead: -``` -#[allow(unused_imports)] -use foo::baz; -#[allow(unused_imports)] -#[macro_use] -extern crate baz; -``` \ No newline at end of file diff --git a/src/docs/useless_conversion.txt b/src/docs/useless_conversion.txt deleted file mode 100644 index 06000a7ad98d5..0000000000000 --- a/src/docs/useless_conversion.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for `Into`, `TryInto`, `From`, `TryFrom`, or `IntoIter` calls -which uselessly convert to the same type. - -### Why is this bad? -Redundant code. - -### Example -``` -// format!() returns a `String` -let s: String = format!("hello").into(); -``` - -Use instead: -``` -let s: String = format!("hello"); -``` \ No newline at end of file diff --git a/src/docs/useless_format.txt b/src/docs/useless_format.txt deleted file mode 100644 index eb4819da1e95a..0000000000000 --- a/src/docs/useless_format.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for the use of `format!("string literal with no -argument")` and `format!("{}", foo)` where `foo` is a string. - -### Why is this bad? -There is no point of doing that. `format!("foo")` can -be replaced by `"foo".to_owned()` if you really need a `String`. The even -worse `&format!("foo")` is often encountered in the wild. `format!("{}", -foo)` can be replaced by `foo.clone()` if `foo: String` or `foo.to_owned()` -if `foo: &str`. - -### Examples -``` -let foo = "foo"; -format!("{}", foo); -``` - -Use instead: -``` -let foo = "foo"; -foo.to_owned(); -``` \ No newline at end of file diff --git a/src/docs/useless_let_if_seq.txt b/src/docs/useless_let_if_seq.txt deleted file mode 100644 index c6dcd57eb2e2d..0000000000000 --- a/src/docs/useless_let_if_seq.txt +++ /dev/null @@ -1,39 +0,0 @@ -### What it does -Checks for variable declarations immediately followed by a -conditional affectation. - -### Why is this bad? -This is not idiomatic Rust. - -### Example -``` -let foo; - -if bar() { - foo = 42; -} else { - foo = 0; -} - -let mut baz = None; - -if bar() { - baz = Some(42); -} -``` - -should be written - -``` -let foo = if bar() { - 42 -} else { - 0 -}; - -let baz = if bar() { - Some(42) -} else { - None -}; -``` \ No newline at end of file diff --git a/src/docs/useless_transmute.txt b/src/docs/useless_transmute.txt deleted file mode 100644 index 1d3a175881455..0000000000000 --- a/src/docs/useless_transmute.txt +++ /dev/null @@ -1,12 +0,0 @@ -### What it does -Checks for transmutes to the original type of the object -and transmutes that could be a cast. - -### Why is this bad? -Readability. The code tricks people into thinking that -something complex is going on. - -### Example -``` -core::intrinsics::transmute(t); // where the result type is the same as `t`'s -``` \ No newline at end of file diff --git a/src/docs/useless_vec.txt b/src/docs/useless_vec.txt deleted file mode 100644 index ee5afc99e4bf2..0000000000000 --- a/src/docs/useless_vec.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -Checks for usage of `&vec![..]` when using `&[..]` would -be possible. - -### Why is this bad? -This is less efficient. - -### Example -``` -fn foo(_x: &[u8]) {} - -foo(&vec![1, 2]); -``` - -Use instead: -``` -foo(&[1, 2]); -``` \ No newline at end of file diff --git a/src/docs/vec_box.txt b/src/docs/vec_box.txt deleted file mode 100644 index 701b1c9ce9b94..0000000000000 --- a/src/docs/vec_box.txt +++ /dev/null @@ -1,26 +0,0 @@ -### What it does -Checks for use of `Vec>` where T: Sized anywhere in the code. -Check the [Box documentation](https://doc.rust-lang.org/std/boxed/index.html) for more information. - -### Why is this bad? -`Vec` already keeps its contents in a separate area on -the heap. So if you `Box` its contents, you just add another level of indirection. - -### Known problems -Vec> makes sense if T is a large type (see [#3530](https://github.com/rust-lang/rust-clippy/issues/3530), -1st comment). - -### Example -``` -struct X { - values: Vec>, -} -``` - -Better: - -``` -struct X { - values: Vec, -} -``` \ No newline at end of file diff --git a/src/docs/vec_init_then_push.txt b/src/docs/vec_init_then_push.txt deleted file mode 100644 index 445f2874796bb..0000000000000 --- a/src/docs/vec_init_then_push.txt +++ /dev/null @@ -1,23 +0,0 @@ -### What it does -Checks for calls to `push` immediately after creating a new `Vec`. - -If the `Vec` is created using `with_capacity` this will only lint if the capacity is a -constant and the number of pushes is greater than or equal to the initial capacity. - -If the `Vec` is extended after the initial sequence of pushes and it was default initialized -then this will only lint after there were at least four pushes. This number may change in -the future. - -### Why is this bad? -The `vec![]` macro is both more performant and easier to read than -multiple `push` calls. - -### Example -``` -let mut v = Vec::new(); -v.push(0); -``` -Use instead: -``` -let v = vec![0]; -``` \ No newline at end of file diff --git a/src/docs/vec_resize_to_zero.txt b/src/docs/vec_resize_to_zero.txt deleted file mode 100644 index 0b92686772bb0..0000000000000 --- a/src/docs/vec_resize_to_zero.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Finds occurrences of `Vec::resize(0, an_int)` - -### Why is this bad? -This is probably an argument inversion mistake. - -### Example -``` -vec!(1, 2, 3, 4, 5).resize(0, 5) -``` - -Use instead: -``` -vec!(1, 2, 3, 4, 5).clear() -``` \ No newline at end of file diff --git a/src/docs/verbose_bit_mask.txt b/src/docs/verbose_bit_mask.txt deleted file mode 100644 index 87a84702925ed..0000000000000 --- a/src/docs/verbose_bit_mask.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for bit masks that can be replaced by a call -to `trailing_zeros` - -### Why is this bad? -`x.trailing_zeros() > 4` is much clearer than `x & 15 -== 0` - -### Known problems -llvm generates better code for `x & 15 == 0` on x86 - -### Example -``` -if x & 0b1111 == 0 { } -``` \ No newline at end of file diff --git a/src/docs/verbose_file_reads.txt b/src/docs/verbose_file_reads.txt deleted file mode 100644 index 9703df423a575..0000000000000 --- a/src/docs/verbose_file_reads.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for use of File::read_to_end and File::read_to_string. - -### Why is this bad? -`fs::{read, read_to_string}` provide the same functionality when `buf` is empty with fewer imports and no intermediate values. -See also: [fs::read docs](https://doc.rust-lang.org/std/fs/fn.read.html), [fs::read_to_string docs](https://doc.rust-lang.org/std/fs/fn.read_to_string.html) - -### Example -``` -let mut f = File::open("foo.txt").unwrap(); -let mut bytes = Vec::new(); -f.read_to_end(&mut bytes).unwrap(); -``` -Can be written more concisely as -``` -let mut bytes = fs::read("foo.txt").unwrap(); -``` \ No newline at end of file diff --git a/src/docs/vtable_address_comparisons.txt b/src/docs/vtable_address_comparisons.txt deleted file mode 100644 index 4a34e4ba78ef4..0000000000000 --- a/src/docs/vtable_address_comparisons.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -Checks for comparisons with an address of a trait vtable. - -### Why is this bad? -Comparing trait objects pointers compares an vtable addresses which -are not guaranteed to be unique and could vary between different code generation units. -Furthermore vtables for different types could have the same address after being merged -together. - -### Example -``` -let a: Rc = ... -let b: Rc = ... -if Rc::ptr_eq(&a, &b) { - ... -} -``` \ No newline at end of file diff --git a/src/docs/while_immutable_condition.txt b/src/docs/while_immutable_condition.txt deleted file mode 100644 index 71800701f4897..0000000000000 --- a/src/docs/while_immutable_condition.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Checks whether variables used within while loop condition -can be (and are) mutated in the body. - -### Why is this bad? -If the condition is unchanged, entering the body of the loop -will lead to an infinite loop. - -### Known problems -If the `while`-loop is in a closure, the check for mutation of the -condition variables in the body can cause false negatives. For example when only `Upvar` `a` is -in the condition and only `Upvar` `b` gets mutated in the body, the lint will not trigger. - -### Example -``` -let i = 0; -while i > 10 { - println!("let me loop forever!"); -} -``` \ No newline at end of file diff --git a/src/docs/while_let_loop.txt b/src/docs/while_let_loop.txt deleted file mode 100644 index ab7bf60975ec5..0000000000000 --- a/src/docs/while_let_loop.txt +++ /dev/null @@ -1,25 +0,0 @@ -### What it does -Detects `loop + match` combinations that are easier -written as a `while let` loop. - -### Why is this bad? -The `while let` loop is usually shorter and more -readable. - -### Known problems -Sometimes the wrong binding is displayed ([#383](https://github.com/rust-lang/rust-clippy/issues/383)). - -### Example -``` -loop { - let x = match y { - Some(x) => x, - None => break, - }; - // .. do something with x -} -// is easier written as -while let Some(x) = y { - // .. do something with x -}; -``` \ No newline at end of file diff --git a/src/docs/while_let_on_iterator.txt b/src/docs/while_let_on_iterator.txt deleted file mode 100644 index af053c541199d..0000000000000 --- a/src/docs/while_let_on_iterator.txt +++ /dev/null @@ -1,20 +0,0 @@ -### What it does -Checks for `while let` expressions on iterators. - -### Why is this bad? -Readability. A simple `for` loop is shorter and conveys -the intent better. - -### Example -``` -while let Some(val) = iter.next() { - .. -} -``` - -Use instead: -``` -for val in &mut iter { - .. -} -``` \ No newline at end of file diff --git a/src/docs/wildcard_dependencies.txt b/src/docs/wildcard_dependencies.txt deleted file mode 100644 index 2affaf9740d9f..0000000000000 --- a/src/docs/wildcard_dependencies.txt +++ /dev/null @@ -1,13 +0,0 @@ -### What it does -Checks for wildcard dependencies in the `Cargo.toml`. - -### Why is this bad? -[As the edition guide says](https://rust-lang-nursery.github.io/edition-guide/rust-2018/cargo-and-crates-io/crates-io-disallows-wildcard-dependencies.html), -it is highly unlikely that you work with any possible version of your dependency, -and wildcard dependencies would cause unnecessary breakage in the ecosystem. - -### Example -``` -[dependencies] -regex = "*" -``` \ No newline at end of file diff --git a/src/docs/wildcard_enum_match_arm.txt b/src/docs/wildcard_enum_match_arm.txt deleted file mode 100644 index 09807c01c652b..0000000000000 --- a/src/docs/wildcard_enum_match_arm.txt +++ /dev/null @@ -1,25 +0,0 @@ -### What it does -Checks for wildcard enum matches using `_`. - -### Why is this bad? -New enum variants added by library updates can be missed. - -### Known problems -Suggested replacements may be incorrect if guards exhaustively cover some -variants, and also may not use correct path to enum if it's not present in the current scope. - -### Example -``` -match x { - Foo::A(_) => {}, - _ => {}, -} -``` - -Use instead: -``` -match x { - Foo::A(_) => {}, - Foo::B(_) => {}, -} -``` \ No newline at end of file diff --git a/src/docs/wildcard_imports.txt b/src/docs/wildcard_imports.txt deleted file mode 100644 index bd56aa5b08282..0000000000000 --- a/src/docs/wildcard_imports.txt +++ /dev/null @@ -1,45 +0,0 @@ -### What it does -Checks for wildcard imports `use _::*`. - -### Why is this bad? -wildcard imports can pollute the namespace. This is especially bad if -you try to import something through a wildcard, that already has been imported by name from -a different source: - -``` -use crate1::foo; // Imports a function named foo -use crate2::*; // Has a function named foo - -foo(); // Calls crate1::foo -``` - -This can lead to confusing error messages at best and to unexpected behavior at worst. - -### Exceptions -Wildcard imports are allowed from modules named `prelude`. Many crates (including the standard library) -provide modules named "prelude" specifically designed for wildcard import. - -`use super::*` is allowed in test modules. This is defined as any module with "test" in the name. - -These exceptions can be disabled using the `warn-on-all-wildcard-imports` configuration flag. - -### Known problems -If macros are imported through the wildcard, this macro is not included -by the suggestion and has to be added by hand. - -Applying the suggestion when explicit imports of the things imported with a glob import -exist, may result in `unused_imports` warnings. - -### Example -``` -use crate1::*; - -foo(); -``` - -Use instead: -``` -use crate1::foo; - -foo(); -``` \ No newline at end of file diff --git a/src/docs/wildcard_in_or_patterns.txt b/src/docs/wildcard_in_or_patterns.txt deleted file mode 100644 index 70468ca41e0b8..0000000000000 --- a/src/docs/wildcard_in_or_patterns.txt +++ /dev/null @@ -1,22 +0,0 @@ -### What it does -Checks for wildcard pattern used with others patterns in same match arm. - -### Why is this bad? -Wildcard pattern already covers any other pattern as it will match anyway. -It makes the code less readable, especially to spot wildcard pattern use in match arm. - -### Example -``` -match s { - "a" => {}, - "bar" | _ => {}, -} -``` - -Use instead: -``` -match s { - "a" => {}, - _ => {}, -} -``` \ No newline at end of file diff --git a/src/docs/write_literal.txt b/src/docs/write_literal.txt deleted file mode 100644 index a7a884d087115..0000000000000 --- a/src/docs/write_literal.txt +++ /dev/null @@ -1,17 +0,0 @@ -### What it does -This lint warns about the use of literals as `write!`/`writeln!` args. - -### Why is this bad? -Using literals as `writeln!` args is inefficient -(c.f., https://github.com/matthiaskrgr/rust-str-bench) and unnecessary -(i.e., just put the literal in the format string) - -### Example -``` -writeln!(buf, "{}", "foo"); -``` - -Use instead: -``` -writeln!(buf, "foo"); -``` \ No newline at end of file diff --git a/src/docs/write_with_newline.txt b/src/docs/write_with_newline.txt deleted file mode 100644 index 22845fd6515bd..0000000000000 --- a/src/docs/write_with_newline.txt +++ /dev/null @@ -1,18 +0,0 @@ -### What it does -This lint warns when you use `write!()` with a format -string that -ends in a newline. - -### Why is this bad? -You should use `writeln!()` instead, which appends the -newline. - -### Example -``` -write!(buf, "Hello {}!\n", name); -``` - -Use instead: -``` -writeln!(buf, "Hello {}!", name); -``` \ No newline at end of file diff --git a/src/docs/writeln_empty_string.txt b/src/docs/writeln_empty_string.txt deleted file mode 100644 index 3b3aeb79a48bc..0000000000000 --- a/src/docs/writeln_empty_string.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -This lint warns when you use `writeln!(buf, "")` to -print a newline. - -### Why is this bad? -You should use `writeln!(buf)`, which is simpler. - -### Example -``` -writeln!(buf, ""); -``` - -Use instead: -``` -writeln!(buf); -``` \ No newline at end of file diff --git a/src/docs/wrong_self_convention.txt b/src/docs/wrong_self_convention.txt deleted file mode 100644 index d6b69ab87f862..0000000000000 --- a/src/docs/wrong_self_convention.txt +++ /dev/null @@ -1,39 +0,0 @@ -### What it does -Checks for methods with certain name prefixes and which -doesn't match how self is taken. The actual rules are: - -|Prefix |Postfix |`self` taken | `self` type | -|-------|------------|-------------------------------|--------------| -|`as_` | none |`&self` or `&mut self` | any | -|`from_`| none | none | any | -|`into_`| none |`self` | any | -|`is_` | none |`&mut self` or `&self` or none | any | -|`to_` | `_mut` |`&mut self` | any | -|`to_` | not `_mut` |`self` | `Copy` | -|`to_` | not `_mut` |`&self` | not `Copy` | - -Note: Clippy doesn't trigger methods with `to_` prefix in: -- Traits definition. -Clippy can not tell if a type that implements a trait is `Copy` or not. -- Traits implementation, when `&self` is taken. -The method signature is controlled by the trait and often `&self` is required for all types that implement the trait -(see e.g. the `std::string::ToString` trait). - -Clippy allows `Pin<&Self>` and `Pin<&mut Self>` if `&self` and `&mut self` is required. - -Please find more info here: -https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv - -### Why is this bad? -Consistency breeds readability. If you follow the -conventions, your users won't be surprised that they, e.g., need to supply a -mutable reference to a `as_..` function. - -### Example -``` -impl X { - fn as_str(self) -> &'static str { - // .. - } -} -``` \ No newline at end of file diff --git a/src/docs/wrong_transmute.txt b/src/docs/wrong_transmute.txt deleted file mode 100644 index 9fc71e0e382e3..0000000000000 --- a/src/docs/wrong_transmute.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for transmutes that can't ever be correct on any -architecture. - -### Why is this bad? -It's basically guaranteed to be undefined behavior. - -### Known problems -When accessing C, users might want to store pointer -sized objects in `extradata` arguments to save an allocation. - -### Example -``` -let ptr: *const T = core::intrinsics::transmute('x') -``` \ No newline at end of file diff --git a/src/docs/zero_divided_by_zero.txt b/src/docs/zero_divided_by_zero.txt deleted file mode 100644 index 394de20c0c0c6..0000000000000 --- a/src/docs/zero_divided_by_zero.txt +++ /dev/null @@ -1,15 +0,0 @@ -### What it does -Checks for `0.0 / 0.0`. - -### Why is this bad? -It's less readable than `f32::NAN` or `f64::NAN`. - -### Example -``` -let nan = 0.0f32 / 0.0; -``` - -Use instead: -``` -let nan = f32::NAN; -``` \ No newline at end of file diff --git a/src/docs/zero_prefixed_literal.txt b/src/docs/zero_prefixed_literal.txt deleted file mode 100644 index 5c5588725363e..0000000000000 --- a/src/docs/zero_prefixed_literal.txt +++ /dev/null @@ -1,32 +0,0 @@ -### What it does -Warns if an integral constant literal starts with `0`. - -### Why is this bad? -In some languages (including the infamous C language -and most of its -family), this marks an octal constant. In Rust however, this is a decimal -constant. This could -be confusing for both the writer and a reader of the constant. - -### Example - -In Rust: -``` -fn main() { - let a = 0123; - println!("{}", a); -} -``` - -prints `123`, while in C: - -``` -#include - -int main() { - int a = 0123; - printf("%d\n", a); -} -``` - -prints `83` (as `83 == 0o123` while `123 == 0o173`). \ No newline at end of file diff --git a/src/docs/zero_ptr.txt b/src/docs/zero_ptr.txt deleted file mode 100644 index e768a02366009..0000000000000 --- a/src/docs/zero_ptr.txt +++ /dev/null @@ -1,16 +0,0 @@ -### What it does -Catch casts from `0` to some pointer type - -### Why is this bad? -This generally means `null` and is better expressed as -{`std`, `core`}`::ptr::`{`null`, `null_mut`}. - -### Example -``` -let a = 0 as *const u32; -``` - -Use instead: -``` -let a = std::ptr::null::(); -``` \ No newline at end of file diff --git a/src/docs/zero_sized_map_values.txt b/src/docs/zero_sized_map_values.txt deleted file mode 100644 index 0502bdbf3950e..0000000000000 --- a/src/docs/zero_sized_map_values.txt +++ /dev/null @@ -1,24 +0,0 @@ -### What it does -Checks for maps with zero-sized value types anywhere in the code. - -### Why is this bad? -Since there is only a single value for a zero-sized type, a map -containing zero sized values is effectively a set. Using a set in that case improves -readability and communicates intent more clearly. - -### Known problems -* A zero-sized type cannot be recovered later if it contains private fields. -* This lints the signature of public items - -### Example -``` -fn unique_words(text: &str) -> HashMap<&str, ()> { - todo!(); -} -``` -Use instead: -``` -fn unique_words(text: &str) -> HashSet<&str> { - todo!(); -} -``` \ No newline at end of file diff --git a/src/docs/zst_offset.txt b/src/docs/zst_offset.txt deleted file mode 100644 index 5810455ee9550..0000000000000 --- a/src/docs/zst_offset.txt +++ /dev/null @@ -1,11 +0,0 @@ -### What it does -Checks for `offset(_)`, `wrapping_`{`add`, `sub`}, etc. on raw pointers to -zero-sized types - -### Why is this bad? -This is a no-op, and likely unintended - -### Example -``` -unsafe { (&() as *const ()).offset(1) }; -``` \ No newline at end of file diff --git a/src/driver.rs b/src/driver.rs index f24d3507823ec..ee2a3ad20d3e5 100644 --- a/src/driver.rs +++ b/src/driver.rs @@ -1,4 +1,5 @@ #![feature(rustc_private)] +#![feature(let_chains)] #![feature(once_cell)] #![cfg_attr(feature = "deny-warnings", deny(warnings))] // warn on lints, that are included in `rust-lang/rust`s bootstrap @@ -71,6 +72,32 @@ fn track_clippy_args(parse_sess: &mut ParseSess, args_env_var: &Option) )); } +/// Track files that may be accessed at runtime in `file_depinfo` so that cargo will re-run clippy +/// when any of them are modified +fn track_files(parse_sess: &mut ParseSess, conf_path_string: Option) { + let file_depinfo = parse_sess.file_depinfo.get_mut(); + + // Used by `clippy::cargo` lints and to determine the MSRV. `cargo clippy` executes `clippy-driver` + // with the current directory set to `CARGO_MANIFEST_DIR` so a relative path is fine + if Path::new("Cargo.toml").exists() { + file_depinfo.insert(Symbol::intern("Cargo.toml")); + } + + // `clippy.toml` + if let Some(path) = conf_path_string { + file_depinfo.insert(Symbol::intern(&path)); + } + + // During development track the `clippy-driver` executable so that cargo will re-run clippy whenever + // it is rebuilt + if cfg!(debug_assertions) + && let Ok(current_exe) = env::current_exe() + && let Some(current_exe) = current_exe.to_str() + { + file_depinfo.insert(Symbol::intern(current_exe)); + } +} + struct DefaultCallbacks; impl rustc_driver::Callbacks for DefaultCallbacks {} @@ -97,10 +124,18 @@ impl rustc_driver::Callbacks for ClippyCallbacks { // JUSTIFICATION: necessary in clippy driver to set `mir_opt_level` #[allow(rustc::bad_opt_access)] fn config(&mut self, config: &mut interface::Config) { + let conf_path = clippy_lints::lookup_conf_file(); + let conf_path_string = if let Ok(Some(path)) = &conf_path { + path.to_str().map(String::from) + } else { + None + }; + let previous = config.register_lints.take(); let clippy_args_var = self.clippy_args_var.take(); config.parse_sess_created = Some(Box::new(move |parse_sess| { track_clippy_args(parse_sess, &clippy_args_var); + track_files(parse_sess, conf_path_string); })); config.register_lints = Some(Box::new(move |sess, lint_store| { // technically we're ~guaranteed that this is none but might as well call anything that @@ -109,7 +144,7 @@ impl rustc_driver::Callbacks for ClippyCallbacks { (previous)(sess, lint_store); } - let conf = clippy_lints::read_conf(sess); + let conf = clippy_lints::read_conf(sess, &conf_path); clippy_lints::register_plugins(lint_store, sess, &conf); clippy_lints::register_pre_expansion_lints(lint_store, sess, &conf); clippy_lints::register_renamed(lint_store); @@ -217,6 +252,13 @@ pub fn main() { exit(rustc_driver::catch_with_exit_code(move || { let mut orig_args: Vec = env::args().collect(); + let sys_root_env = std::env::var("SYSROOT").ok(); + let pass_sysroot_env_if_given = |args: &mut Vec, sys_root_env| { + if let Some(sys_root) = sys_root_env { + args.extend(vec!["--sysroot".into(), sys_root]); + }; + }; + // make "clippy-driver --rustc" work like a subcommand that passes further args to "rustc" // for example `clippy-driver --rustc --version` will print the rustc version that clippy-driver // uses @@ -224,7 +266,10 @@ pub fn main() { orig_args.remove(pos); orig_args[0] = "rustc".to_string(); - return rustc_driver::RunCompiler::new(&orig_args, &mut DefaultCallbacks).run(); + let mut args: Vec = orig_args.clone(); + pass_sysroot_env_if_given(&mut args, sys_root_env); + + return rustc_driver::RunCompiler::new(&args, &mut DefaultCallbacks).run(); } if orig_args.iter().any(|a| a == "--version" || a == "-V") { @@ -247,6 +292,9 @@ pub fn main() { exit(0); } + let mut args: Vec = orig_args.clone(); + pass_sysroot_env_if_given(&mut args, sys_root_env); + let mut no_deps = false; let clippy_args_var = env::var("CLIPPY_ARGS").ok(); let clippy_args = clippy_args_var @@ -275,11 +323,10 @@ pub fn main() { let clippy_enabled = !cap_lints_allow && (!no_deps || in_primary_package); if clippy_enabled { - let mut args: Vec = orig_args.clone(); args.extend(clippy_args); rustc_driver::RunCompiler::new(&args, &mut ClippyCallbacks { clippy_args_var }).run() } else { - rustc_driver::RunCompiler::new(&orig_args, &mut RustcCallbacks { clippy_args_var }).run() + rustc_driver::RunCompiler::new(&args, &mut RustcCallbacks { clippy_args_var }).run() } })) } diff --git a/src/main.rs b/src/main.rs index fce3cdfc462e0..d418d2daa313e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,8 +7,6 @@ use std::env; use std::path::PathBuf; use std::process::{self, Command}; -mod docs; - const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code. Usage: @@ -60,7 +58,7 @@ pub fn main() { if let Some(pos) = env::args().position(|a| a == "--explain") { if let Some(mut lint) = env::args().nth(pos + 1) { lint.make_ascii_lowercase(); - docs::explain(&lint.strip_prefix("clippy::").unwrap_or(&lint).replace('-', "_")); + clippy_lints::explain(&lint.strip_prefix("clippy::").unwrap_or(&lint).replace('-', "_")); } else { show_help(); } diff --git a/tests/ui-cargo/cargo_rust_version/fail_both_diff/src/main.stderr b/tests/ui-cargo/cargo_rust_version/fail_both_diff/src/main.stderr index 9a7d802dc6d3a..163f8bb35e79b 100644 --- a/tests/ui-cargo/cargo_rust_version/fail_both_diff/src/main.stderr +++ b/tests/ui-cargo/cargo_rust_version/fail_both_diff/src/main.stderr @@ -12,5 +12,11 @@ note: the lint level is defined here LL | #![deny(clippy::use_self)] | ^^^^^^^^^^^^^^^^ -error: aborting due to previous error; 1 warning emitted +error: unnecessary structure name repetition + --> $DIR/main.rs:7:9 + | +LL | Foo + | ^^^ help: use the applicable keyword: `Self` + +error: aborting due to 2 previous errors; 1 warning emitted diff --git a/tests/ui-cargo/cargo_rust_version/fail_both_same/src/main.stderr b/tests/ui-cargo/cargo_rust_version/fail_both_same/src/main.stderr index a280e1bacdfdc..259d39b12526c 100644 --- a/tests/ui-cargo/cargo_rust_version/fail_both_same/src/main.stderr +++ b/tests/ui-cargo/cargo_rust_version/fail_both_same/src/main.stderr @@ -10,5 +10,11 @@ note: the lint level is defined here LL | #![deny(clippy::use_self)] | ^^^^^^^^^^^^^^^^ -error: aborting due to previous error +error: unnecessary structure name repetition + --> $DIR/main.rs:7:9 + | +LL | Foo + | ^^^ help: use the applicable keyword: `Self` + +error: aborting due to 2 previous errors diff --git a/tests/ui-cargo/cargo_rust_version/fail_cargo/src/main.stderr b/tests/ui-cargo/cargo_rust_version/fail_cargo/src/main.stderr index a280e1bacdfdc..259d39b12526c 100644 --- a/tests/ui-cargo/cargo_rust_version/fail_cargo/src/main.stderr +++ b/tests/ui-cargo/cargo_rust_version/fail_cargo/src/main.stderr @@ -10,5 +10,11 @@ note: the lint level is defined here LL | #![deny(clippy::use_self)] | ^^^^^^^^^^^^^^^^ -error: aborting due to previous error +error: unnecessary structure name repetition + --> $DIR/main.rs:7:9 + | +LL | Foo + | ^^^ help: use the applicable keyword: `Self` + +error: aborting due to 2 previous errors diff --git a/tests/ui-cargo/cargo_rust_version/fail_clippy/src/main.stderr b/tests/ui-cargo/cargo_rust_version/fail_clippy/src/main.stderr index a280e1bacdfdc..259d39b12526c 100644 --- a/tests/ui-cargo/cargo_rust_version/fail_clippy/src/main.stderr +++ b/tests/ui-cargo/cargo_rust_version/fail_clippy/src/main.stderr @@ -10,5 +10,11 @@ note: the lint level is defined here LL | #![deny(clippy::use_self)] | ^^^^^^^^^^^^^^^^ -error: aborting due to previous error +error: unnecessary structure name repetition + --> $DIR/main.rs:7:9 + | +LL | Foo + | ^^^ help: use the applicable keyword: `Self` + +error: aborting due to 2 previous errors diff --git a/tests/ui-cargo/cargo_rust_version/fail_file_attr/src/main.stderr b/tests/ui-cargo/cargo_rust_version/fail_file_attr/src/main.stderr index 88f6e00922bc4..97e6c3d5a5592 100644 --- a/tests/ui-cargo/cargo_rust_version/fail_file_attr/src/main.stderr +++ b/tests/ui-cargo/cargo_rust_version/fail_file_attr/src/main.stderr @@ -10,5 +10,11 @@ note: the lint level is defined here LL | #![deny(clippy::use_self)] | ^^^^^^^^^^^^^^^^ -error: aborting due to previous error +error: unnecessary structure name repetition + --> $DIR/main.rs:12:9 + | +LL | Foo + | ^^^ help: use the applicable keyword: `Self` + +error: aborting due to 2 previous errors diff --git a/tests/ui-internal/custom_ice_message.rs b/tests/ui-internal/custom_ice_message.rs index 5057a018300e9..4be04f77f5bdc 100644 --- a/tests/ui-internal/custom_ice_message.rs +++ b/tests/ui-internal/custom_ice_message.rs @@ -2,6 +2,7 @@ // normalize-stderr-test: "Clippy version: .*" -> "Clippy version: foo" // normalize-stderr-test: "internal_lints.rs:\d*:\d*" -> "internal_lints.rs" // normalize-stderr-test: "', .*clippy_lints" -> "', clippy_lints" +// normalize-stderr-test: "'rustc'" -> "''" #![deny(clippy::internal)] #![allow(clippy::missing_clippy_version_attribute)] diff --git a/tests/ui-internal/custom_ice_message.stderr b/tests/ui-internal/custom_ice_message.stderr index 07c5941013c1a..2ba5890660fc9 100644 --- a/tests/ui-internal/custom_ice_message.stderr +++ b/tests/ui-internal/custom_ice_message.stderr @@ -1,4 +1,4 @@ -thread 'rustc' panicked at 'Would you like some help with that?', clippy_lints/src/utils/internal_lints/produce_ice.rs:28:9 +thread '' panicked at 'Would you like some help with that?', clippy_lints/src/utils/internal_lints/produce_ice.rs:28:9 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace error: internal compiler error: unexpected panic diff --git a/tests/ui-internal/unnecessary_def_path_hardcoded_path.stderr b/tests/ui-internal/unnecessary_def_path_hardcoded_path.stderr index 2a240cc249b0c..8bfc060e99106 100644 --- a/tests/ui-internal/unnecessary_def_path_hardcoded_path.stderr +++ b/tests/ui-internal/unnecessary_def_path_hardcoded_path.stderr @@ -1,3 +1,12 @@ +error: hardcoded path to a language item + --> $DIR/unnecessary_def_path_hardcoded_path.rs:11:40 + | +LL | const DEREF_MUT_TRAIT: [&str; 4] = ["core", "ops", "deref", "DerefMut"]; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: convert all references to use `LangItem::DerefMut` + = note: `-D clippy::unnecessary-def-path` implied by `-D warnings` + error: hardcoded path to a diagnostic item --> $DIR/unnecessary_def_path_hardcoded_path.rs:10:36 | @@ -5,7 +14,6 @@ LL | const DEREF_TRAIT: [&str; 4] = ["core", "ops", "deref", "Deref"]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: convert all references to use `sym::Deref` - = note: `-D clippy::unnecessary-def-path` implied by `-D warnings` error: hardcoded path to a diagnostic item --> $DIR/unnecessary_def_path_hardcoded_path.rs:12:43 @@ -15,13 +23,5 @@ LL | const DEREF_TRAIT_METHOD: [&str; 5] = ["core", "ops", "deref", "Deref", | = help: convert all references to use `sym::deref_method` -error: hardcoded path to a language item - --> $DIR/unnecessary_def_path_hardcoded_path.rs:11:40 - | -LL | const DEREF_MUT_TRAIT: [&str; 4] = ["core", "ops", "deref", "DerefMut"]; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: convert all references to use `LangItem::DerefMut` - error: aborting due to 3 previous errors diff --git a/tests/ui-toml/arithmetic_side_effects_allowed/arithmetic_side_effects_allowed.rs b/tests/ui-toml/arithmetic_side_effects_allowed/arithmetic_side_effects_allowed.rs index 1aed09b7c7bd4..e8a023ab17643 100644 --- a/tests/ui-toml/arithmetic_side_effects_allowed/arithmetic_side_effects_allowed.rs +++ b/tests/ui-toml/arithmetic_side_effects_allowed/arithmetic_side_effects_allowed.rs @@ -1,6 +1,6 @@ #![warn(clippy::arithmetic_side_effects)] -use core::ops::Add; +use core::ops::{Add, Neg}; #[derive(Clone, Copy)] struct Point { @@ -16,9 +16,18 @@ impl Add for Point { } } +impl Neg for Point { + type Output = Self; + + fn neg(self) -> Self::Output { + todo!() + } +} + fn main() { let _ = Point { x: 1, y: 0 } + Point { x: 2, y: 3 }; let point: Point = Point { x: 1, y: 0 }; let _ = point + point; + let _ = -point; } diff --git a/tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.stderr b/tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.stderr index 4c75998437fd5..825aa1487e7a3 100644 --- a/tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.stderr +++ b/tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.stderr @@ -4,7 +4,7 @@ error: `std::string::String` may not be held across an `await` point per `clippy LL | let _x = String::from("hello"); | ^^ | - = note: strings are bad + = note: strings are bad (from clippy.toml) = note: `-D clippy::await-holding-invalid-type` implied by `-D warnings` error: `std::net::Ipv4Addr` may not be held across an `await` point per `clippy.toml` @@ -19,7 +19,7 @@ error: `std::string::String` may not be held across an `await` point per `clippy LL | let _x = String::from("hi!"); | ^^ | - = note: strings are bad + = note: strings are bad (from clippy.toml) error: aborting due to 3 previous errors diff --git a/tests/ui-toml/expect_used/expect_used.rs b/tests/ui-toml/expect_used/expect_used.rs index 22dcd3ae9d697..bff97d97df77a 100644 --- a/tests/ui-toml/expect_used/expect_used.rs +++ b/tests/ui-toml/expect_used/expect_used.rs @@ -16,14 +16,19 @@ fn main() { expect_result(); } -#[test] -fn test_expect_option() { - let opt = Some(0); - let _ = opt.expect(""); -} +#[cfg(test)] +mod issue9612 { + // should not lint in `#[cfg(test)]` modules + #[test] + fn test_fn() { + let _a: u8 = 2.try_into().unwrap(); + let _a: u8 = 3.try_into().expect(""); -#[test] -fn test_expect_result() { - let res: Result = Ok(0); - let _ = res.expect(""); + util(); + } + + fn util() { + let _a: u8 = 4.try_into().unwrap(); + let _a: u8 = 5.try_into().expect(""); + } } diff --git a/tests/ui-toml/expect_used/expect_used.stderr b/tests/ui-toml/expect_used/expect_used.stderr index 28a08599c67c6..1e9bb48c333ca 100644 --- a/tests/ui-toml/expect_used/expect_used.stderr +++ b/tests/ui-toml/expect_used/expect_used.stderr @@ -1,4 +1,4 @@ -error: used `expect()` on `an Option` value +error: used `expect()` on an `Option` value --> $DIR/expect_used.rs:6:13 | LL | let _ = opt.expect(""); @@ -7,7 +7,7 @@ LL | let _ = opt.expect(""); = help: if this value is `None`, it will panic = note: `-D clippy::expect-used` implied by `-D warnings` -error: used `expect()` on `a Result` value +error: used `expect()` on a `Result` value --> $DIR/expect_used.rs:11:13 | LL | let _ = res.expect(""); diff --git a/tests/ui-toml/mut_key/clippy.toml b/tests/ui-toml/mut_key/clippy.toml new file mode 100644 index 0000000000000..6d33e192ee893 --- /dev/null +++ b/tests/ui-toml/mut_key/clippy.toml @@ -0,0 +1 @@ +ignore-interior-mutability = ["mut_key::Counted"] \ No newline at end of file diff --git a/tests/ui-toml/mut_key/mut_key.rs b/tests/ui-toml/mut_key/mut_key.rs new file mode 100644 index 0000000000000..667c51cb4a3f3 --- /dev/null +++ b/tests/ui-toml/mut_key/mut_key.rs @@ -0,0 +1,53 @@ +// compile-flags: --crate-name mut_key + +#![warn(clippy::mutable_key_type)] + +use std::cmp::{Eq, PartialEq}; +use std::collections::{HashMap, HashSet}; +use std::hash::{Hash, Hasher}; +use std::ops::Deref; +use std::sync::atomic::{AtomicUsize, Ordering}; + +struct Counted { + count: AtomicUsize, + val: T, +} + +impl Clone for Counted { + fn clone(&self) -> Self { + Self { + count: AtomicUsize::new(0), + val: self.val.clone(), + } + } +} + +impl PartialEq for Counted { + fn eq(&self, other: &Self) -> bool { + self.val == other.val + } +} +impl Eq for Counted {} + +impl Hash for Counted { + fn hash(&self, state: &mut H) { + self.val.hash(state); + } +} + +impl Deref for Counted { + type Target = T; + + fn deref(&self) -> &T { + self.count.fetch_add(1, Ordering::AcqRel); + &self.val + } +} + +// This is not linted because `"mut_key::Counted"` is in +// `arc_like_types` in `clippy.toml` +fn should_not_take_this_arg(_v: HashSet>) {} + +fn main() { + should_not_take_this_arg(HashSet::new()); +} diff --git a/tests/ui-toml/print_macro/clippy.toml b/tests/ui-toml/print_macro/clippy.toml new file mode 100644 index 0000000000000..40b1dda5b2ea7 --- /dev/null +++ b/tests/ui-toml/print_macro/clippy.toml @@ -0,0 +1 @@ +allow-print-in-tests = true diff --git a/tests/ui-toml/print_macro/print_macro.rs b/tests/ui-toml/print_macro/print_macro.rs new file mode 100644 index 0000000000000..5aefb6a6b4d46 --- /dev/null +++ b/tests/ui-toml/print_macro/print_macro.rs @@ -0,0 +1,20 @@ +// compile-flags: --test +#![warn(clippy::print_stdout)] +#![warn(clippy::print_stderr)] + +fn foo(n: u32) { + print!("{n}"); + eprint!("{n}"); +} + +#[test] +pub fn foo1() { + print!("{}", 1); + eprint!("{}", 1); +} + +#[cfg(test)] +fn foo3() { + print!("{}", 1); + eprint!("{}", 1); +} diff --git a/tests/ui-toml/print_macro/print_macro.stderr b/tests/ui-toml/print_macro/print_macro.stderr new file mode 100644 index 0000000000000..d4b1ae84fd7dc --- /dev/null +++ b/tests/ui-toml/print_macro/print_macro.stderr @@ -0,0 +1,18 @@ +error: use of `print!` + --> $DIR/print_macro.rs:6:5 + | +LL | print!("{n}"); + | ^^^^^^^^^^^^^ + | + = note: `-D clippy::print-stdout` implied by `-D warnings` + +error: use of `eprint!` + --> $DIR/print_macro.rs:7:5 + | +LL | eprint!("{n}"); + | ^^^^^^^^^^^^^^ + | + = note: `-D clippy::print-stderr` implied by `-D warnings` + +error: aborting due to 2 previous errors + diff --git a/tests/ui-toml/toml_disallowed_methods/clippy.toml b/tests/ui-toml/toml_disallowed_methods/clippy.toml index 28774db625bb7..41dbd5068479b 100644 --- a/tests/ui-toml/toml_disallowed_methods/clippy.toml +++ b/tests/ui-toml/toml_disallowed_methods/clippy.toml @@ -8,4 +8,10 @@ disallowed-methods = [ { path = "regex::Regex::is_match", reason = "no matching allowed" }, # can use an inline table but omit reason { path = "regex::Regex::new" }, + # local paths + "conf_disallowed_methods::local_fn", + "conf_disallowed_methods::local_mod::f", + "conf_disallowed_methods::Struct::method", + "conf_disallowed_methods::Trait::provided_method", + "conf_disallowed_methods::Trait::implemented_method", ] diff --git a/tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs b/tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs index b483f16002897..2f3160c833833 100644 --- a/tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs +++ b/tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs @@ -1,3 +1,5 @@ +// compile-flags: --crate-name conf_disallowed_methods + #![warn(clippy::disallowed_methods)] extern crate futures; @@ -6,6 +8,27 @@ extern crate regex; use futures::stream::{empty, select_all}; use regex::Regex; +fn local_fn() {} + +struct Struct; + +impl Struct { + fn method(&self) {} +} + +trait Trait { + fn provided_method(&self) {} + fn implemented_method(&self); +} + +impl Trait for Struct { + fn implemented_method(&self) {} +} + +mod local_mod { + pub fn f() {} +} + fn main() { let re = Regex::new(r"ab.*c").unwrap(); re.is_match("abc"); @@ -26,4 +49,11 @@ fn main() { // resolve ambiguity between `futures::stream::select_all` the module and the function let same_name_as_module = select_all(vec![empty::<()>()]); + + local_fn(); + local_mod::f(); + let s = Struct; + s.method(); + s.provided_method(); + s.implemented_method(); } diff --git a/tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.stderr b/tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.stderr index 6d78c32e127ee..148d1cae51f10 100644 --- a/tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.stderr +++ b/tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.stderr @@ -1,5 +1,5 @@ error: use of a disallowed method `regex::Regex::new` - --> $DIR/conf_disallowed_methods.rs:10:14 + --> $DIR/conf_disallowed_methods.rs:33:14 | LL | let re = Regex::new(r"ab.*c").unwrap(); | ^^^^^^^^^^^^^^^^^^^^ @@ -7,7 +7,7 @@ LL | let re = Regex::new(r"ab.*c").unwrap(); = note: `-D clippy::disallowed-methods` implied by `-D warnings` error: use of a disallowed method `regex::Regex::is_match` - --> $DIR/conf_disallowed_methods.rs:11:5 + --> $DIR/conf_disallowed_methods.rs:34:5 | LL | re.is_match("abc"); | ^^^^^^^^^^^^^^^^^^ @@ -15,46 +15,76 @@ LL | re.is_match("abc"); = note: no matching allowed (from clippy.toml) error: use of a disallowed method `std::iter::Iterator::sum` - --> $DIR/conf_disallowed_methods.rs:14:5 + --> $DIR/conf_disallowed_methods.rs:37:5 | LL | a.iter().sum::(); | ^^^^^^^^^^^^^^^^^^^^^ error: use of a disallowed method `slice::sort_unstable` - --> $DIR/conf_disallowed_methods.rs:16:5 + --> $DIR/conf_disallowed_methods.rs:39:5 | LL | a.sort_unstable(); | ^^^^^^^^^^^^^^^^^ error: use of a disallowed method `f32::clamp` - --> $DIR/conf_disallowed_methods.rs:18:13 + --> $DIR/conf_disallowed_methods.rs:41:13 | LL | let _ = 2.0f32.clamp(3.0f32, 4.0f32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: use of a disallowed method `regex::Regex::new` - --> $DIR/conf_disallowed_methods.rs:21:61 + --> $DIR/conf_disallowed_methods.rs:44:61 | LL | let indirect: fn(&str) -> Result = Regex::new; | ^^^^^^^^^^ error: use of a disallowed method `f32::clamp` - --> $DIR/conf_disallowed_methods.rs:24:28 + --> $DIR/conf_disallowed_methods.rs:47:28 | LL | let in_call = Box::new(f32::clamp); | ^^^^^^^^^^ error: use of a disallowed method `regex::Regex::new` - --> $DIR/conf_disallowed_methods.rs:25:53 + --> $DIR/conf_disallowed_methods.rs:48:53 | LL | let in_method_call = ["^", "$"].into_iter().map(Regex::new); | ^^^^^^^^^^ error: use of a disallowed method `futures::stream::select_all` - --> $DIR/conf_disallowed_methods.rs:28:31 + --> $DIR/conf_disallowed_methods.rs:51:31 | LL | let same_name_as_module = select_all(vec![empty::<()>()]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 9 previous errors +error: use of a disallowed method `conf_disallowed_methods::local_fn` + --> $DIR/conf_disallowed_methods.rs:53:5 + | +LL | local_fn(); + | ^^^^^^^^^^ + +error: use of a disallowed method `conf_disallowed_methods::local_mod::f` + --> $DIR/conf_disallowed_methods.rs:54:5 + | +LL | local_mod::f(); + | ^^^^^^^^^^^^^^ + +error: use of a disallowed method `conf_disallowed_methods::Struct::method` + --> $DIR/conf_disallowed_methods.rs:56:5 + | +LL | s.method(); + | ^^^^^^^^^^ + +error: use of a disallowed method `conf_disallowed_methods::Trait::provided_method` + --> $DIR/conf_disallowed_methods.rs:57:5 + | +LL | s.provided_method(); + | ^^^^^^^^^^^^^^^^^^^ + +error: use of a disallowed method `conf_disallowed_methods::Trait::implemented_method` + --> $DIR/conf_disallowed_methods.rs:58:5 + | +LL | s.implemented_method(); + | ^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 14 previous errors diff --git a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr index 82ee805413217..f91d285c2e0ee 100644 --- a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr +++ b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr @@ -1,6 +1,7 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of allow-dbg-in-tests allow-expect-in-tests + allow-print-in-tests allow-unwrap-in-tests allowed-scripts arithmetic-side-effects-allowed @@ -20,8 +21,10 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown fie enforced-import-renames enum-variant-name-threshold enum-variant-size-threshold + ignore-interior-mutability large-error-threshold literal-representation-threshold + matches-for-let-else max-fn-params-bools max-include-file-size max-struct-bools diff --git a/tests/ui-toml/unwrap_used/unwrap_used.rs b/tests/ui-toml/unwrap_used/unwrap_used.rs index 0e82fb20e4558..bc8e8c1f0703a 100644 --- a/tests/ui-toml/unwrap_used/unwrap_used.rs +++ b/tests/ui-toml/unwrap_used/unwrap_used.rs @@ -66,8 +66,21 @@ fn main() { } } -#[test] -fn test() { - let boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]); - let _ = boxed_slice.get(1).unwrap(); +#[cfg(test)] +mod issue9612 { + // should not lint in `#[cfg(test)]` modules + #[test] + fn test_fn() { + let _a: u8 = 2.try_into().unwrap(); + let _a: u8 = 3.try_into().expect(""); + + util(); + } + + fn util() { + let _a: u8 = 4.try_into().unwrap(); + let _a: u8 = 5.try_into().expect(""); + // should still warn + let _ = Box::new([0]).get(1).unwrap(); + } } diff --git a/tests/ui-toml/unwrap_used/unwrap_used.stderr b/tests/ui-toml/unwrap_used/unwrap_used.stderr index 681b5eaf54dba..94b5ef663add9 100644 --- a/tests/ui-toml/unwrap_used/unwrap_used.stderr +++ b/tests/ui-toml/unwrap_used/unwrap_used.stderr @@ -10,7 +10,7 @@ note: the lint level is defined here LL | #![deny(clippy::get_unwrap)] | ^^^^^^^^^^^^^^^^^^ -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:35:17 | LL | let _ = boxed_slice.get(1).unwrap(); @@ -25,7 +25,7 @@ error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more co LL | let _ = some_slice.get(0).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_slice[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:36:17 | LL | let _ = some_slice.get(0).unwrap(); @@ -39,7 +39,7 @@ error: called `.get().unwrap()` on a Vec. Using `[]` is more clear and more conc LL | let _ = some_vec.get(0).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_vec[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:37:17 | LL | let _ = some_vec.get(0).unwrap(); @@ -53,7 +53,7 @@ error: called `.get().unwrap()` on a VecDeque. Using `[]` is more clear and more LL | let _ = some_vecdeque.get(0).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_vecdeque[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:38:17 | LL | let _ = some_vecdeque.get(0).unwrap(); @@ -67,7 +67,7 @@ error: called `.get().unwrap()` on a HashMap. Using `[]` is more clear and more LL | let _ = some_hashmap.get(&1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_hashmap[&1]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:39:17 | LL | let _ = some_hashmap.get(&1).unwrap(); @@ -81,7 +81,7 @@ error: called `.get().unwrap()` on a BTreeMap. Using `[]` is more clear and more LL | let _ = some_btreemap.get(&1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_btreemap[&1]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:40:17 | LL | let _ = some_btreemap.get(&1).unwrap(); @@ -95,7 +95,7 @@ error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more co LL | let _: u8 = *boxed_slice.get(1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `boxed_slice[1]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:44:22 | LL | let _: u8 = *boxed_slice.get(1).unwrap(); @@ -109,7 +109,7 @@ error: called `.get_mut().unwrap()` on a slice. Using `[]` is more clear and mor LL | *boxed_slice.get_mut(0).unwrap() = 1; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `boxed_slice[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:49:10 | LL | *boxed_slice.get_mut(0).unwrap() = 1; @@ -123,7 +123,7 @@ error: called `.get_mut().unwrap()` on a slice. Using `[]` is more clear and mor LL | *some_slice.get_mut(0).unwrap() = 1; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `some_slice[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:50:10 | LL | *some_slice.get_mut(0).unwrap() = 1; @@ -137,7 +137,7 @@ error: called `.get_mut().unwrap()` on a Vec. Using `[]` is more clear and more LL | *some_vec.get_mut(0).unwrap() = 1; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `some_vec[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:51:10 | LL | *some_vec.get_mut(0).unwrap() = 1; @@ -151,7 +151,7 @@ error: called `.get_mut().unwrap()` on a VecDeque. Using `[]` is more clear and LL | *some_vecdeque.get_mut(0).unwrap() = 1; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `some_vecdeque[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:52:10 | LL | *some_vecdeque.get_mut(0).unwrap() = 1; @@ -165,7 +165,7 @@ error: called `.get().unwrap()` on a Vec. Using `[]` is more clear and more conc LL | let _ = some_vec.get(0..1).unwrap().to_vec(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `some_vec[0..1]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:64:17 | LL | let _ = some_vec.get(0..1).unwrap().to_vec(); @@ -179,7 +179,7 @@ error: called `.get_mut().unwrap()` on a Vec. Using `[]` is more clear and more LL | let _ = some_vec.get_mut(0..1).unwrap().to_vec(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `some_vec[0..1]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:65:17 | LL | let _ = some_vec.get_mut(0..1).unwrap().to_vec(); @@ -188,10 +188,10 @@ LL | let _ = some_vec.get_mut(0..1).unwrap().to_vec(); = help: if you don't want to handle the `None` case gracefully, consider using `expect()` to provide a better panic message error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise - --> $DIR/unwrap_used.rs:72:13 + --> $DIR/unwrap_used.rs:84:17 | -LL | let _ = boxed_slice.get(1).unwrap(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&boxed_slice[1]` +LL | let _ = Box::new([0]).get(1).unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&Box::new([0])[1]` error: aborting due to 27 previous errors diff --git a/tests/ui-toml/vec_box_sized/test.rs b/tests/ui-toml/vec_box_sized/test.rs index bf04bee16373c..4c46deb585b6c 100644 --- a/tests/ui-toml/vec_box_sized/test.rs +++ b/tests/ui-toml/vec_box_sized/test.rs @@ -7,8 +7,9 @@ struct C { } struct Foo(Vec>); -struct Bar(Vec>); -struct Baz(Vec>); +struct Bar(Vec>); +struct Quux(Vec>); +struct Baz(Vec>); struct BarBaz(Vec>); struct FooBarBaz(Vec>); diff --git a/tests/ui-toml/vec_box_sized/test.stderr b/tests/ui-toml/vec_box_sized/test.stderr index cf194de3c5537..55de68f8ecf47 100644 --- a/tests/ui-toml/vec_box_sized/test.stderr +++ b/tests/ui-toml/vec_box_sized/test.stderr @@ -9,11 +9,11 @@ LL | struct Foo(Vec>); error: `Vec` is already on the heap, the boxing is unnecessary --> $DIR/test.rs:10:12 | -LL | struct Bar(Vec>); - | ^^^^^^^^^^^^^ help: try: `Vec` +LL | struct Bar(Vec>); + | ^^^^^^^^^^^^^ help: try: `Vec` error: `Vec` is already on the heap, the boxing is unnecessary - --> $DIR/test.rs:13:18 + --> $DIR/test.rs:14:18 | LL | struct FooBarBaz(Vec>); | ^^^^^^^^^^^ help: try: `Vec` diff --git a/tests/ui/arithmetic_side_effects.rs b/tests/ui/arithmetic_side_effects.rs index b25e68f13061b..b5ed8988a518f 100644 --- a/tests/ui/arithmetic_side_effects.rs +++ b/tests/ui/arithmetic_side_effects.rs @@ -150,8 +150,12 @@ pub fn non_overflowing_ops_or_ops_already_handled_by_the_compiler_should_not_tri _n = 23 + 85; // Unary - _n = -1; - _n = -(-1); + _n = -2147483647; + _n = -i32::MAX; + _n = -i32::MIN; + _n = -&2147483647; + _n = -&i32::MAX; + _n = -&i32::MIN; } pub fn runtime_ops() { diff --git a/tests/ui/arithmetic_side_effects.stderr b/tests/ui/arithmetic_side_effects.stderr index 0f06e22bae96b..0259a0824e794 100644 --- a/tests/ui/arithmetic_side_effects.stderr +++ b/tests/ui/arithmetic_side_effects.stderr @@ -19,331 +19,331 @@ LL | let _ = inferred_string + ""; | ^^^^^^^^^^^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:161:5 + --> $DIR/arithmetic_side_effects.rs:165:5 | LL | _n += 1; | ^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:162:5 + --> $DIR/arithmetic_side_effects.rs:166:5 | LL | _n += &1; | ^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:163:5 + --> $DIR/arithmetic_side_effects.rs:167:5 | LL | _n -= 1; | ^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:164:5 + --> $DIR/arithmetic_side_effects.rs:168:5 | LL | _n -= &1; | ^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:165:5 + --> $DIR/arithmetic_side_effects.rs:169:5 | LL | _n /= 0; | ^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:166:5 + --> $DIR/arithmetic_side_effects.rs:170:5 | LL | _n /= &0; | ^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:167:5 + --> $DIR/arithmetic_side_effects.rs:171:5 | LL | _n %= 0; | ^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:168:5 + --> $DIR/arithmetic_side_effects.rs:172:5 | LL | _n %= &0; | ^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:169:5 + --> $DIR/arithmetic_side_effects.rs:173:5 | LL | _n *= 2; | ^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:170:5 + --> $DIR/arithmetic_side_effects.rs:174:5 | LL | _n *= &2; | ^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:173:10 + --> $DIR/arithmetic_side_effects.rs:177:10 | LL | _n = _n + 1; | ^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:174:10 + --> $DIR/arithmetic_side_effects.rs:178:10 | LL | _n = _n + &1; | ^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:175:10 + --> $DIR/arithmetic_side_effects.rs:179:10 | LL | _n = 1 + _n; | ^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:176:10 + --> $DIR/arithmetic_side_effects.rs:180:10 | LL | _n = &1 + _n; | ^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:177:10 + --> $DIR/arithmetic_side_effects.rs:181:10 | LL | _n = _n - 1; | ^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:178:10 + --> $DIR/arithmetic_side_effects.rs:182:10 | LL | _n = _n - &1; | ^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:179:10 + --> $DIR/arithmetic_side_effects.rs:183:10 | LL | _n = 1 - _n; | ^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:180:10 + --> $DIR/arithmetic_side_effects.rs:184:10 | LL | _n = &1 - _n; | ^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:181:10 + --> $DIR/arithmetic_side_effects.rs:185:10 | LL | _n = _n / 0; | ^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:182:10 + --> $DIR/arithmetic_side_effects.rs:186:10 | LL | _n = _n / &0; | ^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:183:10 + --> $DIR/arithmetic_side_effects.rs:187:10 | LL | _n = _n % 0; | ^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:184:10 + --> $DIR/arithmetic_side_effects.rs:188:10 | LL | _n = _n % &0; | ^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:185:10 + --> $DIR/arithmetic_side_effects.rs:189:10 | LL | _n = _n * 2; | ^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:186:10 + --> $DIR/arithmetic_side_effects.rs:190:10 | LL | _n = _n * &2; | ^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:187:10 + --> $DIR/arithmetic_side_effects.rs:191:10 | LL | _n = 2 * _n; | ^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:188:10 + --> $DIR/arithmetic_side_effects.rs:192:10 | LL | _n = &2 * _n; | ^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:189:10 + --> $DIR/arithmetic_side_effects.rs:193:10 | LL | _n = 23 + &85; | ^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:190:10 + --> $DIR/arithmetic_side_effects.rs:194:10 | LL | _n = &23 + 85; | ^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:191:10 + --> $DIR/arithmetic_side_effects.rs:195:10 | LL | _n = &23 + &85; | ^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:194:13 + --> $DIR/arithmetic_side_effects.rs:198:13 | LL | let _ = Custom + 0; | ^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:195:13 + --> $DIR/arithmetic_side_effects.rs:199:13 | LL | let _ = Custom + 1; | ^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:196:13 + --> $DIR/arithmetic_side_effects.rs:200:13 | LL | let _ = Custom + 2; | ^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:197:13 + --> $DIR/arithmetic_side_effects.rs:201:13 | LL | let _ = Custom + 0.0; | ^^^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:198:13 + --> $DIR/arithmetic_side_effects.rs:202:13 | LL | let _ = Custom + 1.0; | ^^^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:199:13 + --> $DIR/arithmetic_side_effects.rs:203:13 | LL | let _ = Custom + 2.0; | ^^^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:200:13 + --> $DIR/arithmetic_side_effects.rs:204:13 | LL | let _ = Custom - 0; | ^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:201:13 + --> $DIR/arithmetic_side_effects.rs:205:13 | LL | let _ = Custom - 1; | ^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:202:13 + --> $DIR/arithmetic_side_effects.rs:206:13 | LL | let _ = Custom - 2; | ^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:203:13 + --> $DIR/arithmetic_side_effects.rs:207:13 | LL | let _ = Custom - 0.0; | ^^^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:204:13 + --> $DIR/arithmetic_side_effects.rs:208:13 | LL | let _ = Custom - 1.0; | ^^^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:205:13 + --> $DIR/arithmetic_side_effects.rs:209:13 | LL | let _ = Custom - 2.0; | ^^^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:206:13 + --> $DIR/arithmetic_side_effects.rs:210:13 | LL | let _ = Custom / 0; | ^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:207:13 + --> $DIR/arithmetic_side_effects.rs:211:13 | LL | let _ = Custom / 1; | ^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:208:13 + --> $DIR/arithmetic_side_effects.rs:212:13 | LL | let _ = Custom / 2; | ^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:209:13 + --> $DIR/arithmetic_side_effects.rs:213:13 | LL | let _ = Custom / 0.0; | ^^^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:210:13 + --> $DIR/arithmetic_side_effects.rs:214:13 | LL | let _ = Custom / 1.0; | ^^^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:211:13 + --> $DIR/arithmetic_side_effects.rs:215:13 | LL | let _ = Custom / 2.0; | ^^^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:212:13 + --> $DIR/arithmetic_side_effects.rs:216:13 | LL | let _ = Custom * 0; | ^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:213:13 + --> $DIR/arithmetic_side_effects.rs:217:13 | LL | let _ = Custom * 1; | ^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:214:13 + --> $DIR/arithmetic_side_effects.rs:218:13 | LL | let _ = Custom * 2; | ^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:215:13 + --> $DIR/arithmetic_side_effects.rs:219:13 | LL | let _ = Custom * 0.0; | ^^^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:216:13 + --> $DIR/arithmetic_side_effects.rs:220:13 | LL | let _ = Custom * 1.0; | ^^^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:217:13 + --> $DIR/arithmetic_side_effects.rs:221:13 | LL | let _ = Custom * 2.0; | ^^^^^^^^^^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:220:10 + --> $DIR/arithmetic_side_effects.rs:224:10 | LL | _n = -_n; | ^^^ error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:221:10 + --> $DIR/arithmetic_side_effects.rs:225:10 | LL | _n = -&_n; | ^^^^ diff --git a/tests/ui/auxiliary/doc_unsafe_macros.rs b/tests/ui/auxiliary/doc_unsafe_macros.rs index 869672d1eda5e..3d917e3dc75e9 100644 --- a/tests/ui/auxiliary/doc_unsafe_macros.rs +++ b/tests/ui/auxiliary/doc_unsafe_macros.rs @@ -6,3 +6,11 @@ macro_rules! undocd_unsafe { } }; } +#[macro_export] +macro_rules! undocd_safe { + () => { + pub fn vey_oy() { + unimplemented!(); + } + }; +} diff --git a/tests/ui/blanket_clippy_restriction_lints.rs b/tests/ui/blanket_clippy_restriction_lints.rs index d055f17526b9b..554745368bca4 100644 --- a/tests/ui/blanket_clippy_restriction_lints.rs +++ b/tests/ui/blanket_clippy_restriction_lints.rs @@ -1,3 +1,5 @@ +// compile-flags: -W clippy::restriction + #![warn(clippy::blanket_clippy_restriction_lints)] //! Test that the whole restriction group is not enabled diff --git a/tests/ui/blanket_clippy_restriction_lints.stderr b/tests/ui/blanket_clippy_restriction_lints.stderr index e83eb4d605aa7..2bf89ab69a40c 100644 --- a/tests/ui/blanket_clippy_restriction_lints.stderr +++ b/tests/ui/blanket_clippy_restriction_lints.stderr @@ -1,27 +1,32 @@ -error: restriction lints are not meant to be all enabled - --> $DIR/blanket_clippy_restriction_lints.rs:4:9 +error: `clippy::restriction` is not meant to be enabled as a group + | + = note: because of the command line `--warn clippy::restriction` + = help: enable the restriction lints you need individually + = note: `-D clippy::blanket-clippy-restriction-lints` implied by `-D warnings` + +error: `clippy::restriction` is not meant to be enabled as a group + --> $DIR/blanket_clippy_restriction_lints.rs:6:9 | LL | #![warn(clippy::restriction)] | ^^^^^^^^^^^^^^^^^^^ | - = help: try enabling only the lints you really need - = note: `-D clippy::blanket-clippy-restriction-lints` implied by `-D warnings` + = help: enable the restriction lints you need individually -error: restriction lints are not meant to be all enabled - --> $DIR/blanket_clippy_restriction_lints.rs:5:9 +error: `clippy::restriction` is not meant to be enabled as a group + --> $DIR/blanket_clippy_restriction_lints.rs:7:9 | LL | #![deny(clippy::restriction)] | ^^^^^^^^^^^^^^^^^^^ | - = help: try enabling only the lints you really need + = help: enable the restriction lints you need individually -error: restriction lints are not meant to be all enabled - --> $DIR/blanket_clippy_restriction_lints.rs:6:11 +error: `clippy::restriction` is not meant to be enabled as a group + --> $DIR/blanket_clippy_restriction_lints.rs:8:11 | LL | #![forbid(clippy::restriction)] | ^^^^^^^^^^^^^^^^^^^ | - = help: try enabling only the lints you really need + = help: enable the restriction lints you need individually -error: aborting due to 3 previous errors +error: aborting due to 4 previous errors diff --git a/tests/ui/bool_to_int_with_if.fixed b/tests/ui/bool_to_int_with_if.fixed index 2c8339cdd7f8a..37d3e3286a4b8 100644 --- a/tests/ui/bool_to_int_with_if.fixed +++ b/tests/ui/bool_to_int_with_if.fixed @@ -1,5 +1,6 @@ // run-rustfix +#![feature(let_chains)] #![warn(clippy::bool_to_int_with_if)] #![allow(unused, dead_code, clippy::unnecessary_operation, clippy::no_effect)] @@ -76,6 +77,8 @@ fn main() { 123 }; + pub const SHOULD_NOT_LINT: usize = if true { 1 } else { 0 }; + some_fn(a); } @@ -89,3 +92,22 @@ fn side_effect() {} fn cond(a: bool, b: bool) -> bool { a || b } + +enum Enum { + A, + B, +} + +fn if_let(a: Enum, b: Enum) { + if let Enum::A = a { + 1 + } else { + 0 + }; + + if let Enum::A = a && let Enum::B = b { + 1 + } else { + 0 + }; +} diff --git a/tests/ui/bool_to_int_with_if.rs b/tests/ui/bool_to_int_with_if.rs index 5d9496f01775f..ebdf86fd18560 100644 --- a/tests/ui/bool_to_int_with_if.rs +++ b/tests/ui/bool_to_int_with_if.rs @@ -1,5 +1,6 @@ // run-rustfix +#![feature(let_chains)] #![warn(clippy::bool_to_int_with_if)] #![allow(unused, dead_code, clippy::unnecessary_operation, clippy::no_effect)] @@ -108,6 +109,8 @@ fn main() { 123 }; + pub const SHOULD_NOT_LINT: usize = if true { 1 } else { 0 }; + some_fn(a); } @@ -121,3 +124,22 @@ fn side_effect() {} fn cond(a: bool, b: bool) -> bool { a || b } + +enum Enum { + A, + B, +} + +fn if_let(a: Enum, b: Enum) { + if let Enum::A = a { + 1 + } else { + 0 + }; + + if let Enum::A = a && let Enum::B = b { + 1 + } else { + 0 + }; +} diff --git a/tests/ui/bool_to_int_with_if.stderr b/tests/ui/bool_to_int_with_if.stderr index 4cb5531bef6a1..5cfb75cc0dfcb 100644 --- a/tests/ui/bool_to_int_with_if.stderr +++ b/tests/ui/bool_to_int_with_if.stderr @@ -1,5 +1,5 @@ error: boolean to int conversion using if - --> $DIR/bool_to_int_with_if.rs:15:5 + --> $DIR/bool_to_int_with_if.rs:16:5 | LL | / if a { LL | | 1 @@ -12,7 +12,7 @@ LL | | }; = note: `-D clippy::bool-to-int-with-if` implied by `-D warnings` error: boolean to int conversion using if - --> $DIR/bool_to_int_with_if.rs:20:5 + --> $DIR/bool_to_int_with_if.rs:21:5 | LL | / if a { LL | | 0 @@ -24,7 +24,7 @@ LL | | }; = note: `!a as i32` or `(!a).into()` can also be valid options error: boolean to int conversion using if - --> $DIR/bool_to_int_with_if.rs:25:5 + --> $DIR/bool_to_int_with_if.rs:26:5 | LL | / if !a { LL | | 1 @@ -36,7 +36,7 @@ LL | | }; = note: `!a as i32` or `(!a).into()` can also be valid options error: boolean to int conversion using if - --> $DIR/bool_to_int_with_if.rs:30:5 + --> $DIR/bool_to_int_with_if.rs:31:5 | LL | / if a || b { LL | | 1 @@ -48,7 +48,7 @@ LL | | }; = note: `(a || b) as i32` or `(a || b).into()` can also be valid options error: boolean to int conversion using if - --> $DIR/bool_to_int_with_if.rs:35:5 + --> $DIR/bool_to_int_with_if.rs:36:5 | LL | / if cond(a, b) { LL | | 1 @@ -60,7 +60,7 @@ LL | | }; = note: `cond(a, b) as i32` or `cond(a, b).into()` can also be valid options error: boolean to int conversion using if - --> $DIR/bool_to_int_with_if.rs:40:5 + --> $DIR/bool_to_int_with_if.rs:41:5 | LL | / if x + y < 4 { LL | | 1 @@ -72,7 +72,7 @@ LL | | }; = note: `(x + y < 4) as i32` or `(x + y < 4).into()` can also be valid options error: boolean to int conversion using if - --> $DIR/bool_to_int_with_if.rs:49:12 + --> $DIR/bool_to_int_with_if.rs:50:12 | LL | } else if b { | ____________^ @@ -85,7 +85,7 @@ LL | | }; = note: `b as i32` or `b.into()` can also be valid options error: boolean to int conversion using if - --> $DIR/bool_to_int_with_if.rs:58:12 + --> $DIR/bool_to_int_with_if.rs:59:12 | LL | } else if b { | ____________^ @@ -98,7 +98,7 @@ LL | | }; = note: `!b as i32` or `(!b).into()` can also be valid options error: boolean to int conversion using if - --> $DIR/bool_to_int_with_if.rs:116:5 + --> $DIR/bool_to_int_with_if.rs:119:5 | LL | if a { 1 } else { 0 } | ^^^^^^^^^^^^^^^^^^^^^ help: replace with from: `u8::from(a)` diff --git a/tests/ui/cognitive_complexity.rs b/tests/ui/cognitive_complexity.rs index 912e6788afddc..07bdaff00dc41 100644 --- a/tests/ui/cognitive_complexity.rs +++ b/tests/ui/cognitive_complexity.rs @@ -393,3 +393,19 @@ impl Moo { } } } + +#[clippy::cognitive_complexity = "1"] +mod issue9300 { + async fn a() { + let a = 0; + if a == 0 {} + } + + pub struct S; + impl S { + pub async fn async_method() { + let a = 0; + if a == 0 {} + } + } +} diff --git a/tests/ui/cognitive_complexity.stderr b/tests/ui/cognitive_complexity.stderr index d7f2f24e52f2b..5824631fa83ba 100644 --- a/tests/ui/cognitive_complexity.stderr +++ b/tests/ui/cognitive_complexity.stderr @@ -135,5 +135,21 @@ LL | fn moo(&self) { | = help: you could split it up into multiple smaller functions -error: aborting due to 17 previous errors +error: the function has a cognitive complexity of (2/1) + --> $DIR/cognitive_complexity.rs:399:14 + | +LL | async fn a() { + | ^ + | + = help: you could split it up into multiple smaller functions + +error: the function has a cognitive complexity of (2/1) + --> $DIR/cognitive_complexity.rs:406:22 + | +LL | pub async fn async_method() { + | ^^^^^^^^^^^^ + | + = help: you could split it up into multiple smaller functions + +error: aborting due to 19 previous errors diff --git a/tests/ui/crashes/ice-2774.stderr b/tests/ui/crashes/ice-2774.stderr index 0c2d48f938fcb..1f26c7f4db657 100644 --- a/tests/ui/crashes/ice-2774.stderr +++ b/tests/ui/crashes/ice-2774.stderr @@ -1,4 +1,4 @@ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) +error: the following explicit lifetimes could be elided: 'a --> $DIR/ice-2774.rs:15:1 | LL | pub fn add_barfoos_to_foos<'a>(bars: &HashSet<&'a Bar>) { diff --git a/tests/ui/crashes/ice-9746.rs b/tests/ui/crashes/ice-9746.rs new file mode 100644 index 0000000000000..fbd373c70bf28 --- /dev/null +++ b/tests/ui/crashes/ice-9746.rs @@ -0,0 +1,15 @@ +//! + +trait Trait {} + +struct Struct<'a> { + _inner: &'a Struct<'a>, +} + +impl Trait for Struct<'_> {} + +fn example<'a>(s: &'a Struct) -> Box> { + Box::new(Box::new(Struct { _inner: s })) +} + +fn main() {} diff --git a/tests/ui/crashes/needless_lifetimes_impl_trait.stderr b/tests/ui/crashes/needless_lifetimes_impl_trait.stderr index d68bbe7880213..875d5ab4f21ca 100644 --- a/tests/ui/crashes/needless_lifetimes_impl_trait.stderr +++ b/tests/ui/crashes/needless_lifetimes_impl_trait.stderr @@ -1,4 +1,4 @@ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) +error: the following explicit lifetimes could be elided: 'a --> $DIR/needless_lifetimes_impl_trait.rs:15:5 | LL | fn baz<'a>(&'a self) -> impl Foo + 'a { diff --git a/tests/ui/crate_level_checks/no_std_main_recursion.rs b/tests/ui/crate_level_checks/no_std_main_recursion.rs index 4a5c597dda51f..e1c9fe30a9dfb 100644 --- a/tests/ui/crate_level_checks/no_std_main_recursion.rs +++ b/tests/ui/crate_level_checks/no_std_main_recursion.rs @@ -1,6 +1,5 @@ // compile-flags: -Clink-arg=-nostartfiles // ignore-macos -// ignore-windows #![feature(lang_items, start, libc)] #![no_std] diff --git a/tests/ui/doc_errors.stderr b/tests/ui/doc_errors.stderr index c7b616e289708..d74f2dbfe1baa 100644 --- a/tests/ui/doc_errors.stderr +++ b/tests/ui/doc_errors.stderr @@ -1,52 +1,40 @@ error: docs for function returning `Result` missing `# Errors` section --> $DIR/doc_errors.rs:7:1 | -LL | / pub fn pub_fn_missing_errors_header() -> Result<(), ()> { -LL | | unimplemented!(); -LL | | } - | |_^ +LL | pub fn pub_fn_missing_errors_header() -> Result<(), ()> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `-D clippy::missing-errors-doc` implied by `-D warnings` error: docs for function returning `Result` missing `# Errors` section --> $DIR/doc_errors.rs:11:1 | -LL | / pub async fn async_pub_fn_missing_errors_header() -> Result<(), ()> { -LL | | unimplemented!(); -LL | | } - | |_^ +LL | pub async fn async_pub_fn_missing_errors_header() -> Result<(), ()> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: docs for function returning `Result` missing `# Errors` section --> $DIR/doc_errors.rs:16:1 | -LL | / pub fn pub_fn_returning_io_result() -> io::Result<()> { -LL | | unimplemented!(); -LL | | } - | |_^ +LL | pub fn pub_fn_returning_io_result() -> io::Result<()> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: docs for function returning `Result` missing `# Errors` section --> $DIR/doc_errors.rs:21:1 | -LL | / pub async fn async_pub_fn_returning_io_result() -> io::Result<()> { -LL | | unimplemented!(); -LL | | } - | |_^ +LL | pub async fn async_pub_fn_returning_io_result() -> io::Result<()> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: docs for function returning `Result` missing `# Errors` section --> $DIR/doc_errors.rs:51:5 | -LL | / pub fn pub_method_missing_errors_header() -> Result<(), ()> { -LL | | unimplemented!(); -LL | | } - | |_____^ +LL | pub fn pub_method_missing_errors_header() -> Result<(), ()> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: docs for function returning `Result` missing `# Errors` section --> $DIR/doc_errors.rs:56:5 | -LL | / pub async fn async_pub_method_missing_errors_header() -> Result<(), ()> { -LL | | unimplemented!(); -LL | | } - | |_____^ +LL | pub async fn async_pub_method_missing_errors_header() -> Result<(), ()> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: docs for function returning `Result` missing `# Errors` section --> $DIR/doc_errors.rs:85:5 diff --git a/tests/ui/doc_unnecessary_unsafe.rs b/tests/ui/doc_unnecessary_unsafe.rs new file mode 100644 index 0000000000000..d9e9363b0f4bd --- /dev/null +++ b/tests/ui/doc_unnecessary_unsafe.rs @@ -0,0 +1,148 @@ +// aux-build:doc_unsafe_macros.rs + +#![allow(clippy::let_unit_value)] + +#[macro_use] +extern crate doc_unsafe_macros; + +/// This is has no safety section, and does not need one either +pub fn destroy_the_planet() { + unimplemented!(); +} + +/// This one does not need a `Safety` section +/// +/// # Safety +/// +/// This function shouldn't be called unless the horsemen are ready +pub fn apocalypse(universe: &mut ()) { + unimplemented!(); +} + +/// This is a private function, skip to match behavior with `missing_safety_doc`. +/// +/// # Safety +/// +/// Boo! +fn you_dont_see_me() { + unimplemented!(); +} + +mod private_mod { + /// This is public but unexported function, skip to match behavior with `missing_safety_doc`. + /// + /// # Safety + /// + /// Very safe! + pub fn only_crate_wide_accessible() { + unimplemented!(); + } + + /// # Safety + /// + /// Unnecessary safety! + pub fn republished() { + unimplemented!(); + } +} + +pub use private_mod::republished; + +pub trait SafeTraitSafeMethods { + fn woefully_underdocumented(self); + + /// # Safety + /// + /// Unnecessary! + fn documented(self); +} + +pub trait SafeTrait { + fn method(); +} + +/// # Safety +/// +/// Unnecessary! +pub trait DocumentedSafeTrait { + fn method2(); +} + +pub struct Struct; + +impl SafeTraitSafeMethods for Struct { + fn woefully_underdocumented(self) { + // all is well + } + + fn documented(self) { + // all is still well + } +} + +impl SafeTrait for Struct { + fn method() {} +} + +impl DocumentedSafeTrait for Struct { + fn method2() {} +} + +impl Struct { + /// # Safety + /// + /// Unnecessary! + pub fn documented() -> Self { + unimplemented!(); + } + + pub fn undocumented(&self) { + unimplemented!(); + } + + /// Private, fine again to stay consistent with `missing_safety_doc`. + /// + /// # Safety + /// + /// Unnecessary! + fn private(&self) { + unimplemented!(); + } +} + +macro_rules! very_safe { + () => { + pub fn whee() { + unimplemented!() + } + + /// # Safety + /// + /// Driving is very safe already! + pub fn drive() { + whee() + } + }; +} + +very_safe!(); + +// we don't lint code from external macros +undocd_safe!(); + +fn main() {} + +// do not lint if any parent has `#[doc(hidden)]` attribute +// see #7347 +#[doc(hidden)] +pub mod __macro { + pub struct T; + impl T { + pub unsafe fn f() {} + } +} + +/// # Implementation safety +pub trait DocumentedSafeTraitWithImplementationHeader { + fn method(); +} diff --git a/tests/ui/doc_unnecessary_unsafe.stderr b/tests/ui/doc_unnecessary_unsafe.stderr new file mode 100644 index 0000000000000..83b2efbb346be --- /dev/null +++ b/tests/ui/doc_unnecessary_unsafe.stderr @@ -0,0 +1,51 @@ +error: safe function's docs have unnecessary `# Safety` section + --> $DIR/doc_unnecessary_unsafe.rs:18:1 + | +LL | pub fn apocalypse(universe: &mut ()) { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `-D clippy::unnecessary-safety-doc` implied by `-D warnings` + +error: safe function's docs have unnecessary `# Safety` section + --> $DIR/doc_unnecessary_unsafe.rs:44:5 + | +LL | pub fn republished() { + | ^^^^^^^^^^^^^^^^^^^^ + +error: safe function's docs have unnecessary `# Safety` section + --> $DIR/doc_unnecessary_unsafe.rs:57:5 + | +LL | fn documented(self); + | ^^^^^^^^^^^^^^^^^^^^ + +error: docs for safe trait have unnecessary `# Safety` section + --> $DIR/doc_unnecessary_unsafe.rs:67:1 + | +LL | pub trait DocumentedSafeTrait { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: safe function's docs have unnecessary `# Safety` section + --> $DIR/doc_unnecessary_unsafe.rs:95:5 + | +LL | pub fn documented() -> Self { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: safe function's docs have unnecessary `# Safety` section + --> $DIR/doc_unnecessary_unsafe.rs:122:9 + | +LL | pub fn drive() { + | ^^^^^^^^^^^^^^ +... +LL | very_safe!(); + | ------------ in this macro invocation + | + = note: this error originates in the macro `very_safe` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: docs for safe trait have unnecessary `# Safety` section + --> $DIR/doc_unnecessary_unsafe.rs:146:1 + | +LL | pub trait DocumentedSafeTraitWithImplementationHeader { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 7 previous errors + diff --git a/tests/ui/doc_unsafe.stderr b/tests/ui/doc_unsafe.stderr index 904b88eaef622..a86e191370e3e 100644 --- a/tests/ui/doc_unsafe.stderr +++ b/tests/ui/doc_unsafe.stderr @@ -1,20 +1,16 @@ error: unsafe function's docs miss `# Safety` section --> $DIR/doc_unsafe.rs:9:1 | -LL | / pub unsafe fn destroy_the_planet() { -LL | | unimplemented!(); -LL | | } - | |_^ +LL | pub unsafe fn destroy_the_planet() { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `-D clippy::missing-safety-doc` implied by `-D warnings` error: unsafe function's docs miss `# Safety` section --> $DIR/doc_unsafe.rs:32:5 | -LL | / pub unsafe fn republished() { -LL | | unimplemented!(); -LL | | } - | |_____^ +LL | pub unsafe fn republished() { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: unsafe function's docs miss `# Safety` section --> $DIR/doc_unsafe.rs:40:5 @@ -25,29 +21,23 @@ LL | unsafe fn woefully_underdocumented(self); error: docs for unsafe trait missing `# Safety` section --> $DIR/doc_unsafe.rs:46:1 | -LL | / pub unsafe trait UnsafeTrait { -LL | | fn method(); -LL | | } - | |_^ +LL | pub unsafe trait UnsafeTrait { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: unsafe function's docs miss `# Safety` section --> $DIR/doc_unsafe.rs:76:5 | -LL | / pub unsafe fn more_undocumented_unsafe() -> Self { -LL | | unimplemented!(); -LL | | } - | |_____^ +LL | pub unsafe fn more_undocumented_unsafe() -> Self { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: unsafe function's docs miss `# Safety` section --> $DIR/doc_unsafe.rs:92:9 | -LL | / pub unsafe fn whee() { -LL | | unimplemented!() -LL | | } - | |_________^ +LL | pub unsafe fn whee() { + | ^^^^^^^^^^^^^^^^^^^^ ... -LL | very_unsafe!(); - | -------------- in this macro invocation +LL | very_unsafe!(); + | -------------- in this macro invocation | = note: this error originates in the macro `very_unsafe` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/tests/ui/eq_op.rs b/tests/ui/eq_op.rs index 422f9486503d2..e737955026523 100644 --- a/tests/ui/eq_op.rs +++ b/tests/ui/eq_op.rs @@ -2,6 +2,7 @@ #![warn(clippy::eq_op)] #![allow(clippy::double_parens, clippy::identity_op, clippy::nonminimal_bool)] +#![allow(clippy::suspicious_xor_used_as_pow)] fn main() { // simple values and comparisons diff --git a/tests/ui/eq_op.stderr b/tests/ui/eq_op.stderr index 313ceed2b41fa..d365ab27edc28 100644 --- a/tests/ui/eq_op.stderr +++ b/tests/ui/eq_op.stderr @@ -1,5 +1,5 @@ error: equal expressions as operands to `==` - --> $DIR/eq_op.rs:8:13 + --> $DIR/eq_op.rs:9:13 | LL | let _ = 1 == 1; | ^^^^^^ @@ -7,163 +7,163 @@ LL | let _ = 1 == 1; = note: `-D clippy::eq-op` implied by `-D warnings` error: equal expressions as operands to `==` - --> $DIR/eq_op.rs:9:13 + --> $DIR/eq_op.rs:10:13 | LL | let _ = "no" == "no"; | ^^^^^^^^^^^^ error: equal expressions as operands to `!=` - --> $DIR/eq_op.rs:11:13 + --> $DIR/eq_op.rs:12:13 | LL | let _ = false != false; | ^^^^^^^^^^^^^^ error: equal expressions as operands to `<` - --> $DIR/eq_op.rs:12:13 + --> $DIR/eq_op.rs:13:13 | LL | let _ = 1.5 < 1.5; | ^^^^^^^^^ error: equal expressions as operands to `>=` - --> $DIR/eq_op.rs:13:13 + --> $DIR/eq_op.rs:14:13 | LL | let _ = 1u64 >= 1u64; | ^^^^^^^^^^^^ error: equal expressions as operands to `&` - --> $DIR/eq_op.rs:16:13 + --> $DIR/eq_op.rs:17:13 | LL | let _ = (1u32 as u64) & (1u32 as u64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: equal expressions as operands to `^` - --> $DIR/eq_op.rs:19:17 + --> $DIR/eq_op.rs:20:17 | LL | let _ = 1 ^ ((((((1)))))); | ^^^^^^^^^^^^^^^^^ error: equal expressions as operands to `<` - --> $DIR/eq_op.rs:23:13 + --> $DIR/eq_op.rs:24:13 | LL | let _ = (-(2) < -(2)); | ^^^^^^^^^^^^^ error: equal expressions as operands to `==` - --> $DIR/eq_op.rs:24:13 + --> $DIR/eq_op.rs:25:13 | LL | let _ = ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: equal expressions as operands to `&` - --> $DIR/eq_op.rs:24:14 + --> $DIR/eq_op.rs:25:14 | LL | let _ = ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1)); | ^^^^^^^^^^^^^^^^^ error: equal expressions as operands to `&` - --> $DIR/eq_op.rs:24:35 + --> $DIR/eq_op.rs:25:35 | LL | let _ = ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1)); | ^^^^^^^^^^^^^^^^^ error: equal expressions as operands to `==` - --> $DIR/eq_op.rs:25:13 + --> $DIR/eq_op.rs:26:13 | LL | let _ = (1 * 2) + (3 * 4) == 1 * 2 + 3 * 4; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: equal expressions as operands to `!=` - --> $DIR/eq_op.rs:28:13 + --> $DIR/eq_op.rs:29:13 | LL | let _ = ([1] != [1]); | ^^^^^^^^^^^^ error: equal expressions as operands to `!=` - --> $DIR/eq_op.rs:29:13 + --> $DIR/eq_op.rs:30:13 | LL | let _ = ((1, 2) != (1, 2)); | ^^^^^^^^^^^^^^^^^^ error: equal expressions as operands to `==` - --> $DIR/eq_op.rs:33:13 + --> $DIR/eq_op.rs:34:13 | LL | let _ = 1 + 1 == 2; | ^^^^^^^^^^ error: equal expressions as operands to `==` - --> $DIR/eq_op.rs:34:13 + --> $DIR/eq_op.rs:35:13 | LL | let _ = 1 - 1 == 0; | ^^^^^^^^^^ error: equal expressions as operands to `-` - --> $DIR/eq_op.rs:34:13 + --> $DIR/eq_op.rs:35:13 | LL | let _ = 1 - 1 == 0; | ^^^^^ error: equal expressions as operands to `-` - --> $DIR/eq_op.rs:36:13 + --> $DIR/eq_op.rs:37:13 | LL | let _ = 1 - 1; | ^^^^^ error: equal expressions as operands to `/` - --> $DIR/eq_op.rs:37:13 + --> $DIR/eq_op.rs:38:13 | LL | let _ = 1 / 1; | ^^^^^ error: equal expressions as operands to `&&` - --> $DIR/eq_op.rs:38:13 + --> $DIR/eq_op.rs:39:13 | LL | let _ = true && true; | ^^^^^^^^^^^^ error: equal expressions as operands to `||` - --> $DIR/eq_op.rs:40:13 + --> $DIR/eq_op.rs:41:13 | LL | let _ = true || true; | ^^^^^^^^^^^^ error: equal expressions as operands to `&&` - --> $DIR/eq_op.rs:45:13 + --> $DIR/eq_op.rs:46:13 | LL | let _ = a == b && b == a; | ^^^^^^^^^^^^^^^^ error: equal expressions as operands to `&&` - --> $DIR/eq_op.rs:46:13 + --> $DIR/eq_op.rs:47:13 | LL | let _ = a != b && b != a; | ^^^^^^^^^^^^^^^^ error: equal expressions as operands to `&&` - --> $DIR/eq_op.rs:47:13 + --> $DIR/eq_op.rs:48:13 | LL | let _ = a < b && b > a; | ^^^^^^^^^^^^^^ error: equal expressions as operands to `&&` - --> $DIR/eq_op.rs:48:13 + --> $DIR/eq_op.rs:49:13 | LL | let _ = a <= b && b >= a; | ^^^^^^^^^^^^^^^^ error: equal expressions as operands to `==` - --> $DIR/eq_op.rs:51:13 + --> $DIR/eq_op.rs:52:13 | LL | let _ = a == a; | ^^^^^^ error: equal expressions as operands to `/` - --> $DIR/eq_op.rs:61:20 + --> $DIR/eq_op.rs:62:20 | LL | const D: u32 = A / A; | ^^^^^ error: equal expressions as operands to `==` - --> $DIR/eq_op.rs:92:5 + --> $DIR/eq_op.rs:93:5 | LL | (n1.inner.0).0 == (n1.inner.0).0 && (n1.inner.1).0 == (n2.inner.1).0 && (n1.inner.2).0 == (n2.inner.2).0 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/equatable_if_let.fixed b/tests/ui/equatable_if_let.fixed index 687efdada6e31..9af2ba9627200 100644 --- a/tests/ui/equatable_if_let.fixed +++ b/tests/ui/equatable_if_let.fixed @@ -23,6 +23,11 @@ struct Struct { b: bool, } +struct NoPartialEqStruct { + a: i32, + b: bool, +} + enum NotPartialEq { A, B, @@ -47,6 +52,7 @@ fn main() { let e = Enum::UnitVariant; let f = NotPartialEq::A; let g = NotStructuralEq::A; + let h = NoPartialEqStruct { a: 2, b: false }; // true @@ -66,10 +72,11 @@ fn main() { if let Some(3 | 4) = c {} if let Struct { a, b: false } = d {} if let Struct { a: 2, b: x } = d {} - if let NotPartialEq::A = f {} + if matches!(f, NotPartialEq::A) {} if g == NotStructuralEq::A {} - if let Some(NotPartialEq::A) = Some(f) {} + if matches!(Some(f), Some(NotPartialEq::A)) {} if Some(g) == Some(NotStructuralEq::A) {} + if matches!(h, NoPartialEqStruct { a: 2, b: false }) {} macro_rules! m1 { (x) => { diff --git a/tests/ui/equatable_if_let.rs b/tests/ui/equatable_if_let.rs index 8c467d14d2a9f..c3626c081dd5e 100644 --- a/tests/ui/equatable_if_let.rs +++ b/tests/ui/equatable_if_let.rs @@ -23,6 +23,11 @@ struct Struct { b: bool, } +struct NoPartialEqStruct { + a: i32, + b: bool, +} + enum NotPartialEq { A, B, @@ -47,6 +52,7 @@ fn main() { let e = Enum::UnitVariant; let f = NotPartialEq::A; let g = NotStructuralEq::A; + let h = NoPartialEqStruct { a: 2, b: false }; // true @@ -70,6 +76,7 @@ fn main() { if let NotStructuralEq::A = g {} if let Some(NotPartialEq::A) = Some(f) {} if let Some(NotStructuralEq::A) = Some(g) {} + if let NoPartialEqStruct { a: 2, b: false } = h {} macro_rules! m1 { (x) => { diff --git a/tests/ui/equatable_if_let.stderr b/tests/ui/equatable_if_let.stderr index 9c4c3cc3682e6..40ca75b8da22c 100644 --- a/tests/ui/equatable_if_let.stderr +++ b/tests/ui/equatable_if_let.stderr @@ -1,5 +1,5 @@ error: this pattern matching can be expressed using equality - --> $DIR/equatable_if_let.rs:53:8 + --> $DIR/equatable_if_let.rs:59:8 | LL | if let 2 = a {} | ^^^^^^^^^ help: try: `a == 2` @@ -7,64 +7,82 @@ LL | if let 2 = a {} = note: `-D clippy::equatable-if-let` implied by `-D warnings` error: this pattern matching can be expressed using equality - --> $DIR/equatable_if_let.rs:54:8 + --> $DIR/equatable_if_let.rs:60:8 | LL | if let Ordering::Greater = a.cmp(&b) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `a.cmp(&b) == Ordering::Greater` error: this pattern matching can be expressed using equality - --> $DIR/equatable_if_let.rs:55:8 + --> $DIR/equatable_if_let.rs:61:8 | LL | if let Some(2) = c {} | ^^^^^^^^^^^^^^^ help: try: `c == Some(2)` error: this pattern matching can be expressed using equality - --> $DIR/equatable_if_let.rs:56:8 + --> $DIR/equatable_if_let.rs:62:8 | LL | if let Struct { a: 2, b: false } = d {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `d == (Struct { a: 2, b: false })` error: this pattern matching can be expressed using equality - --> $DIR/equatable_if_let.rs:57:8 + --> $DIR/equatable_if_let.rs:63:8 | LL | if let Enum::TupleVariant(32, 64) = e {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `e == Enum::TupleVariant(32, 64)` error: this pattern matching can be expressed using equality - --> $DIR/equatable_if_let.rs:58:8 + --> $DIR/equatable_if_let.rs:64:8 | LL | if let Enum::RecordVariant { a: 64, b: 32 } = e {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `e == (Enum::RecordVariant { a: 64, b: 32 })` error: this pattern matching can be expressed using equality - --> $DIR/equatable_if_let.rs:59:8 + --> $DIR/equatable_if_let.rs:65:8 | LL | if let Enum::UnitVariant = e {} | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `e == Enum::UnitVariant` error: this pattern matching can be expressed using equality - --> $DIR/equatable_if_let.rs:60:8 + --> $DIR/equatable_if_let.rs:66:8 | LL | if let (Enum::UnitVariant, &Struct { a: 2, b: false }) = (e, &d) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(e, &d) == (Enum::UnitVariant, &Struct { a: 2, b: false })` +error: this pattern matching can be expressed using `matches!` + --> $DIR/equatable_if_let.rs:75:8 + | +LL | if let NotPartialEq::A = f {} + | ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(f, NotPartialEq::A)` + error: this pattern matching can be expressed using equality - --> $DIR/equatable_if_let.rs:70:8 + --> $DIR/equatable_if_let.rs:76:8 | LL | if let NotStructuralEq::A = g {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `g == NotStructuralEq::A` +error: this pattern matching can be expressed using `matches!` + --> $DIR/equatable_if_let.rs:77:8 + | +LL | if let Some(NotPartialEq::A) = Some(f) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(Some(f), Some(NotPartialEq::A))` + error: this pattern matching can be expressed using equality - --> $DIR/equatable_if_let.rs:72:8 + --> $DIR/equatable_if_let.rs:78:8 | LL | if let Some(NotStructuralEq::A) = Some(g) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(g) == Some(NotStructuralEq::A)` -error: this pattern matching can be expressed using equality +error: this pattern matching can be expressed using `matches!` --> $DIR/equatable_if_let.rs:79:8 | +LL | if let NoPartialEqStruct { a: 2, b: false } = h {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(h, NoPartialEqStruct { a: 2, b: false })` + +error: this pattern matching can be expressed using equality + --> $DIR/equatable_if_let.rs:86:8 + | LL | if let m1!(x) = "abc" { | ^^^^^^^^^^^^^^^^^^ help: try: `"abc" == m1!(x)` -error: aborting due to 11 previous errors +error: aborting due to 14 previous errors diff --git a/tests/ui/expect.stderr b/tests/ui/expect.stderr index f6738865cac12..c08e0dbbf7449 100644 --- a/tests/ui/expect.stderr +++ b/tests/ui/expect.stderr @@ -1,4 +1,4 @@ -error: used `expect()` on `an Option` value +error: used `expect()` on an `Option` value --> $DIR/expect.rs:5:13 | LL | let _ = opt.expect(""); @@ -7,7 +7,7 @@ LL | let _ = opt.expect(""); = help: if this value is `None`, it will panic = note: `-D clippy::expect-used` implied by `-D warnings` -error: used `expect()` on `a Result` value +error: used `expect()` on a `Result` value --> $DIR/expect.rs:10:13 | LL | let _ = res.expect(""); @@ -15,7 +15,7 @@ LL | let _ = res.expect(""); | = help: if this value is an `Err`, it will panic -error: used `expect_err()` on `a Result` value +error: used `expect_err()` on a `Result` value --> $DIR/expect.rs:11:13 | LL | let _ = res.expect_err(""); diff --git a/tests/ui/explicit_auto_deref.fixed b/tests/ui/explicit_auto_deref.fixed index d1d35e5c0eb46..59ff5e4040a3d 100644 --- a/tests/ui/explicit_auto_deref.fixed +++ b/tests/ui/explicit_auto_deref.fixed @@ -266,4 +266,15 @@ fn main() { } x }; + + trait WithAssoc { + type Assoc: ?Sized; + } + impl WithAssoc for String { + type Assoc = str; + } + fn takes_assoc(_: &T::Assoc) -> T { + unimplemented!() + } + let _: String = takes_assoc(&*String::new()); } diff --git a/tests/ui/explicit_auto_deref.rs b/tests/ui/explicit_auto_deref.rs index deedafad153b9..bcfb60c327886 100644 --- a/tests/ui/explicit_auto_deref.rs +++ b/tests/ui/explicit_auto_deref.rs @@ -266,4 +266,15 @@ fn main() { } *x }; + + trait WithAssoc { + type Assoc: ?Sized; + } + impl WithAssoc for String { + type Assoc = str; + } + fn takes_assoc(_: &T::Assoc) -> T { + unimplemented!() + } + let _: String = takes_assoc(&*String::new()); } diff --git a/tests/ui/fn_params_excessive_bools.rs b/tests/ui/fn_params_excessive_bools.rs index f805bcc9ba8af..f53e531629aae 100644 --- a/tests/ui/fn_params_excessive_bools.rs +++ b/tests/ui/fn_params_excessive_bools.rs @@ -2,6 +2,7 @@ #![allow(clippy::too_many_arguments)] extern "C" { + // Should not lint, most of the time users have no control over extern function signatures fn f(_: bool, _: bool, _: bool, _: bool); } @@ -22,8 +23,12 @@ fn t(_: S, _: S, _: Box, _: Vec, _: bool, _: bool, _: bool, _: bool) {} struct S; trait Trait { + // should warn for trait functions with and without body fn f(_: bool, _: bool, _: bool, _: bool); fn g(_: bool, _: bool, _: bool, _: Vec); + #[allow(clippy::fn_params_excessive_bools)] + fn h(_: bool, _: bool, _: bool, _: bool, _: bool, _: bool); + fn i(_: bool, _: bool, _: bool, _: bool) {} } impl S { @@ -34,8 +39,11 @@ impl S { } impl Trait for S { + // Should not lint because the trait might not be changeable by the user + // We only lint in the trait definition fn f(_: bool, _: bool, _: bool, _: bool) {} fn g(_: bool, _: bool, _: bool, _: Vec) {} + fn h(_: bool, _: bool, _: bool, _: bool, _: bool, _: bool) {} } fn main() { diff --git a/tests/ui/fn_params_excessive_bools.stderr b/tests/ui/fn_params_excessive_bools.stderr index 11627105691b7..43363b46972c3 100644 --- a/tests/ui/fn_params_excessive_bools.stderr +++ b/tests/ui/fn_params_excessive_bools.stderr @@ -1,5 +1,5 @@ error: more than 3 bools in function parameters - --> $DIR/fn_params_excessive_bools.rs:18:1 + --> $DIR/fn_params_excessive_bools.rs:19:1 | LL | fn g(_: bool, _: bool, _: bool, _: bool) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -8,7 +8,7 @@ LL | fn g(_: bool, _: bool, _: bool, _: bool) {} = note: `-D clippy::fn-params-excessive-bools` implied by `-D warnings` error: more than 3 bools in function parameters - --> $DIR/fn_params_excessive_bools.rs:21:1 + --> $DIR/fn_params_excessive_bools.rs:22:1 | LL | fn t(_: S, _: S, _: Box, _: Vec, _: bool, _: bool, _: bool, _: bool) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -16,7 +16,7 @@ LL | fn t(_: S, _: S, _: Box, _: Vec, _: bool, _: bool, _: bool, _: bool = help: consider refactoring bools into two-variant enums error: more than 3 bools in function parameters - --> $DIR/fn_params_excessive_bools.rs:25:5 + --> $DIR/fn_params_excessive_bools.rs:27:5 | LL | fn f(_: bool, _: bool, _: bool, _: bool); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -24,7 +24,15 @@ LL | fn f(_: bool, _: bool, _: bool, _: bool); = help: consider refactoring bools into two-variant enums error: more than 3 bools in function parameters - --> $DIR/fn_params_excessive_bools.rs:30:5 + --> $DIR/fn_params_excessive_bools.rs:31:5 + | +LL | fn i(_: bool, _: bool, _: bool, _: bool) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: consider refactoring bools into two-variant enums + +error: more than 3 bools in function parameters + --> $DIR/fn_params_excessive_bools.rs:35:5 | LL | fn f(&self, _: bool, _: bool, _: bool, _: bool) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -32,7 +40,7 @@ LL | fn f(&self, _: bool, _: bool, _: bool, _: bool) {} = help: consider refactoring bools into two-variant enums error: more than 3 bools in function parameters - --> $DIR/fn_params_excessive_bools.rs:42:5 + --> $DIR/fn_params_excessive_bools.rs:50:5 | LL | / fn n(_: bool, _: u32, _: bool, _: Box, _: bool, _: bool) { LL | | fn nn(_: bool, _: bool, _: bool, _: bool) {} @@ -42,12 +50,12 @@ LL | | } = help: consider refactoring bools into two-variant enums error: more than 3 bools in function parameters - --> $DIR/fn_params_excessive_bools.rs:43:9 + --> $DIR/fn_params_excessive_bools.rs:51:9 | LL | fn nn(_: bool, _: bool, _: bool, _: bool) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: consider refactoring bools into two-variant enums -error: aborting due to 6 previous errors +error: aborting due to 7 previous errors diff --git a/tests/ui/from_raw_with_void_ptr.rs b/tests/ui/from_raw_with_void_ptr.rs new file mode 100644 index 0000000000000..8484da2415ab3 --- /dev/null +++ b/tests/ui/from_raw_with_void_ptr.rs @@ -0,0 +1,34 @@ +#![warn(clippy::from_raw_with_void_ptr)] + +use std::ffi::c_void; +use std::rc::Rc; +use std::sync::Arc; + +fn main() { + // must lint + let ptr = Box::into_raw(Box::new(42usize)) as *mut c_void; + let _ = unsafe { Box::from_raw(ptr) }; + + // shouldn't be linted + let _ = unsafe { Box::from_raw(ptr as *mut usize) }; + + // shouldn't be linted + let should_not_lint_ptr = Box::into_raw(Box::new(12u8)) as *mut u8; + let _ = unsafe { Box::from_raw(should_not_lint_ptr as *mut u8) }; + + // must lint + let ptr = Rc::into_raw(Rc::new(42usize)) as *mut c_void; + let _ = unsafe { Rc::from_raw(ptr) }; + + // must lint + let ptr = Arc::into_raw(Arc::new(42usize)) as *mut c_void; + let _ = unsafe { Arc::from_raw(ptr) }; + + // must lint + let ptr = std::rc::Weak::into_raw(Rc::downgrade(&Rc::new(42usize))) as *mut c_void; + let _ = unsafe { std::rc::Weak::from_raw(ptr) }; + + // must lint + let ptr = std::sync::Weak::into_raw(Arc::downgrade(&Arc::new(42usize))) as *mut c_void; + let _ = unsafe { std::sync::Weak::from_raw(ptr) }; +} diff --git a/tests/ui/from_raw_with_void_ptr.stderr b/tests/ui/from_raw_with_void_ptr.stderr new file mode 100644 index 0000000000000..96e4af12ba386 --- /dev/null +++ b/tests/ui/from_raw_with_void_ptr.stderr @@ -0,0 +1,63 @@ +error: creating a `Box` from a void raw pointer + --> $DIR/from_raw_with_void_ptr.rs:10:22 + | +LL | let _ = unsafe { Box::from_raw(ptr) }; + | ^^^^^^^^^^^^^^^^^^ + | +help: cast this to a pointer of the appropriate type + --> $DIR/from_raw_with_void_ptr.rs:10:36 + | +LL | let _ = unsafe { Box::from_raw(ptr) }; + | ^^^ + = note: `-D clippy::from-raw-with-void-ptr` implied by `-D warnings` + +error: creating a `Rc` from a void raw pointer + --> $DIR/from_raw_with_void_ptr.rs:21:22 + | +LL | let _ = unsafe { Rc::from_raw(ptr) }; + | ^^^^^^^^^^^^^^^^^ + | +help: cast this to a pointer of the appropriate type + --> $DIR/from_raw_with_void_ptr.rs:21:35 + | +LL | let _ = unsafe { Rc::from_raw(ptr) }; + | ^^^ + +error: creating a `Arc` from a void raw pointer + --> $DIR/from_raw_with_void_ptr.rs:25:22 + | +LL | let _ = unsafe { Arc::from_raw(ptr) }; + | ^^^^^^^^^^^^^^^^^^ + | +help: cast this to a pointer of the appropriate type + --> $DIR/from_raw_with_void_ptr.rs:25:36 + | +LL | let _ = unsafe { Arc::from_raw(ptr) }; + | ^^^ + +error: creating a `Weak` from a void raw pointer + --> $DIR/from_raw_with_void_ptr.rs:29:22 + | +LL | let _ = unsafe { std::rc::Weak::from_raw(ptr) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: cast this to a pointer of the appropriate type + --> $DIR/from_raw_with_void_ptr.rs:29:46 + | +LL | let _ = unsafe { std::rc::Weak::from_raw(ptr) }; + | ^^^ + +error: creating a `Weak` from a void raw pointer + --> $DIR/from_raw_with_void_ptr.rs:33:22 + | +LL | let _ = unsafe { std::sync::Weak::from_raw(ptr) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: cast this to a pointer of the appropriate type + --> $DIR/from_raw_with_void_ptr.rs:33:48 + | +LL | let _ = unsafe { std::sync::Weak::from_raw(ptr) }; + | ^^^ + +error: aborting due to 5 previous errors + diff --git a/tests/ui/get_unwrap.stderr b/tests/ui/get_unwrap.stderr index 937f859040831..6dee4d5b4b624 100644 --- a/tests/ui/get_unwrap.stderr +++ b/tests/ui/get_unwrap.stderr @@ -10,7 +10,7 @@ note: the lint level is defined here LL | #![deny(clippy::get_unwrap)] | ^^^^^^^^^^^^^^^^^^ -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/get_unwrap.rs:35:17 | LL | let _ = boxed_slice.get(1).unwrap(); @@ -25,7 +25,7 @@ error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more co LL | let _ = some_slice.get(0).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_slice[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/get_unwrap.rs:36:17 | LL | let _ = some_slice.get(0).unwrap(); @@ -39,7 +39,7 @@ error: called `.get().unwrap()` on a Vec. Using `[]` is more clear and more conc LL | let _ = some_vec.get(0).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_vec[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/get_unwrap.rs:37:17 | LL | let _ = some_vec.get(0).unwrap(); @@ -53,7 +53,7 @@ error: called `.get().unwrap()` on a VecDeque. Using `[]` is more clear and more LL | let _ = some_vecdeque.get(0).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_vecdeque[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/get_unwrap.rs:38:17 | LL | let _ = some_vecdeque.get(0).unwrap(); @@ -67,7 +67,7 @@ error: called `.get().unwrap()` on a HashMap. Using `[]` is more clear and more LL | let _ = some_hashmap.get(&1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_hashmap[&1]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/get_unwrap.rs:39:17 | LL | let _ = some_hashmap.get(&1).unwrap(); @@ -81,7 +81,7 @@ error: called `.get().unwrap()` on a BTreeMap. Using `[]` is more clear and more LL | let _ = some_btreemap.get(&1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_btreemap[&1]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/get_unwrap.rs:40:17 | LL | let _ = some_btreemap.get(&1).unwrap(); @@ -95,7 +95,7 @@ error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more co LL | let _: u8 = *boxed_slice.get(1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `boxed_slice[1]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/get_unwrap.rs:44:22 | LL | let _: u8 = *boxed_slice.get(1).unwrap(); @@ -109,7 +109,7 @@ error: called `.get_mut().unwrap()` on a slice. Using `[]` is more clear and mor LL | *boxed_slice.get_mut(0).unwrap() = 1; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `boxed_slice[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/get_unwrap.rs:49:10 | LL | *boxed_slice.get_mut(0).unwrap() = 1; @@ -123,7 +123,7 @@ error: called `.get_mut().unwrap()` on a slice. Using `[]` is more clear and mor LL | *some_slice.get_mut(0).unwrap() = 1; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `some_slice[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/get_unwrap.rs:50:10 | LL | *some_slice.get_mut(0).unwrap() = 1; @@ -137,7 +137,7 @@ error: called `.get_mut().unwrap()` on a Vec. Using `[]` is more clear and more LL | *some_vec.get_mut(0).unwrap() = 1; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `some_vec[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/get_unwrap.rs:51:10 | LL | *some_vec.get_mut(0).unwrap() = 1; @@ -151,7 +151,7 @@ error: called `.get_mut().unwrap()` on a VecDeque. Using `[]` is more clear and LL | *some_vecdeque.get_mut(0).unwrap() = 1; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `some_vecdeque[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/get_unwrap.rs:52:10 | LL | *some_vecdeque.get_mut(0).unwrap() = 1; @@ -165,7 +165,7 @@ error: called `.get().unwrap()` on a Vec. Using `[]` is more clear and more conc LL | let _ = some_vec.get(0..1).unwrap().to_vec(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `some_vec[0..1]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/get_unwrap.rs:64:17 | LL | let _ = some_vec.get(0..1).unwrap().to_vec(); @@ -179,7 +179,7 @@ error: called `.get_mut().unwrap()` on a Vec. Using `[]` is more clear and more LL | let _ = some_vec.get_mut(0..1).unwrap().to_vec(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `some_vec[0..1]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/get_unwrap.rs:65:17 | LL | let _ = some_vec.get_mut(0..1).unwrap().to_vec(); diff --git a/tests/ui/infallible_destructuring_match.fixed b/tests/ui/infallible_destructuring_match.fixed index b8e40d995531a..61985e56b769e 100644 --- a/tests/ui/infallible_destructuring_match.fixed +++ b/tests/ui/infallible_destructuring_match.fixed @@ -9,6 +9,9 @@ enum SingleVariantEnum { struct TupleStruct(i32); +struct NonCopy; +struct TupleStructWithNonCopy(NonCopy); + enum EmptyEnum {} macro_rules! match_enum { @@ -71,6 +74,15 @@ fn infallible_destructuring_match_struct() { let TupleStruct(data) = wrapper; } +fn infallible_destructuring_match_struct_with_noncopy() { + let wrapper = TupleStructWithNonCopy(NonCopy); + + // This should lint! (keeping `ref` in the suggestion) + let TupleStructWithNonCopy(ref data) = wrapper; + + let TupleStructWithNonCopy(ref data) = wrapper; +} + macro_rules! match_never_enum { ($param:expr) => { let data = match $param { diff --git a/tests/ui/infallible_destructuring_match.rs b/tests/ui/infallible_destructuring_match.rs index 106cd438b90e7..f2768245bbc41 100644 --- a/tests/ui/infallible_destructuring_match.rs +++ b/tests/ui/infallible_destructuring_match.rs @@ -9,6 +9,9 @@ enum SingleVariantEnum { struct TupleStruct(i32); +struct NonCopy; +struct TupleStructWithNonCopy(NonCopy); + enum EmptyEnum {} macro_rules! match_enum { @@ -75,6 +78,17 @@ fn infallible_destructuring_match_struct() { let TupleStruct(data) = wrapper; } +fn infallible_destructuring_match_struct_with_noncopy() { + let wrapper = TupleStructWithNonCopy(NonCopy); + + // This should lint! (keeping `ref` in the suggestion) + let data = match wrapper { + TupleStructWithNonCopy(ref n) => n, + }; + + let TupleStructWithNonCopy(ref data) = wrapper; +} + macro_rules! match_never_enum { ($param:expr) => { let data = match $param { diff --git a/tests/ui/infallible_destructuring_match.stderr b/tests/ui/infallible_destructuring_match.stderr index 1b78db42014a2..f8a50f0223d63 100644 --- a/tests/ui/infallible_destructuring_match.stderr +++ b/tests/ui/infallible_destructuring_match.stderr @@ -1,5 +1,5 @@ error: you seem to be trying to use `match` to destructure a single infallible pattern. Consider using `let` - --> $DIR/infallible_destructuring_match.rs:26:5 + --> $DIR/infallible_destructuring_match.rs:29:5 | LL | / let data = match wrapper { LL | | SingleVariantEnum::Variant(i) => i, @@ -9,7 +9,7 @@ LL | | }; = note: `-D clippy::infallible-destructuring-match` implied by `-D warnings` error: you seem to be trying to use `match` to destructure a single infallible pattern. Consider using `let` - --> $DIR/infallible_destructuring_match.rs:58:5 + --> $DIR/infallible_destructuring_match.rs:61:5 | LL | / let data = match wrapper { LL | | TupleStruct(i) => i, @@ -17,12 +17,20 @@ LL | | }; | |______^ help: try this: `let TupleStruct(data) = wrapper;` error: you seem to be trying to use `match` to destructure a single infallible pattern. Consider using `let` - --> $DIR/infallible_destructuring_match.rs:90:5 + --> $DIR/infallible_destructuring_match.rs:85:5 + | +LL | / let data = match wrapper { +LL | | TupleStructWithNonCopy(ref n) => n, +LL | | }; + | |______^ help: try this: `let TupleStructWithNonCopy(ref data) = wrapper;` + +error: you seem to be trying to use `match` to destructure a single infallible pattern. Consider using `let` + --> $DIR/infallible_destructuring_match.rs:104:5 | LL | / let data = match wrapper { LL | | Ok(i) => i, LL | | }; | |______^ help: try this: `let Ok(data) = wrapper;` -error: aborting due to 3 previous errors +error: aborting due to 4 previous errors diff --git a/tests/ui/issue_4266.stderr b/tests/ui/issue_4266.stderr index fb2a93c9580e0..fd553aa4538ad 100644 --- a/tests/ui/issue_4266.stderr +++ b/tests/ui/issue_4266.stderr @@ -1,4 +1,4 @@ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) +error: the following explicit lifetimes could be elided: 'a --> $DIR/issue_4266.rs:4:1 | LL | async fn sink1<'a>(_: &'a str) {} // lint @@ -6,7 +6,7 @@ LL | async fn sink1<'a>(_: &'a str) {} // lint | = note: `-D clippy::needless-lifetimes` implied by `-D warnings` -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) +error: the following explicit lifetimes could be elided: 'a --> $DIR/issue_4266.rs:8:1 | LL | async fn one_to_one<'a>(s: &'a str) -> &'a str { diff --git a/tests/ui/let_underscore_drop.rs b/tests/ui/let_underscore_drop.rs deleted file mode 100644 index 11b50492ab290..0000000000000 --- a/tests/ui/let_underscore_drop.rs +++ /dev/null @@ -1,28 +0,0 @@ -#![warn(clippy::let_underscore_drop)] -#![allow(clippy::let_unit_value)] - -struct Droppable; - -impl Drop for Droppable { - fn drop(&mut self) {} -} - -fn main() { - let unit = (); - let boxed = Box::new(()); - let droppable = Droppable; - let optional = Some(Droppable); - - let _ = (); - let _ = Box::new(()); - let _ = Droppable; - let _ = Some(Droppable); - - // no lint for reference - let _ = droppable_ref(); -} - -#[must_use] -fn droppable_ref() -> &'static mut Droppable { - unimplemented!() -} diff --git a/tests/ui/let_underscore_drop.stderr b/tests/ui/let_underscore_drop.stderr deleted file mode 100644 index 324b7cd431d46..0000000000000 --- a/tests/ui/let_underscore_drop.stderr +++ /dev/null @@ -1,27 +0,0 @@ -error: non-binding `let` on a type that implements `Drop` - --> $DIR/let_underscore_drop.rs:17:5 - | -LL | let _ = Box::new(()); - | ^^^^^^^^^^^^^^^^^^^^^ - | - = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop` - = note: `-D clippy::let-underscore-drop` implied by `-D warnings` - -error: non-binding `let` on a type that implements `Drop` - --> $DIR/let_underscore_drop.rs:18:5 - | -LL | let _ = Droppable; - | ^^^^^^^^^^^^^^^^^^ - | - = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop` - -error: non-binding `let` on a type that implements `Drop` - --> $DIR/let_underscore_drop.rs:19:5 - | -LL | let _ = Some(Droppable); - | ^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop` - -error: aborting due to 3 previous errors - diff --git a/tests/ui/let_underscore_future.rs b/tests/ui/let_underscore_future.rs new file mode 100644 index 0000000000000..d8f54cdca9120 --- /dev/null +++ b/tests/ui/let_underscore_future.rs @@ -0,0 +1,20 @@ +use std::future::Future; + +async fn some_async_fn() {} + +fn sync_side_effects() {} +fn custom() -> impl Future { + sync_side_effects(); + async {} +} + +fn do_something_to_future(future: &mut impl Future) {} + +fn main() { + let _ = some_async_fn(); + let _ = custom(); + + let mut future = some_async_fn(); + do_something_to_future(&mut future); + let _ = future; +} diff --git a/tests/ui/let_underscore_future.stderr b/tests/ui/let_underscore_future.stderr new file mode 100644 index 0000000000000..33a748736a880 --- /dev/null +++ b/tests/ui/let_underscore_future.stderr @@ -0,0 +1,27 @@ +error: non-binding `let` on a future + --> $DIR/let_underscore_future.rs:14:5 + | +LL | let _ = some_async_fn(); + | ^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: consider awaiting the future or dropping explicitly with `std::mem::drop` + = note: `-D clippy::let-underscore-future` implied by `-D warnings` + +error: non-binding `let` on a future + --> $DIR/let_underscore_future.rs:15:5 + | +LL | let _ = custom(); + | ^^^^^^^^^^^^^^^^^ + | + = help: consider awaiting the future or dropping explicitly with `std::mem::drop` + +error: non-binding `let` on a future + --> $DIR/let_underscore_future.rs:19:5 + | +LL | let _ = future; + | ^^^^^^^^^^^^^^^ + | + = help: consider awaiting the future or dropping explicitly with `std::mem::drop` + +error: aborting due to 3 previous errors + diff --git a/tests/ui/let_underscore_lock.rs b/tests/ui/let_underscore_lock.rs index 7a7c4e9249952..4dff4d766bcc3 100644 --- a/tests/ui/let_underscore_lock.rs +++ b/tests/ui/let_underscore_lock.rs @@ -3,20 +3,6 @@ extern crate parking_lot; fn main() { - let m = std::sync::Mutex::new(()); - let rw = std::sync::RwLock::new(()); - - let _ = m.lock(); - let _ = rw.read(); - let _ = rw.write(); - let _ = m.try_lock(); - let _ = rw.try_read(); - let _ = rw.try_write(); - - // These shouldn't throw an error. - let _ = m; - let _ = rw; - use parking_lot::{lock_api::RawMutex, Mutex, RwLock}; let p_m: Mutex<()> = Mutex::const_new(RawMutex::INIT, ()); @@ -34,3 +20,20 @@ fn main() { let _ = p_m1; let _ = p_rw; } + +fn uplifted() { + // shouldn't lint std locks as they were uplifted as rustc's `let_underscore_lock` + + let m = std::sync::Mutex::new(()); + let rw = std::sync::RwLock::new(()); + + let _ = m.lock(); + let _ = rw.read(); + let _ = rw.write(); + let _ = m.try_lock(); + let _ = rw.try_read(); + let _ = rw.try_write(); + + let _ = m; + let _ = rw; +} diff --git a/tests/ui/let_underscore_lock.stderr b/tests/ui/let_underscore_lock.stderr index d7779e7b6c48a..f137d4112092d 100644 --- a/tests/ui/let_underscore_lock.stderr +++ b/tests/ui/let_underscore_lock.stderr @@ -1,83 +1,35 @@ -error: non-binding let on a synchronization lock +error: non-binding `let` on a synchronization lock --> $DIR/let_underscore_lock.rs:9:5 | -LL | let _ = m.lock(); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop` - = note: `-D clippy::let-underscore-lock` implied by `-D warnings` - -error: non-binding let on a synchronization lock - --> $DIR/let_underscore_lock.rs:10:5 - | -LL | let _ = rw.read(); - | ^^^^^^^^^^^^^^^^^^ - | - = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop` - -error: non-binding let on a synchronization lock - --> $DIR/let_underscore_lock.rs:11:5 - | -LL | let _ = rw.write(); - | ^^^^^^^^^^^^^^^^^^^ - | - = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop` - -error: non-binding let on a synchronization lock - --> $DIR/let_underscore_lock.rs:12:5 - | -LL | let _ = m.try_lock(); - | ^^^^^^^^^^^^^^^^^^^^^ - | - = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop` - -error: non-binding let on a synchronization lock - --> $DIR/let_underscore_lock.rs:13:5 - | -LL | let _ = rw.try_read(); - | ^^^^^^^^^^^^^^^^^^^^^^ - | - = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop` - -error: non-binding let on a synchronization lock - --> $DIR/let_underscore_lock.rs:14:5 - | -LL | let _ = rw.try_write(); - | ^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop` - -error: non-binding let on a synchronization lock - --> $DIR/let_underscore_lock.rs:23:5 - | LL | let _ = p_m.lock(); | ^^^^^^^^^^^^^^^^^^^ | = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop` + = note: `-D clippy::let-underscore-lock` implied by `-D warnings` -error: non-binding let on a synchronization lock - --> $DIR/let_underscore_lock.rs:26:5 +error: non-binding `let` on a synchronization lock + --> $DIR/let_underscore_lock.rs:12:5 | LL | let _ = p_m1.lock(); | ^^^^^^^^^^^^^^^^^^^^ | = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop` -error: non-binding let on a synchronization lock - --> $DIR/let_underscore_lock.rs:29:5 +error: non-binding `let` on a synchronization lock + --> $DIR/let_underscore_lock.rs:15:5 | LL | let _ = p_rw.read(); | ^^^^^^^^^^^^^^^^^^^^ | = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop` -error: non-binding let on a synchronization lock - --> $DIR/let_underscore_lock.rs:30:5 +error: non-binding `let` on a synchronization lock + --> $DIR/let_underscore_lock.rs:16:5 | LL | let _ = p_rw.write(); | ^^^^^^^^^^^^^^^^^^^^^ | = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop` -error: aborting due to 10 previous errors +error: aborting due to 4 previous errors diff --git a/tests/ui/let_underscore_must_use.stderr b/tests/ui/let_underscore_must_use.stderr index bae60f2ff9b79..28d760eb46ecc 100644 --- a/tests/ui/let_underscore_must_use.stderr +++ b/tests/ui/let_underscore_must_use.stderr @@ -1,4 +1,4 @@ -error: non-binding let on a result of a `#[must_use]` function +error: non-binding `let` on a result of a `#[must_use]` function --> $DIR/let_underscore_must_use.rs:67:5 | LL | let _ = f(); @@ -7,7 +7,7 @@ LL | let _ = f(); = help: consider explicitly using function result = note: `-D clippy::let-underscore-must-use` implied by `-D warnings` -error: non-binding let on an expression with `#[must_use]` type +error: non-binding `let` on an expression with `#[must_use]` type --> $DIR/let_underscore_must_use.rs:68:5 | LL | let _ = g(); @@ -15,7 +15,7 @@ LL | let _ = g(); | = help: consider explicitly using expression value -error: non-binding let on a result of a `#[must_use]` function +error: non-binding `let` on a result of a `#[must_use]` function --> $DIR/let_underscore_must_use.rs:70:5 | LL | let _ = l(0_u32); @@ -23,7 +23,7 @@ LL | let _ = l(0_u32); | = help: consider explicitly using function result -error: non-binding let on a result of a `#[must_use]` function +error: non-binding `let` on a result of a `#[must_use]` function --> $DIR/let_underscore_must_use.rs:74:5 | LL | let _ = s.f(); @@ -31,7 +31,7 @@ LL | let _ = s.f(); | = help: consider explicitly using function result -error: non-binding let on an expression with `#[must_use]` type +error: non-binding `let` on an expression with `#[must_use]` type --> $DIR/let_underscore_must_use.rs:75:5 | LL | let _ = s.g(); @@ -39,7 +39,7 @@ LL | let _ = s.g(); | = help: consider explicitly using expression value -error: non-binding let on a result of a `#[must_use]` function +error: non-binding `let` on a result of a `#[must_use]` function --> $DIR/let_underscore_must_use.rs:78:5 | LL | let _ = S::h(); @@ -47,7 +47,7 @@ LL | let _ = S::h(); | = help: consider explicitly using function result -error: non-binding let on an expression with `#[must_use]` type +error: non-binding `let` on an expression with `#[must_use]` type --> $DIR/let_underscore_must_use.rs:79:5 | LL | let _ = S::p(); @@ -55,7 +55,7 @@ LL | let _ = S::p(); | = help: consider explicitly using expression value -error: non-binding let on a result of a `#[must_use]` function +error: non-binding `let` on a result of a `#[must_use]` function --> $DIR/let_underscore_must_use.rs:81:5 | LL | let _ = S::a(); @@ -63,7 +63,7 @@ LL | let _ = S::a(); | = help: consider explicitly using function result -error: non-binding let on an expression with `#[must_use]` type +error: non-binding `let` on an expression with `#[must_use]` type --> $DIR/let_underscore_must_use.rs:83:5 | LL | let _ = if true { Ok(()) } else { Err(()) }; @@ -71,7 +71,7 @@ LL | let _ = if true { Ok(()) } else { Err(()) }; | = help: consider explicitly using expression value -error: non-binding let on a result of a `#[must_use]` function +error: non-binding `let` on a result of a `#[must_use]` function --> $DIR/let_underscore_must_use.rs:87:5 | LL | let _ = a.is_ok(); @@ -79,7 +79,7 @@ LL | let _ = a.is_ok(); | = help: consider explicitly using function result -error: non-binding let on an expression with `#[must_use]` type +error: non-binding `let` on an expression with `#[must_use]` type --> $DIR/let_underscore_must_use.rs:89:5 | LL | let _ = a.map(|_| ()); @@ -87,7 +87,7 @@ LL | let _ = a.map(|_| ()); | = help: consider explicitly using expression value -error: non-binding let on an expression with `#[must_use]` type +error: non-binding `let` on an expression with `#[must_use]` type --> $DIR/let_underscore_must_use.rs:91:5 | LL | let _ = a; diff --git a/tests/ui/manual_flatten.rs b/tests/ui/manual_flatten.rs index 96cd87c0e19a7..552213a7ff229 100644 --- a/tests/ui/manual_flatten.rs +++ b/tests/ui/manual_flatten.rs @@ -10,7 +10,7 @@ fn main() { } } - // Test for loop over implicitly implicitly adjusted `Iterator` with `if let` statement + // Test for loop over implicitly adjusted `Iterator` with `if let` statement let y: Vec> = vec![]; for n in y.clone() { if let Ok(n) = n { diff --git a/tests/ui/manual_instant_elapsed.fixed b/tests/ui/manual_instant_elapsed.fixed index 0fa776b7b2e4e..85a91543c893a 100644 --- a/tests/ui/manual_instant_elapsed.fixed +++ b/tests/ui/manual_instant_elapsed.fixed @@ -1,6 +1,7 @@ // run-rustfix #![warn(clippy::manual_instant_elapsed)] #![allow(clippy::unnecessary_operation)] +#![allow(clippy::unchecked_duration_subtraction)] #![allow(unused_variables)] #![allow(unused_must_use)] diff --git a/tests/ui/manual_instant_elapsed.rs b/tests/ui/manual_instant_elapsed.rs index 5b11b84535ddc..c98cb15b91649 100644 --- a/tests/ui/manual_instant_elapsed.rs +++ b/tests/ui/manual_instant_elapsed.rs @@ -1,6 +1,7 @@ // run-rustfix #![warn(clippy::manual_instant_elapsed)] #![allow(clippy::unnecessary_operation)] +#![allow(clippy::unchecked_duration_subtraction)] #![allow(unused_variables)] #![allow(unused_must_use)] diff --git a/tests/ui/manual_instant_elapsed.stderr b/tests/ui/manual_instant_elapsed.stderr index 5537f5642a23c..4ce1f689107e9 100644 --- a/tests/ui/manual_instant_elapsed.stderr +++ b/tests/ui/manual_instant_elapsed.stderr @@ -1,5 +1,5 @@ error: manual implementation of `Instant::elapsed` - --> $DIR/manual_instant_elapsed.rs:17:20 + --> $DIR/manual_instant_elapsed.rs:18:20 | LL | let duration = Instant::now() - prev_instant; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `prev_instant.elapsed()` @@ -7,7 +7,7 @@ LL | let duration = Instant::now() - prev_instant; = note: `-D clippy::manual-instant-elapsed` implied by `-D warnings` error: manual implementation of `Instant::elapsed` - --> $DIR/manual_instant_elapsed.rs:26:5 + --> $DIR/manual_instant_elapsed.rs:27:5 | LL | Instant::now() - *ref_to_instant; // to ensure parens are added correctly | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(*ref_to_instant).elapsed()` diff --git a/tests/ui/manual_is_ascii_check.fixed b/tests/ui/manual_is_ascii_check.fixed new file mode 100644 index 0000000000000..765bb785994e2 --- /dev/null +++ b/tests/ui/manual_is_ascii_check.fixed @@ -0,0 +1,45 @@ +// run-rustfix + +#![feature(custom_inner_attributes)] +#![allow(unused, dead_code)] +#![warn(clippy::manual_is_ascii_check)] + +fn main() { + assert!('x'.is_ascii_lowercase()); + assert!('X'.is_ascii_uppercase()); + assert!(b'x'.is_ascii_lowercase()); + assert!(b'X'.is_ascii_uppercase()); + + let num = '2'; + assert!(num.is_ascii_digit()); + assert!(b'1'.is_ascii_digit()); + assert!('x'.is_ascii_alphabetic()); + + assert!(matches!('x', 'A'..='Z' | 'a'..='z' | '_')); +} + +fn msrv_1_23() { + #![clippy::msrv = "1.23"] + + assert!(matches!(b'1', b'0'..=b'9')); + assert!(matches!('X', 'A'..='Z')); + assert!(matches!('x', 'A'..='Z' | 'a'..='z')); +} + +fn msrv_1_24() { + #![clippy::msrv = "1.24"] + + assert!(b'1'.is_ascii_digit()); + assert!('X'.is_ascii_uppercase()); + assert!('x'.is_ascii_alphabetic()); +} + +fn msrv_1_46() { + #![clippy::msrv = "1.46"] + const FOO: bool = matches!('x', '0'..='9'); +} + +fn msrv_1_47() { + #![clippy::msrv = "1.47"] + const FOO: bool = 'x'.is_ascii_digit(); +} diff --git a/tests/ui/manual_is_ascii_check.rs b/tests/ui/manual_is_ascii_check.rs new file mode 100644 index 0000000000000..be13316104123 --- /dev/null +++ b/tests/ui/manual_is_ascii_check.rs @@ -0,0 +1,45 @@ +// run-rustfix + +#![feature(custom_inner_attributes)] +#![allow(unused, dead_code)] +#![warn(clippy::manual_is_ascii_check)] + +fn main() { + assert!(matches!('x', 'a'..='z')); + assert!(matches!('X', 'A'..='Z')); + assert!(matches!(b'x', b'a'..=b'z')); + assert!(matches!(b'X', b'A'..=b'Z')); + + let num = '2'; + assert!(matches!(num, '0'..='9')); + assert!(matches!(b'1', b'0'..=b'9')); + assert!(matches!('x', 'A'..='Z' | 'a'..='z')); + + assert!(matches!('x', 'A'..='Z' | 'a'..='z' | '_')); +} + +fn msrv_1_23() { + #![clippy::msrv = "1.23"] + + assert!(matches!(b'1', b'0'..=b'9')); + assert!(matches!('X', 'A'..='Z')); + assert!(matches!('x', 'A'..='Z' | 'a'..='z')); +} + +fn msrv_1_24() { + #![clippy::msrv = "1.24"] + + assert!(matches!(b'1', b'0'..=b'9')); + assert!(matches!('X', 'A'..='Z')); + assert!(matches!('x', 'A'..='Z' | 'a'..='z')); +} + +fn msrv_1_46() { + #![clippy::msrv = "1.46"] + const FOO: bool = matches!('x', '0'..='9'); +} + +fn msrv_1_47() { + #![clippy::msrv = "1.47"] + const FOO: bool = matches!('x', '0'..='9'); +} diff --git a/tests/ui/manual_is_ascii_check.stderr b/tests/ui/manual_is_ascii_check.stderr new file mode 100644 index 0000000000000..c0a9d4db1a159 --- /dev/null +++ b/tests/ui/manual_is_ascii_check.stderr @@ -0,0 +1,70 @@ +error: manual check for common ascii range + --> $DIR/manual_is_ascii_check.rs:8:13 + | +LL | assert!(matches!('x', 'a'..='z')); + | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'x'.is_ascii_lowercase()` + | + = note: `-D clippy::manual-is-ascii-check` implied by `-D warnings` + +error: manual check for common ascii range + --> $DIR/manual_is_ascii_check.rs:9:13 + | +LL | assert!(matches!('X', 'A'..='Z')); + | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'X'.is_ascii_uppercase()` + +error: manual check for common ascii range + --> $DIR/manual_is_ascii_check.rs:10:13 + | +LL | assert!(matches!(b'x', b'a'..=b'z')); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b'x'.is_ascii_lowercase()` + +error: manual check for common ascii range + --> $DIR/manual_is_ascii_check.rs:11:13 + | +LL | assert!(matches!(b'X', b'A'..=b'Z')); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b'X'.is_ascii_uppercase()` + +error: manual check for common ascii range + --> $DIR/manual_is_ascii_check.rs:14:13 + | +LL | assert!(matches!(num, '0'..='9')); + | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `num.is_ascii_digit()` + +error: manual check for common ascii range + --> $DIR/manual_is_ascii_check.rs:15:13 + | +LL | assert!(matches!(b'1', b'0'..=b'9')); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b'1'.is_ascii_digit()` + +error: manual check for common ascii range + --> $DIR/manual_is_ascii_check.rs:16:13 + | +LL | assert!(matches!('x', 'A'..='Z' | 'a'..='z')); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'x'.is_ascii_alphabetic()` + +error: manual check for common ascii range + --> $DIR/manual_is_ascii_check.rs:32:13 + | +LL | assert!(matches!(b'1', b'0'..=b'9')); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b'1'.is_ascii_digit()` + +error: manual check for common ascii range + --> $DIR/manual_is_ascii_check.rs:33:13 + | +LL | assert!(matches!('X', 'A'..='Z')); + | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'X'.is_ascii_uppercase()` + +error: manual check for common ascii range + --> $DIR/manual_is_ascii_check.rs:34:13 + | +LL | assert!(matches!('x', 'A'..='Z' | 'a'..='z')); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'x'.is_ascii_alphabetic()` + +error: manual check for common ascii range + --> $DIR/manual_is_ascii_check.rs:44:23 + | +LL | const FOO: bool = matches!('x', '0'..='9'); + | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'x'.is_ascii_digit()` + +error: aborting due to 11 previous errors + diff --git a/tests/ui/manual_let_else.rs b/tests/ui/manual_let_else.rs new file mode 100644 index 0000000000000..2ef40e5911af4 --- /dev/null +++ b/tests/ui/manual_let_else.rs @@ -0,0 +1,237 @@ +#![allow(unused_braces, unused_variables, dead_code)] +#![allow( + clippy::collapsible_else_if, + clippy::unused_unit, + clippy::let_unit_value, + clippy::match_single_binding, + clippy::never_loop +)] +#![warn(clippy::manual_let_else)] + +fn g() -> Option<()> { + None +} + +fn main() {} + +fn fire() { + let v = if let Some(v_some) = g() { v_some } else { return }; + let v = if let Some(v_some) = g() { + v_some + } else { + return; + }; + + let v = if let Some(v) = g() { + // Blocks around the identity should have no impact + { + { v } + } + } else { + // Some computation should still make it fire + g(); + return; + }; + + // continue and break diverge + loop { + let v = if let Some(v_some) = g() { v_some } else { continue }; + let v = if let Some(v_some) = g() { v_some } else { break }; + } + + // panic also diverges + let v = if let Some(v_some) = g() { v_some } else { panic!() }; + + // abort also diverges + let v = if let Some(v_some) = g() { + v_some + } else { + std::process::abort() + }; + + // If whose two branches diverge also diverges + let v = if let Some(v_some) = g() { + v_some + } else { + if true { return } else { panic!() } + }; + + // Diverging after an if still makes the block diverge: + let v = if let Some(v_some) = g() { + v_some + } else { + if true {} + panic!(); + }; + + // A match diverges if all branches diverge: + // Note: the corresponding let-else requires a ; at the end of the match + // as otherwise the type checker does not turn it into a ! type. + let v = if let Some(v_some) = g() { + v_some + } else { + match () { + _ if panic!() => {}, + _ => panic!(), + } + }; + + // An if's expression can cause divergence: + let v = if let Some(v_some) = g() { v_some } else { if panic!() {} }; + + // An expression of a match can cause divergence: + let v = if let Some(v_some) = g() { + v_some + } else { + match panic!() { + _ => {}, + } + }; + + // Top level else if + let v = if let Some(v_some) = g() { + v_some + } else if true { + return; + } else { + panic!("diverge"); + }; + + // All match arms diverge + let v = if let Some(v_some) = g() { + v_some + } else { + match (g(), g()) { + (Some(_), None) => return, + (None, Some(_)) => { + if true { + return; + } else { + panic!(); + } + }, + _ => return, + } + }; + + // Tuples supported for the declared variables + let (v, w) = if let Some(v_some) = g().map(|v| (v, 42)) { + v_some + } else { + return; + }; + + // Tuples supported for the identity block and pattern + let v = if let (Some(v_some), w_some) = (g(), 0) { + (w_some, v_some) + } else { + return; + }; + + // entirely inside macro lints + macro_rules! create_binding_if_some { + ($n:ident, $e:expr) => { + let $n = if let Some(v) = $e { v } else { return }; + }; + } + create_binding_if_some!(w, g()); +} + +fn not_fire() { + let v = if let Some(v_some) = g() { + // Nothing returned. Should not fire. + } else { + return; + }; + + let w = 0; + let v = if let Some(v_some) = g() { + // Different variable than v_some. Should not fire. + w + } else { + return; + }; + + let v = if let Some(v_some) = g() { + // Computation in then clause. Should not fire. + g(); + v_some + } else { + return; + }; + + let v = if let Some(v_some) = g() { + v_some + } else { + if false { + return; + } + // This doesn't diverge. Should not fire. + () + }; + + let v = if let Some(v_some) = g() { + v_some + } else { + // There is one match arm that doesn't diverge. Should not fire. + match (g(), g()) { + (Some(_), None) => return, + (None, Some(_)) => return, + (Some(_), Some(_)) => (), + _ => return, + } + }; + + let v = if let Some(v_some) = g() { + v_some + } else { + // loop with a break statement inside does not diverge. + loop { + break; + } + }; + + enum Uninhabited {} + fn un() -> Uninhabited { + panic!() + } + let v = if let Some(v_some) = None { + v_some + } else { + // Don't lint if the type is uninhabited but not ! + un() + }; + + fn question_mark() -> Option<()> { + let v = if let Some(v) = g() { + v + } else { + // Question mark does not diverge + g()? + }; + Some(v) + } + + // Macro boundary inside let + macro_rules! some_or_return { + ($e:expr) => { + if let Some(v) = $e { v } else { return } + }; + } + let v = some_or_return!(g()); + + // Also macro boundary inside let, but inside a macro + macro_rules! create_binding_if_some_nf { + ($n:ident, $e:expr) => { + let $n = some_or_return!($e); + }; + } + create_binding_if_some_nf!(v, g()); + + // Already a let-else + let Some(a) = (if let Some(b) = Some(Some(())) { b } else { return }) else { panic!() }; + + // If a type annotation is present, don't lint as + // expressing the type might be too hard + let v: () = if let Some(v_some) = g() { v_some } else { panic!() }; +} diff --git a/tests/ui/manual_let_else.stderr b/tests/ui/manual_let_else.stderr new file mode 100644 index 0000000000000..453b68b8bd003 --- /dev/null +++ b/tests/ui/manual_let_else.stderr @@ -0,0 +1,263 @@ +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:18:5 + | +LL | let v = if let Some(v_some) = g() { v_some } else { return }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Some(v_some) = g() else { return };` + | + = note: `-D clippy::manual-let-else` implied by `-D warnings` + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:19:5 + | +LL | / let v = if let Some(v_some) = g() { +LL | | v_some +LL | | } else { +LL | | return; +LL | | }; + | |______^ + | +help: consider writing + | +LL ~ let Some(v_some) = g() else { +LL + return; +LL + }; + | + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:25:5 + | +LL | / let v = if let Some(v) = g() { +LL | | // Blocks around the identity should have no impact +LL | | { +LL | | { v } +... | +LL | | return; +LL | | }; + | |______^ + | +help: consider writing + | +LL ~ let Some(v) = g() else { +LL + // Some computation should still make it fire +LL + g(); +LL + return; +LL + }; + | + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:38:9 + | +LL | let v = if let Some(v_some) = g() { v_some } else { continue }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Some(v_some) = g() else { continue };` + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:39:9 + | +LL | let v = if let Some(v_some) = g() { v_some } else { break }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Some(v_some) = g() else { break };` + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:43:5 + | +LL | let v = if let Some(v_some) = g() { v_some } else { panic!() }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Some(v_some) = g() else { panic!() };` + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:46:5 + | +LL | / let v = if let Some(v_some) = g() { +LL | | v_some +LL | | } else { +LL | | std::process::abort() +LL | | }; + | |______^ + | +help: consider writing + | +LL ~ let Some(v_some) = g() else { +LL + std::process::abort() +LL + }; + | + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:53:5 + | +LL | / let v = if let Some(v_some) = g() { +LL | | v_some +LL | | } else { +LL | | if true { return } else { panic!() } +LL | | }; + | |______^ + | +help: consider writing + | +LL ~ let Some(v_some) = g() else { +LL + if true { return } else { panic!() } +LL + }; + | + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:60:5 + | +LL | / let v = if let Some(v_some) = g() { +LL | | v_some +LL | | } else { +LL | | if true {} +LL | | panic!(); +LL | | }; + | |______^ + | +help: consider writing + | +LL ~ let Some(v_some) = g() else { +LL + if true {} +LL + panic!(); +LL + }; + | + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:70:5 + | +LL | / let v = if let Some(v_some) = g() { +LL | | v_some +LL | | } else { +LL | | match () { +... | +LL | | } +LL | | }; + | |______^ + | +help: consider writing + | +LL ~ let Some(v_some) = g() else { +LL + match () { +LL + _ if panic!() => {}, +LL + _ => panic!(), +LL + } +LL + }; + | + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:80:5 + | +LL | let v = if let Some(v_some) = g() { v_some } else { if panic!() {} }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Some(v_some) = g() else { if panic!() {} };` + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:83:5 + | +LL | / let v = if let Some(v_some) = g() { +LL | | v_some +LL | | } else { +LL | | match panic!() { +LL | | _ => {}, +LL | | } +LL | | }; + | |______^ + | +help: consider writing + | +LL ~ let Some(v_some) = g() else { +LL + match panic!() { +LL + _ => {}, +LL + } +LL + }; + | + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:92:5 + | +LL | / let v = if let Some(v_some) = g() { +LL | | v_some +LL | | } else if true { +LL | | return; +LL | | } else { +LL | | panic!("diverge"); +LL | | }; + | |______^ + | +help: consider writing + | +LL ~ let Some(v_some) = g() else { if true { +LL + return; +LL + } else { +LL + panic!("diverge"); +LL + } }; + | + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:101:5 + | +LL | / let v = if let Some(v_some) = g() { +LL | | v_some +LL | | } else { +LL | | match (g(), g()) { +... | +LL | | } +LL | | }; + | |______^ + | +help: consider writing + | +LL ~ let Some(v_some) = g() else { +LL + match (g(), g()) { +LL + (Some(_), None) => return, +LL + (None, Some(_)) => { +LL + if true { +LL + return; +LL + } else { +LL + panic!(); +LL + } +LL + }, +LL + _ => return, +LL + } +LL + }; + | + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:118:5 + | +LL | / let (v, w) = if let Some(v_some) = g().map(|v| (v, 42)) { +LL | | v_some +LL | | } else { +LL | | return; +LL | | }; + | |______^ + | +help: consider writing + | +LL ~ let Some(v_some) = g().map(|v| (v, 42)) else { +LL + return; +LL + }; + | + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:125:5 + | +LL | / let v = if let (Some(v_some), w_some) = (g(), 0) { +LL | | (w_some, v_some) +LL | | } else { +LL | | return; +LL | | }; + | |______^ + | +help: consider writing + | +LL ~ let (Some(v_some), w_some) = (g(), 0) else { +LL + return; +LL + }; + | + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:134:13 + | +LL | let $n = if let Some(v) = $e { v } else { return }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Some(v) = g() else { return };` +... +LL | create_binding_if_some!(w, g()); + | ------------------------------- in this macro invocation + | + = note: this error originates in the macro `create_binding_if_some` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 17 previous errors + diff --git a/tests/ui/manual_let_else_match.rs b/tests/ui/manual_let_else_match.rs new file mode 100644 index 0000000000000..93c86ca24fea3 --- /dev/null +++ b/tests/ui/manual_let_else_match.rs @@ -0,0 +1,121 @@ +#![allow(unused_braces, unused_variables, dead_code)] +#![allow(clippy::collapsible_else_if, clippy::let_unit_value)] +#![warn(clippy::manual_let_else)] +// Ensure that we don't conflict with match -> if let lints +#![warn(clippy::single_match_else, clippy::single_match)] + +fn f() -> Result { + Ok(0) +} + +fn g() -> Option<()> { + None +} + +fn h() -> (Option<()>, Option<()>) { + (None, None) +} + +enum Variant { + Foo, + Bar(u32), + Baz(u32), +} + +fn build_enum() -> Variant { + Variant::Foo +} + +fn main() {} + +fn fire() { + let v = match g() { + Some(v_some) => v_some, + None => return, + }; + + let v = match g() { + Some(v_some) => v_some, + _ => return, + }; + + loop { + // More complex pattern for the identity arm and diverging arm + let v = match h() { + (Some(_), Some(_)) | (None, None) => continue, + (Some(v), None) | (None, Some(v)) => v, + }; + // Custom enums are supported as long as the "else" arm is a simple _ + let v = match build_enum() { + _ => continue, + Variant::Bar(v) | Variant::Baz(v) => v, + }; + } + + // There is a _ in the diverging arm + // TODO also support unused bindings aka _v + let v = match f() { + Ok(v) => v, + Err(_) => return, + }; + + // Err(()) is an allowed pattern + let v = match f().map_err(|_| ()) { + Ok(v) => v, + Err(()) => return, + }; +} + +fn not_fire() { + // Multiple diverging arms + let v = match h() { + _ => panic!(), + (None, Some(_v)) => return, + (Some(v), None) => v, + }; + + // Multiple identity arms + let v = match h() { + _ => panic!(), + (None, Some(v)) => v, + (Some(v), None) => v, + }; + + // No diverging arm at all, only identity arms. + // This is no case for let else, but destructuring assignment. + let v = match f() { + Ok(v) => v, + Err(e) => e, + }; + + // The identity arm has a guard + let v = match g() { + Some(v) if g().is_none() => v, + _ => return, + }; + + // The diverging arm has a guard + let v = match f() { + Err(v) if v > 0 => panic!(), + Ok(v) | Err(v) => v, + }; + + // The diverging arm creates a binding + let v = match f() { + Ok(v) => v, + Err(e) => panic!("error: {e}"), + }; + + // Custom enum where the diverging arm + // explicitly mentions the variant + let v = match build_enum() { + Variant::Foo => return, + Variant::Bar(v) | Variant::Baz(v) => v, + }; + + // The custom enum is surrounded by an Err() + let v = match Err(build_enum()) { + Ok(v) | Err(Variant::Bar(v) | Variant::Baz(v)) => v, + Err(Variant::Foo) => return, + }; +} diff --git a/tests/ui/manual_let_else_match.stderr b/tests/ui/manual_let_else_match.stderr new file mode 100644 index 0000000000000..38be5ac545473 --- /dev/null +++ b/tests/ui/manual_let_else_match.stderr @@ -0,0 +1,58 @@ +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else_match.rs:32:5 + | +LL | / let v = match g() { +LL | | Some(v_some) => v_some, +LL | | None => return, +LL | | }; + | |______^ help: consider writing: `let Some(v_some) = g() else { return };` + | + = note: `-D clippy::manual-let-else` implied by `-D warnings` + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else_match.rs:37:5 + | +LL | / let v = match g() { +LL | | Some(v_some) => v_some, +LL | | _ => return, +LL | | }; + | |______^ help: consider writing: `let Some(v_some) = g() else { return };` + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else_match.rs:44:9 + | +LL | / let v = match h() { +LL | | (Some(_), Some(_)) | (None, None) => continue, +LL | | (Some(v), None) | (None, Some(v)) => v, +LL | | }; + | |__________^ help: consider writing: `let (Some(v), None) | (None, Some(v)) = h() else { continue };` + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else_match.rs:49:9 + | +LL | / let v = match build_enum() { +LL | | _ => continue, +LL | | Variant::Bar(v) | Variant::Baz(v) => v, +LL | | }; + | |__________^ help: consider writing: `let Variant::Bar(v) | Variant::Baz(v) = build_enum() else { continue };` + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else_match.rs:57:5 + | +LL | / let v = match f() { +LL | | Ok(v) => v, +LL | | Err(_) => return, +LL | | }; + | |______^ help: consider writing: `let Ok(v) = f() else { return };` + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else_match.rs:63:5 + | +LL | / let v = match f().map_err(|_| ()) { +LL | | Ok(v) => v, +LL | | Err(()) => return, +LL | | }; + | |______^ help: consider writing: `let Ok(v) = f().map_err(|_| ()) else { return };` + +error: aborting due to 6 previous errors + diff --git a/tests/ui/manual_ok_or.fixed b/tests/ui/manual_ok_or.fixed index d864f85545349..fc8511626b3d1 100644 --- a/tests/ui/manual_ok_or.fixed +++ b/tests/ui/manual_ok_or.fixed @@ -1,5 +1,6 @@ // run-rustfix #![warn(clippy::manual_ok_or)] +#![allow(clippy::or_fun_call)] #![allow(clippy::disallowed_names)] #![allow(clippy::redundant_closure)] #![allow(dead_code)] diff --git a/tests/ui/manual_ok_or.rs b/tests/ui/manual_ok_or.rs index 6264768460ef6..b5303d33f5fd1 100644 --- a/tests/ui/manual_ok_or.rs +++ b/tests/ui/manual_ok_or.rs @@ -1,5 +1,6 @@ // run-rustfix #![warn(clippy::manual_ok_or)] +#![allow(clippy::or_fun_call)] #![allow(clippy::disallowed_names)] #![allow(clippy::redundant_closure)] #![allow(dead_code)] diff --git a/tests/ui/manual_ok_or.stderr b/tests/ui/manual_ok_or.stderr index 65459a097384b..b4a17f143e3fc 100644 --- a/tests/ui/manual_ok_or.stderr +++ b/tests/ui/manual_ok_or.stderr @@ -1,5 +1,5 @@ error: this pattern reimplements `Option::ok_or` - --> $DIR/manual_ok_or.rs:11:5 + --> $DIR/manual_ok_or.rs:12:5 | LL | foo.map_or(Err("error"), |v| Ok(v)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `foo.ok_or("error")` @@ -7,19 +7,19 @@ LL | foo.map_or(Err("error"), |v| Ok(v)); = note: `-D clippy::manual-ok-or` implied by `-D warnings` error: this pattern reimplements `Option::ok_or` - --> $DIR/manual_ok_or.rs:14:5 + --> $DIR/manual_ok_or.rs:15:5 | LL | foo.map_or(Err("error"), Ok); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `foo.ok_or("error")` error: this pattern reimplements `Option::ok_or` - --> $DIR/manual_ok_or.rs:17:5 + --> $DIR/manual_ok_or.rs:18:5 | LL | None::.map_or(Err("error"), |v| Ok(v)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `None::.ok_or("error")` error: this pattern reimplements `Option::ok_or` - --> $DIR/manual_ok_or.rs:21:5 + --> $DIR/manual_ok_or.rs:22:5 | LL | / foo.map_or(Err::( LL | | &format!( diff --git a/tests/ui/map_flatten_fixable.fixed b/tests/ui/map_flatten_fixable.fixed index 312819a0a2cf8..53628ef6531aa 100644 --- a/tests/ui/map_flatten_fixable.fixed +++ b/tests/ui/map_flatten_fixable.fixed @@ -1,7 +1,6 @@ // run-rustfix #![warn(clippy::all, clippy::pedantic)] -#![allow(clippy::let_underscore_drop)] #![allow(clippy::missing_docs_in_private_items)] #![allow(clippy::map_identity)] #![allow(clippy::redundant_closure)] diff --git a/tests/ui/map_flatten_fixable.rs b/tests/ui/map_flatten_fixable.rs index 3fbf4f9a1b044..76016c8ed3cd1 100644 --- a/tests/ui/map_flatten_fixable.rs +++ b/tests/ui/map_flatten_fixable.rs @@ -1,7 +1,6 @@ // run-rustfix #![warn(clippy::all, clippy::pedantic)] -#![allow(clippy::let_underscore_drop)] #![allow(clippy::missing_docs_in_private_items)] #![allow(clippy::map_identity)] #![allow(clippy::redundant_closure)] diff --git a/tests/ui/map_flatten_fixable.stderr b/tests/ui/map_flatten_fixable.stderr index c91f0b9ae94fe..b6b0c4d09c37b 100644 --- a/tests/ui/map_flatten_fixable.stderr +++ b/tests/ui/map_flatten_fixable.stderr @@ -1,5 +1,5 @@ error: called `map(..).flatten()` on `Iterator` - --> $DIR/map_flatten_fixable.rs:18:47 + --> $DIR/map_flatten_fixable.rs:17:47 | LL | let _: Vec<_> = vec![5_i8; 6].into_iter().map(option_id).flatten().collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `filter_map` and remove the `.flatten()`: `filter_map(option_id)` @@ -7,43 +7,43 @@ LL | let _: Vec<_> = vec![5_i8; 6].into_iter().map(option_id).flatten().coll = note: `-D clippy::map-flatten` implied by `-D warnings` error: called `map(..).flatten()` on `Iterator` - --> $DIR/map_flatten_fixable.rs:19:47 + --> $DIR/map_flatten_fixable.rs:18:47 | LL | let _: Vec<_> = vec![5_i8; 6].into_iter().map(option_id_ref).flatten().collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `filter_map` and remove the `.flatten()`: `filter_map(option_id_ref)` error: called `map(..).flatten()` on `Iterator` - --> $DIR/map_flatten_fixable.rs:20:47 + --> $DIR/map_flatten_fixable.rs:19:47 | LL | let _: Vec<_> = vec![5_i8; 6].into_iter().map(option_id_closure).flatten().collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `filter_map` and remove the `.flatten()`: `filter_map(option_id_closure)` error: called `map(..).flatten()` on `Iterator` - --> $DIR/map_flatten_fixable.rs:21:47 + --> $DIR/map_flatten_fixable.rs:20:47 | LL | let _: Vec<_> = vec![5_i8; 6].into_iter().map(|x| x.checked_add(1)).flatten().collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `filter_map` and remove the `.flatten()`: `filter_map(|x| x.checked_add(1))` error: called `map(..).flatten()` on `Iterator` - --> $DIR/map_flatten_fixable.rs:24:47 + --> $DIR/map_flatten_fixable.rs:23:47 | LL | let _: Vec<_> = vec![5_i8; 6].into_iter().map(|x| 0..x).flatten().collect(); | ^^^^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `flat_map` and remove the `.flatten()`: `flat_map(|x| 0..x)` error: called `map(..).flatten()` on `Option` - --> $DIR/map_flatten_fixable.rs:27:40 + --> $DIR/map_flatten_fixable.rs:26:40 | LL | let _: Option<_> = (Some(Some(1))).map(|x| x).flatten(); | ^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `and_then` and remove the `.flatten()`: `and_then(|x| x)` error: called `map(..).flatten()` on `Result` - --> $DIR/map_flatten_fixable.rs:30:42 + --> $DIR/map_flatten_fixable.rs:29:42 | LL | let _: Result<_, &str> = (Ok(Ok(1))).map(|x| x).flatten(); | ^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `and_then` and remove the `.flatten()`: `and_then(|x| x)` error: called `map(..).flatten()` on `Iterator` - --> $DIR/map_flatten_fixable.rs:39:10 + --> $DIR/map_flatten_fixable.rs:38:10 | LL | .map(|n| match n { | __________^ @@ -72,7 +72,7 @@ LL ~ }); | error: called `map(..).flatten()` on `Option` - --> $DIR/map_flatten_fixable.rs:59:10 + --> $DIR/map_flatten_fixable.rs:58:10 | LL | .map(|_| { | __________^ diff --git a/tests/ui/match_expr_like_matches_macro.fixed b/tests/ui/match_expr_like_matches_macro.fixed index 2498007694c56..968f462f8a029 100644 --- a/tests/ui/match_expr_like_matches_macro.fixed +++ b/tests/ui/match_expr_like_matches_macro.fixed @@ -2,7 +2,12 @@ #![feature(custom_inner_attributes)] #![warn(clippy::match_like_matches_macro)] -#![allow(unreachable_patterns, dead_code, clippy::equatable_if_let)] +#![allow( + unreachable_patterns, + dead_code, + clippy::equatable_if_let, + clippy::needless_borrowed_reference +)] fn main() { let x = Some(5); diff --git a/tests/ui/match_expr_like_matches_macro.rs b/tests/ui/match_expr_like_matches_macro.rs index b4e48499bd0fb..c6b479e27c5ac 100644 --- a/tests/ui/match_expr_like_matches_macro.rs +++ b/tests/ui/match_expr_like_matches_macro.rs @@ -2,7 +2,12 @@ #![feature(custom_inner_attributes)] #![warn(clippy::match_like_matches_macro)] -#![allow(unreachable_patterns, dead_code, clippy::equatable_if_let)] +#![allow( + unreachable_patterns, + dead_code, + clippy::equatable_if_let, + clippy::needless_borrowed_reference +)] fn main() { let x = Some(5); diff --git a/tests/ui/match_expr_like_matches_macro.stderr b/tests/ui/match_expr_like_matches_macro.stderr index f1d1c23aeb0de..a4df8008ac239 100644 --- a/tests/ui/match_expr_like_matches_macro.stderr +++ b/tests/ui/match_expr_like_matches_macro.stderr @@ -1,5 +1,5 @@ error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:11:14 + --> $DIR/match_expr_like_matches_macro.rs:16:14 | LL | let _y = match x { | ______________^ @@ -11,7 +11,7 @@ LL | | }; = note: `-D clippy::match-like-matches-macro` implied by `-D warnings` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:17:14 + --> $DIR/match_expr_like_matches_macro.rs:22:14 | LL | let _w = match x { | ______________^ @@ -21,7 +21,7 @@ LL | | }; | |_____^ help: try this: `matches!(x, Some(_))` error: redundant pattern matching, consider using `is_none()` - --> $DIR/match_expr_like_matches_macro.rs:23:14 + --> $DIR/match_expr_like_matches_macro.rs:28:14 | LL | let _z = match x { | ______________^ @@ -33,7 +33,7 @@ LL | | }; = note: `-D clippy::redundant-pattern-matching` implied by `-D warnings` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:29:15 + --> $DIR/match_expr_like_matches_macro.rs:34:15 | LL | let _zz = match x { | _______________^ @@ -43,13 +43,13 @@ LL | | }; | |_____^ help: try this: `!matches!(x, Some(r) if r == 0)` error: if let .. else expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:35:16 + --> $DIR/match_expr_like_matches_macro.rs:40:16 | LL | let _zzz = if let Some(5) = x { true } else { false }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `matches!(x, Some(5))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:59:20 + --> $DIR/match_expr_like_matches_macro.rs:64:20 | LL | let _ans = match x { | ____________________^ @@ -60,7 +60,7 @@ LL | | }; | |_________^ help: try this: `matches!(x, E::A(_) | E::B(_))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:69:20 + --> $DIR/match_expr_like_matches_macro.rs:74:20 | LL | let _ans = match x { | ____________________^ @@ -73,7 +73,7 @@ LL | | }; | |_________^ help: try this: `matches!(x, E::A(_) | E::B(_))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:79:20 + --> $DIR/match_expr_like_matches_macro.rs:84:20 | LL | let _ans = match x { | ____________________^ @@ -84,7 +84,7 @@ LL | | }; | |_________^ help: try this: `!matches!(x, E::B(_) | E::C)` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:139:18 + --> $DIR/match_expr_like_matches_macro.rs:144:18 | LL | let _z = match &z { | __________________^ @@ -94,7 +94,7 @@ LL | | }; | |_________^ help: try this: `matches!(z, Some(3))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:148:18 + --> $DIR/match_expr_like_matches_macro.rs:153:18 | LL | let _z = match &z { | __________________^ @@ -104,7 +104,7 @@ LL | | }; | |_________^ help: try this: `matches!(&z, Some(3))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:165:21 + --> $DIR/match_expr_like_matches_macro.rs:170:21 | LL | let _ = match &z { | _____________________^ @@ -114,7 +114,7 @@ LL | | }; | |_____________^ help: try this: `matches!(&z, AnEnum::X)` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:179:20 + --> $DIR/match_expr_like_matches_macro.rs:184:20 | LL | let _res = match &val { | ____________________^ @@ -124,7 +124,7 @@ LL | | }; | |_________^ help: try this: `matches!(&val, &Some(ref _a))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:191:20 + --> $DIR/match_expr_like_matches_macro.rs:196:20 | LL | let _res = match &val { | ____________________^ @@ -134,7 +134,7 @@ LL | | }; | |_________^ help: try this: `matches!(&val, &Some(ref _a))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:251:14 + --> $DIR/match_expr_like_matches_macro.rs:256:14 | LL | let _y = match Some(5) { | ______________^ diff --git a/tests/ui/missing_panics_doc.stderr b/tests/ui/missing_panics_doc.stderr index c9ded7f1ad03e..183c262ce0b51 100644 --- a/tests/ui/missing_panics_doc.stderr +++ b/tests/ui/missing_panics_doc.stderr @@ -1,11 +1,8 @@ error: docs for function which may panic missing `# Panics` section --> $DIR/missing_panics_doc.rs:6:1 | -LL | / pub fn unwrap() { -LL | | let result = Err("Hi"); -LL | | result.unwrap() -LL | | } - | |_^ +LL | pub fn unwrap() { + | ^^^^^^^^^^^^^^^ | note: first possible panic found here --> $DIR/missing_panics_doc.rs:8:5 @@ -17,10 +14,8 @@ LL | result.unwrap() error: docs for function which may panic missing `# Panics` section --> $DIR/missing_panics_doc.rs:12:1 | -LL | / pub fn panic() { -LL | | panic!("This function panics") -LL | | } - | |_^ +LL | pub fn panic() { + | ^^^^^^^^^^^^^^ | note: first possible panic found here --> $DIR/missing_panics_doc.rs:13:5 @@ -31,10 +26,8 @@ LL | panic!("This function panics") error: docs for function which may panic missing `# Panics` section --> $DIR/missing_panics_doc.rs:17:1 | -LL | / pub fn todo() { -LL | | todo!() -LL | | } - | |_^ +LL | pub fn todo() { + | ^^^^^^^^^^^^^ | note: first possible panic found here --> $DIR/missing_panics_doc.rs:18:5 @@ -45,14 +38,8 @@ LL | todo!() error: docs for function which may panic missing `# Panics` section --> $DIR/missing_panics_doc.rs:22:1 | -LL | / pub fn inner_body(opt: Option) { -LL | | opt.map(|x| { -LL | | if x == 10 { -LL | | panic!() -LL | | } -LL | | }); -LL | | } - | |_^ +LL | pub fn inner_body(opt: Option) { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: first possible panic found here --> $DIR/missing_panics_doc.rs:25:13 @@ -63,10 +50,8 @@ LL | panic!() error: docs for function which may panic missing `# Panics` section --> $DIR/missing_panics_doc.rs:31:1 | -LL | / pub fn unreachable_and_panic() { -LL | | if true { unreachable!() } else { panic!() } -LL | | } - | |_^ +LL | pub fn unreachable_and_panic() { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: first possible panic found here --> $DIR/missing_panics_doc.rs:32:39 @@ -77,11 +62,8 @@ LL | if true { unreachable!() } else { panic!() } error: docs for function which may panic missing `# Panics` section --> $DIR/missing_panics_doc.rs:36:1 | -LL | / pub fn assert_eq() { -LL | | let x = 0; -LL | | assert_eq!(x, 0); -LL | | } - | |_^ +LL | pub fn assert_eq() { + | ^^^^^^^^^^^^^^^^^^ | note: first possible panic found here --> $DIR/missing_panics_doc.rs:38:5 @@ -92,11 +74,8 @@ LL | assert_eq!(x, 0); error: docs for function which may panic missing `# Panics` section --> $DIR/missing_panics_doc.rs:42:1 | -LL | / pub fn assert_ne() { -LL | | let x = 0; -LL | | assert_ne!(x, 0); -LL | | } - | |_^ +LL | pub fn assert_ne() { + | ^^^^^^^^^^^^^^^^^^ | note: first possible panic found here --> $DIR/missing_panics_doc.rs:44:5 diff --git a/tests/ui/mut_from_ref.rs b/tests/ui/mut_from_ref.rs index 370dbd5882161..7de1533059470 100644 --- a/tests/ui/mut_from_ref.rs +++ b/tests/ui/mut_from_ref.rs @@ -1,4 +1,4 @@ -#![allow(unused)] +#![allow(unused, clippy::needless_lifetimes)] #![warn(clippy::mut_from_ref)] struct Foo; diff --git a/tests/ui/mut_mut.rs b/tests/ui/mut_mut.rs index ac8fd9d8fb093..ee3a856566cc1 100644 --- a/tests/ui/mut_mut.rs +++ b/tests/ui/mut_mut.rs @@ -57,3 +57,20 @@ fn issue6922() { // do not lint from an external macro mut_mut!(); } + +mod issue9035 { + use std::fmt::Display; + + struct Foo<'a> { + inner: &'a mut dyn Display, + } + + impl Foo<'_> { + fn foo(&mut self) { + let hlp = &mut self.inner; + bar(hlp); + } + } + + fn bar(_: &mut impl Display) {} +} diff --git a/tests/ui/mut_range_bound.rs b/tests/ui/mut_range_bound.rs index e1ae1ef928223..7fdeb27ed988f 100644 --- a/tests/ui/mut_range_bound.rs +++ b/tests/ui/mut_range_bound.rs @@ -76,7 +76,7 @@ fn mut_range_bound_no_immediate_break() { let mut n = 3; for i in n..10 { if n == 4 { - n = 1; // FIXME: warning because is is not immediately followed by break + n = 1; // FIXME: warning because it is not immediately followed by break let _ = 2; break; } diff --git a/tests/ui/mut_range_bound.stderr b/tests/ui/mut_range_bound.stderr index e0c8dced382ef..b679b7a0aaf82 100644 --- a/tests/ui/mut_range_bound.stderr +++ b/tests/ui/mut_range_bound.stderr @@ -50,7 +50,7 @@ LL | m = 2; // warning because it is not immediately followed by break error: attempt to mutate range bound within loop --> $DIR/mut_range_bound.rs:79:13 | -LL | n = 1; // FIXME: warning because is is not immediately followed by break +LL | n = 1; // FIXME: warning because it is not immediately followed by break | ^ | = note: the range of the loop is unchanged diff --git a/tests/ui/needless_borrow.fixed b/tests/ui/needless_borrow.fixed index 340e89d2db1d2..85b6b639d5549 100644 --- a/tests/ui/needless_borrow.fixed +++ b/tests/ui/needless_borrow.fixed @@ -385,3 +385,128 @@ mod used_more_than_once { fn use_x(_: impl AsRef) {} fn use_x_again(_: impl AsRef) {} } + +// https://github.com/rust-lang/rust-clippy/issues/9111#issuecomment-1277114280 +#[allow(dead_code)] +mod issue_9111 { + struct A; + + impl Extend for A { + fn extend>(&mut self, _: T) { + unimplemented!() + } + } + + impl<'a> Extend<&'a u8> for A { + fn extend>(&mut self, _: T) { + unimplemented!() + } + } + + fn main() { + let mut a = A; + a.extend(&[]); // vs a.extend([]); + } +} + +#[allow(dead_code)] +mod issue_9710 { + fn main() { + let string = String::new(); + for _i in 0..10 { + f(&string); + } + } + + fn f>(_: T) {} +} + +#[allow(dead_code)] +mod issue_9739 { + fn foo(_it: impl IntoIterator) {} + + fn main() { + foo(if std::env::var_os("HI").is_some() { + &[0] + } else { + &[] as &[u32] + }); + } +} + +#[allow(dead_code)] +mod issue_9739_method_variant { + struct S; + + impl S { + fn foo(&self, _it: impl IntoIterator) {} + } + + fn main() { + S.foo(if std::env::var_os("HI").is_some() { + &[0] + } else { + &[] as &[u32] + }); + } +} + +#[allow(dead_code)] +mod issue_9782 { + fn foo>(t: T) { + println!("{}", std::mem::size_of::()); + let _t: &[u8] = t.as_ref(); + } + + fn main() { + let a: [u8; 100] = [0u8; 100]; + + // 100 + foo::<[u8; 100]>(a); + foo(a); + + // 16 + foo::<&[u8]>(&a); + foo(a.as_slice()); + + // 8 + foo::<&[u8; 100]>(&a); + foo(a); + } +} + +#[allow(dead_code)] +mod issue_9782_type_relative_variant { + struct S; + + impl S { + fn foo>(t: T) { + println!("{}", std::mem::size_of::()); + let _t: &[u8] = t.as_ref(); + } + } + + fn main() { + let a: [u8; 100] = [0u8; 100]; + + S::foo::<&[u8; 100]>(&a); + } +} + +#[allow(dead_code)] +mod issue_9782_method_variant { + struct S; + + impl S { + fn foo>(&self, t: T) { + println!("{}", std::mem::size_of::()); + let _t: &[u8] = t.as_ref(); + } + } + + fn main() { + let a: [u8; 100] = [0u8; 100]; + + S.foo::<&[u8; 100]>(&a); + } +} diff --git a/tests/ui/needless_borrow.rs b/tests/ui/needless_borrow.rs index c93711ac8e284..7b97bcf3817ec 100644 --- a/tests/ui/needless_borrow.rs +++ b/tests/ui/needless_borrow.rs @@ -385,3 +385,128 @@ mod used_more_than_once { fn use_x(_: impl AsRef) {} fn use_x_again(_: impl AsRef) {} } + +// https://github.com/rust-lang/rust-clippy/issues/9111#issuecomment-1277114280 +#[allow(dead_code)] +mod issue_9111 { + struct A; + + impl Extend for A { + fn extend>(&mut self, _: T) { + unimplemented!() + } + } + + impl<'a> Extend<&'a u8> for A { + fn extend>(&mut self, _: T) { + unimplemented!() + } + } + + fn main() { + let mut a = A; + a.extend(&[]); // vs a.extend([]); + } +} + +#[allow(dead_code)] +mod issue_9710 { + fn main() { + let string = String::new(); + for _i in 0..10 { + f(&string); + } + } + + fn f>(_: T) {} +} + +#[allow(dead_code)] +mod issue_9739 { + fn foo(_it: impl IntoIterator) {} + + fn main() { + foo(if std::env::var_os("HI").is_some() { + &[0] + } else { + &[] as &[u32] + }); + } +} + +#[allow(dead_code)] +mod issue_9739_method_variant { + struct S; + + impl S { + fn foo(&self, _it: impl IntoIterator) {} + } + + fn main() { + S.foo(if std::env::var_os("HI").is_some() { + &[0] + } else { + &[] as &[u32] + }); + } +} + +#[allow(dead_code)] +mod issue_9782 { + fn foo>(t: T) { + println!("{}", std::mem::size_of::()); + let _t: &[u8] = t.as_ref(); + } + + fn main() { + let a: [u8; 100] = [0u8; 100]; + + // 100 + foo::<[u8; 100]>(a); + foo(a); + + // 16 + foo::<&[u8]>(&a); + foo(a.as_slice()); + + // 8 + foo::<&[u8; 100]>(&a); + foo(&a); + } +} + +#[allow(dead_code)] +mod issue_9782_type_relative_variant { + struct S; + + impl S { + fn foo>(t: T) { + println!("{}", std::mem::size_of::()); + let _t: &[u8] = t.as_ref(); + } + } + + fn main() { + let a: [u8; 100] = [0u8; 100]; + + S::foo::<&[u8; 100]>(&a); + } +} + +#[allow(dead_code)] +mod issue_9782_method_variant { + struct S; + + impl S { + fn foo>(&self, t: T) { + println!("{}", std::mem::size_of::()); + let _t: &[u8] = t.as_ref(); + } + } + + fn main() { + let a: [u8; 100] = [0u8; 100]; + + S.foo::<&[u8; 100]>(&a); + } +} diff --git a/tests/ui/needless_borrow.stderr b/tests/ui/needless_borrow.stderr index 8b593268bec21..485e6b84c868b 100644 --- a/tests/ui/needless_borrow.stderr +++ b/tests/ui/needless_borrow.stderr @@ -210,5 +210,11 @@ error: the borrowed expression implements the required traits LL | use_x(&x); | ^^ help: change this to: `x` -error: aborting due to 35 previous errors +error: the borrowed expression implements the required traits + --> $DIR/needless_borrow.rs:474:13 + | +LL | foo(&a); + | ^^ help: change this to: `a` + +error: aborting due to 36 previous errors diff --git a/tests/ui/needless_borrowed_ref.fixed b/tests/ui/needless_borrowed_ref.fixed index bcb4eb2dd48a6..0c47ceb7b6791 100644 --- a/tests/ui/needless_borrowed_ref.fixed +++ b/tests/ui/needless_borrowed_ref.fixed @@ -1,11 +1,32 @@ // run-rustfix #![warn(clippy::needless_borrowed_reference)] -#![allow(unused, clippy::needless_borrow)] +#![allow( + unused, + irrefutable_let_patterns, + non_shorthand_field_patterns, + clippy::needless_borrow +)] fn main() {} -fn should_lint(array: [u8; 4], slice: &[u8], slice_of_refs: &[&u8], vec: Vec) { +struct Struct { + a: usize, + b: usize, + c: usize, +} + +struct TupleStruct(u8, u8, u8); + +fn should_lint( + array: [u8; 4], + slice: &[u8], + slice_of_refs: &[&u8], + vec: Vec, + tuple: (u8, u8, u8), + tuple_struct: TupleStruct, + s: Struct, +) { let mut v = Vec::::new(); let _ = v.iter_mut().filter(|a| a.is_empty()); @@ -24,16 +45,54 @@ fn should_lint(array: [u8; 4], slice: &[u8], slice_of_refs: &[&u8], vec: Vec if let [a, b, ..] = slice {} if let [a, .., b] = slice {} if let [.., a, b] = slice {} + + if let [a, _] = slice {} + + if let (a, b, c) = &tuple {} + if let (a, _, c) = &tuple {} + if let (a, ..) = &tuple {} + + if let TupleStruct(a, ..) = &tuple_struct {} + + if let Struct { + a, + b: b, + c: renamed, + } = &s + {} + + if let Struct { a, b: _, .. } = &s {} } -fn should_not_lint(array: [u8; 4], slice: &[u8], slice_of_refs: &[&u8], vec: Vec) { +fn should_not_lint( + array: [u8; 4], + slice: &[u8], + slice_of_refs: &[&u8], + vec: Vec, + tuple: (u8, u8, u8), + tuple_struct: TupleStruct, + s: Struct, +) { if let [ref a] = slice {} if let &[ref a, b] = slice {} if let &[ref a, .., b] = slice {} + if let &(ref a, b, ..) = &tuple {} + if let &TupleStruct(ref a, b, ..) = &tuple_struct {} + if let &Struct { ref a, b, .. } = &s {} + // must not be removed as variables must be bound consistently across | patterns if let (&[ref a], _) | ([], ref a) = (slice_of_refs, &1u8) {} + // the `&`s here technically could be removed, but it'd be noisy and without a `ref` doesn't match + // the lint name + if let &[] = slice {} + if let &[_] = slice {} + if let &[..] = slice {} + if let &(..) = &tuple {} + if let &TupleStruct(..) = &tuple_struct {} + if let &Struct { .. } = &s {} + let mut var2 = 5; let thingy2 = Some(&mut var2); if let Some(&mut ref mut v) = thingy2 { @@ -59,6 +118,6 @@ fn foo(a: &Animal, b: &Animal) { // lifetime mismatch error if there is no '&ref' before `feature(nll)` stabilization in 1.63 (&Animal::Cat(v), &ref k) | (&ref k, &Animal::Cat(v)) => (), // ^ and ^ should **not** be linted - (&Animal::Dog(ref a), &Animal::Dog(_)) => (), // ^ should **not** be linted + (Animal::Dog(a), &Animal::Dog(_)) => (), } } diff --git a/tests/ui/needless_borrowed_ref.rs b/tests/ui/needless_borrowed_ref.rs index f6de1a6d83d1b..f883bb0c88917 100644 --- a/tests/ui/needless_borrowed_ref.rs +++ b/tests/ui/needless_borrowed_ref.rs @@ -1,11 +1,32 @@ // run-rustfix #![warn(clippy::needless_borrowed_reference)] -#![allow(unused, clippy::needless_borrow)] +#![allow( + unused, + irrefutable_let_patterns, + non_shorthand_field_patterns, + clippy::needless_borrow +)] fn main() {} -fn should_lint(array: [u8; 4], slice: &[u8], slice_of_refs: &[&u8], vec: Vec) { +struct Struct { + a: usize, + b: usize, + c: usize, +} + +struct TupleStruct(u8, u8, u8); + +fn should_lint( + array: [u8; 4], + slice: &[u8], + slice_of_refs: &[&u8], + vec: Vec, + tuple: (u8, u8, u8), + tuple_struct: TupleStruct, + s: Struct, +) { let mut v = Vec::::new(); let _ = v.iter_mut().filter(|&ref a| a.is_empty()); @@ -24,16 +45,54 @@ fn should_lint(array: [u8; 4], slice: &[u8], slice_of_refs: &[&u8], vec: Vec if let &[ref a, ref b, ..] = slice {} if let &[ref a, .., ref b] = slice {} if let &[.., ref a, ref b] = slice {} + + if let &[ref a, _] = slice {} + + if let &(ref a, ref b, ref c) = &tuple {} + if let &(ref a, _, ref c) = &tuple {} + if let &(ref a, ..) = &tuple {} + + if let &TupleStruct(ref a, ..) = &tuple_struct {} + + if let &Struct { + ref a, + b: ref b, + c: ref renamed, + } = &s + {} + + if let &Struct { ref a, b: _, .. } = &s {} } -fn should_not_lint(array: [u8; 4], slice: &[u8], slice_of_refs: &[&u8], vec: Vec) { +fn should_not_lint( + array: [u8; 4], + slice: &[u8], + slice_of_refs: &[&u8], + vec: Vec, + tuple: (u8, u8, u8), + tuple_struct: TupleStruct, + s: Struct, +) { if let [ref a] = slice {} if let &[ref a, b] = slice {} if let &[ref a, .., b] = slice {} + if let &(ref a, b, ..) = &tuple {} + if let &TupleStruct(ref a, b, ..) = &tuple_struct {} + if let &Struct { ref a, b, .. } = &s {} + // must not be removed as variables must be bound consistently across | patterns if let (&[ref a], _) | ([], ref a) = (slice_of_refs, &1u8) {} + // the `&`s here technically could be removed, but it'd be noisy and without a `ref` doesn't match + // the lint name + if let &[] = slice {} + if let &[_] = slice {} + if let &[..] = slice {} + if let &(..) = &tuple {} + if let &TupleStruct(..) = &tuple_struct {} + if let &Struct { .. } = &s {} + let mut var2 = 5; let thingy2 = Some(&mut var2); if let Some(&mut ref mut v) = thingy2 { @@ -59,6 +118,6 @@ fn foo(a: &Animal, b: &Animal) { // lifetime mismatch error if there is no '&ref' before `feature(nll)` stabilization in 1.63 (&Animal::Cat(v), &ref k) | (&ref k, &Animal::Cat(v)) => (), // ^ and ^ should **not** be linted - (&Animal::Dog(ref a), &Animal::Dog(_)) => (), // ^ should **not** be linted + (Animal::Dog(a), &Animal::Dog(_)) => (), } } diff --git a/tests/ui/needless_borrowed_ref.stderr b/tests/ui/needless_borrowed_ref.stderr index 7453542e673f3..8d0f0c258dd26 100644 --- a/tests/ui/needless_borrowed_ref.stderr +++ b/tests/ui/needless_borrowed_ref.stderr @@ -1,5 +1,5 @@ error: this pattern takes a reference on something that is being dereferenced - --> $DIR/needless_borrowed_ref.rs:10:34 + --> $DIR/needless_borrowed_ref.rs:31:34 | LL | let _ = v.iter_mut().filter(|&ref a| a.is_empty()); | ^^^^^^ @@ -12,7 +12,7 @@ LL + let _ = v.iter_mut().filter(|a| a.is_empty()); | error: this pattern takes a reference on something that is being dereferenced - --> $DIR/needless_borrowed_ref.rs:14:17 + --> $DIR/needless_borrowed_ref.rs:35:17 | LL | if let Some(&ref v) = thingy {} | ^^^^^^ @@ -24,7 +24,7 @@ LL + if let Some(v) = thingy {} | error: this pattern takes a reference on something that is being dereferenced - --> $DIR/needless_borrowed_ref.rs:16:14 + --> $DIR/needless_borrowed_ref.rs:37:14 | LL | if let &[&ref a, ref b] = slice_of_refs {} | ^^^^^^ @@ -36,7 +36,7 @@ LL + if let &[a, ref b] = slice_of_refs {} | error: dereferencing a slice pattern where every element takes a reference - --> $DIR/needless_borrowed_ref.rs:18:9 + --> $DIR/needless_borrowed_ref.rs:39:9 | LL | let &[ref a, ..] = &array; | ^^^^^^^^^^^^ @@ -48,7 +48,7 @@ LL + let [a, ..] = &array; | error: dereferencing a slice pattern where every element takes a reference - --> $DIR/needless_borrowed_ref.rs:19:9 + --> $DIR/needless_borrowed_ref.rs:40:9 | LL | let &[ref a, ref b, ..] = &array; | ^^^^^^^^^^^^^^^^^^^ @@ -60,7 +60,7 @@ LL + let [a, b, ..] = &array; | error: dereferencing a slice pattern where every element takes a reference - --> $DIR/needless_borrowed_ref.rs:21:12 + --> $DIR/needless_borrowed_ref.rs:42:12 | LL | if let &[ref a, ref b] = slice {} | ^^^^^^^^^^^^^^^ @@ -72,7 +72,7 @@ LL + if let [a, b] = slice {} | error: dereferencing a slice pattern where every element takes a reference - --> $DIR/needless_borrowed_ref.rs:22:12 + --> $DIR/needless_borrowed_ref.rs:43:12 | LL | if let &[ref a, ref b] = &vec[..] {} | ^^^^^^^^^^^^^^^ @@ -84,7 +84,7 @@ LL + if let [a, b] = &vec[..] {} | error: dereferencing a slice pattern where every element takes a reference - --> $DIR/needless_borrowed_ref.rs:24:12 + --> $DIR/needless_borrowed_ref.rs:45:12 | LL | if let &[ref a, ref b, ..] = slice {} | ^^^^^^^^^^^^^^^^^^^ @@ -96,7 +96,7 @@ LL + if let [a, b, ..] = slice {} | error: dereferencing a slice pattern where every element takes a reference - --> $DIR/needless_borrowed_ref.rs:25:12 + --> $DIR/needless_borrowed_ref.rs:46:12 | LL | if let &[ref a, .., ref b] = slice {} | ^^^^^^^^^^^^^^^^^^^ @@ -108,7 +108,7 @@ LL + if let [a, .., b] = slice {} | error: dereferencing a slice pattern where every element takes a reference - --> $DIR/needless_borrowed_ref.rs:26:12 + --> $DIR/needless_borrowed_ref.rs:47:12 | LL | if let &[.., ref a, ref b] = slice {} | ^^^^^^^^^^^^^^^^^^^ @@ -119,5 +119,96 @@ LL - if let &[.., ref a, ref b] = slice {} LL + if let [.., a, b] = slice {} | -error: aborting due to 10 previous errors +error: dereferencing a slice pattern where every element takes a reference + --> $DIR/needless_borrowed_ref.rs:49:12 + | +LL | if let &[ref a, _] = slice {} + | ^^^^^^^^^^^ + | +help: try removing the `&` and `ref` parts + | +LL - if let &[ref a, _] = slice {} +LL + if let [a, _] = slice {} + | + +error: dereferencing a tuple pattern where every element takes a reference + --> $DIR/needless_borrowed_ref.rs:51:12 + | +LL | if let &(ref a, ref b, ref c) = &tuple {} + | ^^^^^^^^^^^^^^^^^^^^^^ + | +help: try removing the `&` and `ref` parts + | +LL - if let &(ref a, ref b, ref c) = &tuple {} +LL + if let (a, b, c) = &tuple {} + | + +error: dereferencing a tuple pattern where every element takes a reference + --> $DIR/needless_borrowed_ref.rs:52:12 + | +LL | if let &(ref a, _, ref c) = &tuple {} + | ^^^^^^^^^^^^^^^^^^ + | +help: try removing the `&` and `ref` parts + | +LL - if let &(ref a, _, ref c) = &tuple {} +LL + if let (a, _, c) = &tuple {} + | + +error: dereferencing a tuple pattern where every element takes a reference + --> $DIR/needless_borrowed_ref.rs:53:12 + | +LL | if let &(ref a, ..) = &tuple {} + | ^^^^^^^^^^^^ + | +help: try removing the `&` and `ref` parts + | +LL - if let &(ref a, ..) = &tuple {} +LL + if let (a, ..) = &tuple {} + | + +error: dereferencing a tuple pattern where every element takes a reference + --> $DIR/needless_borrowed_ref.rs:55:12 + | +LL | if let &TupleStruct(ref a, ..) = &tuple_struct {} + | ^^^^^^^^^^^^^^^^^^^^^^^ + | +help: try removing the `&` and `ref` parts + | +LL - if let &TupleStruct(ref a, ..) = &tuple_struct {} +LL + if let TupleStruct(a, ..) = &tuple_struct {} + | + +error: dereferencing a struct pattern where every field's pattern takes a reference + --> $DIR/needless_borrowed_ref.rs:57:12 + | +LL | if let &Struct { + | ____________^ +LL | | ref a, +LL | | b: ref b, +LL | | c: ref renamed, +LL | | } = &s + | |_____^ + | +help: try removing the `&` and `ref` parts + | +LL ~ if let Struct { +LL ~ a, +LL ~ b: b, +LL ~ c: renamed, + | + +error: dereferencing a struct pattern where every field's pattern takes a reference + --> $DIR/needless_borrowed_ref.rs:64:12 + | +LL | if let &Struct { ref a, b: _, .. } = &s {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: try removing the `&` and `ref` parts + | +LL - if let &Struct { ref a, b: _, .. } = &s {} +LL + if let Struct { a, b: _, .. } = &s {} + | + +error: aborting due to 17 previous errors diff --git a/tests/ui/needless_collect.fixed b/tests/ui/needless_collect.fixed index 6ecbbcb624955..2659ad384885e 100644 --- a/tests/ui/needless_collect.fixed +++ b/tests/ui/needless_collect.fixed @@ -33,4 +33,33 @@ fn main() { // `BinaryHeap` doesn't have `contains` method sample.iter().count(); sample.iter().next().is_none(); + + // Don't lint string from str + let _ = ["", ""].into_iter().collect::().is_empty(); + + let _ = sample.iter().next().is_none(); + let _ = sample.iter().any(|x| x == &0); + + struct VecWrapper(Vec); + impl core::ops::Deref for VecWrapper { + type Target = Vec; + fn deref(&self) -> &Self::Target { + &self.0 + } + } + impl IntoIterator for VecWrapper { + type IntoIter = as IntoIterator>::IntoIter; + type Item = as IntoIterator>::Item; + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } + } + impl FromIterator for VecWrapper { + fn from_iter>(iter: I) -> Self { + Self(Vec::from_iter(iter)) + } + } + + let _ = sample.iter().next().is_none(); + let _ = sample.iter().any(|x| x == &0); } diff --git a/tests/ui/needless_collect.rs b/tests/ui/needless_collect.rs index 8dc69bcf5b38d..535ec82982b13 100644 --- a/tests/ui/needless_collect.rs +++ b/tests/ui/needless_collect.rs @@ -33,4 +33,33 @@ fn main() { // `BinaryHeap` doesn't have `contains` method sample.iter().collect::>().len(); sample.iter().collect::>().is_empty(); + + // Don't lint string from str + let _ = ["", ""].into_iter().collect::().is_empty(); + + let _ = sample.iter().collect::>().is_empty(); + let _ = sample.iter().collect::>().contains(&&0); + + struct VecWrapper(Vec); + impl core::ops::Deref for VecWrapper { + type Target = Vec; + fn deref(&self) -> &Self::Target { + &self.0 + } + } + impl IntoIterator for VecWrapper { + type IntoIter = as IntoIterator>::IntoIter; + type Item = as IntoIterator>::Item; + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } + } + impl FromIterator for VecWrapper { + fn from_iter>(iter: I) -> Self { + Self(Vec::from_iter(iter)) + } + } + + let _ = sample.iter().collect::>().is_empty(); + let _ = sample.iter().collect::>().contains(&&0); } diff --git a/tests/ui/needless_collect.stderr b/tests/ui/needless_collect.stderr index 039091627a8d6..584d2a1d8356f 100644 --- a/tests/ui/needless_collect.stderr +++ b/tests/ui/needless_collect.stderr @@ -66,5 +66,29 @@ error: avoid using `collect()` when not needed LL | sample.iter().collect::>().is_empty(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()` -error: aborting due to 11 previous errors +error: avoid using `collect()` when not needed + --> $DIR/needless_collect.rs:40:27 + | +LL | let _ = sample.iter().collect::>().is_empty(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()` + +error: avoid using `collect()` when not needed + --> $DIR/needless_collect.rs:41:27 + | +LL | let _ = sample.iter().collect::>().contains(&&0); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `any(|x| x == &0)` + +error: avoid using `collect()` when not needed + --> $DIR/needless_collect.rs:63:27 + | +LL | let _ = sample.iter().collect::>().is_empty(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()` + +error: avoid using `collect()` when not needed + --> $DIR/needless_collect.rs:64:27 + | +LL | let _ = sample.iter().collect::>().contains(&&0); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `any(|x| x == &0)` + +error: aborting due to 15 previous errors diff --git a/tests/ui/needless_collect_indirect.rs b/tests/ui/needless_collect_indirect.rs index 6d213b46c20cb..fe4209e99b2f5 100644 --- a/tests/ui/needless_collect_indirect.rs +++ b/tests/ui/needless_collect_indirect.rs @@ -1,4 +1,5 @@ #![allow(clippy::uninlined_format_args)] +#![warn(clippy::needless_collect)] use std::collections::{BinaryHeap, HashMap, HashSet, LinkedList, VecDeque}; diff --git a/tests/ui/needless_collect_indirect.stderr b/tests/ui/needless_collect_indirect.stderr index 99e1b91d8fea2..790d725907f32 100644 --- a/tests/ui/needless_collect_indirect.stderr +++ b/tests/ui/needless_collect_indirect.stderr @@ -1,5 +1,5 @@ error: avoid using `collect()` when not needed - --> $DIR/needless_collect_indirect.rs:7:39 + --> $DIR/needless_collect_indirect.rs:8:39 | LL | let indirect_iter = sample.iter().collect::>(); | ^^^^^^^ @@ -14,7 +14,7 @@ LL ~ sample.iter().map(|x| (x, x + 1)).collect::>(); | error: avoid using `collect()` when not needed - --> $DIR/needless_collect_indirect.rs:9:38 + --> $DIR/needless_collect_indirect.rs:10:38 | LL | let indirect_len = sample.iter().collect::>(); | ^^^^^^^ @@ -28,7 +28,7 @@ LL ~ sample.iter().count(); | error: avoid using `collect()` when not needed - --> $DIR/needless_collect_indirect.rs:11:40 + --> $DIR/needless_collect_indirect.rs:12:40 | LL | let indirect_empty = sample.iter().collect::>(); | ^^^^^^^ @@ -42,7 +42,7 @@ LL ~ sample.iter().next().is_none(); | error: avoid using `collect()` when not needed - --> $DIR/needless_collect_indirect.rs:13:43 + --> $DIR/needless_collect_indirect.rs:14:43 | LL | let indirect_contains = sample.iter().collect::>(); | ^^^^^^^ @@ -56,7 +56,7 @@ LL ~ sample.iter().any(|x| x == &5); | error: avoid using `collect()` when not needed - --> $DIR/needless_collect_indirect.rs:25:48 + --> $DIR/needless_collect_indirect.rs:26:48 | LL | let non_copy_contains = sample.into_iter().collect::>(); | ^^^^^^^ @@ -70,7 +70,7 @@ LL ~ sample.into_iter().any(|x| x == a); | error: avoid using `collect()` when not needed - --> $DIR/needless_collect_indirect.rs:54:51 + --> $DIR/needless_collect_indirect.rs:55:51 | LL | let buffer: Vec<&str> = string.split('/').collect(); | ^^^^^^^ @@ -84,7 +84,7 @@ LL ~ string.split('/').count() | error: avoid using `collect()` when not needed - --> $DIR/needless_collect_indirect.rs:59:55 + --> $DIR/needless_collect_indirect.rs:60:55 | LL | let indirect_len: VecDeque<_> = sample.iter().collect(); | ^^^^^^^ @@ -98,7 +98,7 @@ LL ~ sample.iter().count() | error: avoid using `collect()` when not needed - --> $DIR/needless_collect_indirect.rs:64:57 + --> $DIR/needless_collect_indirect.rs:65:57 | LL | let indirect_len: LinkedList<_> = sample.iter().collect(); | ^^^^^^^ @@ -112,7 +112,7 @@ LL ~ sample.iter().count() | error: avoid using `collect()` when not needed - --> $DIR/needless_collect_indirect.rs:69:57 + --> $DIR/needless_collect_indirect.rs:70:57 | LL | let indirect_len: BinaryHeap<_> = sample.iter().collect(); | ^^^^^^^ @@ -126,7 +126,7 @@ LL ~ sample.iter().count() | error: avoid using `collect()` when not needed - --> $DIR/needless_collect_indirect.rs:129:59 + --> $DIR/needless_collect_indirect.rs:130:59 | LL | let y: Vec = vec.iter().map(|k| k * k).collect(); | ^^^^^^^ @@ -143,7 +143,7 @@ LL ~ vec.iter().map(|k| k * k).any(|x| x == i); | error: avoid using `collect()` when not needed - --> $DIR/needless_collect_indirect.rs:154:59 + --> $DIR/needless_collect_indirect.rs:155:59 | LL | let y: Vec = vec.iter().map(|k| k * k).collect(); | ^^^^^^^ @@ -160,7 +160,7 @@ LL ~ vec.iter().map(|k| k * k).any(|x| x == n); | error: avoid using `collect()` when not needed - --> $DIR/needless_collect_indirect.rs:183:63 + --> $DIR/needless_collect_indirect.rs:184:63 | LL | let y: Vec = vec.iter().map(|k| k * k).collect(); | ^^^^^^^ @@ -177,7 +177,7 @@ LL ~ vec.iter().map(|k| k * k).any(|x| x == n); | error: avoid using `collect()` when not needed - --> $DIR/needless_collect_indirect.rs:219:59 + --> $DIR/needless_collect_indirect.rs:220:59 | LL | let y: Vec = vec.iter().map(|k| k * k).collect(); | ^^^^^^^ @@ -195,7 +195,7 @@ LL ~ vec.iter().map(|k| k * k).any(|x| x == n); | error: avoid using `collect()` when not needed - --> $DIR/needless_collect_indirect.rs:244:26 + --> $DIR/needless_collect_indirect.rs:245:26 | LL | let w = v.iter().collect::>(); | ^^^^^^^ @@ -211,7 +211,7 @@ LL ~ for _ in 0..v.iter().count() { | error: avoid using `collect()` when not needed - --> $DIR/needless_collect_indirect.rs:266:30 + --> $DIR/needless_collect_indirect.rs:267:30 | LL | let mut w = v.iter().collect::>(); | ^^^^^^^ @@ -227,7 +227,7 @@ LL ~ while 1 == v.iter().count() { | error: avoid using `collect()` when not needed - --> $DIR/needless_collect_indirect.rs:288:30 + --> $DIR/needless_collect_indirect.rs:289:30 | LL | let mut w = v.iter().collect::>(); | ^^^^^^^ diff --git a/tests/ui/needless_lifetimes.rs b/tests/ui/needless_lifetimes.rs index fc686b1dac0e1..2efc936752ef9 100644 --- a/tests/ui/needless_lifetimes.rs +++ b/tests/ui/needless_lifetimes.rs @@ -29,11 +29,20 @@ fn multiple_in_and_out_1<'a>(x: &'a u8, _y: &'a u8) -> &'a u8 { x } -// No error; multiple input refs. -fn multiple_in_and_out_2<'a, 'b>(x: &'a u8, _y: &'b u8) -> &'a u8 { +// Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid: +// fn multiple_in_and_out_2a<'a>(x: &'a u8, _y: &u8) -> &'a u8 +// ^^^ +fn multiple_in_and_out_2a<'a, 'b>(x: &'a u8, _y: &'b u8) -> &'a u8 { x } +// Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid: +// fn multiple_in_and_out_2b<'b>(_x: &u8, y: &'b u8) -> &'b u8 +// ^^^ +fn multiple_in_and_out_2b<'a, 'b>(_x: &'a u8, y: &'b u8) -> &'b u8 { + y +} + // No error; multiple input refs async fn func<'a>(args: &[&'a str]) -> Option<&'a str> { args.get(0).cloned() @@ -44,11 +53,20 @@ fn in_static_and_out<'a>(x: &'a u8, _y: &'static u8) -> &'a u8 { x } -// No error. -fn deep_reference_1<'a, 'b>(x: &'a u8, _y: &'b u8) -> Result<&'a u8, ()> { +// Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid: +// fn deep_reference_1a<'a>(x: &'a u8, _y: &u8) -> Result<&'a u8, ()> +// ^^^ +fn deep_reference_1a<'a, 'b>(x: &'a u8, _y: &'b u8) -> Result<&'a u8, ()> { Ok(x) } +// Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid: +// fn deep_reference_1b<'b>(_x: &u8, y: &'b u8) -> Result<&'b u8, ()> +// ^^^ +fn deep_reference_1b<'a, 'b>(_x: &'a u8, y: &'b u8) -> Result<&'b u8, ()> { + Ok(y) +} + // No error; two input refs. fn deep_reference_2<'a>(x: Result<&'a u8, &'a u8>) -> &'a u8 { x.unwrap() @@ -129,11 +147,20 @@ impl X { &self.x } - // No error; multiple input refs. - fn self_and_in_out<'s, 't>(&'s self, _x: &'t u8) -> &'s u8 { + // Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid: + // fn self_and_in_out_1<'s>(&'s self, _x: &u8) -> &'s u8 + // ^^^ + fn self_and_in_out_1<'s, 't>(&'s self, _x: &'t u8) -> &'s u8 { &self.x } + // Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid: + // fn self_and_in_out_2<'t>(&self, x: &'t u8) -> &'t u8 + // ^^^^^ + fn self_and_in_out_2<'s, 't>(&'s self, x: &'t u8) -> &'t u8 { + x + } + fn distinct_self_and_in<'s, 't>(&'s self, _x: &'t u8) {} // No error; same lifetimes on two params. @@ -167,8 +194,19 @@ fn struct_with_lt3<'a>(_foo: &Foo<'a>) -> &'a str { unimplemented!() } -// No warning; two input lifetimes. -fn struct_with_lt4<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str { +// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is +// valid: +// fn struct_with_lt4a<'a>(_foo: &'a Foo<'_>) -> &'a str +// ^^ +fn struct_with_lt4a<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str { + unimplemented!() +} + +// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is +// valid: +// fn struct_with_lt4b<'b>(_foo: &Foo<'b>) -> &'b str +// ^^^^ +fn struct_with_lt4b<'a, 'b>(_foo: &'a Foo<'b>) -> &'b str { unimplemented!() } @@ -203,8 +241,19 @@ fn alias_with_lt3<'a>(_foo: &FooAlias<'a>) -> &'a str { unimplemented!() } -// No warning; two input lifetimes. -fn alias_with_lt4<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str { +// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is +// valid: +// fn alias_with_lt4a<'a>(_foo: &'a FooAlias<'_>) -> &'a str +// ^^ +fn alias_with_lt4a<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str { + unimplemented!() +} + +// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is +// valid: +// fn alias_with_lt4b<'b>(_foo: &FooAlias<'b>) -> &'b str +// ^^^^^^^^^ +fn alias_with_lt4b<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'b str { unimplemented!() } @@ -419,4 +468,31 @@ mod issue7296 { } } +mod pr_9743_false_negative_fix { + #![allow(unused)] + + fn foo<'a>(x: &'a u8, y: &'_ u8) {} + + fn bar<'a>(x: &'a u8, y: &'_ u8, z: &'_ u8) {} +} + +mod pr_9743_output_lifetime_checks { + #![allow(unused)] + + // lint: only one input + fn one_input<'a>(x: &'a u8) -> &'a u8 { + unimplemented!() + } + + // lint: multiple inputs, output would not be elided + fn multiple_inputs_output_not_elided<'a, 'b>(x: &'a u8, y: &'b u8, z: &'b u8) -> &'b u8 { + unimplemented!() + } + + // don't lint: multiple inputs, output would be elided (which would create an ambiguity) + fn multiple_inputs_output_would_be_elided<'a, 'b>(x: &'a u8, y: &'b u8, z: &'b u8) -> &'a u8 { + unimplemented!() + } +} + fn main() {} diff --git a/tests/ui/needless_lifetimes.stderr b/tests/ui/needless_lifetimes.stderr index 3c428fd4674ce..5a7cf13c86dde 100644 --- a/tests/ui/needless_lifetimes.stderr +++ b/tests/ui/needless_lifetimes.stderr @@ -1,4 +1,4 @@ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) +error: the following explicit lifetimes could be elided: 'a, 'b --> $DIR/needless_lifetimes.rs:11:1 | LL | fn distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8) {} @@ -6,185 +6,311 @@ LL | fn distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8) {} | = note: `-D clippy::needless-lifetimes` implied by `-D warnings` -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) +error: the following explicit lifetimes could be elided: 'a, 'b --> $DIR/needless_lifetimes.rs:13:1 | LL | fn distinct_and_static<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: &'static u8) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) +error: the following explicit lifetimes could be elided: 'a --> $DIR/needless_lifetimes.rs:23:1 | LL | fn in_and_out<'a>(x: &'a u8, _y: u8) -> &'a u8 { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:57:1 +error: the following explicit lifetimes could be elided: 'b + --> $DIR/needless_lifetimes.rs:35:1 + | +LL | fn multiple_in_and_out_2a<'a, 'b>(x: &'a u8, _y: &'b u8) -> &'a u8 { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:42:1 + | +LL | fn multiple_in_and_out_2b<'a, 'b>(_x: &'a u8, y: &'b u8) -> &'b u8 { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: the following explicit lifetimes could be elided: 'b + --> $DIR/needless_lifetimes.rs:59:1 + | +LL | fn deep_reference_1a<'a, 'b>(x: &'a u8, _y: &'b u8) -> Result<&'a u8, ()> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:66:1 + | +LL | fn deep_reference_1b<'a, 'b>(_x: &'a u8, y: &'b u8) -> Result<&'b u8, ()> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:75:1 | LL | fn deep_reference_3<'a>(x: &'a u8, _y: u8) -> Result<&'a u8, ()> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:62:1 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:80:1 | LL | fn where_clause_without_lt<'a, T>(x: &'a u8, _y: u8) -> Result<&'a u8, ()> | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:74:1 +error: the following explicit lifetimes could be elided: 'a, 'b + --> $DIR/needless_lifetimes.rs:92:1 | LL | fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: replace with `'_` in generic arguments such as here + --> $DIR/needless_lifetimes.rs:92:37 + | +LL | fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) {} + | ^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:98:1 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:116:1 | LL | fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I> | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: replace with `'_` in generic arguments such as here + --> $DIR/needless_lifetimes.rs:116:32 + | +LL | fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I> + | ^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:128:5 +error: the following explicit lifetimes could be elided: 's + --> $DIR/needless_lifetimes.rs:146:5 | LL | fn self_and_out<'s>(&'s self) -> &'s u8 { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:137:5 +error: the following explicit lifetimes could be elided: 't + --> $DIR/needless_lifetimes.rs:153:5 + | +LL | fn self_and_in_out_1<'s, 't>(&'s self, _x: &'t u8) -> &'s u8 { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: the following explicit lifetimes could be elided: 's + --> $DIR/needless_lifetimes.rs:160:5 + | +LL | fn self_and_in_out_2<'s, 't>(&'s self, x: &'t u8) -> &'t u8 { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: the following explicit lifetimes could be elided: 's, 't + --> $DIR/needless_lifetimes.rs:164:5 | LL | fn distinct_self_and_in<'s, 't>(&'s self, _x: &'t u8) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:156:1 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:183:1 | LL | fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: replace with `'_` in generic arguments such as here + --> $DIR/needless_lifetimes.rs:183:33 + | +LL | fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str { + | ^^ + +error: the following explicit lifetimes could be elided: 'b + --> $DIR/needless_lifetimes.rs:201:1 + | +LL | fn struct_with_lt4a<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: replace with `'_` in generic arguments such as here + --> $DIR/needless_lifetimes.rs:201:43 + | +LL | fn struct_with_lt4a<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str { + | ^^ + +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:209:1 + | +LL | fn struct_with_lt4b<'a, 'b>(_foo: &'a Foo<'b>) -> &'b str { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:186:1 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:224:1 | LL | fn trait_obj_elided2<'a>(_arg: &'a dyn Drop) -> &'a str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:192:1 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:230:1 | LL | fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: replace with `'_` in generic arguments such as here + --> $DIR/needless_lifetimes.rs:230:37 + | +LL | fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str { + | ^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:211:1 +error: the following explicit lifetimes could be elided: 'b + --> $DIR/needless_lifetimes.rs:248:1 + | +LL | fn alias_with_lt4a<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: replace with `'_` in generic arguments such as here + --> $DIR/needless_lifetimes.rs:248:47 + | +LL | fn alias_with_lt4a<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str { + | ^^ + +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:256:1 + | +LL | fn alias_with_lt4b<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'b str { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:260:1 | LL | fn named_input_elided_output<'a>(_arg: &'a str) -> &str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:219:1 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:268:1 | LL | fn trait_bound_ok<'a, T: WithLifetime<'static>>(_: &'a u8, _: T) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:255:1 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:304:1 | LL | fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: replace with `'_` in generic arguments such as here + --> $DIR/needless_lifetimes.rs:304:47 + | +LL | fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> { + | ^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:262:9 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:311:9 | LL | fn needless_lt<'a>(x: &'a u8) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:266:9 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:315:9 | LL | fn needless_lt<'a>(_x: &'a u8) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:279:9 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:328:9 | LL | fn baz<'a>(&'a self) -> impl Foo + 'a { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:311:5 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:360:5 | LL | fn impl_trait_elidable_nested_anonymous_lifetimes<'a>(i: &'a i32, f: impl Fn(&i32) -> &i32) -> &'a i32 { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:320:5 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:369:5 | LL | fn generics_elidable<'a, T: Fn(&i32) -> &i32>(i: &'a i32, f: T) -> &'a i32 { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:332:5 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:381:5 | LL | fn where_clause_elidadable<'a, T>(i: &'a i32, f: T) -> &'a i32 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:347:5 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:396:5 | LL | fn pointer_fn_elidable<'a>(i: &'a i32, f: fn(&i32) -> &i32) -> &'a i32 { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:360:5 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:409:5 | LL | fn nested_fn_pointer_3<'a>(_: &'a i32) -> fn(fn(&i32) -> &i32) -> i32 { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:363:5 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:412:5 | LL | fn nested_fn_pointer_4<'a>(_: &'a i32) -> impl Fn(fn(&i32)) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:385:9 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:434:9 | LL | fn implicit<'a>(&'a self) -> &'a () { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:388:9 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:437:9 | LL | fn implicit_mut<'a>(&'a mut self) -> &'a () { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:399:9 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:448:9 | LL | fn lifetime_elsewhere<'a>(self: Box, here: &'a ()) -> &'a () { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:405:9 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:454:9 | LL | fn implicit<'a>(&'a self) -> &'a (); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:406:9 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:455:9 | LL | fn implicit_provided<'a>(&'a self) -> &'a () { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:415:9 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:464:9 | LL | fn lifetime_elsewhere<'a>(self: Box, here: &'a ()) -> &'a (); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) - --> $DIR/needless_lifetimes.rs:416:9 +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:465:9 | LL | fn lifetime_elsewhere_provided<'a>(self: Box, here: &'a ()) -> &'a () { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 31 previous errors +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:474:5 + | +LL | fn foo<'a>(x: &'a u8, y: &'_ u8) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:476:5 + | +LL | fn bar<'a>(x: &'a u8, y: &'_ u8, z: &'_ u8) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:483:5 + | +LL | fn one_input<'a>(x: &'a u8) -> &'a u8 { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: the following explicit lifetimes could be elided: 'a + --> $DIR/needless_lifetimes.rs:488:5 + | +LL | fn multiple_inputs_output_not_elided<'a, 'b>(x: &'a u8, y: &'b u8, z: &'b u8) -> &'b u8 { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 45 previous errors diff --git a/tests/ui/never_loop.rs b/tests/ui/never_loop.rs index 3dbef19890e9c..28e8f459d4429 100644 --- a/tests/ui/never_loop.rs +++ b/tests/ui/never_loop.rs @@ -229,6 +229,27 @@ pub fn test18() { }; } +// Issue #9831: unconditional break to internal labeled block +pub fn test19() { + fn thing(iter: impl Iterator) { + for _ in iter { + 'b: { + break 'b; + } + } + } +} + +pub fn test20() { + 'a: loop { + 'b: { + break 'b 'c: { + break 'a; + }; + } + } +} + fn main() { test1(); test2(); diff --git a/tests/ui/never_loop.stderr b/tests/ui/never_loop.stderr index 3033f019244a8..b7029bf8bed47 100644 --- a/tests/ui/never_loop.stderr +++ b/tests/ui/never_loop.stderr @@ -114,5 +114,17 @@ LL | | break x; LL | | }; | |_____^ -error: aborting due to 10 previous errors +error: this loop never actually loops + --> $DIR/never_loop.rs:244:5 + | +LL | / 'a: loop { +LL | | 'b: { +LL | | break 'b 'c: { +LL | | break 'a; +LL | | }; +LL | | } +LL | | } + | |_____^ + +error: aborting due to 11 previous errors diff --git a/tests/ui/new_ret_no_self.rs b/tests/ui/new_ret_no_self.rs index 2f315ffe2983e..f69982d63a898 100644 --- a/tests/ui/new_ret_no_self.rs +++ b/tests/ui/new_ret_no_self.rs @@ -350,3 +350,53 @@ impl RetOtherSelf { RetOtherSelf(RetOtherSelfWrapper(t)) } } + +mod issue7344 { + struct RetImplTraitSelf(T); + + impl RetImplTraitSelf { + // should not trigger lint + fn new(t: T) -> impl Into { + Self(t) + } + } + + struct RetImplTraitNoSelf(T); + + impl RetImplTraitNoSelf { + // should trigger lint + fn new(t: T) -> impl Into { + 1 + } + } + + trait Trait2 {} + impl Trait2 for () {} + + struct RetImplTraitSelf2(T); + + impl RetImplTraitSelf2 { + // should not trigger lint + fn new(t: T) -> impl Trait2<(), Self> { + unimplemented!() + } + } + + struct RetImplTraitNoSelf2(T); + + impl RetImplTraitNoSelf2 { + // should trigger lint + fn new(t: T) -> impl Trait2<(), i32> { + unimplemented!() + } + } + + struct RetImplTraitSelfAdt<'a>(&'a str); + + impl<'a> RetImplTraitSelfAdt<'a> { + // should not trigger lint + fn new<'b: 'a>(s: &'b str) -> impl Into> { + RetImplTraitSelfAdt(s) + } + } +} diff --git a/tests/ui/new_ret_no_self.stderr b/tests/ui/new_ret_no_self.stderr index 8217bc6187f93..bc13be47927b1 100644 --- a/tests/ui/new_ret_no_self.stderr +++ b/tests/ui/new_ret_no_self.stderr @@ -76,5 +76,21 @@ LL | | unimplemented!(); LL | | } | |_________^ -error: aborting due to 10 previous errors +error: methods called `new` usually return `Self` + --> $DIR/new_ret_no_self.rs:368:9 + | +LL | / fn new(t: T) -> impl Into { +LL | | 1 +LL | | } + | |_________^ + +error: methods called `new` usually return `Self` + --> $DIR/new_ret_no_self.rs:389:9 + | +LL | / fn new(t: T) -> impl Trait2<(), i32> { +LL | | unimplemented!() +LL | | } + | |_________^ + +error: aborting due to 12 previous errors diff --git a/tests/ui/option_if_let_else.fixed b/tests/ui/option_if_let_else.fixed index f15ac551bb3cc..0456005dce496 100644 --- a/tests/ui/option_if_let_else.fixed +++ b/tests/ui/option_if_let_else.fixed @@ -189,3 +189,12 @@ fn main() { let _ = res.map_or(1, |a| a + 1); let _ = res.map_or(5, |a| a + 1); } + +#[allow(dead_code)] +fn issue9742() -> Option<&'static str> { + // should not lint because of guards + match Some("foo ") { + Some(name) if name.starts_with("foo") => Some(name.trim()), + _ => None, + } +} diff --git a/tests/ui/option_if_let_else.rs b/tests/ui/option_if_let_else.rs index 9eeaea12d3bc9..23b148752cbfa 100644 --- a/tests/ui/option_if_let_else.rs +++ b/tests/ui/option_if_let_else.rs @@ -230,3 +230,12 @@ fn main() { }; let _ = if let Ok(a) = res { a + 1 } else { 5 }; } + +#[allow(dead_code)] +fn issue9742() -> Option<&'static str> { + // should not lint because of guards + match Some("foo ") { + Some(name) if name.starts_with("foo") => Some(name.trim()), + _ => None, + } +} diff --git a/tests/ui/or_fun_call.fixed b/tests/ui/or_fun_call.fixed index 23b1aa8bebd53..be9a65506e131 100644 --- a/tests/ui/or_fun_call.fixed +++ b/tests/ui/or_fun_call.fixed @@ -236,4 +236,20 @@ mod issue9608 { } } +mod issue8993 { + fn g() -> i32 { + 3 + } + + fn f(n: i32) -> i32 { + n + } + + fn test_map_or() { + let _ = Some(4).map_or_else(g, |v| v); + let _ = Some(4).map_or_else(g, f); + let _ = Some(4).map_or(0, f); + } +} + fn main() {} diff --git a/tests/ui/or_fun_call.rs b/tests/ui/or_fun_call.rs index 039998f22dd71..628c970463890 100644 --- a/tests/ui/or_fun_call.rs +++ b/tests/ui/or_fun_call.rs @@ -236,4 +236,20 @@ mod issue9608 { } } +mod issue8993 { + fn g() -> i32 { + 3 + } + + fn f(n: i32) -> i32 { + n + } + + fn test_map_or() { + let _ = Some(4).map_or(g(), |v| v); + let _ = Some(4).map_or(g(), f); + let _ = Some(4).map_or(0, f); + } +} + fn main() {} diff --git a/tests/ui/or_fun_call.stderr b/tests/ui/or_fun_call.stderr index 113ba150c6192..ba3001db7a5f4 100644 --- a/tests/ui/or_fun_call.stderr +++ b/tests/ui/or_fun_call.stderr @@ -156,5 +156,17 @@ error: use of `unwrap_or` followed by a call to `new` LL | .unwrap_or(String::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_default()` -error: aborting due to 26 previous errors +error: use of `map_or` followed by a function call + --> $DIR/or_fun_call.rs:249:25 + | +LL | let _ = Some(4).map_or(g(), |v| v); + | ^^^^^^^^^^^^^^^^^^ help: try this: `map_or_else(g, |v| v)` + +error: use of `map_or` followed by a function call + --> $DIR/or_fun_call.rs:250:25 + | +LL | let _ = Some(4).map_or(g(), f); + | ^^^^^^^^^^^^^^ help: try this: `map_or_else(g, f)` + +error: aborting due to 28 previous errors diff --git a/tests/ui/question_mark.fixed b/tests/ui/question_mark.fixed index 993389232cc29..5c49d46da7260 100644 --- a/tests/ui/question_mark.fixed +++ b/tests/ui/question_mark.fixed @@ -134,6 +134,9 @@ fn result_func(x: Result) -> Result { return func_returning_result(); } + // no warning + let _ = if let Err(e) = x { Err(e) } else { Ok(0) }; + Ok(y) } diff --git a/tests/ui/question_mark.rs b/tests/ui/question_mark.rs index 9ae0d88829af5..d057df6a9b35d 100644 --- a/tests/ui/question_mark.rs +++ b/tests/ui/question_mark.rs @@ -166,6 +166,9 @@ fn result_func(x: Result) -> Result { return func_returning_result(); } + // no warning + let _ = if let Err(e) = x { Err(e) } else { Ok(0) }; + Ok(y) } diff --git a/tests/ui/question_mark.stderr b/tests/ui/question_mark.stderr index 1b6cd524b2f23..23172d7e535dd 100644 --- a/tests/ui/question_mark.stderr +++ b/tests/ui/question_mark.stderr @@ -115,7 +115,7 @@ LL | | } | |_____^ help: replace it with: `x?;` error: this block may be rewritten with the `?` operator - --> $DIR/question_mark.rs:193:5 + --> $DIR/question_mark.rs:196:5 | LL | / if let Err(err) = func_returning_result() { LL | | return Err(err); @@ -123,7 +123,7 @@ LL | | } | |_____^ help: replace it with: `func_returning_result()?;` error: this block may be rewritten with the `?` operator - --> $DIR/question_mark.rs:200:5 + --> $DIR/question_mark.rs:203:5 | LL | / if let Err(err) = func_returning_result() { LL | | return Err(err); diff --git a/tests/ui/rename.fixed b/tests/ui/rename.fixed index 8beae8dee0854..689928f047946 100644 --- a/tests/ui/rename.fixed +++ b/tests/ui/rename.fixed @@ -12,7 +12,6 @@ #![allow(clippy::disallowed_methods)] #![allow(clippy::disallowed_types)] #![allow(clippy::mixed_read_write_in_expression)] -#![allow(for_loops_over_fallibles)] #![allow(clippy::useless_conversion)] #![allow(clippy::match_result_ok)] #![allow(clippy::overly_complex_bool_expr)] @@ -27,9 +26,11 @@ #![allow(clippy::recursive_format_impl)] #![allow(clippy::invisible_characters)] #![allow(drop_bounds)] +#![allow(for_loops_over_fallibles)] #![allow(array_into_iter)] #![allow(invalid_atomic_ordering)] #![allow(invalid_value)] +#![allow(let_underscore_drop)] #![allow(enum_intrinsics_non_enums)] #![allow(non_fmt_panics)] #![allow(named_arguments_used_positionally)] @@ -45,8 +46,6 @@ #![warn(clippy::disallowed_methods)] #![warn(clippy::disallowed_types)] #![warn(clippy::mixed_read_write_in_expression)] -#![warn(for_loops_over_fallibles)] -#![warn(for_loops_over_fallibles)] #![warn(clippy::useless_conversion)] #![warn(clippy::match_result_ok)] #![warn(clippy::overly_complex_bool_expr)] @@ -66,9 +65,12 @@ #![warn(clippy::invisible_characters)] #![warn(drop_bounds)] #![warn(for_loops_over_fallibles)] +#![warn(for_loops_over_fallibles)] +#![warn(for_loops_over_fallibles)] #![warn(array_into_iter)] #![warn(invalid_atomic_ordering)] #![warn(invalid_value)] +#![warn(let_underscore_drop)] #![warn(enum_intrinsics_non_enums)] #![warn(non_fmt_panics)] #![warn(named_arguments_used_positionally)] diff --git a/tests/ui/rename.rs b/tests/ui/rename.rs index 9e665047baaeb..b74aa650ffd47 100644 --- a/tests/ui/rename.rs +++ b/tests/ui/rename.rs @@ -12,7 +12,6 @@ #![allow(clippy::disallowed_methods)] #![allow(clippy::disallowed_types)] #![allow(clippy::mixed_read_write_in_expression)] -#![allow(for_loops_over_fallibles)] #![allow(clippy::useless_conversion)] #![allow(clippy::match_result_ok)] #![allow(clippy::overly_complex_bool_expr)] @@ -27,9 +26,11 @@ #![allow(clippy::recursive_format_impl)] #![allow(clippy::invisible_characters)] #![allow(drop_bounds)] +#![allow(for_loops_over_fallibles)] #![allow(array_into_iter)] #![allow(invalid_atomic_ordering)] #![allow(invalid_value)] +#![allow(let_underscore_drop)] #![allow(enum_intrinsics_non_enums)] #![allow(non_fmt_panics)] #![allow(named_arguments_used_positionally)] @@ -45,8 +46,6 @@ #![warn(clippy::disallowed_method)] #![warn(clippy::disallowed_type)] #![warn(clippy::eval_order_dependence)] -#![warn(clippy::for_loop_over_option)] -#![warn(clippy::for_loop_over_result)] #![warn(clippy::identity_conversion)] #![warn(clippy::if_let_some_result)] #![warn(clippy::logic_bug)] @@ -65,10 +64,13 @@ #![warn(clippy::to_string_in_display)] #![warn(clippy::zero_width_space)] #![warn(clippy::drop_bounds)] +#![warn(clippy::for_loop_over_option)] +#![warn(clippy::for_loop_over_result)] #![warn(clippy::for_loops_over_fallibles)] #![warn(clippy::into_iter_on_array)] #![warn(clippy::invalid_atomic_ordering)] #![warn(clippy::invalid_ref)] +#![warn(clippy::let_underscore_drop)] #![warn(clippy::mem_discriminant_non_enum)] #![warn(clippy::panic_params)] #![warn(clippy::positional_named_format_parameters)] diff --git a/tests/ui/rename.stderr b/tests/ui/rename.stderr index 63eb565185f07..622a32c5908ae 100644 --- a/tests/ui/rename.stderr +++ b/tests/ui/rename.stderr @@ -1,5 +1,5 @@ error: lint `clippy::blacklisted_name` has been renamed to `clippy::disallowed_names` - --> $DIR/rename.rs:39:9 + --> $DIR/rename.rs:40:9 | LL | #![warn(clippy::blacklisted_name)] | ^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::disallowed_names` @@ -7,232 +7,238 @@ LL | #![warn(clippy::blacklisted_name)] = note: `-D renamed-and-removed-lints` implied by `-D warnings` error: lint `clippy::block_in_if_condition_expr` has been renamed to `clippy::blocks_in_if_conditions` - --> $DIR/rename.rs:40:9 + --> $DIR/rename.rs:41:9 | LL | #![warn(clippy::block_in_if_condition_expr)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::blocks_in_if_conditions` error: lint `clippy::block_in_if_condition_stmt` has been renamed to `clippy::blocks_in_if_conditions` - --> $DIR/rename.rs:41:9 + --> $DIR/rename.rs:42:9 | LL | #![warn(clippy::block_in_if_condition_stmt)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::blocks_in_if_conditions` error: lint `clippy::box_vec` has been renamed to `clippy::box_collection` - --> $DIR/rename.rs:42:9 + --> $DIR/rename.rs:43:9 | LL | #![warn(clippy::box_vec)] | ^^^^^^^^^^^^^^^ help: use the new name: `clippy::box_collection` error: lint `clippy::const_static_lifetime` has been renamed to `clippy::redundant_static_lifetimes` - --> $DIR/rename.rs:43:9 + --> $DIR/rename.rs:44:9 | LL | #![warn(clippy::const_static_lifetime)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::redundant_static_lifetimes` error: lint `clippy::cyclomatic_complexity` has been renamed to `clippy::cognitive_complexity` - --> $DIR/rename.rs:44:9 + --> $DIR/rename.rs:45:9 | LL | #![warn(clippy::cyclomatic_complexity)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::cognitive_complexity` error: lint `clippy::disallowed_method` has been renamed to `clippy::disallowed_methods` - --> $DIR/rename.rs:45:9 + --> $DIR/rename.rs:46:9 | LL | #![warn(clippy::disallowed_method)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::disallowed_methods` error: lint `clippy::disallowed_type` has been renamed to `clippy::disallowed_types` - --> $DIR/rename.rs:46:9 + --> $DIR/rename.rs:47:9 | LL | #![warn(clippy::disallowed_type)] | ^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::disallowed_types` error: lint `clippy::eval_order_dependence` has been renamed to `clippy::mixed_read_write_in_expression` - --> $DIR/rename.rs:47:9 + --> $DIR/rename.rs:48:9 | LL | #![warn(clippy::eval_order_dependence)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::mixed_read_write_in_expression` -error: lint `clippy::for_loop_over_option` has been renamed to `for_loops_over_fallibles` - --> $DIR/rename.rs:48:9 - | -LL | #![warn(clippy::for_loop_over_option)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `for_loops_over_fallibles` - -error: lint `clippy::for_loop_over_result` has been renamed to `for_loops_over_fallibles` - --> $DIR/rename.rs:49:9 - | -LL | #![warn(clippy::for_loop_over_result)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `for_loops_over_fallibles` - error: lint `clippy::identity_conversion` has been renamed to `clippy::useless_conversion` - --> $DIR/rename.rs:50:9 + --> $DIR/rename.rs:49:9 | LL | #![warn(clippy::identity_conversion)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::useless_conversion` error: lint `clippy::if_let_some_result` has been renamed to `clippy::match_result_ok` - --> $DIR/rename.rs:51:9 + --> $DIR/rename.rs:50:9 | LL | #![warn(clippy::if_let_some_result)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::match_result_ok` error: lint `clippy::logic_bug` has been renamed to `clippy::overly_complex_bool_expr` - --> $DIR/rename.rs:52:9 + --> $DIR/rename.rs:51:9 | LL | #![warn(clippy::logic_bug)] | ^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::overly_complex_bool_expr` error: lint `clippy::new_without_default_derive` has been renamed to `clippy::new_without_default` - --> $DIR/rename.rs:53:9 + --> $DIR/rename.rs:52:9 | LL | #![warn(clippy::new_without_default_derive)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::new_without_default` error: lint `clippy::option_and_then_some` has been renamed to `clippy::bind_instead_of_map` - --> $DIR/rename.rs:54:9 + --> $DIR/rename.rs:53:9 | LL | #![warn(clippy::option_and_then_some)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::bind_instead_of_map` error: lint `clippy::option_expect_used` has been renamed to `clippy::expect_used` - --> $DIR/rename.rs:55:9 + --> $DIR/rename.rs:54:9 | LL | #![warn(clippy::option_expect_used)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::expect_used` error: lint `clippy::option_map_unwrap_or` has been renamed to `clippy::map_unwrap_or` - --> $DIR/rename.rs:56:9 + --> $DIR/rename.rs:55:9 | LL | #![warn(clippy::option_map_unwrap_or)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::map_unwrap_or` error: lint `clippy::option_map_unwrap_or_else` has been renamed to `clippy::map_unwrap_or` - --> $DIR/rename.rs:57:9 + --> $DIR/rename.rs:56:9 | LL | #![warn(clippy::option_map_unwrap_or_else)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::map_unwrap_or` error: lint `clippy::option_unwrap_used` has been renamed to `clippy::unwrap_used` - --> $DIR/rename.rs:58:9 + --> $DIR/rename.rs:57:9 | LL | #![warn(clippy::option_unwrap_used)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::unwrap_used` error: lint `clippy::ref_in_deref` has been renamed to `clippy::needless_borrow` - --> $DIR/rename.rs:59:9 + --> $DIR/rename.rs:58:9 | LL | #![warn(clippy::ref_in_deref)] | ^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::needless_borrow` error: lint `clippy::result_expect_used` has been renamed to `clippy::expect_used` - --> $DIR/rename.rs:60:9 + --> $DIR/rename.rs:59:9 | LL | #![warn(clippy::result_expect_used)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::expect_used` error: lint `clippy::result_map_unwrap_or_else` has been renamed to `clippy::map_unwrap_or` - --> $DIR/rename.rs:61:9 + --> $DIR/rename.rs:60:9 | LL | #![warn(clippy::result_map_unwrap_or_else)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::map_unwrap_or` error: lint `clippy::result_unwrap_used` has been renamed to `clippy::unwrap_used` - --> $DIR/rename.rs:62:9 + --> $DIR/rename.rs:61:9 | LL | #![warn(clippy::result_unwrap_used)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::unwrap_used` error: lint `clippy::single_char_push_str` has been renamed to `clippy::single_char_add_str` - --> $DIR/rename.rs:63:9 + --> $DIR/rename.rs:62:9 | LL | #![warn(clippy::single_char_push_str)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::single_char_add_str` error: lint `clippy::stutter` has been renamed to `clippy::module_name_repetitions` - --> $DIR/rename.rs:64:9 + --> $DIR/rename.rs:63:9 | LL | #![warn(clippy::stutter)] | ^^^^^^^^^^^^^^^ help: use the new name: `clippy::module_name_repetitions` error: lint `clippy::to_string_in_display` has been renamed to `clippy::recursive_format_impl` - --> $DIR/rename.rs:65:9 + --> $DIR/rename.rs:64:9 | LL | #![warn(clippy::to_string_in_display)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::recursive_format_impl` error: lint `clippy::zero_width_space` has been renamed to `clippy::invisible_characters` - --> $DIR/rename.rs:66:9 + --> $DIR/rename.rs:65:9 | LL | #![warn(clippy::zero_width_space)] | ^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::invisible_characters` error: lint `clippy::drop_bounds` has been renamed to `drop_bounds` - --> $DIR/rename.rs:67:9 + --> $DIR/rename.rs:66:9 | LL | #![warn(clippy::drop_bounds)] | ^^^^^^^^^^^^^^^^^^^ help: use the new name: `drop_bounds` -error: lint `clippy::for_loops_over_fallibles` has been renamed to `for_loops_over_fallibles` +error: lint `clippy::for_loop_over_option` has been renamed to `for_loops_over_fallibles` + --> $DIR/rename.rs:67:9 + | +LL | #![warn(clippy::for_loop_over_option)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `for_loops_over_fallibles` + +error: lint `clippy::for_loop_over_result` has been renamed to `for_loops_over_fallibles` --> $DIR/rename.rs:68:9 | +LL | #![warn(clippy::for_loop_over_result)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `for_loops_over_fallibles` + +error: lint `clippy::for_loops_over_fallibles` has been renamed to `for_loops_over_fallibles` + --> $DIR/rename.rs:69:9 + | LL | #![warn(clippy::for_loops_over_fallibles)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `for_loops_over_fallibles` error: lint `clippy::into_iter_on_array` has been renamed to `array_into_iter` - --> $DIR/rename.rs:69:9 + --> $DIR/rename.rs:70:9 | LL | #![warn(clippy::into_iter_on_array)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `array_into_iter` error: lint `clippy::invalid_atomic_ordering` has been renamed to `invalid_atomic_ordering` - --> $DIR/rename.rs:70:9 + --> $DIR/rename.rs:71:9 | LL | #![warn(clippy::invalid_atomic_ordering)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `invalid_atomic_ordering` error: lint `clippy::invalid_ref` has been renamed to `invalid_value` - --> $DIR/rename.rs:71:9 + --> $DIR/rename.rs:72:9 | LL | #![warn(clippy::invalid_ref)] | ^^^^^^^^^^^^^^^^^^^ help: use the new name: `invalid_value` +error: lint `clippy::let_underscore_drop` has been renamed to `let_underscore_drop` + --> $DIR/rename.rs:73:9 + | +LL | #![warn(clippy::let_underscore_drop)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `let_underscore_drop` + error: lint `clippy::mem_discriminant_non_enum` has been renamed to `enum_intrinsics_non_enums` - --> $DIR/rename.rs:72:9 + --> $DIR/rename.rs:74:9 | LL | #![warn(clippy::mem_discriminant_non_enum)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `enum_intrinsics_non_enums` error: lint `clippy::panic_params` has been renamed to `non_fmt_panics` - --> $DIR/rename.rs:73:9 + --> $DIR/rename.rs:75:9 | LL | #![warn(clippy::panic_params)] | ^^^^^^^^^^^^^^^^^^^^ help: use the new name: `non_fmt_panics` error: lint `clippy::positional_named_format_parameters` has been renamed to `named_arguments_used_positionally` - --> $DIR/rename.rs:74:9 + --> $DIR/rename.rs:76:9 | LL | #![warn(clippy::positional_named_format_parameters)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `named_arguments_used_positionally` error: lint `clippy::temporary_cstring_as_ptr` has been renamed to `temporary_cstring_as_ptr` - --> $DIR/rename.rs:75:9 + --> $DIR/rename.rs:77:9 | LL | #![warn(clippy::temporary_cstring_as_ptr)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `temporary_cstring_as_ptr` error: lint `clippy::unknown_clippy_lints` has been renamed to `unknown_lints` - --> $DIR/rename.rs:76:9 + --> $DIR/rename.rs:78:9 | LL | #![warn(clippy::unknown_clippy_lints)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `unknown_lints` error: lint `clippy::unused_label` has been renamed to `unused_labels` - --> $DIR/rename.rs:77:9 + --> $DIR/rename.rs:79:9 | LL | #![warn(clippy::unused_label)] | ^^^^^^^^^^^^^^^^^^^^ help: use the new name: `unused_labels` -error: aborting due to 39 previous errors +error: aborting due to 40 previous errors diff --git a/tests/ui/result_large_err.rs b/tests/ui/result_large_err.rs index f7df3b8565501..9dd27d6dc01aa 100644 --- a/tests/ui/result_large_err.rs +++ b/tests/ui/result_large_err.rs @@ -50,6 +50,18 @@ impl LargeErrorVariants<()> { } } +enum MultipleLargeVariants { + _Biggest([u8; 1024]), + _AlsoBig([u8; 512]), + _Ok(usize), +} + +impl MultipleLargeVariants { + fn large_enum_error() -> Result<(), Self> { + Ok(()) + } +} + trait TraitForcesLargeError { fn large_error() -> Result<(), [u8; 512]> { Ok(()) diff --git a/tests/ui/result_large_err.stderr b/tests/ui/result_large_err.stderr index bea101fe20bf2..c386edfd21571 100644 --- a/tests/ui/result_large_err.stderr +++ b/tests/ui/result_large_err.stderr @@ -42,13 +42,29 @@ LL | pub fn param_large_error() -> Result<(), (u128, R, FullyDefinedLargeErro error: the `Err`-variant returned from this function is very large --> $DIR/result_large_err.rs:48:34 | +LL | _Omg([u8; 512]), + | --------------- the largest variant contains at least 512 bytes +... LL | pub fn large_enum_error() -> Result<(), Self> { - | ^^^^^^^^^^^^^^^^ the `Err`-variant is at least 513 bytes + | ^^^^^^^^^^^^^^^^ | = help: try reducing the size of `LargeErrorVariants<()>`, for example by boxing large elements or replacing it with `Box>` error: the `Err`-variant returned from this function is very large - --> $DIR/result_large_err.rs:54:25 + --> $DIR/result_large_err.rs:60:30 + | +LL | _Biggest([u8; 1024]), + | -------------------- the largest variant contains at least 1024 bytes +LL | _AlsoBig([u8; 512]), + | ------------------- the variant `_AlsoBig` contains at least 512 bytes +... +LL | fn large_enum_error() -> Result<(), Self> { + | ^^^^^^^^^^^^^^^^ + | + = help: try reducing the size of `MultipleLargeVariants`, for example by boxing large elements or replacing it with `Box` + +error: the `Err`-variant returned from this function is very large + --> $DIR/result_large_err.rs:66:25 | LL | fn large_error() -> Result<(), [u8; 512]> { | ^^^^^^^^^^^^^^^^^^^^^ the `Err`-variant is at least 512 bytes @@ -56,7 +72,7 @@ LL | fn large_error() -> Result<(), [u8; 512]> { = help: try reducing the size of `[u8; 512]`, for example by boxing large elements or replacing it with `Box<[u8; 512]>` error: the `Err`-variant returned from this function is very large - --> $DIR/result_large_err.rs:73:29 + --> $DIR/result_large_err.rs:85:29 | LL | pub fn large_union_err() -> Result<(), FullyDefinedUnionError> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `Err`-variant is at least 512 bytes @@ -64,7 +80,7 @@ LL | pub fn large_union_err() -> Result<(), FullyDefinedUnionError> { = help: try reducing the size of `FullyDefinedUnionError`, for example by boxing large elements or replacing it with `Box` error: the `Err`-variant returned from this function is very large - --> $DIR/result_large_err.rs:82:40 + --> $DIR/result_large_err.rs:94:40 | LL | pub fn param_large_union() -> Result<(), UnionError> { | ^^^^^^^^^^^^^^^^^^^^^^^^^ the `Err`-variant is at least 512 bytes @@ -72,7 +88,7 @@ LL | pub fn param_large_union() -> Result<(), UnionError> { = help: try reducing the size of `UnionError`, for example by boxing large elements or replacing it with `Box>` error: the `Err`-variant returned from this function is very large - --> $DIR/result_large_err.rs:91:34 + --> $DIR/result_large_err.rs:103:34 | LL | pub fn array_error_subst() -> Result<(), ArrayError> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `Err`-variant is at least 128 bytes @@ -80,12 +96,12 @@ LL | pub fn array_error_subst() -> Result<(), ArrayError> { = help: try reducing the size of `ArrayError`, for example by boxing large elements or replacing it with `Box>` error: the `Err`-variant returned from this function is very large - --> $DIR/result_large_err.rs:95:31 + --> $DIR/result_large_err.rs:107:31 | LL | pub fn array_error() -> Result<(), ArrayError<(i32, T), U>> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `Err`-variant is at least 128 bytes | = help: try reducing the size of `ArrayError<(i32, T), U>`, for example by boxing large elements or replacing it with `Box>` -error: aborting due to 11 previous errors +error: aborting due to 12 previous errors diff --git a/tests/ui/seek_from_current.fixed b/tests/ui/seek_from_current.fixed new file mode 100644 index 0000000000000..4b5303324bc6f --- /dev/null +++ b/tests/ui/seek_from_current.fixed @@ -0,0 +1,26 @@ +// run-rustfix +#![warn(clippy::seek_from_current)] +#![feature(custom_inner_attributes)] + +use std::fs::File; +use std::io::{self, Seek, SeekFrom, Write}; + +fn _msrv_1_50() -> io::Result<()> { + #![clippy::msrv = "1.50"] + let mut f = File::create("foo.txt")?; + f.write_all(b"Hi!")?; + f.seek(SeekFrom::Current(0))?; + f.seek(SeekFrom::Current(1))?; + Ok(()) +} + +fn _msrv_1_51() -> io::Result<()> { + #![clippy::msrv = "1.51"] + let mut f = File::create("foo.txt")?; + f.write_all(b"Hi!")?; + f.stream_position()?; + f.seek(SeekFrom::Current(1))?; + Ok(()) +} + +fn main() {} diff --git a/tests/ui/seek_from_current.rs b/tests/ui/seek_from_current.rs new file mode 100644 index 0000000000000..f93639261a180 --- /dev/null +++ b/tests/ui/seek_from_current.rs @@ -0,0 +1,26 @@ +// run-rustfix +#![warn(clippy::seek_from_current)] +#![feature(custom_inner_attributes)] + +use std::fs::File; +use std::io::{self, Seek, SeekFrom, Write}; + +fn _msrv_1_50() -> io::Result<()> { + #![clippy::msrv = "1.50"] + let mut f = File::create("foo.txt")?; + f.write_all(b"Hi!")?; + f.seek(SeekFrom::Current(0))?; + f.seek(SeekFrom::Current(1))?; + Ok(()) +} + +fn _msrv_1_51() -> io::Result<()> { + #![clippy::msrv = "1.51"] + let mut f = File::create("foo.txt")?; + f.write_all(b"Hi!")?; + f.seek(SeekFrom::Current(0))?; + f.seek(SeekFrom::Current(1))?; + Ok(()) +} + +fn main() {} diff --git a/tests/ui/seek_from_current.stderr b/tests/ui/seek_from_current.stderr new file mode 100644 index 0000000000000..db1125b53cdf5 --- /dev/null +++ b/tests/ui/seek_from_current.stderr @@ -0,0 +1,10 @@ +error: using `SeekFrom::Current` to start from current position + --> $DIR/seek_from_current.rs:21:5 + | +LL | f.seek(SeekFrom::Current(0))?; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `f.stream_position()` + | + = note: `-D clippy::seek-from-current` implied by `-D warnings` + +error: aborting due to previous error + diff --git a/tests/ui/seek_to_start_instead_of_rewind.fixed b/tests/ui/seek_to_start_instead_of_rewind.fixed new file mode 100644 index 0000000000000..464b6cdef6393 --- /dev/null +++ b/tests/ui/seek_to_start_instead_of_rewind.fixed @@ -0,0 +1,137 @@ +// run-rustfix +#![allow(unused)] +#![feature(custom_inner_attributes)] +#![warn(clippy::seek_to_start_instead_of_rewind)] + +use std::fs::OpenOptions; +use std::io::{Read, Seek, SeekFrom, Write}; + +struct StructWithSeekMethod {} + +impl StructWithSeekMethod { + fn seek(&mut self, from: SeekFrom) {} +} + +trait MySeekTrait { + fn seek(&mut self, from: SeekFrom) {} +} + +struct StructWithSeekTrait {} +impl MySeekTrait for StructWithSeekTrait {} + +// This should NOT trigger clippy warning because +// StructWithSeekMethod does not implement std::io::Seek; +fn seek_to_start_false_method(t: &mut StructWithSeekMethod) { + t.seek(SeekFrom::Start(0)); +} + +// This should NOT trigger clippy warning because +// StructWithSeekMethod does not implement std::io::Seek; +fn seek_to_start_method_owned_false(mut t: StructWithSeekMethod) { + t.seek(SeekFrom::Start(0)); +} + +// This should NOT trigger clippy warning because +// StructWithSeekMethod does not implement std::io::Seek; +fn seek_to_start_false_trait(t: &mut StructWithSeekTrait) { + t.seek(SeekFrom::Start(0)); +} + +// This should NOT trigger clippy warning because +// StructWithSeekMethod does not implement std::io::Seek; +fn seek_to_start_false_trait_owned(mut t: StructWithSeekTrait) { + t.seek(SeekFrom::Start(0)); +} + +// This should NOT trigger clippy warning because +// StructWithSeekMethod does not implement std::io::Seek; +fn seek_to_start_false_trait_bound(t: &mut T) { + t.seek(SeekFrom::Start(0)); +} + +// This should trigger clippy warning +fn seek_to_start(t: &mut T) { + t.rewind(); +} + +// This should trigger clippy warning +fn owned_seek_to_start(mut t: T) { + t.rewind(); +} + +// This should NOT trigger clippy warning because +// it does not seek to start +fn seek_to_5(t: &mut T) { + t.seek(SeekFrom::Start(5)); +} + +// This should NOT trigger clippy warning because +// it does not seek to start +fn seek_to_end(t: &mut T) { + t.seek(SeekFrom::End(0)); +} + +fn main() { + let mut f = OpenOptions::new() + .write(true) + .read(true) + .create(true) + .open("foo.txt") + .unwrap(); + + let mut my_struct_trait = StructWithSeekTrait {}; + seek_to_start_false_trait_bound(&mut my_struct_trait); + + let hello = "Hello!\n"; + write!(f, "{hello}").unwrap(); + seek_to_5(&mut f); + seek_to_end(&mut f); + seek_to_start(&mut f); + + let mut buf = String::new(); + f.read_to_string(&mut buf).unwrap(); + + assert_eq!(&buf, hello); +} + +fn msrv_1_54() { + #![clippy::msrv = "1.54"] + + let mut f = OpenOptions::new() + .write(true) + .read(true) + .create(true) + .open("foo.txt") + .unwrap(); + + let hello = "Hello!\n"; + write!(f, "{hello}").unwrap(); + + f.seek(SeekFrom::Start(0)); + + let mut buf = String::new(); + f.read_to_string(&mut buf).unwrap(); + + assert_eq!(&buf, hello); +} + +fn msrv_1_55() { + #![clippy::msrv = "1.55"] + + let mut f = OpenOptions::new() + .write(true) + .read(true) + .create(true) + .open("foo.txt") + .unwrap(); + + let hello = "Hello!\n"; + write!(f, "{hello}").unwrap(); + + f.rewind(); + + let mut buf = String::new(); + f.read_to_string(&mut buf).unwrap(); + + assert_eq!(&buf, hello); +} diff --git a/tests/ui/seek_to_start_instead_of_rewind.rs b/tests/ui/seek_to_start_instead_of_rewind.rs new file mode 100644 index 0000000000000..68e09bd7c1f0f --- /dev/null +++ b/tests/ui/seek_to_start_instead_of_rewind.rs @@ -0,0 +1,137 @@ +// run-rustfix +#![allow(unused)] +#![feature(custom_inner_attributes)] +#![warn(clippy::seek_to_start_instead_of_rewind)] + +use std::fs::OpenOptions; +use std::io::{Read, Seek, SeekFrom, Write}; + +struct StructWithSeekMethod {} + +impl StructWithSeekMethod { + fn seek(&mut self, from: SeekFrom) {} +} + +trait MySeekTrait { + fn seek(&mut self, from: SeekFrom) {} +} + +struct StructWithSeekTrait {} +impl MySeekTrait for StructWithSeekTrait {} + +// This should NOT trigger clippy warning because +// StructWithSeekMethod does not implement std::io::Seek; +fn seek_to_start_false_method(t: &mut StructWithSeekMethod) { + t.seek(SeekFrom::Start(0)); +} + +// This should NOT trigger clippy warning because +// StructWithSeekMethod does not implement std::io::Seek; +fn seek_to_start_method_owned_false(mut t: StructWithSeekMethod) { + t.seek(SeekFrom::Start(0)); +} + +// This should NOT trigger clippy warning because +// StructWithSeekMethod does not implement std::io::Seek; +fn seek_to_start_false_trait(t: &mut StructWithSeekTrait) { + t.seek(SeekFrom::Start(0)); +} + +// This should NOT trigger clippy warning because +// StructWithSeekMethod does not implement std::io::Seek; +fn seek_to_start_false_trait_owned(mut t: StructWithSeekTrait) { + t.seek(SeekFrom::Start(0)); +} + +// This should NOT trigger clippy warning because +// StructWithSeekMethod does not implement std::io::Seek; +fn seek_to_start_false_trait_bound(t: &mut T) { + t.seek(SeekFrom::Start(0)); +} + +// This should trigger clippy warning +fn seek_to_start(t: &mut T) { + t.seek(SeekFrom::Start(0)); +} + +// This should trigger clippy warning +fn owned_seek_to_start(mut t: T) { + t.seek(SeekFrom::Start(0)); +} + +// This should NOT trigger clippy warning because +// it does not seek to start +fn seek_to_5(t: &mut T) { + t.seek(SeekFrom::Start(5)); +} + +// This should NOT trigger clippy warning because +// it does not seek to start +fn seek_to_end(t: &mut T) { + t.seek(SeekFrom::End(0)); +} + +fn main() { + let mut f = OpenOptions::new() + .write(true) + .read(true) + .create(true) + .open("foo.txt") + .unwrap(); + + let mut my_struct_trait = StructWithSeekTrait {}; + seek_to_start_false_trait_bound(&mut my_struct_trait); + + let hello = "Hello!\n"; + write!(f, "{hello}").unwrap(); + seek_to_5(&mut f); + seek_to_end(&mut f); + seek_to_start(&mut f); + + let mut buf = String::new(); + f.read_to_string(&mut buf).unwrap(); + + assert_eq!(&buf, hello); +} + +fn msrv_1_54() { + #![clippy::msrv = "1.54"] + + let mut f = OpenOptions::new() + .write(true) + .read(true) + .create(true) + .open("foo.txt") + .unwrap(); + + let hello = "Hello!\n"; + write!(f, "{hello}").unwrap(); + + f.seek(SeekFrom::Start(0)); + + let mut buf = String::new(); + f.read_to_string(&mut buf).unwrap(); + + assert_eq!(&buf, hello); +} + +fn msrv_1_55() { + #![clippy::msrv = "1.55"] + + let mut f = OpenOptions::new() + .write(true) + .read(true) + .create(true) + .open("foo.txt") + .unwrap(); + + let hello = "Hello!\n"; + write!(f, "{hello}").unwrap(); + + f.seek(SeekFrom::Start(0)); + + let mut buf = String::new(); + f.read_to_string(&mut buf).unwrap(); + + assert_eq!(&buf, hello); +} diff --git a/tests/ui/seek_to_start_instead_of_rewind.stderr b/tests/ui/seek_to_start_instead_of_rewind.stderr new file mode 100644 index 0000000000000..de0eec5d909cd --- /dev/null +++ b/tests/ui/seek_to_start_instead_of_rewind.stderr @@ -0,0 +1,22 @@ +error: used `seek` to go to the start of the stream + --> $DIR/seek_to_start_instead_of_rewind.rs:54:7 + | +LL | t.seek(SeekFrom::Start(0)); + | ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `rewind()` + | + = note: `-D clippy::seek-to-start-instead-of-rewind` implied by `-D warnings` + +error: used `seek` to go to the start of the stream + --> $DIR/seek_to_start_instead_of_rewind.rs:59:7 + | +LL | t.seek(SeekFrom::Start(0)); + | ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `rewind()` + +error: used `seek` to go to the start of the stream + --> $DIR/seek_to_start_instead_of_rewind.rs:131:7 + | +LL | f.seek(SeekFrom::Start(0)); + | ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `rewind()` + +error: aborting due to 3 previous errors + diff --git a/tests/ui/single_component_path_imports.stderr b/tests/ui/single_component_path_imports.stderr index 509c88ac256a8..71dcc25d6e5b8 100644 --- a/tests/ui/single_component_path_imports.stderr +++ b/tests/ui/single_component_path_imports.stderr @@ -1,16 +1,16 @@ error: this import is redundant - --> $DIR/single_component_path_imports.rs:23:5 + --> $DIR/single_component_path_imports.rs:5:1 | -LL | use regex; - | ^^^^^^^^^^ help: remove it entirely +LL | use regex; + | ^^^^^^^^^^ help: remove it entirely | = note: `-D clippy::single-component-path-imports` implied by `-D warnings` error: this import is redundant - --> $DIR/single_component_path_imports.rs:5:1 + --> $DIR/single_component_path_imports.rs:23:5 | -LL | use regex; - | ^^^^^^^^^^ help: remove it entirely +LL | use regex; + | ^^^^^^^^^^ help: remove it entirely error: aborting due to 2 previous errors diff --git a/tests/ui/single_component_path_imports_nested_first.stderr b/tests/ui/single_component_path_imports_nested_first.stderr index 633546f6419a2..330f285202d0c 100644 --- a/tests/ui/single_component_path_imports_nested_first.stderr +++ b/tests/ui/single_component_path_imports_nested_first.stderr @@ -1,3 +1,11 @@ +error: this import is redundant + --> $DIR/single_component_path_imports_nested_first.rs:4:1 + | +LL | use regex; + | ^^^^^^^^^^ help: remove it entirely + | + = note: `-D clippy::single-component-path-imports` implied by `-D warnings` + error: this import is redundant --> $DIR/single_component_path_imports_nested_first.rs:13:10 | @@ -5,7 +13,6 @@ LL | use {regex, serde}; | ^^^^^ | = help: remove this import - = note: `-D clippy::single-component-path-imports` implied by `-D warnings` error: this import is redundant --> $DIR/single_component_path_imports_nested_first.rs:13:17 @@ -15,11 +22,5 @@ LL | use {regex, serde}; | = help: remove this import -error: this import is redundant - --> $DIR/single_component_path_imports_nested_first.rs:4:1 - | -LL | use regex; - | ^^^^^^^^^^ help: remove it entirely - error: aborting due to 3 previous errors diff --git a/tests/ui/string_extend.fixed b/tests/ui/string_extend.fixed index 1883a9f832578..d200d7310fca5 100644 --- a/tests/ui/string_extend.fixed +++ b/tests/ui/string_extend.fixed @@ -29,4 +29,7 @@ fn main() { let f = HasChars; s.extend(f.chars()); + + // issue #9735 + s.push_str(&abc[0..2]); } diff --git a/tests/ui/string_extend.rs b/tests/ui/string_extend.rs index 07d0baa1be6c7..0dd96a3b21035 100644 --- a/tests/ui/string_extend.rs +++ b/tests/ui/string_extend.rs @@ -29,4 +29,7 @@ fn main() { let f = HasChars; s.extend(f.chars()); + + // issue #9735 + s.extend(abc[0..2].chars()); } diff --git a/tests/ui/string_extend.stderr b/tests/ui/string_extend.stderr index 6af8c9e1662b5..b35c77fd96113 100644 --- a/tests/ui/string_extend.stderr +++ b/tests/ui/string_extend.stderr @@ -18,5 +18,11 @@ error: calling `.extend(_.chars())` LL | s.extend(def.chars()); | ^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.push_str(&def)` -error: aborting due to 3 previous errors +error: calling `.extend(_.chars())` + --> $DIR/string_extend.rs:34:5 + | +LL | s.extend(abc[0..2].chars()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.push_str(&abc[0..2])` + +error: aborting due to 4 previous errors diff --git a/tests/ui/suspicious_xor_used_as_pow.rs b/tests/ui/suspicious_xor_used_as_pow.rs new file mode 100644 index 0000000000000..eb9fc63fb1d46 --- /dev/null +++ b/tests/ui/suspicious_xor_used_as_pow.rs @@ -0,0 +1,34 @@ +#![allow(unused)] +#![warn(clippy::suspicious_xor_used_as_pow)] +#![allow(clippy::eq_op)] + +macro_rules! macro_test { + () => { + 13 + }; +} + +macro_rules! macro_test_inside { + () => { + 1 ^ 2 // should warn even if inside macro + }; +} + +fn main() { + // Should warn: + let _ = 2 ^ 5; + let _ = 2i32 ^ 9i32; + let _ = 2i32 ^ 2i32; + let _ = 50i32 ^ 3i32; + let _ = 5i32 ^ 8i32; + let _ = 2i32 ^ 32i32; + macro_test_inside!(); + + // Should not warn: + let x = 0x02; + let _ = x ^ 2; + let _ = 2 ^ x; + let _ = x ^ 5; + let _ = 10 ^ 0b0101; + let _ = 2i32 ^ macro_test!(); +} diff --git a/tests/ui/suspicious_xor_used_as_pow.stderr b/tests/ui/suspicious_xor_used_as_pow.stderr new file mode 100644 index 0000000000000..8bb3c8fbeebd3 --- /dev/null +++ b/tests/ui/suspicious_xor_used_as_pow.stderr @@ -0,0 +1,51 @@ +error: `^` is not the exponentiation operator + --> $DIR/suspicious_xor_used_as_pow.rs:19:13 + | +LL | let _ = 2 ^ 5; + | ^^^^^ help: did you mean to write: `2.pow(5)` + | + = note: `-D clippy::suspicious-xor-used-as-pow` implied by `-D warnings` + +error: `^` is not the exponentiation operator + --> $DIR/suspicious_xor_used_as_pow.rs:20:13 + | +LL | let _ = 2i32 ^ 9i32; + | ^^^^^^^^^^^ help: did you mean to write: `2_i32.pow(9_i32)` + +error: `^` is not the exponentiation operator + --> $DIR/suspicious_xor_used_as_pow.rs:21:13 + | +LL | let _ = 2i32 ^ 2i32; + | ^^^^^^^^^^^ help: did you mean to write: `2_i32.pow(2_i32)` + +error: `^` is not the exponentiation operator + --> $DIR/suspicious_xor_used_as_pow.rs:22:13 + | +LL | let _ = 50i32 ^ 3i32; + | ^^^^^^^^^^^^ help: did you mean to write: `50_i32.pow(3_i32)` + +error: `^` is not the exponentiation operator + --> $DIR/suspicious_xor_used_as_pow.rs:23:13 + | +LL | let _ = 5i32 ^ 8i32; + | ^^^^^^^^^^^ help: did you mean to write: `5_i32.pow(8_i32)` + +error: `^` is not the exponentiation operator + --> $DIR/suspicious_xor_used_as_pow.rs:24:13 + | +LL | let _ = 2i32 ^ 32i32; + | ^^^^^^^^^^^^ help: did you mean to write: `2_i32.pow(32_i32)` + +error: `^` is not the exponentiation operator + --> $DIR/suspicious_xor_used_as_pow.rs:13:9 + | +LL | 1 ^ 2 // should warn even if inside macro + | ^^^^^ help: did you mean to write: `1.pow(2)` +... +LL | macro_test_inside!(); + | -------------------- in this macro invocation + | + = note: this error originates in the macro `macro_test_inside` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 7 previous errors + diff --git a/tests/ui/swap.fixed b/tests/ui/swap.fixed index 24b229235d33a..805a2ba5a5984 100644 --- a/tests/ui/swap.fixed +++ b/tests/ui/swap.fixed @@ -155,3 +155,12 @@ fn issue_8154() { let s = S3(&mut s); std::mem::swap(&mut s.0.x, &mut s.0.y); } + +const fn issue_9864(mut u: u32) -> u32 { + let mut v = 10; + + let temp = u; + u = v; + v = temp; + u + v +} diff --git a/tests/ui/swap.rs b/tests/ui/swap.rs index a318c27919c8a..a8c878479523f 100644 --- a/tests/ui/swap.rs +++ b/tests/ui/swap.rs @@ -179,3 +179,12 @@ fn issue_8154() { s.0.x = s.0.y; s.0.y = t; } + +const fn issue_9864(mut u: u32) -> u32 { + let mut v = 10; + + let temp = u; + u = v; + v = temp; + u + v +} diff --git a/tests/ui/transmute.rs b/tests/ui/transmute.rs index 001c910239aff..1cbacf0feab54 100644 --- a/tests/ui/transmute.rs +++ b/tests/ui/transmute.rs @@ -1,4 +1,4 @@ -#![allow(dead_code, clippy::borrow_as_ptr)] +#![allow(dead_code, clippy::borrow_as_ptr, clippy::needless_lifetimes)] extern crate core; diff --git a/tests/ui/trivially_copy_pass_by_ref.rs b/tests/ui/trivially_copy_pass_by_ref.rs index af4f3b18443bd..c0af011d33d06 100644 --- a/tests/ui/trivially_copy_pass_by_ref.rs +++ b/tests/ui/trivially_copy_pass_by_ref.rs @@ -3,6 +3,7 @@ #![deny(clippy::trivially_copy_pass_by_ref)] #![allow( clippy::disallowed_names, + clippy::needless_lifetimes, clippy::redundant_field_names, clippy::uninlined_format_args )] diff --git a/tests/ui/trivially_copy_pass_by_ref.stderr b/tests/ui/trivially_copy_pass_by_ref.stderr index 6a8eca9655343..8c5cfa8a0f17c 100644 --- a/tests/ui/trivially_copy_pass_by_ref.stderr +++ b/tests/ui/trivially_copy_pass_by_ref.stderr @@ -1,5 +1,5 @@ error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte) - --> $DIR/trivially_copy_pass_by_ref.rs:50:11 + --> $DIR/trivially_copy_pass_by_ref.rs:51:11 | LL | fn bad(x: &u32, y: &Foo, z: &Baz) {} | ^^^^ help: consider passing by value instead: `u32` @@ -11,103 +11,103 @@ LL | #![deny(clippy::trivially_copy_pass_by_ref)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte) - --> $DIR/trivially_copy_pass_by_ref.rs:50:20 + --> $DIR/trivially_copy_pass_by_ref.rs:51:20 | LL | fn bad(x: &u32, y: &Foo, z: &Baz) {} | ^^^^ help: consider passing by value instead: `Foo` error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte) - --> $DIR/trivially_copy_pass_by_ref.rs:50:29 + --> $DIR/trivially_copy_pass_by_ref.rs:51:29 | LL | fn bad(x: &u32, y: &Foo, z: &Baz) {} | ^^^^ help: consider passing by value instead: `Baz` error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte) - --> $DIR/trivially_copy_pass_by_ref.rs:57:12 + --> $DIR/trivially_copy_pass_by_ref.rs:58:12 | LL | fn bad(&self, x: &u32, y: &Foo, z: &Baz) {} | ^^^^^ help: consider passing by value instead: `self` error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte) - --> $DIR/trivially_copy_pass_by_ref.rs:57:22 + --> $DIR/trivially_copy_pass_by_ref.rs:58:22 | LL | fn bad(&self, x: &u32, y: &Foo, z: &Baz) {} | ^^^^ help: consider passing by value instead: `u32` error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte) - --> $DIR/trivially_copy_pass_by_ref.rs:57:31 + --> $DIR/trivially_copy_pass_by_ref.rs:58:31 | LL | fn bad(&self, x: &u32, y: &Foo, z: &Baz) {} | ^^^^ help: consider passing by value instead: `Foo` error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte) - --> $DIR/trivially_copy_pass_by_ref.rs:57:40 + --> $DIR/trivially_copy_pass_by_ref.rs:58:40 | LL | fn bad(&self, x: &u32, y: &Foo, z: &Baz) {} | ^^^^ help: consider passing by value instead: `Baz` error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte) - --> $DIR/trivially_copy_pass_by_ref.rs:59:16 + --> $DIR/trivially_copy_pass_by_ref.rs:60:16 | LL | fn bad2(x: &u32, y: &Foo, z: &Baz) {} | ^^^^ help: consider passing by value instead: `u32` error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte) - --> $DIR/trivially_copy_pass_by_ref.rs:59:25 + --> $DIR/trivially_copy_pass_by_ref.rs:60:25 | LL | fn bad2(x: &u32, y: &Foo, z: &Baz) {} | ^^^^ help: consider passing by value instead: `Foo` error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte) - --> $DIR/trivially_copy_pass_by_ref.rs:59:34 + --> $DIR/trivially_copy_pass_by_ref.rs:60:34 | LL | fn bad2(x: &u32, y: &Foo, z: &Baz) {} | ^^^^ help: consider passing by value instead: `Baz` error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte) - --> $DIR/trivially_copy_pass_by_ref.rs:61:35 + --> $DIR/trivially_copy_pass_by_ref.rs:62:35 | LL | fn bad_issue7518(self, other: &Self) {} | ^^^^^ help: consider passing by value instead: `Self` error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte) - --> $DIR/trivially_copy_pass_by_ref.rs:73:16 + --> $DIR/trivially_copy_pass_by_ref.rs:74:16 | LL | fn bad2(x: &u32, y: &Foo, z: &Baz) {} | ^^^^ help: consider passing by value instead: `u32` error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte) - --> $DIR/trivially_copy_pass_by_ref.rs:73:25 + --> $DIR/trivially_copy_pass_by_ref.rs:74:25 | LL | fn bad2(x: &u32, y: &Foo, z: &Baz) {} | ^^^^ help: consider passing by value instead: `Foo` error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte) - --> $DIR/trivially_copy_pass_by_ref.rs:73:34 + --> $DIR/trivially_copy_pass_by_ref.rs:74:34 | LL | fn bad2(x: &u32, y: &Foo, z: &Baz) {} | ^^^^ help: consider passing by value instead: `Baz` error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte) - --> $DIR/trivially_copy_pass_by_ref.rs:77:34 + --> $DIR/trivially_copy_pass_by_ref.rs:78:34 | LL | fn trait_method(&self, _foo: &Foo); | ^^^^ help: consider passing by value instead: `Foo` error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte) - --> $DIR/trivially_copy_pass_by_ref.rs:109:21 + --> $DIR/trivially_copy_pass_by_ref.rs:110:21 | LL | fn foo_never(x: &i32) { | ^^^^ help: consider passing by value instead: `i32` error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte) - --> $DIR/trivially_copy_pass_by_ref.rs:114:15 + --> $DIR/trivially_copy_pass_by_ref.rs:115:15 | LL | fn foo(x: &i32) { | ^^^^ help: consider passing by value instead: `i32` error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte) - --> $DIR/trivially_copy_pass_by_ref.rs:141:37 + --> $DIR/trivially_copy_pass_by_ref.rs:142:37 | LL | fn _unrelated_lifetimes<'a, 'b>(_x: &'a u32, y: &'b u32) -> &'b u32 { | ^^^^^^^ help: consider passing by value instead: `u32` diff --git a/tests/ui/unchecked_duration_subtraction.fixed b/tests/ui/unchecked_duration_subtraction.fixed new file mode 100644 index 0000000000000..a0e49a8beb1ed --- /dev/null +++ b/tests/ui/unchecked_duration_subtraction.fixed @@ -0,0 +1,17 @@ +// run-rustfix +#![warn(clippy::unchecked_duration_subtraction)] + +use std::time::{Duration, Instant}; + +fn main() { + let _first = Instant::now(); + let second = Duration::from_secs(3); + + let _ = _first.checked_sub(second).unwrap(); + + let _ = Instant::now().checked_sub(Duration::from_secs(5)).unwrap(); + + let _ = _first.checked_sub(Duration::from_secs(5)).unwrap(); + + let _ = Instant::now().checked_sub(second).unwrap(); +} diff --git a/tests/ui/unchecked_duration_subtraction.rs b/tests/ui/unchecked_duration_subtraction.rs new file mode 100644 index 0000000000000..a14a7ea57cc50 --- /dev/null +++ b/tests/ui/unchecked_duration_subtraction.rs @@ -0,0 +1,17 @@ +// run-rustfix +#![warn(clippy::unchecked_duration_subtraction)] + +use std::time::{Duration, Instant}; + +fn main() { + let _first = Instant::now(); + let second = Duration::from_secs(3); + + let _ = _first - second; + + let _ = Instant::now() - Duration::from_secs(5); + + let _ = _first - Duration::from_secs(5); + + let _ = Instant::now() - second; +} diff --git a/tests/ui/unchecked_duration_subtraction.stderr b/tests/ui/unchecked_duration_subtraction.stderr new file mode 100644 index 0000000000000..a2e0aa1d7c089 --- /dev/null +++ b/tests/ui/unchecked_duration_subtraction.stderr @@ -0,0 +1,28 @@ +error: unchecked subtraction of a 'Duration' from an 'Instant' + --> $DIR/unchecked_duration_subtraction.rs:10:13 + | +LL | let _ = _first - second; + | ^^^^^^^^^^^^^^^ help: try: `_first.checked_sub(second).unwrap()` + | + = note: `-D clippy::unchecked-duration-subtraction` implied by `-D warnings` + +error: unchecked subtraction of a 'Duration' from an 'Instant' + --> $DIR/unchecked_duration_subtraction.rs:12:13 + | +LL | let _ = Instant::now() - Duration::from_secs(5); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Instant::now().checked_sub(Duration::from_secs(5)).unwrap()` + +error: unchecked subtraction of a 'Duration' from an 'Instant' + --> $DIR/unchecked_duration_subtraction.rs:14:13 + | +LL | let _ = _first - Duration::from_secs(5); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `_first.checked_sub(Duration::from_secs(5)).unwrap()` + +error: unchecked subtraction of a 'Duration' from an 'Instant' + --> $DIR/unchecked_duration_subtraction.rs:16:13 + | +LL | let _ = Instant::now() - second; + | ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Instant::now().checked_sub(second).unwrap()` + +error: aborting due to 4 previous errors + diff --git a/tests/ui/undocumented_unsafe_blocks.rs b/tests/ui/undocumented_unsafe_blocks.rs index 08aee43321514..cbc6768033ec8 100644 --- a/tests/ui/undocumented_unsafe_blocks.rs +++ b/tests/ui/undocumented_unsafe_blocks.rs @@ -490,4 +490,23 @@ unsafe impl CrateRoot for () {} // SAFETY: ok unsafe impl CrateRoot for (i32) {} +fn issue_9142() { + // SAFETY: ok + let _ = + // we need this comment to avoid rustfmt putting + // it all on one line + unsafe {}; + + // SAFETY: this is more than one level away, so it should warn + let _ = { + if unsafe { true } { + todo!(); + } else { + let bar = unsafe {}; + todo!(); + bar + } + }; +} + fn main() {} diff --git a/tests/ui/undocumented_unsafe_blocks.stderr b/tests/ui/undocumented_unsafe_blocks.stderr index 2c466ff5c733b..ba4de9806d175 100644 --- a/tests/ui/undocumented_unsafe_blocks.stderr +++ b/tests/ui/undocumented_unsafe_blocks.stderr @@ -263,5 +263,29 @@ LL | unsafe impl CrateRoot for () {} | = help: consider adding a safety comment on the preceding line -error: aborting due to 31 previous errors +error: unsafe block missing a safety comment + --> $DIR/undocumented_unsafe_blocks.rs:498:9 + | +LL | unsafe {}; + | ^^^^^^^^^ + | + = help: consider adding a safety comment on the preceding line + +error: unsafe block missing a safety comment + --> $DIR/undocumented_unsafe_blocks.rs:502:12 + | +LL | if unsafe { true } { + | ^^^^^^^^^^^^^^^ + | + = help: consider adding a safety comment on the preceding line + +error: unsafe block missing a safety comment + --> $DIR/undocumented_unsafe_blocks.rs:505:23 + | +LL | let bar = unsafe {}; + | ^^^^^^^^^ + | + = help: consider adding a safety comment on the preceding line + +error: aborting due to 34 previous errors diff --git a/tests/ui/unnecessary_join.stderr b/tests/ui/unnecessary_join.stderr index 0b14b143affd6..e919a6d1d8aa3 100644 --- a/tests/ui/unnecessary_join.stderr +++ b/tests/ui/unnecessary_join.stderr @@ -1,4 +1,4 @@ -error: called `.collect>().join("")` on an iterator +error: called `.collect::>().join("")` on an iterator --> $DIR/unnecessary_join.rs:11:10 | LL | .collect::>() @@ -8,7 +8,7 @@ LL | | .join(""); | = note: `-D clippy::unnecessary-join` implied by `-D warnings` -error: called `.collect>().join("")` on an iterator +error: called `.collect::>().join("")` on an iterator --> $DIR/unnecessary_join.rs:20:10 | LL | .collect::>() diff --git a/tests/ui/unused_rounding.fixed b/tests/ui/unused_rounding.fixed index 54f85806ac3b3..38fe6c34cfec1 100644 --- a/tests/ui/unused_rounding.fixed +++ b/tests/ui/unused_rounding.fixed @@ -6,4 +6,9 @@ fn main() { let _ = 1.0f64; let _ = 1.00f32; let _ = 2e-54f64.floor(); + + // issue9866 + let _ = 3.3_f32.round(); + let _ = 3.3_f64.round(); + let _ = 3.0_f32; } diff --git a/tests/ui/unused_rounding.rs b/tests/ui/unused_rounding.rs index 8d007bc4a1dc8..a5cac64d023ae 100644 --- a/tests/ui/unused_rounding.rs +++ b/tests/ui/unused_rounding.rs @@ -6,4 +6,9 @@ fn main() { let _ = 1.0f64.floor(); let _ = 1.00f32.round(); let _ = 2e-54f64.floor(); + + // issue9866 + let _ = 3.3_f32.round(); + let _ = 3.3_f64.round(); + let _ = 3.0_f32.round(); } diff --git a/tests/ui/unused_rounding.stderr b/tests/ui/unused_rounding.stderr index 6cfb02e040283..1eeb5d1de8832 100644 --- a/tests/ui/unused_rounding.stderr +++ b/tests/ui/unused_rounding.stderr @@ -18,5 +18,11 @@ error: used the `round` method with a whole number float LL | let _ = 1.00f32.round(); | ^^^^^^^^^^^^^^^ help: remove the `round` method call: `1.00f32` -error: aborting due to 3 previous errors +error: used the `round` method with a whole number float + --> $DIR/unused_rounding.rs:13:13 + | +LL | let _ = 3.0_f32.round(); + | ^^^^^^^^^^^^^^^ help: remove the `round` method call: `3.0_f32` + +error: aborting due to 4 previous errors diff --git a/tests/ui/unused_unit.fixed b/tests/ui/unused_unit.fixed index 7bb43cf7ae82d..3dd640b86f0b3 100644 --- a/tests/ui/unused_unit.fixed +++ b/tests/ui/unused_unit.fixed @@ -7,6 +7,7 @@ // test of the JSON error format. #![feature(custom_inner_attributes)] +#![feature(closure_lifetime_binder)] #![rustfmt::skip] #![deny(clippy::unused_unit)] @@ -87,3 +88,9 @@ fn macro_expr() { } e!() } + +mod issue9748 { + fn main() { + let _ = for<'a> |_: &'a u32| -> () {}; + } +} diff --git a/tests/ui/unused_unit.rs b/tests/ui/unused_unit.rs index 21073fb802ada..bddecf06fb76d 100644 --- a/tests/ui/unused_unit.rs +++ b/tests/ui/unused_unit.rs @@ -7,6 +7,7 @@ // test of the JSON error format. #![feature(custom_inner_attributes)] +#![feature(closure_lifetime_binder)] #![rustfmt::skip] #![deny(clippy::unused_unit)] @@ -87,3 +88,9 @@ fn macro_expr() { } e!() } + +mod issue9748 { + fn main() { + let _ = for<'a> |_: &'a u32| -> () {}; + } +} diff --git a/tests/ui/unused_unit.stderr b/tests/ui/unused_unit.stderr index 0d2cb77855be1..ce06738cfe473 100644 --- a/tests/ui/unused_unit.stderr +++ b/tests/ui/unused_unit.stderr @@ -1,119 +1,119 @@ error: unneeded unit return type - --> $DIR/unused_unit.rs:19:58 + --> $DIR/unused_unit.rs:20:58 | LL | pub fn get_unit (), G>(&self, f: F, _g: G) -> () | ^^^^^^ help: remove the `-> ()` | note: the lint level is defined here - --> $DIR/unused_unit.rs:12:9 + --> $DIR/unused_unit.rs:13:9 | LL | #![deny(clippy::unused_unit)] | ^^^^^^^^^^^^^^^^^^^ error: unneeded unit return type - --> $DIR/unused_unit.rs:19:28 + --> $DIR/unused_unit.rs:20:28 | LL | pub fn get_unit (), G>(&self, f: F, _g: G) -> () | ^^^^^^ help: remove the `-> ()` error: unneeded unit return type - --> $DIR/unused_unit.rs:20:18 + --> $DIR/unused_unit.rs:21:18 | LL | where G: Fn() -> () { | ^^^^^^ help: remove the `-> ()` error: unneeded unit return type - --> $DIR/unused_unit.rs:21:26 + --> $DIR/unused_unit.rs:22:26 | LL | let _y: &dyn Fn() -> () = &f; | ^^^^^^ help: remove the `-> ()` error: unneeded unit return type - --> $DIR/unused_unit.rs:28:18 + --> $DIR/unused_unit.rs:29:18 | LL | fn into(self) -> () { | ^^^^^^ help: remove the `-> ()` error: unneeded unit expression - --> $DIR/unused_unit.rs:29:9 + --> $DIR/unused_unit.rs:30:9 | LL | () | ^^ help: remove the final `()` error: unneeded unit return type - --> $DIR/unused_unit.rs:34:29 + --> $DIR/unused_unit.rs:35:29 | LL | fn redundant (), G, H>(&self, _f: F, _g: G, _h: H) | ^^^^^^ help: remove the `-> ()` error: unneeded unit return type - --> $DIR/unused_unit.rs:36:19 + --> $DIR/unused_unit.rs:37:19 | LL | G: FnMut() -> (), | ^^^^^^ help: remove the `-> ()` error: unneeded unit return type - --> $DIR/unused_unit.rs:37:16 + --> $DIR/unused_unit.rs:38:16 | LL | H: Fn() -> (); | ^^^^^^ help: remove the `-> ()` error: unneeded unit return type - --> $DIR/unused_unit.rs:41:29 + --> $DIR/unused_unit.rs:42:29 | LL | fn redundant (), G, H>(&self, _f: F, _g: G, _h: H) | ^^^^^^ help: remove the `-> ()` error: unneeded unit return type - --> $DIR/unused_unit.rs:43:19 + --> $DIR/unused_unit.rs:44:19 | LL | G: FnMut() -> (), | ^^^^^^ help: remove the `-> ()` error: unneeded unit return type - --> $DIR/unused_unit.rs:44:16 + --> $DIR/unused_unit.rs:45:16 | LL | H: Fn() -> () {} | ^^^^^^ help: remove the `-> ()` error: unneeded unit return type - --> $DIR/unused_unit.rs:47:17 + --> $DIR/unused_unit.rs:48:17 | LL | fn return_unit() -> () { () } | ^^^^^^ help: remove the `-> ()` error: unneeded unit expression - --> $DIR/unused_unit.rs:47:26 + --> $DIR/unused_unit.rs:48:26 | LL | fn return_unit() -> () { () } | ^^ help: remove the final `()` error: unneeded `()` - --> $DIR/unused_unit.rs:57:14 + --> $DIR/unused_unit.rs:58:14 | LL | break(); | ^^ help: remove the `()` error: unneeded `()` - --> $DIR/unused_unit.rs:59:11 + --> $DIR/unused_unit.rs:60:11 | LL | return(); | ^^ help: remove the `()` error: unneeded unit return type - --> $DIR/unused_unit.rs:76:10 + --> $DIR/unused_unit.rs:77:10 | LL | fn test()->(){} | ^^^^ help: remove the `-> ()` error: unneeded unit return type - --> $DIR/unused_unit.rs:79:11 + --> $DIR/unused_unit.rs:80:11 | LL | fn test2() ->(){} | ^^^^^ help: remove the `-> ()` error: unneeded unit return type - --> $DIR/unused_unit.rs:82:11 + --> $DIR/unused_unit.rs:83:11 | LL | fn test3()-> (){} | ^^^^^ help: remove the `-> ()` diff --git a/tests/ui/unwrap.stderr b/tests/ui/unwrap.stderr index e88d580f7bd28..d49bf2b322837 100644 --- a/tests/ui/unwrap.stderr +++ b/tests/ui/unwrap.stderr @@ -1,4 +1,4 @@ -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap.rs:5:13 | LL | let _ = opt.unwrap(); @@ -7,7 +7,7 @@ LL | let _ = opt.unwrap(); = help: if you don't want to handle the `None` case gracefully, consider using `expect()` to provide a better panic message = note: `-D clippy::unwrap-used` implied by `-D warnings` -error: used `unwrap()` on `a Result` value +error: used `unwrap()` on a `Result` value --> $DIR/unwrap.rs:10:13 | LL | let _ = res.unwrap(); @@ -15,7 +15,7 @@ LL | let _ = res.unwrap(); | = help: if you don't want to handle the `Err` case gracefully, consider using `expect()` to provide a better panic message -error: used `unwrap_err()` on `a Result` value +error: used `unwrap_err()` on a `Result` value --> $DIR/unwrap.rs:11:13 | LL | let _ = res.unwrap_err(); diff --git a/tests/ui/unwrap_expect_used.stderr b/tests/ui/unwrap_expect_used.stderr index 211d2be18342d..fe4ecef114535 100644 --- a/tests/ui/unwrap_expect_used.stderr +++ b/tests/ui/unwrap_expect_used.stderr @@ -1,4 +1,4 @@ -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_expect_used.rs:23:5 | LL | Some(3).unwrap(); @@ -7,7 +7,7 @@ LL | Some(3).unwrap(); = help: if this value is `None`, it will panic = note: `-D clippy::unwrap-used` implied by `-D warnings` -error: used `expect()` on `an Option` value +error: used `expect()` on an `Option` value --> $DIR/unwrap_expect_used.rs:24:5 | LL | Some(3).expect("Hello world!"); @@ -16,7 +16,7 @@ LL | Some(3).expect("Hello world!"); = help: if this value is `None`, it will panic = note: `-D clippy::expect-used` implied by `-D warnings` -error: used `unwrap()` on `a Result` value +error: used `unwrap()` on a `Result` value --> $DIR/unwrap_expect_used.rs:31:5 | LL | a.unwrap(); @@ -24,7 +24,7 @@ LL | a.unwrap(); | = help: if this value is an `Err`, it will panic -error: used `expect()` on `a Result` value +error: used `expect()` on a `Result` value --> $DIR/unwrap_expect_used.rs:32:5 | LL | a.expect("Hello world!"); @@ -32,7 +32,7 @@ LL | a.expect("Hello world!"); | = help: if this value is an `Err`, it will panic -error: used `unwrap_err()` on `a Result` value +error: used `unwrap_err()` on a `Result` value --> $DIR/unwrap_expect_used.rs:33:5 | LL | a.unwrap_err(); @@ -40,7 +40,7 @@ LL | a.unwrap_err(); | = help: if this value is an `Ok`, it will panic -error: used `expect_err()` on `a Result` value +error: used `expect_err()` on a `Result` value --> $DIR/unwrap_expect_used.rs:34:5 | LL | a.expect_err("Hello error!"); diff --git a/tests/ui/unwrap_or.rs b/tests/ui/unwrap_or.rs index bfb41e4394731..a0c003f5b1eac 100644 --- a/tests/ui/unwrap_or.rs +++ b/tests/ui/unwrap_or.rs @@ -1,4 +1,4 @@ -#![warn(clippy::all)] +#![warn(clippy::all, clippy::or_fun_call)] fn main() { let s = Some(String::from("test string")).unwrap_or("Fail".to_string()).len(); diff --git a/tests/ui/use_self_trait.fixed b/tests/ui/use_self_trait.fixed index 9bcd692fb3511..4e779308d0241 100644 --- a/tests/ui/use_self_trait.fixed +++ b/tests/ui/use_self_trait.fixed @@ -47,8 +47,7 @@ impl Mul for Bad { impl Clone for Bad { fn clone(&self) -> Self { - // FIXME: applicable here - Bad + Self } } @@ -112,4 +111,42 @@ impl NameTrait for u8 { } } +mod impl_in_macro { + macro_rules! parse_ip_impl { + // minimized from serde=1.0.118 + ($ty:ty) => { + impl FooTrait for $ty { + fn new() -> Self { + <$ty>::bar() + } + } + }; + } + + struct Foo; + + trait FooTrait { + fn new() -> Self; + } + + impl Foo { + fn bar() -> Self { + Self + } + } + parse_ip_impl!(Foo); // Should not lint +} + +mod full_path_replacement { + trait Error { + fn custom(_msg: T) -> Self; + } + + impl Error for std::fmt::Error { + fn custom(_msg: T) -> Self { + Self // Should lint + } + } +} + fn main() {} diff --git a/tests/ui/use_self_trait.rs b/tests/ui/use_self_trait.rs index de305d40f330b..325dc73b21ea9 100644 --- a/tests/ui/use_self_trait.rs +++ b/tests/ui/use_self_trait.rs @@ -47,7 +47,6 @@ impl Mul for Bad { impl Clone for Bad { fn clone(&self) -> Self { - // FIXME: applicable here Bad } } @@ -112,4 +111,42 @@ impl NameTrait for u8 { } } +mod impl_in_macro { + macro_rules! parse_ip_impl { + // minimized from serde=1.0.118 + ($ty:ty) => { + impl FooTrait for $ty { + fn new() -> Self { + <$ty>::bar() + } + } + }; + } + + struct Foo; + + trait FooTrait { + fn new() -> Self; + } + + impl Foo { + fn bar() -> Self { + Self + } + } + parse_ip_impl!(Foo); // Should not lint +} + +mod full_path_replacement { + trait Error { + fn custom(_msg: T) -> Self; + } + + impl Error for std::fmt::Error { + fn custom(_msg: T) -> Self { + std::fmt::Error // Should lint + } + } +} + fn main() {} diff --git a/tests/ui/use_self_trait.stderr b/tests/ui/use_self_trait.stderr index 55af3ff2a93d9..090729b9c3d54 100644 --- a/tests/ui/use_self_trait.stderr +++ b/tests/ui/use_self_trait.stderr @@ -84,5 +84,17 @@ error: unnecessary structure name repetition LL | fn mul(self, rhs: Bad) -> Bad { | ^^^ help: use the applicable keyword: `Self` -error: aborting due to 14 previous errors +error: unnecessary structure name repetition + --> $DIR/use_self_trait.rs:50:9 + | +LL | Bad + | ^^^ help: use the applicable keyword: `Self` + +error: unnecessary structure name repetition + --> $DIR/use_self_trait.rs:147:13 + | +LL | std::fmt::Error // Should lint + | ^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` + +error: aborting due to 16 previous errors diff --git a/tests/ui/useless_attribute.fixed b/tests/ui/useless_attribute.fixed index c23231a99e9f0..871e4fb5c3a9f 100644 --- a/tests/ui/useless_attribute.fixed +++ b/tests/ui/useless_attribute.fixed @@ -1,6 +1,7 @@ // run-rustfix // aux-build:proc_macro_derive.rs +#![allow(unused)] #![warn(clippy::useless_attribute)] #![warn(unreachable_pub)] #![feature(rustc_private)] @@ -16,6 +17,13 @@ extern crate rustc_middle; #[macro_use] extern crate proc_macro_derive; +fn test_indented_attr() { + #![allow(clippy::almost_swapped)] + use std::collections::HashSet; + + let _ = HashSet::::default(); +} + // don't lint on unused_import for `use` items #[allow(unused_imports)] use std::collections; @@ -63,13 +71,16 @@ mod c { pub(crate) struct S; } -fn test_indented_attr() { - #![allow(clippy::almost_swapped)] - use std::collections::HashSet; - - let _ = HashSet::::default(); +// https://github.com/rust-lang/rust-clippy/issues/7511 +pub mod split { + #[allow(clippy::module_name_repetitions)] + pub use regex::SplitN; } +// https://github.com/rust-lang/rust-clippy/issues/8768 +#[allow(clippy::single_component_path_imports)] +use regex; + fn main() { test_indented_attr(); } diff --git a/tests/ui/useless_attribute.rs b/tests/ui/useless_attribute.rs index 7a7b198ea6078..cb50736ba395a 100644 --- a/tests/ui/useless_attribute.rs +++ b/tests/ui/useless_attribute.rs @@ -1,6 +1,7 @@ // run-rustfix // aux-build:proc_macro_derive.rs +#![allow(unused)] #![warn(clippy::useless_attribute)] #![warn(unreachable_pub)] #![feature(rustc_private)] @@ -16,6 +17,13 @@ extern crate rustc_middle; #[macro_use] extern crate proc_macro_derive; +fn test_indented_attr() { + #[allow(clippy::almost_swapped)] + use std::collections::HashSet; + + let _ = HashSet::::default(); +} + // don't lint on unused_import for `use` items #[allow(unused_imports)] use std::collections; @@ -63,13 +71,16 @@ mod c { pub(crate) struct S; } -fn test_indented_attr() { - #[allow(clippy::almost_swapped)] - use std::collections::HashSet; - - let _ = HashSet::::default(); +// https://github.com/rust-lang/rust-clippy/issues/7511 +pub mod split { + #[allow(clippy::module_name_repetitions)] + pub use regex::SplitN; } +// https://github.com/rust-lang/rust-clippy/issues/8768 +#[allow(clippy::single_component_path_imports)] +use regex; + fn main() { test_indented_attr(); } diff --git a/tests/ui/useless_attribute.stderr b/tests/ui/useless_attribute.stderr index 255d287635531..a7ea0df22945a 100644 --- a/tests/ui/useless_attribute.stderr +++ b/tests/ui/useless_attribute.stderr @@ -1,5 +1,5 @@ error: useless lint attribute - --> $DIR/useless_attribute.rs:8:1 + --> $DIR/useless_attribute.rs:9:1 | LL | #[allow(dead_code)] | ^^^^^^^^^^^^^^^^^^^ help: if you just forgot a `!`, use: `#![allow(dead_code)]` @@ -7,13 +7,13 @@ LL | #[allow(dead_code)] = note: `-D clippy::useless-attribute` implied by `-D warnings` error: useless lint attribute - --> $DIR/useless_attribute.rs:9:1 + --> $DIR/useless_attribute.rs:10:1 | LL | #[cfg_attr(feature = "cargo-clippy", allow(dead_code))] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if you just forgot a `!`, use: `#![cfg_attr(feature = "cargo-clippy", allow(dead_code)` error: useless lint attribute - --> $DIR/useless_attribute.rs:67:5 + --> $DIR/useless_attribute.rs:21:5 | LL | #[allow(clippy::almost_swapped)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if you just forgot a `!`, use: `#![allow(clippy::almost_swapped)]` diff --git a/tests/versioncheck.rs b/tests/versioncheck.rs index a6d8d0307ce53..7a85386a3df4b 100644 --- a/tests/versioncheck.rs +++ b/tests/versioncheck.rs @@ -6,7 +6,7 @@ use rustc_tools_util::VersionInfo; use std::fs; #[test] -fn check_that_clippy_lints_and_clippy_utils_have_the_same_version_as_clippy() { +fn consistent_clippy_crate_versions() { fn read_version(path: &str) -> String { let contents = fs::read_to_string(path).unwrap_or_else(|e| panic!("error reading `{path}`: {e:?}")); contents @@ -24,11 +24,16 @@ fn check_that_clippy_lints_and_clippy_utils_have_the_same_version_as_clippy() { } let clippy_version = read_version("Cargo.toml"); - let clippy_lints_version = read_version("clippy_lints/Cargo.toml"); - let clippy_utils_version = read_version("clippy_utils/Cargo.toml"); - assert_eq!(clippy_version, clippy_lints_version); - assert_eq!(clippy_version, clippy_utils_version); + let paths = [ + "declare_clippy_lint/Cargo.toml", + "clippy_lints/Cargo.toml", + "clippy_utils/Cargo.toml", + ]; + + for path in paths { + assert_eq!(clippy_version, read_version(path), "{path} version differs"); + } } #[test] From 28fb084ec70844a03f83dbd7a776ca0917b890be Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Mon, 21 Nov 2022 20:52:12 +0100 Subject: [PATCH 036/244] Fix declare_clippy_lint crate --- declare_clippy_lint/Cargo.toml | 3 +++ declare_clippy_lint/src/lib.rs | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/declare_clippy_lint/Cargo.toml b/declare_clippy_lint/Cargo.toml index 578109840fb70..082570f1fe5d8 100644 --- a/declare_clippy_lint/Cargo.toml +++ b/declare_clippy_lint/Cargo.toml @@ -11,3 +11,6 @@ proc-macro = true itertools = "0.10.1" quote = "1.0.21" syn = "1.0.100" + +[features] +deny-warnings = [] diff --git a/declare_clippy_lint/src/lib.rs b/declare_clippy_lint/src/lib.rs index 962766916dd1f..26210556d6526 100644 --- a/declare_clippy_lint/src/lib.rs +++ b/declare_clippy_lint/src/lib.rs @@ -1,5 +1,7 @@ #![feature(let_chains)] #![cfg_attr(feature = "deny-warnings", deny(warnings))] +// warn on lints, that are included in `rust-lang/rust`s bootstrap +#![warn(rust_2018_idioms, unused_lifetimes)] use proc_macro::TokenStream; use quote::{format_ident, quote}; @@ -29,7 +31,7 @@ struct ClippyLint { } impl Parse for ClippyLint { - fn parse(input: ParseStream) -> Result { + fn parse(input: ParseStream<'_>) -> Result { let attrs = input.call(Attribute::parse_outer)?; let mut in_code = false; From 6fce4691d523f0d22596c53e6fe420b442894482 Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Mon, 21 Nov 2022 21:04:59 +0100 Subject: [PATCH 037/244] Clippy: Don't import GenericParamDefKind --- clippy_utils/src/ty.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index 897edfc5495f4..a26cdf6471645 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -13,9 +13,9 @@ use rustc_infer::infer::TyCtxtInferExt; use rustc_lint::LateContext; use rustc_middle::mir::interpret::{ConstValue, Scalar}; use rustc_middle::ty::{ - self, AdtDef, AssocKind, Binder, BoundRegion, DefIdTree, FnSig, GenericParamDefKind, IntTy, List, ParamEnv, - Predicate, PredicateKind, ProjectionTy, Region, RegionKind, SubstsRef, Ty, TyCtxt, TypeSuperVisitable, - TypeVisitable, TypeVisitor, UintTy, VariantDef, VariantDiscr, + self, AdtDef, AssocKind, Binder, BoundRegion, DefIdTree, FnSig, IntTy, List, ParamEnv, Predicate, PredicateKind, + ProjectionTy, Region, RegionKind, SubstsRef, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor, UintTy, + VariantDef, VariantDiscr, }; use rustc_middle::ty::{GenericArg, GenericArgKind}; use rustc_span::symbol::Ident; @@ -1011,7 +1011,7 @@ pub fn make_projection<'tcx>( assoc_item.def_id, substs.len(), generic_count, - params.map(GenericParamDefKind::descr).collect::>(), + params.map(ty::GenericParamDefKind::descr).collect::>(), substs, ); @@ -1022,9 +1022,9 @@ pub fn make_projection<'tcx>( .find(|(_, (param, arg))| { !matches!( (param, arg), - (GenericParamDefKind::Lifetime, GenericArgKind::Lifetime(_)) - | (GenericParamDefKind::Type { .. }, GenericArgKind::Type(_)) - | (GenericParamDefKind::Const { .. }, GenericArgKind::Const(_)) + (ty::GenericParamDefKind::Lifetime, GenericArgKind::Lifetime(_)) + | (ty::GenericParamDefKind::Type { .. }, GenericArgKind::Type(_)) + | (ty::GenericParamDefKind::Const { .. }, GenericArgKind::Const(_)) ) }) { @@ -1036,7 +1036,7 @@ pub fn make_projection<'tcx>( idx, param.descr(), arg, - params.map(GenericParamDefKind::descr).collect::>(), + params.map(ty::GenericParamDefKind::descr).collect::>(), substs, ); } From ca57832db636fedeeb529a1cc1feab0befef2ab0 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 21 Nov 2022 20:16:36 +0000 Subject: [PATCH 038/244] Add more regression tests --- .../rustc_borrowck/src/region_infer/mod.rs | 12 +++--- .../imply_bounds_from_bounds.rs | 25 ++++++++++++ .../imply_bounds_from_bounds_param.rs | 38 +++++++++++++++++++ .../imply_bounds_from_bounds_param.stderr | 16 ++++++++ .../type-alias-impl-trait/self_implication.rs | 38 +++++++++++++++++++ 5 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 src/test/ui/type-alias-impl-trait/imply_bounds_from_bounds.rs create mode 100644 src/test/ui/type-alias-impl-trait/imply_bounds_from_bounds_param.rs create mode 100644 src/test/ui/type-alias-impl-trait/imply_bounds_from_bounds_param.stderr create mode 100644 src/test/ui/type-alias-impl-trait/self_implication.rs diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index b35abbd107b9c..90e2b6b698cf8 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -1705,6 +1705,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { }); } + #[instrument(level = "debug", skip(self, infcx, errors_buffer))] fn check_member_constraints( &self, infcx: &InferCtxt<'tcx>, @@ -1712,22 +1713,21 @@ impl<'tcx> RegionInferenceContext<'tcx> { ) { let member_constraints = self.member_constraints.clone(); for m_c_i in member_constraints.all_indices() { - debug!("check_member_constraint(m_c_i={:?})", m_c_i); + debug!(?m_c_i); let m_c = &member_constraints[m_c_i]; let member_region_vid = m_c.member_region_vid; debug!( - "check_member_constraint: member_region_vid={:?} with value {}", - member_region_vid, - self.region_value_str(member_region_vid), + ?member_region_vid, + value = ?self.region_value_str(member_region_vid), ); let choice_regions = member_constraints.choice_regions(m_c_i); - debug!("check_member_constraint: choice_regions={:?}", choice_regions); + debug!(?choice_regions); // Did the member region wind up equal to any of the option regions? if let Some(o) = choice_regions.iter().find(|&&o_r| self.eval_equal(o_r, m_c.member_region_vid)) { - debug!("check_member_constraint: evaluated as equal to {:?}", o); + debug!("evaluated as equal to {:?}", o); continue; } diff --git a/src/test/ui/type-alias-impl-trait/imply_bounds_from_bounds.rs b/src/test/ui/type-alias-impl-trait/imply_bounds_from_bounds.rs new file mode 100644 index 0000000000000..ee9bce15d3478 --- /dev/null +++ b/src/test/ui/type-alias-impl-trait/imply_bounds_from_bounds.rs @@ -0,0 +1,25 @@ +// check-pass + +#![feature(type_alias_impl_trait)] + +trait Callable { + type Output; + fn call() -> Self::Output; +} + +impl<'a> Callable for &'a () { + type Output = impl Sized; + fn call() -> Self::Output {} +} + +fn test<'a>() -> impl Sized { + <&'a () as Callable>::call() +} + +fn want_static(_: T) {} + +fn test2<'a>() { + want_static(<&'a () as Callable>::call()); +} + +fn main() {} diff --git a/src/test/ui/type-alias-impl-trait/imply_bounds_from_bounds_param.rs b/src/test/ui/type-alias-impl-trait/imply_bounds_from_bounds_param.rs new file mode 100644 index 0000000000000..ae21a9134a416 --- /dev/null +++ b/src/test/ui/type-alias-impl-trait/imply_bounds_from_bounds_param.rs @@ -0,0 +1,38 @@ +#![feature(type_alias_impl_trait)] + +trait Callable { + type Output; + fn call(x: Self) -> Self::Output; +} + +trait PlusOne { + fn plus_one(&mut self); +} + +impl<'a> PlusOne for &'a mut i32 { + fn plus_one(&mut self) { + **self += 1; + } +} + +impl Callable for T { + type Output = impl PlusOne; + fn call(t: T) -> Self::Output { t } +} + +fn test<'a>(y: &'a mut i32) -> impl PlusOne { + <&'a mut i32 as Callable>::call(y) + //~^ ERROR hidden type for `impl PlusOne` captures lifetime that does not appear in bounds +} + +fn main() { + let mut z = 42; + let mut thing = test(&mut z); + let mut thing2 = test(&mut z); + thing.plus_one(); + assert_eq!(z, 43); + thing2.plus_one(); + assert_eq!(z, 44); + thing.plus_one(); + assert_eq!(z, 45); +} diff --git a/src/test/ui/type-alias-impl-trait/imply_bounds_from_bounds_param.stderr b/src/test/ui/type-alias-impl-trait/imply_bounds_from_bounds_param.stderr new file mode 100644 index 0000000000000..0ed8a703b6dc5 --- /dev/null +++ b/src/test/ui/type-alias-impl-trait/imply_bounds_from_bounds_param.stderr @@ -0,0 +1,16 @@ +error[E0700]: hidden type for `impl PlusOne` captures lifetime that does not appear in bounds + --> $DIR/imply_bounds_from_bounds_param.rs:24:5 + | +LL | fn test<'a>(y: &'a mut i32) -> impl PlusOne { + | -- hidden type `<&'a mut i32 as Callable>::Output` captures the lifetime `'a` as defined here +LL | <&'a mut i32 as Callable>::call(y) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: to declare that `impl PlusOne` captures `'a`, you can add an explicit `'a` lifetime bound + | +LL | fn test<'a>(y: &'a mut i32) -> impl PlusOne + 'a { + | ++++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0700`. diff --git a/src/test/ui/type-alias-impl-trait/self_implication.rs b/src/test/ui/type-alias-impl-trait/self_implication.rs new file mode 100644 index 0000000000000..4e805ee308f4d --- /dev/null +++ b/src/test/ui/type-alias-impl-trait/self_implication.rs @@ -0,0 +1,38 @@ +// check-pass + +#![feature(type_alias_impl_trait)] +fn foo() { + struct Foo<'a> { + x: &'a mut u8, + } + impl<'a> Foo<'a> { + fn foo(&self) -> impl Sized {} + } + // use site + let mut x = 5; + let y = Foo { x: &mut x }; + let z = y.foo(); + let _a = &x; // invalidate the `&'a mut`in `y` + let _b = z; // this should *not* check that `'a` in the type `Foo<'a>::foo::opaque` is live +} + +fn bar() { + struct Foo<'a> { + x: &'a mut u8, + } + + // desugared + type FooX<'a> = impl Sized; + impl<'a> Foo<'a> { + fn foo(&self) -> FooX<'a> {} + } + + // use site + let mut x = 5; + let y = Foo { x: &mut x }; + let z = y.foo(); + let _a = &x; // invalidate the `&'a mut`in `y` + let _b = z; // this should *not* check that `'a` in the type `Foo<'a>::foo::opaque` is live +} + +fn main() {} From c7828221e3fe88d54be2c2c7d0dda226f6be60c4 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 17 Nov 2022 13:00:35 +0000 Subject: [PATCH 039/244] Allow iterators instead of requiring slices that will get turned into iterators --- clippy_lints/src/bool_assert_comparison.rs | 2 +- clippy_lints/src/dereference.rs | 2 +- clippy_utils/src/ty.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/bool_assert_comparison.rs b/clippy_lints/src/bool_assert_comparison.rs index 4bd55c1429c3b..82d368bb8bc2c 100644 --- a/clippy_lints/src/bool_assert_comparison.rs +++ b/clippy_lints/src/bool_assert_comparison.rs @@ -59,7 +59,7 @@ fn is_impl_not_trait_with_bool_out(cx: &LateContext<'_>, e: &Expr<'_>) -> bool { ) }) .map_or(false, |assoc_item| { - let proj = cx.tcx.mk_projection(assoc_item.def_id, cx.tcx.mk_substs_trait(ty, &[])); + let proj = cx.tcx.mk_projection(assoc_item.def_id, cx.tcx.mk_substs_trait(ty, [])); let nty = cx.tcx.normalize_erasing_regions(cx.param_env, proj); nty.is_bool() diff --git a/clippy_lints/src/dereference.rs b/clippy_lints/src/dereference.rs index 218dbeaddcade..03d865af374a2 100644 --- a/clippy_lints/src/dereference.rs +++ b/clippy_lints/src/dereference.rs @@ -1263,7 +1263,7 @@ fn replace_types<'tcx>( let item_def_id = projection_predicate.projection_ty.item_def_id; let assoc_item = cx.tcx.associated_item(item_def_id); let projection = cx.tcx - .mk_projection(assoc_item.def_id, cx.tcx.mk_substs_trait(new_ty, &[])); + .mk_projection(assoc_item.def_id, cx.tcx.mk_substs_trait(new_ty, [])); if let Ok(projected_ty) = cx.tcx.try_normalize_erasing_regions(cx.param_env, projection) && substs[term_param_ty.index as usize] != ty::GenericArg::from(projected_ty) diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index 3a144c2bb2239..a1698a61e6014 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -79,7 +79,7 @@ pub fn get_associated_type<'tcx>( .associated_items(trait_id) .find_by_name_and_kind(cx.tcx, Ident::from_str(name), ty::AssocKind::Type, trait_id) .and_then(|assoc| { - let proj = cx.tcx.mk_projection(assoc.def_id, cx.tcx.mk_substs_trait(ty, &[])); + let proj = cx.tcx.mk_projection(assoc.def_id, cx.tcx.mk_substs_trait(ty, [])); cx.tcx.try_normalize_erasing_regions(cx.param_env, proj).ok() }) } From f60e43ee05620e413d5e7dc069b3334ff4d1d0ed Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Fri, 18 Nov 2022 19:58:07 +0000 Subject: [PATCH 040/244] Fix clippy's missing substs --- clippy_lints/src/derive.rs | 4 ++-- clippy_lints/src/eta_reduction.rs | 4 +++- clippy_lints/src/methods/unnecessary_to_owned.rs | 4 ++-- clippy_lints/src/needless_pass_by_value.rs | 4 ++-- clippy_lints/src/neg_cmp_op_on_partial_ord.rs | 2 +- clippy_utils/src/ty.rs | 14 +++++++++----- 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/clippy_lints/src/derive.rs b/clippy_lints/src/derive.rs index 102a02138bc87..1d9af7cdbd358 100644 --- a/clippy_lints/src/derive.rs +++ b/clippy_lints/src/derive.rs @@ -466,12 +466,12 @@ fn check_partial_eq_without_eq<'tcx>(cx: &LateContext<'tcx>, span: Span, trait_r if let Some(def_id) = trait_ref.trait_def_id(); if cx.tcx.is_diagnostic_item(sym::PartialEq, def_id); let param_env = param_env_for_derived_eq(cx.tcx, adt.did(), eq_trait_def_id); - if !implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, &[]); + if !implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, []); // If all of our fields implement `Eq`, we can implement `Eq` too if adt .all_fields() .map(|f| f.ty(cx.tcx, substs)) - .all(|ty| implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, &[])); + .all(|ty| implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, [])); then { span_lint_and_sugg( cx, diff --git a/clippy_lints/src/eta_reduction.rs b/clippy_lints/src/eta_reduction.rs index 7b9786d7e570f..ea4e5e052d029 100644 --- a/clippy_lints/src/eta_reduction.rs +++ b/clippy_lints/src/eta_reduction.rs @@ -119,11 +119,13 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction { let callee_ty_unadjusted = cx.typeck_results().expr_ty(callee).peel_refs(); if !is_type_diagnostic_item(cx, callee_ty_unadjusted, sym::Arc); if !is_type_diagnostic_item(cx, callee_ty_unadjusted, sym::Rc); + if let ty::Closure(_, substs) = *closure_ty.kind(); then { span_lint_and_then(cx, REDUNDANT_CLOSURE, expr.span, "redundant closure", |diag| { if let Some(mut snippet) = snippet_opt(cx, callee.span) { if let Some(fn_mut_id) = cx.tcx.lang_items().fn_mut_trait() - && implements_trait(cx, callee_ty.peel_refs(), fn_mut_id, &[]) + && let args = cx.tcx.erase_late_bound_regions(ty::ClosureSubsts { substs }.sig()).inputs() + && implements_trait(cx, callee_ty.peel_refs(), fn_mut_id, &args.iter().copied().map(Into::into).collect::>()) && path_to_local(callee).map_or(false, |l| local_used_after_expr(cx, l, expr)) { // Mutable closure is used after current expr; we cannot consume it. diff --git a/clippy_lints/src/methods/unnecessary_to_owned.rs b/clippy_lints/src/methods/unnecessary_to_owned.rs index c7775313ecd02..375ebc903b400 100644 --- a/clippy_lints/src/methods/unnecessary_to_owned.rs +++ b/clippy_lints/src/methods/unnecessary_to_owned.rs @@ -474,7 +474,7 @@ fn is_cow_into_owned(cx: &LateContext<'_>, method_name: Symbol, method_def_id: D } /// Returns true if the named method is `ToString::to_string` and it's called on a type that -/// is string-like i.e. implements `AsRef` or `Deref`. +/// is string-like i.e. implements `AsRef` or `Deref`. fn is_to_string_on_string_like<'a>( cx: &LateContext<'_>, call_expr: &'a Expr<'a>, @@ -490,7 +490,7 @@ fn is_to_string_on_string_like<'a>( && let GenericArgKind::Type(ty) = generic_arg.unpack() && let Some(deref_trait_id) = cx.tcx.get_diagnostic_item(sym::Deref) && let Some(as_ref_trait_id) = cx.tcx.get_diagnostic_item(sym::AsRef) - && (implements_trait(cx, ty, deref_trait_id, &[cx.tcx.types.str_.into()]) || + && (get_associated_type(cx, ty, deref_trait_id, "Target") == Some(cx.tcx.types.str_) || implements_trait(cx, ty, as_ref_trait_id, &[cx.tcx.types.str_.into()])) { true } else { diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index 79aa15b06ef4d..eeff15bbfb422 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -1,7 +1,7 @@ use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then}; use clippy_utils::ptr::get_spans; use clippy_utils::source::{snippet, snippet_opt}; -use clippy_utils::ty::{implements_trait, is_copy, is_type_diagnostic_item, is_type_lang_item}; +use clippy_utils::ty::{implements_trait, implements_trait_with_env, is_copy, is_type_diagnostic_item, is_type_lang_item}; use clippy_utils::{get_trait_def_id, is_self, paths}; use if_chain::if_chain; use rustc_ast::ast::Attribute; @@ -185,7 +185,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue { if !ty.is_mutable_ptr(); if !is_copy(cx, ty); if ty.is_sized(cx.tcx, cx.param_env); - if !allowed_traits.iter().any(|&t| implements_trait(cx, ty, t, &[])); + if !allowed_traits.iter().any(|&t| implements_trait_with_env(cx.tcx, cx.param_env, ty, t, [None])); if !implements_borrow_trait; if !all_borrowable_trait; diff --git a/clippy_lints/src/neg_cmp_op_on_partial_ord.rs b/clippy_lints/src/neg_cmp_op_on_partial_ord.rs index 5c2b96f5b2ce6..a022fc156fca2 100644 --- a/clippy_lints/src/neg_cmp_op_on_partial_ord.rs +++ b/clippy_lints/src/neg_cmp_op_on_partial_ord.rs @@ -65,7 +65,7 @@ impl<'tcx> LateLintPass<'tcx> for NoNegCompOpForPartialOrd { let implements_partial_ord = { if let Some(id) = cx.tcx.lang_items().partial_ord_trait() { - implements_trait(cx, ty, id, &[]) + implements_trait(cx, ty, id, &[ty.into()]) } else { return; } diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index a1698a61e6014..a8047fe9e5ea1 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -9,7 +9,7 @@ use rustc_hir as hir; use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res}; use rustc_hir::def_id::DefId; use rustc_hir::{Expr, FnDecl, LangItem, TyKind, Unsafety}; -use rustc_infer::infer::TyCtxtInferExt; +use rustc_infer::infer::{TyCtxtInferExt, type_variable::{TypeVariableOrigin, TypeVariableOriginKind}}; use rustc_lint::LateContext; use rustc_middle::mir::interpret::{ConstValue, Scalar}; use rustc_middle::ty::{ @@ -18,7 +18,7 @@ use rustc_middle::ty::{ }; use rustc_middle::ty::{GenericArg, GenericArgKind}; use rustc_span::symbol::Ident; -use rustc_span::{sym, Span, Symbol}; +use rustc_span::{sym, Span, Symbol, DUMMY_SP}; use rustc_target::abi::{Size, VariantIdx}; use rustc_trait_selection::infer::InferCtxtExt; use rustc_trait_selection::traits::query::normalize::AtExt; @@ -153,7 +153,7 @@ pub fn implements_trait<'tcx>( trait_id: DefId, ty_params: &[GenericArg<'tcx>], ) -> bool { - implements_trait_with_env(cx.tcx, cx.param_env, ty, trait_id, ty_params) + implements_trait_with_env(cx.tcx, cx.param_env, ty, trait_id, ty_params.iter().map(|&arg| Some(arg))) } /// Same as `implements_trait` but allows using a `ParamEnv` different from the lint context. @@ -162,7 +162,7 @@ pub fn implements_trait_with_env<'tcx>( param_env: ParamEnv<'tcx>, ty: Ty<'tcx>, trait_id: DefId, - ty_params: &[GenericArg<'tcx>], + ty_params: impl IntoIterator>>, ) -> bool { // Clippy shouldn't have infer types assert!(!ty.needs_infer()); @@ -171,8 +171,12 @@ pub fn implements_trait_with_env<'tcx>( if ty.has_escaping_bound_vars() { return false; } - let ty_params = tcx.mk_substs(ty_params.iter()); let infcx = tcx.infer_ctxt().build(); + let orig = TypeVariableOrigin { + kind: TypeVariableOriginKind::MiscVariable, + span: DUMMY_SP, + }; + let ty_params = tcx.mk_substs(ty_params.into_iter().map(|arg| arg.unwrap_or_else(|| infcx.next_ty_var(orig).into()))); infcx .type_implements_trait(trait_id, ty, ty_params, param_env) .must_apply_modulo_regions() From 595ae838558f767a4d54b1c69843f6c53314ebc9 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 21 Nov 2022 12:24:53 +0000 Subject: [PATCH 041/244] Stop passing the self-type as a separate argument. --- clippy_lints/src/dereference.rs | 12 ++++-------- clippy_lints/src/ptr.rs | 4 ++-- clippy_utils/src/ty.rs | 2 +- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/clippy_lints/src/dereference.rs b/clippy_lints/src/dereference.rs index 03d865af374a2..c4e7f8bfe1e20 100644 --- a/clippy_lints/src/dereference.rs +++ b/clippy_lints/src/dereference.rs @@ -842,14 +842,10 @@ fn walk_parents<'tcx>( } else if let Some(trait_id) = cx.tcx.trait_of_item(id) && let arg_ty = cx.tcx.erase_regions(cx.typeck_results().expr_ty_adjusted(e)) && let ty::Ref(_, sub_ty, _) = *arg_ty.kind() - && let subs = match cx + && let subs = cx .typeck_results() - .node_substs_opt(parent.hir_id) - .and_then(|subs| subs.get(1..)) - { - Some(subs) => cx.tcx.mk_substs(subs.iter().copied()), - None => cx.tcx.mk_substs(std::iter::empty::>()), - } && let impl_ty = if cx.tcx.fn_sig(id).skip_binder().inputs()[0].is_ref() { + .node_substs_opt(parent.hir_id).map(|subs| &subs[1..]).unwrap_or_default() + && let impl_ty = if cx.tcx.fn_sig(id).skip_binder().inputs()[0].is_ref() { // Trait methods taking `&self` sub_ty } else { @@ -858,7 +854,7 @@ fn walk_parents<'tcx>( } && impl_ty.is_ref() && let infcx = cx.tcx.infer_ctxt().build() && infcx - .type_implements_trait(trait_id, impl_ty, subs, cx.param_env) + .type_implements_trait(trait_id, [impl_ty.into()].into_iter().chain(subs.iter().copied()), cx.param_env) .must_apply_modulo_regions() { return Some(Position::MethodReceiverRefImpl) diff --git a/clippy_lints/src/ptr.rs b/clippy_lints/src/ptr.rs index ea00650d42aa1..5420a0e782ea8 100644 --- a/clippy_lints/src/ptr.rs +++ b/clippy_lints/src/ptr.rs @@ -692,7 +692,7 @@ fn matches_preds<'tcx>( let infcx = cx.tcx.infer_ctxt().build(); preds.iter().all(|&p| match cx.tcx.erase_late_bound_regions(p) { ExistentialPredicate::Trait(p) => infcx - .type_implements_trait(p.def_id, ty, p.substs, cx.param_env) + .type_implements_trait(p.def_id, [ty.into()].into_iter().chain(p.substs.iter()), cx.param_env) .must_apply_modulo_regions(), ExistentialPredicate::Projection(p) => infcx.predicate_must_hold_modulo_regions(&Obligation::new( cx.tcx, @@ -704,7 +704,7 @@ fn matches_preds<'tcx>( )), )), ExistentialPredicate::AutoTrait(p) => infcx - .type_implements_trait(p, ty, List::empty(), cx.param_env) + .type_implements_trait(p, [ty], cx.param_env) .must_apply_modulo_regions(), }) } diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index a8047fe9e5ea1..5ec6f29fe9163 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -178,7 +178,7 @@ pub fn implements_trait_with_env<'tcx>( }; let ty_params = tcx.mk_substs(ty_params.into_iter().map(|arg| arg.unwrap_or_else(|| infcx.next_ty_var(orig).into()))); infcx - .type_implements_trait(trait_id, ty, ty_params, param_env) + .type_implements_trait(trait_id, [ty.into()].into_iter().chain(ty_params), param_env) .must_apply_modulo_regions() } From eb850aef96ed1914a82feb1e297a6c9f5b71cae3 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 21 Nov 2022 16:52:01 +0100 Subject: [PATCH 042/244] Use `as_closure` helper method Co-authored-by: lcnr --- clippy_lints/src/eta_reduction.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/eta_reduction.rs b/clippy_lints/src/eta_reduction.rs index ea4e5e052d029..f34cbee03558b 100644 --- a/clippy_lints/src/eta_reduction.rs +++ b/clippy_lints/src/eta_reduction.rs @@ -124,7 +124,7 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction { span_lint_and_then(cx, REDUNDANT_CLOSURE, expr.span, "redundant closure", |diag| { if let Some(mut snippet) = snippet_opt(cx, callee.span) { if let Some(fn_mut_id) = cx.tcx.lang_items().fn_mut_trait() - && let args = cx.tcx.erase_late_bound_regions(ty::ClosureSubsts { substs }.sig()).inputs() + && let args = cx.tcx.erase_late_bound_regions(substs.as_closure().sig()).inputs() && implements_trait(cx, callee_ty.peel_refs(), fn_mut_id, &args.iter().copied().map(Into::into).collect::>()) && path_to_local(callee).map_or(false, |l| local_used_after_expr(cx, l, expr)) { From 48b10feedbee5b8554fa82696ce5e836933f189c Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 18 Nov 2022 11:24:21 +1100 Subject: [PATCH 043/244] Split `MacArgs` in two. `MacArgs` is an enum with three variants: `Empty`, `Delimited`, and `Eq`. It's used in two ways: - For representing attribute macro arguments (e.g. in `AttrItem`), where all three variants are used. - For representing function-like macros (e.g. in `MacCall` and `MacroDef`), where only the `Delimited` variant is used. In other words, `MacArgs` is used in two quite different places due to them having partial overlap. I find this makes the code hard to read. It also leads to various unreachable code paths, and allows invalid values (such as accidentally using `MacArgs::Empty` in a `MacCall`). This commit splits `MacArgs` in two: - `DelimArgs` is a new struct just for the "delimited arguments" case. It is now used in `MacCall` and `MacroDef`. - `AttrArgs` is a renaming of the old `MacArgs` enum for the attribute macro case. Its `Delimited` variant now contains a `DelimArgs`. Various other related things are renamed as well. These changes make the code clearer, avoids several unreachable paths, and disallows the invalid values. --- clippy_lints/src/crate_in_macro_def.rs | 2 +- clippy_utils/src/ast_utils.rs | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/clippy_lints/src/crate_in_macro_def.rs b/clippy_lints/src/crate_in_macro_def.rs index 20cc330e035f8..b2fe0386f945d 100644 --- a/clippy_lints/src/crate_in_macro_def.rs +++ b/clippy_lints/src/crate_in_macro_def.rs @@ -55,7 +55,7 @@ impl EarlyLintPass for CrateInMacroDef { if_chain! { if item.attrs.iter().any(is_macro_export); if let ItemKind::MacroDef(macro_def) = &item.kind; - let tts = macro_def.body.inner_tokens(); + let tts = macro_def.body.tokens.clone(); if let Some(span) = contains_unhygienic_crate_reference(&tts); then { span_lint_and_sugg( diff --git a/clippy_utils/src/ast_utils.rs b/clippy_utils/src/ast_utils.rs index 23aed4b5ba2f4..87b378bfd1982 100644 --- a/clippy_utils/src/ast_utils.rs +++ b/clippy_utils/src/ast_utils.rs @@ -388,7 +388,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool { && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind)) }, (MacCall(l), MacCall(r)) => eq_mac_call(l, r), - (MacroDef(l), MacroDef(r)) => l.macro_rules == r.macro_rules && eq_mac_args(&l.body, &r.body), + (MacroDef(l), MacroDef(r)) => l.macro_rules == r.macro_rules && eq_delim_args(&l.body, &r.body), _ => false, } } @@ -709,7 +709,7 @@ pub fn eq_assoc_constraint(l: &AssocConstraint, r: &AssocConstraint) -> bool { } pub fn eq_mac_call(l: &MacCall, r: &MacCall) -> bool { - eq_path(&l.path, &r.path) && eq_mac_args(&l.args, &r.args) + eq_path(&l.path, &r.path) && eq_delim_args(&l.args, &r.args) } pub fn eq_attr(l: &Attribute, r: &Attribute) -> bool { @@ -717,18 +717,22 @@ pub fn eq_attr(l: &Attribute, r: &Attribute) -> bool { l.style == r.style && match (&l.kind, &r.kind) { (DocComment(l1, l2), DocComment(r1, r2)) => l1 == r1 && l2 == r2, - (Normal(l), Normal(r)) => eq_path(&l.item.path, &r.item.path) && eq_mac_args(&l.item.args, &r.item.args), + (Normal(l), Normal(r)) => eq_path(&l.item.path, &r.item.path) && eq_attr_args(&l.item.args, &r.item.args), _ => false, } } -pub fn eq_mac_args(l: &MacArgs, r: &MacArgs) -> bool { - use MacArgs::*; +pub fn eq_attr_args(l: &AttrArgs, r: &AttrArgs) -> bool { + use AttrArgs::*; match (l, r) { (Empty, Empty) => true, - (Delimited(_, ld, lts), Delimited(_, rd, rts)) => ld == rd && lts.eq_unspanned(rts), - (Eq(_, MacArgsEq::Ast(le)), Eq(_, MacArgsEq::Ast(re))) => eq_expr(le, re), - (Eq(_, MacArgsEq::Hir(ll)), Eq(_, MacArgsEq::Hir(rl))) => ll.kind == rl.kind, + (Delimited(la), Delimited(ra)) => eq_delim_args(la, ra), + (Eq(_, AttrArgsEq::Ast(le)), Eq(_, AttrArgsEq::Ast(re))) => eq_expr(le, re), + (Eq(_, AttrArgsEq::Hir(ll)), Eq(_, AttrArgsEq::Hir(rl))) => ll.kind == rl.kind, _ => false, } } + +pub fn eq_delim_args(l: &DelimArgs, r: &DelimArgs) -> bool { + l.delim == r.delim && l.tokens.eq_unspanned(&r.tokens) +} From daf93df430eddbaeb31fea2fcb91c974b0b7bcf5 Mon Sep 17 00:00:00 2001 From: Ding Xiang Fei Date: Tue, 22 Nov 2022 07:29:39 +0800 Subject: [PATCH 044/244] restore control flow on error in EUV --- .../rustc_hir_typeck/src/expr_use_visitor.rs | 17 +++++++++++------ compiler/rustc_hir_typeck/src/upvar.rs | 2 +- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs index 275f7d12148c9..e5e798f4b933f 100644 --- a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs +++ b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs @@ -252,11 +252,11 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> { hir::ExprKind::Match(ref discr, arms, _) => { let discr_place = return_if_err!(self.mc.cat_expr(discr)); - self.maybe_read_scrutinee( + return_if_err!(self.maybe_read_scrutinee( discr, discr_place.clone(), arms.iter().map(|arm| arm.pat), - ); + )); // treatment of the discriminant is handled while walking the arms. for arm in arms { @@ -390,7 +390,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> { discr: &Expr<'_>, discr_place: PlaceWithHirId<'tcx>, pats: impl Iterator>, - ) { + ) -> Result<(), ()> { // Matching should not always be considered a use of the place, hence // discr does not necessarily need to be borrowed. // We only want to borrow discr if the pattern contain something other @@ -398,7 +398,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> { let ExprUseVisitor { ref mc, body_owner: _, delegate: _ } = *self; let mut needs_to_be_read = false; for pat in pats { - return_if_err!(mc.cat_pattern(discr_place.clone(), pat, |place, pat| { + mc.cat_pattern(discr_place.clone(), pat, |place, pat| { match &pat.kind { PatKind::Binding(.., opt_sub_pat) => { // If the opt_sub_pat is None, than the binding does not count as @@ -453,7 +453,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> { // examined } } - })); + })? } if needs_to_be_read { @@ -474,6 +474,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> { // that the discriminant has been initialized. self.walk_expr(discr); } + Ok(()) } fn walk_local( @@ -490,7 +491,11 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> { f(self); if let Some(els) = els { // borrowing because we need to test the discriminant - self.maybe_read_scrutinee(expr, expr_place.clone(), from_ref(pat).iter()); + return_if_err!(self.maybe_read_scrutinee( + expr, + expr_place.clone(), + from_ref(pat).iter() + )); self.walk_block(els) } self.walk_irrefutable_pat(&expr_place, &pat); diff --git a/compiler/rustc_hir_typeck/src/upvar.rs b/compiler/rustc_hir_typeck/src/upvar.rs index 4dea40829f622..b68508f9e683e 100644 --- a/compiler/rustc_hir_typeck/src/upvar.rs +++ b/compiler/rustc_hir_typeck/src/upvar.rs @@ -2184,7 +2184,7 @@ fn determine_place_ancestry_relation<'tcx>( place_a: &Place<'tcx>, place_b: &Place<'tcx>, ) -> PlaceAncestryRelation { - // If Place A and Place B, don't start off from the same root variable, they are divergent. + // If Place A and Place B don't start off from the same root variable, they are divergent. if place_a.base != place_b.base { return PlaceAncestryRelation::Divergent; } From e95d40980b3044a6a9cad1b5e72eb57390790d18 Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Tue, 22 Nov 2022 14:30:29 +0100 Subject: [PATCH 045/244] Clippy: Workaround for let_chains issue --- src/driver.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/driver.rs b/src/driver.rs index ee2a3ad20d3e5..ad6132a49baae 100644 --- a/src/driver.rs +++ b/src/driver.rs @@ -90,11 +90,12 @@ fn track_files(parse_sess: &mut ParseSess, conf_path_string: Option) { // During development track the `clippy-driver` executable so that cargo will re-run clippy whenever // it is rebuilt - if cfg!(debug_assertions) - && let Ok(current_exe) = env::current_exe() - && let Some(current_exe) = current_exe.to_str() - { - file_depinfo.insert(Symbol::intern(current_exe)); + if cfg!(debug_assertions) { + if let Ok(current_exe) = env::current_exe() + && let Some(current_exe) = current_exe.to_str() + { + file_depinfo.insert(Symbol::intern(current_exe)); + } } } From 2cda73f617028712c097124f8f38ace4660c066d Mon Sep 17 00:00:00 2001 From: Samuel Moelius Date: Tue, 22 Nov 2022 13:47:38 -0500 Subject: [PATCH 046/244] Use `walk_generic_arg` --- clippy_lints/src/lifetimes.rs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/clippy_lints/src/lifetimes.rs b/clippy_lints/src/lifetimes.rs index d9acaa99c6d19..897428797e57c 100644 --- a/clippy_lints/src/lifetimes.rs +++ b/clippy_lints/src/lifetimes.rs @@ -3,7 +3,7 @@ use clippy_utils::trait_ref_of_method; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_hir::intravisit::nested_filter::{self as hir_nested_filter, NestedFilter}; use rustc_hir::intravisit::{ - walk_fn_decl, walk_generic_param, walk_generics, walk_impl_item_ref, walk_item, walk_param_bound, + walk_fn_decl, walk_generic_arg, walk_generic_param, walk_generics, walk_impl_item_ref, walk_item, walk_param_bound, walk_poly_trait_ref, walk_trait_ref, walk_ty, Visitor, }; use rustc_hir::lang_items; @@ -503,14 +503,7 @@ impl<'a, 'tcx> Visitor<'tcx> for RefVisitor<'a, 'tcx> { { self.lifetime_generic_arg_spans.entry(def_id).or_insert(l.span); } - // Replace with `walk_generic_arg` if/when https://github.com/rust-lang/rust/pull/103692 lands. - // walk_generic_arg(self, generic_arg); - match generic_arg { - GenericArg::Lifetime(lt) => self.visit_lifetime(lt), - GenericArg::Type(ty) => self.visit_ty(ty), - GenericArg::Const(ct) => self.visit_anon_const(&ct.value), - GenericArg::Infer(inf) => self.visit_infer(inf), - } + walk_generic_arg(self, generic_arg); } } From 284ce9ed0d18b4582e1d377a501355fa685300fd Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Tue, 22 Nov 2022 19:52:46 +0000 Subject: [PATCH 047/244] Move `get_associated_type` from `clippy` to `rustc_lint` --- .../src/methods/iter_overeager_cloned.rs | 4 +-- .../src/methods/unnecessary_iter_cloned.rs | 4 +-- .../src/methods/unnecessary_to_owned.rs | 27 ++++++++++++------- clippy_utils/src/ty.rs | 19 +------------ 4 files changed, 23 insertions(+), 31 deletions(-) diff --git a/clippy_lints/src/methods/iter_overeager_cloned.rs b/clippy_lints/src/methods/iter_overeager_cloned.rs index 06a39c5997e20..b4210d875104b 100644 --- a/clippy_lints/src/methods/iter_overeager_cloned.rs +++ b/clippy_lints/src/methods/iter_overeager_cloned.rs @@ -1,6 +1,6 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::source::snippet_opt; -use clippy_utils::ty::{get_associated_type, implements_trait, is_copy}; +use clippy_utils::ty::{implements_trait, is_copy}; use rustc_errors::Applicability; use rustc_hir::Expr; use rustc_lint::LateContext; @@ -25,7 +25,7 @@ pub(super) fn check<'tcx>( && let Some(method_id) = typeck.type_dependent_def_id(cloned_call.hir_id) && cx.tcx.trait_of_item(method_id) == Some(iter_id) && let cloned_recv_ty = typeck.expr_ty_adjusted(cloned_recv) - && let Some(iter_assoc_ty) = get_associated_type(cx, cloned_recv_ty, iter_id, "Item") + && let Some(iter_assoc_ty) = cx.get_associated_type(cloned_recv_ty, iter_id, "Item") && matches!(*iter_assoc_ty.kind(), ty::Ref(_, ty, _) if !is_copy(cx, ty)) { if needs_into_iter diff --git a/clippy_lints/src/methods/unnecessary_iter_cloned.rs b/clippy_lints/src/methods/unnecessary_iter_cloned.rs index 4eb579af7a12a..52a4ff7d1ae4f 100644 --- a/clippy_lints/src/methods/unnecessary_iter_cloned.rs +++ b/clippy_lints/src/methods/unnecessary_iter_cloned.rs @@ -2,7 +2,7 @@ use super::utils::clone_or_copy_needed; use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::higher::ForLoop; use clippy_utils::source::snippet_opt; -use clippy_utils::ty::{get_associated_type, get_iterator_item_ty, implements_trait}; +use clippy_utils::ty::{get_iterator_item_ty, implements_trait}; use clippy_utils::{fn_def_id, get_parent_expr}; use rustc_errors::Applicability; use rustc_hir::{def_id::DefId, Expr, ExprKind}; @@ -54,7 +54,7 @@ pub fn check_for_loop_iter( if let Some(into_iterator_trait_id) = cx.tcx.get_diagnostic_item(sym::IntoIterator); let collection_ty = cx.typeck_results().expr_ty(collection); if implements_trait(cx, collection_ty, into_iterator_trait_id, &[]); - if let Some(into_iter_item_ty) = get_associated_type(cx, collection_ty, into_iterator_trait_id, "Item"); + if let Some(into_iter_item_ty) = cx.get_associated_type(collection_ty, into_iterator_trait_id, "Item"); if iter_item_ty == into_iter_item_ty; if let Some(collection_snippet) = snippet_opt(cx, collection.span); diff --git a/clippy_lints/src/methods/unnecessary_to_owned.rs b/clippy_lints/src/methods/unnecessary_to_owned.rs index 375ebc903b400..8b000cd754cd1 100644 --- a/clippy_lints/src/methods/unnecessary_to_owned.rs +++ b/clippy_lints/src/methods/unnecessary_to_owned.rs @@ -2,9 +2,11 @@ use super::implicit_clone::is_clone_like; use super::unnecessary_iter_cloned::{self, is_into_iter}; use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::source::snippet_opt; -use clippy_utils::ty::{get_associated_type, get_iterator_item_ty, implements_trait, is_copy, peel_mid_ty_refs}; +use clippy_utils::ty::{get_iterator_item_ty, implements_trait, is_copy, peel_mid_ty_refs}; use clippy_utils::visitors::find_all_ret_expressions; -use clippy_utils::{fn_def_id, get_parent_expr, is_diag_item_method, is_diag_trait_item, return_ty}; +use clippy_utils::{ + fn_def_id, get_parent_expr, is_diag_item_method, is_diag_trait_item, return_ty, +}; use clippy_utils::{meets_msrv, msrvs}; use rustc_errors::Applicability; use rustc_hir::{def_id::DefId, BorrowKind, Expr, ExprKind, ItemKind, Node}; @@ -18,7 +20,9 @@ use rustc_middle::ty::EarlyBinder; use rustc_middle::ty::{self, ParamTy, PredicateKind, ProjectionPredicate, TraitPredicate, Ty}; use rustc_semver::RustcVersion; use rustc_span::{sym, Symbol}; -use rustc_trait_selection::traits::{query::evaluate_obligation::InferCtxtExt as _, Obligation, ObligationCause}; +use rustc_trait_selection::traits::{ + query::evaluate_obligation::InferCtxtExt as _, Obligation, ObligationCause, +}; use std::cmp::max; use super::UNNECESSARY_TO_OWNED; @@ -146,7 +150,7 @@ fn check_addr_of_expr( if_chain! { if let Some(deref_trait_id) = cx.tcx.get_diagnostic_item(sym::Deref); if implements_trait(cx, receiver_ty, deref_trait_id, &[]); - if get_associated_type(cx, receiver_ty, deref_trait_id, "Target") == Some(target_ty); + if cx.get_associated_type(receiver_ty, deref_trait_id, "Target") == Some(target_ty); then { if n_receiver_refs > 0 { span_lint_and_sugg( @@ -341,13 +345,13 @@ fn get_input_traits_and_projections<'tcx>( if trait_predicate.trait_ref.self_ty() == input { trait_predicates.push(trait_predicate); } - }, + } PredicateKind::Projection(projection_predicate) => { if projection_predicate.projection_ty.self_ty() == input { projection_predicates.push(projection_predicate); } - }, - _ => {}, + } + _ => {} } } (trait_predicates, projection_predicates) @@ -462,7 +466,12 @@ fn is_cloned_or_copied(cx: &LateContext<'_>, method_name: Symbol, method_def_id: /// Returns true if the named method can be used to convert the receiver to its "owned" /// representation. -fn is_to_owned_like<'a>(cx: &LateContext<'a>, call_expr: &Expr<'a>, method_name: Symbol, method_def_id: DefId) -> bool { +fn is_to_owned_like<'a>( + cx: &LateContext<'a>, + call_expr: &Expr<'a>, + method_name: Symbol, + method_def_id: DefId, +) -> bool { is_clone_like(cx, method_name.as_str(), method_def_id) || is_cow_into_owned(cx, method_name, method_def_id) || is_to_string_on_string_like(cx, call_expr, method_name, method_def_id) @@ -490,7 +499,7 @@ fn is_to_string_on_string_like<'a>( && let GenericArgKind::Type(ty) = generic_arg.unpack() && let Some(deref_trait_id) = cx.tcx.get_diagnostic_item(sym::Deref) && let Some(as_ref_trait_id) = cx.tcx.get_diagnostic_item(sym::AsRef) - && (get_associated_type(cx, ty, deref_trait_id, "Target") == Some(cx.tcx.types.str_) || + && (cx.get_associated_type(ty, deref_trait_id, "Target") == Some(cx.tcx.types.str_) || implements_trait(cx, ty, as_ref_trait_id, &[cx.tcx.types.str_.into()])) { true } else { diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index 1b65b701409e4..8284dc5c28c0b 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -117,24 +117,7 @@ pub fn contains_ty_adt_constructor_opaque<'tcx>(cx: &LateContext<'tcx>, ty: Ty<' pub fn get_iterator_item_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option> { cx.tcx .get_diagnostic_item(sym::Iterator) - .and_then(|iter_did| get_associated_type(cx, ty, iter_did, "Item")) -} - -/// Returns the associated type `name` for `ty` as an implementation of `trait_id`. -/// Do not invoke without first verifying that the type implements the trait. -pub fn get_associated_type<'tcx>( - cx: &LateContext<'tcx>, - ty: Ty<'tcx>, - trait_id: DefId, - name: &str, -) -> Option> { - cx.tcx - .associated_items(trait_id) - .find_by_name_and_kind(cx.tcx, Ident::from_str(name), ty::AssocKind::Type, trait_id) - .and_then(|assoc| { - let proj = cx.tcx.mk_projection(assoc.def_id, cx.tcx.mk_substs_trait(ty, [])); - cx.tcx.try_normalize_erasing_regions(cx.param_env, proj).ok() - }) + .and_then(|iter_did| cx.get_associated_type(ty, iter_did, "Item")) } /// Get the diagnostic name of a type, e.g. `sym::HashMap`. To check if a type From 93cfcedfd5582ef4d2966d39eb32d2aa3e3f2dee Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 5 Nov 2022 22:41:07 +0000 Subject: [PATCH 048/244] Separate lifetime ident from resolution in HIR. --- clippy_lints/src/lifetimes.rs | 26 ++++++++++---------------- clippy_lints/src/manual_async_fn.rs | 4 ++-- clippy_lints/src/ptr.rs | 19 +++++++------------ clippy_lints/src/types/borrowed_box.rs | 4 ++-- clippy_utils/src/hir_utils.rs | 16 +++++----------- 5 files changed, 26 insertions(+), 43 deletions(-) diff --git a/clippy_lints/src/lifetimes.rs b/clippy_lints/src/lifetimes.rs index 0bb9eca15287d..5df8b486f3327 100644 --- a/clippy_lints/src/lifetimes.rs +++ b/clippy_lints/src/lifetimes.rs @@ -10,7 +10,7 @@ use rustc_hir::lang_items; use rustc_hir::FnRetTy::Return; use rustc_hir::{ BareFnTy, BodyId, FnDecl, GenericArg, GenericBound, GenericParam, GenericParamKind, Generics, Impl, ImplItem, - ImplItemKind, Item, ItemKind, Lifetime, LifetimeName, ParamName, PolyTraitRef, PredicateOrigin, TraitFn, TraitItem, + ImplItemKind, Item, ItemKind, Lifetime, LifetimeName, PolyTraitRef, PredicateOrigin, TraitFn, TraitItem, TraitItemKind, Ty, TyKind, WherePredicate, }; use rustc_lint::{LateContext, LateLintPass}; @@ -180,7 +180,7 @@ fn check_fn_inner<'tcx>( _ => None, }); for bound in lifetimes { - if bound.name != LifetimeName::Static && !bound.is_elided() { + if !bound.is_static() && !bound.is_elided() { return; } } @@ -414,17 +414,13 @@ impl<'a, 'tcx> RefVisitor<'a, 'tcx> { fn record(&mut self, lifetime: &Option) { if let Some(ref lt) = *lifetime { - if lt.name == LifetimeName::Static { + if lt.is_static() { self.lts.push(RefLt::Static); - } else if let LifetimeName::Param(_, ParamName::Fresh) = lt.name { + } else if lt.is_anonymous() { // Fresh lifetimes generated should be ignored. self.lts.push(RefLt::Unnamed); - } else if lt.is_elided() { - self.lts.push(RefLt::Unnamed); - } else if let LifetimeName::Param(def_id, _) = lt.name { + } else if let LifetimeName::Param(def_id) = lt.res { self.lts.push(RefLt::Named(def_id)); - } else { - self.lts.push(RefLt::Unnamed); } } else { self.lts.push(RefLt::Unnamed); @@ -472,7 +468,7 @@ impl<'a, 'tcx> Visitor<'tcx> for RefVisitor<'a, 'tcx> { walk_item(self, item); self.lts.truncate(len); self.lts.extend(bounds.iter().filter_map(|bound| match bound { - GenericArg::Lifetime(l) => Some(if let LifetimeName::Param(def_id, _) = l.name { + GenericArg::Lifetime(l) => Some(if let LifetimeName::Param(def_id) = l.res { RefLt::Named(def_id) } else { RefLt::Unnamed @@ -498,10 +494,8 @@ impl<'a, 'tcx> Visitor<'tcx> for RefVisitor<'a, 'tcx> { } fn visit_generic_arg(&mut self, generic_arg: &'tcx GenericArg<'tcx>) { - if let GenericArg::Lifetime(l) = generic_arg - && let LifetimeName::Param(def_id, _) = l.name - { - self.lifetime_generic_arg_spans.entry(def_id).or_insert(l.span); + if let GenericArg::Lifetime(l) = generic_arg && let LifetimeName::Param(def_id) = l.res { + self.lifetime_generic_arg_spans.entry(def_id).or_insert(l.ident.span); } // Replace with `walk_generic_arg` if/when https://github.com/rust-lang/rust/pull/103692 lands. // walk_generic_arg(self, generic_arg); @@ -577,7 +571,7 @@ where // for lifetimes as parameters of generics fn visit_lifetime(&mut self, lifetime: &'tcx Lifetime) { - self.map.remove(&lifetime.name.ident().name); + self.map.remove(&lifetime.ident.name); } fn visit_generic_param(&mut self, param: &'tcx GenericParam<'_>) { @@ -653,7 +647,7 @@ struct BodyLifetimeChecker { impl<'tcx> Visitor<'tcx> for BodyLifetimeChecker { // for lifetimes as parameters of generics fn visit_lifetime(&mut self, lifetime: &'tcx Lifetime) { - if lifetime.name.ident().name != kw::UnderscoreLifetime && lifetime.name.ident().name != kw::StaticLifetime { + if lifetime.ident.name != kw::UnderscoreLifetime && lifetime.ident.name != kw::StaticLifetime { self.lifetimes_used_in_body = true; } } diff --git a/clippy_lints/src/manual_async_fn.rs b/clippy_lints/src/manual_async_fn.rs index 5c6a342b3d074..553980ebf7970 100644 --- a/clippy_lints/src/manual_async_fn.rs +++ b/clippy_lints/src/manual_async_fn.rs @@ -118,7 +118,7 @@ fn future_trait_ref<'tcx>( .iter() .filter_map(|bound| { if let GenericArg::Lifetime(lt) = bound { - Some(lt.name) + Some(lt.res) } else { None } @@ -153,7 +153,7 @@ fn captures_all_lifetimes(inputs: &[Ty<'_>], output_lifetimes: &[LifetimeName]) .iter() .filter_map(|ty| { if let TyKind::Rptr(lt, _) = ty.kind { - Some(lt.name) + Some(lt.res) } else { None } diff --git a/clippy_lints/src/ptr.rs b/clippy_lints/src/ptr.rs index 5420a0e782ea8..ab960edb75765 100644 --- a/clippy_lints/src/ptr.rs +++ b/clippy_lints/src/ptr.rs @@ -12,8 +12,8 @@ use rustc_hir::hir_id::HirIdMap; use rustc_hir::intravisit::{walk_expr, Visitor}; use rustc_hir::{ self as hir, AnonConst, BinOpKind, BindingAnnotation, Body, Expr, ExprKind, FnRetTy, FnSig, GenericArg, - ImplItemKind, ItemKind, Lifetime, LifetimeName, Mutability, Node, Param, ParamName, PatKind, QPath, TraitFn, - TraitItem, TraitItemKind, TyKind, Unsafety, + ImplItemKind, ItemKind, Lifetime, Mutability, Node, Param, PatKind, QPath, TraitFn, TraitItem, TraitItemKind, + TyKind, Unsafety, }; use rustc_infer::infer::TyCtxtInferExt; use rustc_infer::traits::{Obligation, ObligationCause}; @@ -343,21 +343,16 @@ impl PtrArg<'_> { } struct RefPrefix { - lt: LifetimeName, + lt: Lifetime, mutability: Mutability, } impl fmt::Display for RefPrefix { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { use fmt::Write; f.write_char('&')?; - match self.lt { - LifetimeName::Param(_, ParamName::Plain(name)) => { - name.fmt(f)?; - f.write_char(' ')?; - }, - LifetimeName::Infer => f.write_str("'_ ")?, - LifetimeName::Static => f.write_str("'static ")?, - _ => (), + if !self.lt.is_anonymous() { + self.lt.ident.fmt(f)?; + f.write_char(' ')?; } f.write_str(self.mutability.prefix_str()) } @@ -495,7 +490,7 @@ fn check_fn_args<'cx, 'tcx: 'cx>( ty_name: name.ident.name, method_renames, ref_prefix: RefPrefix { - lt: lt.name, + lt: lt.clone(), mutability, }, deref_ty, diff --git a/clippy_lints/src/types/borrowed_box.rs b/clippy_lints/src/types/borrowed_box.rs index 9c6629958401b..65dfe7637ea99 100644 --- a/clippy_lints/src/types/borrowed_box.rs +++ b/clippy_lints/src/types/borrowed_box.rs @@ -31,10 +31,10 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, lt: &Lifetime, m return false; } - let ltopt = if lt.name.is_anonymous() { + let ltopt = if lt.is_anonymous() { String::new() } else { - format!("{} ", lt.name.ident().as_str()) + format!("{} ", lt.ident.as_str()) }; if mut_ty.mutbl == Mutability::Mut { diff --git a/clippy_utils/src/hir_utils.rs b/clippy_utils/src/hir_utils.rs index 0231a51adf482..48982517751ec 100644 --- a/clippy_utils/src/hir_utils.rs +++ b/clippy_utils/src/hir_utils.rs @@ -7,7 +7,7 @@ use rustc_hir::def::Res; use rustc_hir::HirIdMap; use rustc_hir::{ ArrayLen, BinOpKind, BindingAnnotation, Block, BodyId, Closure, Expr, ExprField, ExprKind, FnRetTy, GenericArg, - GenericArgs, Guard, HirId, InlineAsmOperand, Let, Lifetime, LifetimeName, ParamName, Pat, PatField, PatKind, Path, + GenericArgs, Guard, HirId, InlineAsmOperand, Let, Lifetime, LifetimeName, Pat, PatField, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, Ty, TyKind, TypeBinding, }; use rustc_lexer::{tokenize, TokenKind}; @@ -337,7 +337,7 @@ impl HirEqInterExpr<'_, '_, '_> { } fn eq_lifetime(left: &Lifetime, right: &Lifetime) -> bool { - left.name == right.name + left.res == right.res } fn eq_pat_field(&mut self, left: &PatField<'_>, right: &PatField<'_>) -> bool { @@ -925,16 +925,10 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> { } pub fn hash_lifetime(&mut self, lifetime: &Lifetime) { - std::mem::discriminant(&lifetime.name).hash(&mut self.s); - if let LifetimeName::Param(param_id, ref name) = lifetime.name { - std::mem::discriminant(name).hash(&mut self.s); + lifetime.ident.name.hash(&mut self.s); + std::mem::discriminant(&lifetime.res).hash(&mut self.s); + if let LifetimeName::Param(param_id) = lifetime.res { param_id.hash(&mut self.s); - match name { - ParamName::Plain(ref ident) => { - ident.name.hash(&mut self.s); - }, - ParamName::Fresh | ParamName::Error => {}, - } } } From 2a530dce53b93ff8f880ffa3e90d5fc53668ba89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 3 Nov 2022 09:19:23 -0700 Subject: [PATCH 049/244] Fix clippy code --- clippy_lints/src/suspicious_operation_groupings.rs | 2 +- clippy_utils/src/ast_utils.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/suspicious_operation_groupings.rs b/clippy_lints/src/suspicious_operation_groupings.rs index 78e83880e1a61..e111c7d229151 100644 --- a/clippy_lints/src/suspicious_operation_groupings.rs +++ b/clippy_lints/src/suspicious_operation_groupings.rs @@ -582,7 +582,7 @@ fn ident_difference_expr_with_base_location( | (Block(_, _), Block(_, _)) | (Closure(_), Closure(_)) | (Match(_, _), Match(_, _)) - | (Loop(_, _), Loop(_, _)) + | (Loop(_, _, _), Loop(_, _, _)) | (ForLoop(_, _, _, _), ForLoop(_, _, _, _)) | (While(_, _, _), While(_, _, _)) | (If(_, _, _), If(_, _, _)) diff --git a/clippy_utils/src/ast_utils.rs b/clippy_utils/src/ast_utils.rs index 5c595f74eff26..6bcf0bbd7eb75 100644 --- a/clippy_utils/src/ast_utils.rs +++ b/clippy_utils/src/ast_utils.rs @@ -171,7 +171,7 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool { (ForLoop(lp, li, lt, ll), ForLoop(rp, ri, rt, rl)) => { eq_label(ll, rl) && eq_pat(lp, rp) && eq_expr(li, ri) && eq_block(lt, rt) }, - (Loop(lt, ll), Loop(rt, rl)) => eq_label(ll, rl) && eq_block(lt, rt), + (Loop(lt, ll, _), Loop(rt, rl, _)) => eq_label(ll, rl) && eq_block(lt, rt), (Block(lb, ll), Block(rb, rl)) => eq_label(ll, rl) && eq_block(lb, rb), (TryBlock(l), TryBlock(r)) => eq_block(l, r), (Yield(l), Yield(r)) | (Ret(l), Ret(r)) => eq_expr_opt(l, r), From 1ebdcca8b97feca67ce077a2bad87e7655bc774a Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Fri, 18 Nov 2022 22:56:22 +0100 Subject: [PATCH 050/244] Avoid `GenFuture` shim when compiling async constructs Previously, async constructs would be lowered to "normal" generators, with an additional `from_generator` / `GenFuture` shim in between to convert from `Generator` to `Future`. The compiler will now special-case these generators internally so that async constructs will *directly* implement `Future` without the need to go through the `from_generator` / `GenFuture` shim. The primary motivation for this change was hiding this implementation detail in stack traces and debuginfo, but it can in theory also help the optimizer as there is less abstractions to see through. --- clippy_lints/src/doc.rs | 4 +--- clippy_lints/src/manual_async_fn.rs | 2 +- tests/ui/author/blocks.stdout | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/doc.rs b/clippy_lints/src/doc.rs index 4557e43288542..ae5f9424b2323 100644 --- a/clippy_lints/src/doc.rs +++ b/clippy_lints/src/doc.rs @@ -427,9 +427,7 @@ fn lint_for_missing_headers( let body = cx.tcx.hir().body(body_id); let ret_ty = typeck.expr_ty(body.value); if implements_trait(cx, ret_ty, future, &[]); - if let ty::Opaque(_, subs) = ret_ty.kind(); - if let Some(gen) = subs.types().next(); - if let ty::Generator(_, subs, _) = gen.kind(); + if let ty::Generator(_, subs, _) = ret_ty.kind(); if is_type_diagnostic_item(cx, subs.as_generator().return_ty(), sym::Result); then { span_lint( diff --git a/clippy_lints/src/manual_async_fn.rs b/clippy_lints/src/manual_async_fn.rs index 5c6a342b3d074..6a98df4991259 100644 --- a/clippy_lints/src/manual_async_fn.rs +++ b/clippy_lints/src/manual_async_fn.rs @@ -177,7 +177,7 @@ fn desugared_async_block<'tcx>(cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) if let Some(args) = cx .tcx .lang_items() - .from_generator_fn() + .identity_future_fn() .and_then(|def_id| match_function_call_with_def_id(cx, block_expr, def_id)); if args.len() == 1; if let Expr { diff --git a/tests/ui/author/blocks.stdout b/tests/ui/author/blocks.stdout index 9de0550d81d00..c6acf24c21ecf 100644 --- a/tests/ui/author/blocks.stdout +++ b/tests/ui/author/blocks.stdout @@ -45,7 +45,7 @@ if let ExprKind::Closure(CaptureBy::Value, fn_decl, body_id, _, None) = expr.kin && expr1 = &cx.tcx.hir().body(body_id).value && let ExprKind::Call(func, args) = expr1.kind && let ExprKind::Path(ref qpath) = func.kind - && matches!(qpath, QPath::LangItem(LangItem::FromGenerator, _)) + && matches!(qpath, QPath::LangItem(LangItem::IdentityFuture, _)) && args.len() == 1 && let ExprKind::Closure(CaptureBy::Value, fn_decl1, body_id1, _, Some(Movability::Static)) = args[0].kind && let FnRetTy::DefaultReturn(_) = fn_decl1.output From e598af6f2710464e20a796d0d3eba642747bcbe3 Mon Sep 17 00:00:00 2001 From: Harald Hoyer Date: Thu, 24 Nov 2022 09:38:14 +0100 Subject: [PATCH 051/244] feat: implement TcpStream shutdown for wasm32-wasi Signed-off-by: Harald Hoyer --- library/std/src/sys/wasi/net.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/library/std/src/sys/wasi/net.rs b/library/std/src/sys/wasi/net.rs index 590d268c38087..cf4ebba1a39a6 100644 --- a/library/std/src/sys/wasi/net.rs +++ b/library/std/src/sys/wasi/net.rs @@ -119,8 +119,14 @@ impl TcpStream { unsupported() } - pub fn shutdown(&self, _: Shutdown) -> io::Result<()> { - unsupported() + pub fn shutdown(&self, how: Shutdown) -> io::Result<()> { + let wasi_how = match how { + Shutdown::Read => wasi::SDFLAGS_RD, + Shutdown::Write => wasi::SDFLAGS_WR, + Shutdown::Both => wasi::SDFLAGS_RD | wasi::SDFLAGS_WR, + }; + + unsafe { wasi::sock_shutdown(self.socket().as_raw_fd() as _, wasi_how).map_err(err2io) } } pub fn duplicate(&self) -> io::Result { From 0aaea40eb2015a109d0be49c0d08f6435c6ce437 Mon Sep 17 00:00:00 2001 From: hkalbasi Date: Tue, 1 Nov 2022 19:50:30 +0330 Subject: [PATCH 052/244] move some layout logic to rustc_target::abi::layout --- clippy_lints/src/casts/cast_possible_truncation.rs | 5 ++--- clippy_lints/src/lib.rs | 1 - 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/casts/cast_possible_truncation.rs b/clippy_lints/src/casts/cast_possible_truncation.rs index 88deb4565eb21..adbcfd3189b76 100644 --- a/clippy_lints/src/casts/cast_possible_truncation.rs +++ b/clippy_lints/src/casts/cast_possible_truncation.rs @@ -2,12 +2,11 @@ use clippy_utils::consts::{constant, Constant}; use clippy_utils::diagnostics::span_lint; use clippy_utils::expr_or_init; use clippy_utils::ty::{get_discriminant_value, is_isize_or_usize}; -use rustc_ast::ast; -use rustc_attr::IntType; use rustc_hir::def::{DefKind, Res}; use rustc_hir::{BinOpKind, Expr, ExprKind}; use rustc_lint::LateContext; use rustc_middle::ty::{self, FloatTy, Ty}; +use rustc_target::abi::IntegerType; use super::{utils, CAST_ENUM_TRUNCATION, CAST_POSSIBLE_TRUNCATION}; @@ -122,7 +121,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, let cast_from_ptr_size = def.repr().int.map_or(true, |ty| { matches!( ty, - IntType::SignedInt(ast::IntTy::Isize) | IntType::UnsignedInt(ast::UintTy::Usize) + IntegerType::Pointer(_), ) }); let suffix = match (cast_from_ptr_size, is_isize_or_usize(cast_to)) { diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index b481314abedc8..601990cd6a316 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -26,7 +26,6 @@ extern crate rustc_arena; extern crate rustc_ast; extern crate rustc_ast_pretty; -extern crate rustc_attr; extern crate rustc_data_structures; extern crate rustc_driver; extern crate rustc_errors; From 42db5e5e6275fdd563e6975bf29d675ea47d37e5 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 6 Nov 2022 11:40:31 +0000 Subject: [PATCH 053/244] Use kw::Empty for elided lifetimes in path. --- clippy_lints/src/lifetimes.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/lifetimes.rs b/clippy_lints/src/lifetimes.rs index 5df8b486f3327..220941dcd5dbf 100644 --- a/clippy_lints/src/lifetimes.rs +++ b/clippy_lints/src/lifetimes.rs @@ -10,8 +10,8 @@ use rustc_hir::lang_items; use rustc_hir::FnRetTy::Return; use rustc_hir::{ BareFnTy, BodyId, FnDecl, GenericArg, GenericBound, GenericParam, GenericParamKind, Generics, Impl, ImplItem, - ImplItemKind, Item, ItemKind, Lifetime, LifetimeName, PolyTraitRef, PredicateOrigin, TraitFn, TraitItem, - TraitItemKind, Ty, TyKind, WherePredicate, + ImplItemKind, Item, ItemKind, Lifetime, LifetimeName, LifetimeParamKind, PolyTraitRef, PredicateOrigin, TraitFn, + TraitItem, TraitItemKind, Ty, TyKind, WherePredicate, }; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::hir::nested_filter as middle_nested_filter; @@ -595,7 +595,9 @@ fn report_extra_lifetimes<'tcx>(cx: &LateContext<'tcx>, func: &'tcx FnDecl<'_>, .params .iter() .filter_map(|par| match par.kind { - GenericParamKind::Lifetime { .. } => Some((par.name.ident().name, par.span)), + GenericParamKind::Lifetime { + kind: LifetimeParamKind::Explicit, + } => Some((par.name.ident().name, par.span)), _ => None, }) .collect(); @@ -620,7 +622,9 @@ fn report_extra_impl_lifetimes<'tcx>(cx: &LateContext<'tcx>, impl_: &'tcx Impl<' .params .iter() .filter_map(|par| match par.kind { - GenericParamKind::Lifetime { .. } => Some((par.name.ident().name, par.span)), + GenericParamKind::Lifetime { + kind: LifetimeParamKind::Explicit, + } => Some((par.name.ident().name, par.span)), _ => None, }) .collect(); @@ -647,7 +651,7 @@ struct BodyLifetimeChecker { impl<'tcx> Visitor<'tcx> for BodyLifetimeChecker { // for lifetimes as parameters of generics fn visit_lifetime(&mut self, lifetime: &'tcx Lifetime) { - if lifetime.ident.name != kw::UnderscoreLifetime && lifetime.ident.name != kw::StaticLifetime { + if !lifetime.is_anonymous() && lifetime.ident.name != kw::StaticLifetime { self.lifetimes_used_in_body = true; } } From 4d0fb089217067b1c7ec21205303e8fd95f60fbc Mon Sep 17 00:00:00 2001 From: Alex Macleod Date: Thu, 24 Nov 2022 21:33:25 +0000 Subject: [PATCH 054/244] Fix remark for `rfcs/0001-syntax-tree-patterns.md` --- rfcs/0001-syntax-tree-patterns.md | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/rfcs/0001-syntax-tree-patterns.md b/rfcs/0001-syntax-tree-patterns.md index 8d285b2ef44c7..9161986a7b774 100644 --- a/rfcs/0001-syntax-tree-patterns.md +++ b/rfcs/0001-syntax-tree-patterns.md @@ -1,3 +1,5 @@ + + - Feature Name: syntax-tree-patterns - Start Date: 2019-03-12 - RFC PR: (leave this empty) @@ -6,13 +8,11 @@ > Note: This project is part of my Master's Thesis (supervised by [@oli-obk](https://github.com/oli-obk)) # Summary -[summary]: #summary Introduce a domain-specific language (similar to regular expressions) that allows to describe lints using *syntax tree patterns*. # Motivation -[motivation]: #motivation Finding parts of a syntax tree (AST, HIR, ...) that have certain properties (e.g. "*an if that has a block as its condition*") is a major task when writing lints. For non-trivial lints, it often requires nested pattern matching of AST / HIR nodes. For example, testing that an expression is a boolean literal requires the following checks: @@ -68,7 +68,6 @@ A lot of complexity in writing lints currently seems to come from having to manu While regular expressions are very useful when searching for patterns in flat character sequences, they cannot easily be applied to hierarchical data structures like syntax trees. This RFC therefore proposes a pattern matching system that is inspired by regular expressions and designed for hierarchical syntax trees. # Guide-level explanation -[guide-level-explanation]: #guide-level-explanation This proposal adds a `pattern!` macro that can be used to specify a syntax tree pattern to search for. A simple pattern is shown below: @@ -281,7 +280,6 @@ The following table gives an summary of the pattern syntax: ## The result type -[the-result-type]: #the-result-type A lot of lints require checks that go beyond what the pattern syntax described above can express. For example, a lint might want to check whether a node was created as part of a macro expansion or whether there's no comment above a node. Another example would be a lint that wants to match two nodes that have the same value (as needed by lints like `almost_swapped`). Instead of allowing users to write these checks into the pattern directly (which might make patterns hard to read), the proposed solution allows users to assign names to parts of a pattern expression. When matching a pattern against a syntax tree node, the return value will contain references to all nodes that were matched by these named subpatterns. This is similar to capture groups in regular expressions. @@ -372,7 +370,6 @@ As a "real-world" example, I re-implemented the `collapsible_if` lint using patt # Reference-level explanation -[reference-level-explanation]: #reference-level-explanation ## Overview @@ -517,7 +514,6 @@ All `IsMatch` implementations for matching the current *PatternTree* against `sy # Drawbacks -[drawbacks]: #drawbacks #### Performance @@ -571,7 +567,6 @@ Even though I'd expect that a lot of lints can be written using the proposed pat # Rationale and alternatives -[rationale-and-alternatives]: #rationale-and-alternatives Specifying lints using syntax tree patterns has a couple of advantages compared to the current approach of manually writing matching code. First, syntax tree patterns allow users to describe patterns in a simple and expressive way. This makes it easier to write new lints for both novices and experts and also makes reading / modifying existing lints simpler. @@ -632,14 +627,12 @@ The issue of users not knowing about the *PatternTree* structure could be solved For some simple cases (like the first example above), it might be possible to successfully mix Rust and pattern syntax. This space could be further explored in a future extension. # Prior art -[prior-art]: #prior-art The pattern syntax is heavily inspired by regular expressions (repetitions, alternatives, sequences, ...). From what I've seen until now, other linters also implement lints that directly work on syntax tree data structures, just like clippy does currently. I would therefore consider the pattern syntax to be *new*, but please correct me if I'm wrong. # Unresolved questions -[unresolved-questions]: #unresolved-questions #### How to handle multiple matches? @@ -657,7 +650,6 @@ This pattern matches arrays that end with at least one literal. Now given the ar I haven't looked much into this yet because I don't know how relevant it is for most lints. The current implementation simply returns the first match it finds. # Future possibilities -[future-possibilities]: #future-possibilities #### Implement rest of Rust Syntax From ecca8c532850a2cce403264bd3c85961a38c8308 Mon Sep 17 00:00:00 2001 From: Markus Everling Date: Fri, 25 Nov 2022 03:36:11 +0100 Subject: [PATCH 055/244] Changes according to code review --- .../alloc/src/collections/vec_deque/drain.rs | 79 ++++---- .../alloc/src/collections/vec_deque/iter.rs | 30 ++- .../src/collections/vec_deque/iter_mut.rs | 10 +- .../alloc/src/collections/vec_deque/mod.rs | 182 ++++++++++-------- .../src/collections/vec_deque/spec_extend.rs | 11 +- .../alloc/src/collections/vec_deque/tests.rs | 13 +- 6 files changed, 181 insertions(+), 144 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/drain.rs b/library/alloc/src/collections/vec_deque/drain.rs index a4a96bad5e042..89feb361ddc11 100644 --- a/library/alloc/src/collections/vec_deque/drain.rs +++ b/library/alloc/src/collections/vec_deque/drain.rs @@ -53,27 +53,36 @@ impl<'a, T, A: Allocator> Drain<'a, T, A> { } // Only returns pointers to the slices, as that's - // all we need to drop them - fn as_slices(&self) -> (*mut [T], *mut [T]) { + // all we need to drop them. May only be called if `self.remaining != 0`. + unsafe fn as_slices(&self) -> (*mut [T], *mut [T]) { unsafe { let deque = self.deque.as_ref(); - let wrapped_start = deque.wrap_idx(self.idx); - - if self.remaining <= deque.capacity() - wrapped_start { - // there's only one contigous slice - ( - ptr::slice_from_raw_parts_mut(deque.ptr().add(wrapped_start), self.remaining), - &mut [] as *mut [T], - ) + // FIXME: This is doing almost exactly the same thing as the else branch in `VecDeque::slice_ranges`. + // Unfortunately, we can't just call `slice_ranges` here, as the deque's `len` is currently + // just `drain_start`, so the range check would (almost) always panic. Between temporarily + // adjusting the deques `len` to call `slice_ranges`, and just copy pasting the `slice_ranges` + // implementation, this seemed like the less hacky solution, though it might be good to + // find a better one in the future. + + // because `self.remaining != 0`, we know that `self.idx < deque.original_len`, so it's a valid + // logical index. + let wrapped_start = deque.to_physical_idx(self.idx); + + let head_len = deque.capacity() - wrapped_start; + + let (a_range, b_range) = if head_len >= self.remaining { + (wrapped_start..wrapped_start + self.remaining, 0..0) } else { - let head_len = deque.capacity() - wrapped_start; - // this will never overflow due to the if condition let tail_len = self.remaining - head_len; - ( - ptr::slice_from_raw_parts_mut(deque.ptr().add(wrapped_start), head_len), - ptr::slice_from_raw_parts_mut(deque.ptr(), tail_len), - ) - } + (wrapped_start..deque.capacity(), 0..tail_len) + }; + + // SAFETY: the range `self.idx..self.idx+self.remaining` lies strictly inside + // the range `0..deque.original_len`. because of this, and because of the fact + // that we acquire `a_range` and `b_range` exactly like `slice_ranges` would, + // it's guaranteed that `a_range` and `b_range` represent valid ranges into + // the deques buffer. + (deque.buffer_range(a_range), deque.buffer_range(b_range)) } } } @@ -103,8 +112,9 @@ impl Drop for Drain<'_, T, A> { impl<'r, 'a, T, A: Allocator> Drop for DropGuard<'r, 'a, T, A> { fn drop(&mut self) { if self.0.remaining != 0 { - let (front, back) = self.0.as_slices(); unsafe { + // SAFETY: We just checked that `self.remaining != 0`. + let (front, back) = self.0.as_slices(); ptr::drop_in_place(front); ptr::drop_in_place(back); } @@ -133,7 +143,7 @@ impl Drop for Drain<'_, T, A> { source_deque.len = 0; } (0, _) => { - source_deque.head = source_deque.wrap_idx(drain_len); + source_deque.head = source_deque.to_physical_idx(drain_len); source_deque.len = orig_len - drain_len; } (_, 0) => { @@ -143,15 +153,15 @@ impl Drop for Drain<'_, T, A> { if head_len <= tail_len { source_deque.wrap_copy( source_deque.head, - source_deque.wrap_idx(drain_len), + source_deque.to_physical_idx(drain_len), head_len, ); - source_deque.head = source_deque.wrap_idx(drain_len); + source_deque.head = source_deque.to_physical_idx(drain_len); source_deque.len = orig_len - drain_len; } else { source_deque.wrap_copy( - source_deque.wrap_idx(head_len + drain_len), - source_deque.wrap_idx(head_len), + source_deque.to_physical_idx(head_len + drain_len), + source_deque.to_physical_idx(head_len), tail_len, ); source_deque.len = orig_len - drain_len; @@ -162,14 +172,17 @@ impl Drop for Drain<'_, T, A> { } let guard = DropGuard(self); - let (front, back) = guard.0.as_slices(); - unsafe { - // since idx is a logical index, we don't need to worry about wrapping. - guard.0.idx += front.len(); - guard.0.remaining -= front.len(); - ptr::drop_in_place(front); - guard.0.remaining = 0; - ptr::drop_in_place(back); + if guard.0.remaining != 0 { + unsafe { + // SAFETY: We just checked that `self.remaining != 0`. + let (front, back) = guard.0.as_slices(); + // since idx is a logical index, we don't need to worry about wrapping. + guard.0.idx += front.len(); + guard.0.remaining -= front.len(); + ptr::drop_in_place(front); + guard.0.remaining = 0; + ptr::drop_in_place(back); + } } // Dropping `guard` handles moving the remaining elements into place. @@ -185,7 +198,7 @@ impl Iterator for Drain<'_, T, A> { if self.remaining == 0 { return None; } - let wrapped_idx = unsafe { self.deque.as_ref().wrap_idx(self.idx) }; + let wrapped_idx = unsafe { self.deque.as_ref().to_physical_idx(self.idx) }; self.idx += 1; self.remaining -= 1; Some(unsafe { self.deque.as_mut().buffer_read(wrapped_idx) }) @@ -206,7 +219,7 @@ impl DoubleEndedIterator for Drain<'_, T, A> { return None; } self.remaining -= 1; - let wrapped_idx = unsafe { self.deque.as_ref().wrap_idx(self.idx + self.remaining) }; + let wrapped_idx = unsafe { self.deque.as_ref().to_physical_idx(self.idx + self.remaining) }; Some(unsafe { self.deque.as_mut().buffer_read(wrapped_idx) }) } } diff --git a/library/alloc/src/collections/vec_deque/iter.rs b/library/alloc/src/collections/vec_deque/iter.rs index d4e66db4903c3..d9f3937144d04 100644 --- a/library/alloc/src/collections/vec_deque/iter.rs +++ b/library/alloc/src/collections/vec_deque/iter.rs @@ -39,15 +39,6 @@ impl Clone for Iter<'_, T> { impl<'a, T> Iterator for Iter<'a, T> { type Item = &'a T; - fn advance_by(&mut self, n: usize) -> Result<(), usize> { - let m = match self.i1.advance_by(n) { - Ok(_) => return Ok(()), - Err(m) => m, - }; - mem::swap(&mut self.i1, &mut self.i2); - self.i1.advance_by(n - m).map_err(|o| o + m) - } - #[inline] fn next(&mut self) -> Option<&'a T> { match self.i1.next() { @@ -64,6 +55,15 @@ impl<'a, T> Iterator for Iter<'a, T> { } } + fn advance_by(&mut self, n: usize) -> Result<(), usize> { + let m = match self.i1.advance_by(n) { + Ok(_) => return Ok(()), + Err(m) => m, + }; + mem::swap(&mut self.i1, &mut self.i2); + self.i1.advance_by(n - m).map_err(|o| o + m) + } + #[inline] fn size_hint(&self) -> (usize, Option) { let len = self.len(); @@ -75,17 +75,16 @@ impl<'a, T> Iterator for Iter<'a, T> { F: FnMut(Acc, Self::Item) -> Acc, { let accum = self.i1.fold(accum, &mut f); - self.i2.fold(accum, f) + self.i2.fold(accum, &mut f) } fn try_fold(&mut self, init: B, mut f: F) -> R where - Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try, { let acc = self.i1.try_fold(init, &mut f)?; - self.i2.try_fold(acc, f) + self.i2.try_fold(acc, &mut f) } #[inline] @@ -117,7 +116,7 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> { None => { // most of the time, the iterator will either always // call next(), or always call next_back(). By swapping - // the iterators once the first one is empty, we ensure + // the iterators once the second one is empty, we ensure // that the first branch is taken as often as possible, // without sacrificing correctness, as i2 is empty anyways mem::swap(&mut self.i1, &mut self.i2); @@ -141,17 +140,16 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> { F: FnMut(Acc, Self::Item) -> Acc, { let accum = self.i2.rfold(accum, &mut f); - self.i1.rfold(accum, f) + self.i1.rfold(accum, &mut f) } fn try_rfold(&mut self, init: B, mut f: F) -> R where - Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try, { let acc = self.i2.try_rfold(init, &mut f)?; - self.i1.try_rfold(acc, f) + self.i1.try_rfold(acc, &mut f) } } diff --git a/library/alloc/src/collections/vec_deque/iter_mut.rs b/library/alloc/src/collections/vec_deque/iter_mut.rs index 7c955663bde7e..2c59d95cd53e9 100644 --- a/library/alloc/src/collections/vec_deque/iter_mut.rs +++ b/library/alloc/src/collections/vec_deque/iter_mut.rs @@ -67,17 +67,16 @@ impl<'a, T> Iterator for IterMut<'a, T> { F: FnMut(Acc, Self::Item) -> Acc, { let accum = self.i1.fold(accum, &mut f); - self.i2.fold(accum, f) + self.i2.fold(accum, &mut f) } fn try_fold(&mut self, init: B, mut f: F) -> R where - Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try, { let acc = self.i1.try_fold(init, &mut f)?; - self.i2.try_fold(acc, f) + self.i2.try_fold(acc, &mut f) } #[inline] @@ -133,17 +132,16 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> { F: FnMut(Acc, Self::Item) -> Acc, { let accum = self.i2.rfold(accum, &mut f); - self.i1.rfold(accum, f) + self.i1.rfold(accum, &mut f) } fn try_rfold(&mut self, init: B, mut f: F) -> R where - Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try, { let acc = self.i2.try_rfold(init, &mut f)?; - self.i1.try_rfold(acc, f) + self.i1.try_rfold(acc, &mut f) } } diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index bb0e11d6c2d20..52b46e448c458 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -91,7 +91,12 @@ pub struct VecDeque< T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, > { + // `self[0]`, if it exists, is `buf[head]`. + // `head < buf.capacity()`, unless `buf.capacity() == 0` when `head == 0`. head: usize, + // the number of initialized elements, starting from the one at `head` and potentially wrapping around. + // if `len == 0`, the exact value of `head` is unimportant. + // if `T` is zero-Sized, then `self.len <= usize::MAX`, otherwise `self.len <= isize::MAX as usize`. len: usize, buf: RawVec, } @@ -106,15 +111,7 @@ impl Clone for VecDeque { fn clone_from(&mut self, other: &Self) { self.clear(); - self.head = 0; - self.reserve(other.len); - - let (a, b) = other.as_slices(); - unsafe { - self.write_iter(0, a.iter().cloned(), &mut 0); - self.write_iter(a.len(), b.iter().cloned(), &mut 0); - } - self.len = other.len; + self.extend(other.iter().cloned()); } } @@ -133,13 +130,11 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for VecDeque { } } - if mem::needs_drop::() { - let (front, back) = self.as_mut_slices(); - unsafe { - let _back_dropper = Dropper(back); - // use drop for [T] - ptr::drop_in_place(front); - } + let (front, back) = self.as_mut_slices(); + unsafe { + let _back_dropper = Dropper(back); + // use drop for [T] + ptr::drop_in_place(front); } // RawVec handles deallocation } @@ -175,6 +170,15 @@ impl VecDeque { } } + /// Returns a slice pointer into the buffer. + /// `range` must lie inside `0..self.capacity()`. + #[inline] + unsafe fn buffer_range(&self, range: Range) -> *mut [T] { + unsafe { + ptr::slice_from_raw_parts_mut(self.ptr().add(range.start), range.end - range.start) + } + } + /// Returns `true` if the buffer is at full capacity. #[inline] fn is_full(&self) -> bool { @@ -189,7 +193,7 @@ impl VecDeque { } #[inline] - fn wrap_idx(&self, idx: usize) -> usize { + fn to_physical_idx(&self, idx: usize) -> usize { self.wrap_add(self.head, idx) } @@ -473,6 +477,10 @@ impl VecDeque { debug_assert!(new_capacity >= old_capacity); // Move the shortest contiguous section of the ring buffer + // + // H := head + // L := last element (`self.to_physical_idx(self.len - 1)`) + // // H L // [o o o o o o o . ] // H L @@ -597,7 +605,7 @@ impl VecDeque { #[stable(feature = "rust1", since = "1.0.0")] pub fn get(&self, index: usize) -> Option<&T> { if index < self.len { - let idx = self.wrap_idx(index); + let idx = self.to_physical_idx(index); unsafe { Some(&*self.ptr().add(idx)) } } else { None @@ -626,7 +634,7 @@ impl VecDeque { #[stable(feature = "rust1", since = "1.0.0")] pub fn get_mut(&mut self, index: usize) -> Option<&mut T> { if index < self.len { - let idx = self.wrap_idx(index); + let idx = self.to_physical_idx(index); unsafe { Some(&mut *self.ptr().add(idx)) } } else { None @@ -660,8 +668,8 @@ impl VecDeque { pub fn swap(&mut self, i: usize, j: usize) { assert!(i < self.len()); assert!(j < self.len()); - let ri = self.wrap_idx(i); - let rj = self.wrap_idx(j); + let ri = self.to_physical_idx(i); + let rj = self.to_physical_idx(j); unsafe { ptr::swap(self.ptr().add(ri), self.ptr().add(rj)) } } @@ -913,6 +921,8 @@ impl VecDeque { if self.len == 0 { self.head = 0; } else if self.head >= target_cap && tail_outside { + // H := head + // L := last element // H L // [. . . . . . . . o o o o o o o . ] // H L @@ -923,6 +933,8 @@ impl VecDeque { } self.head = 0; } else if self.head < target_cap && tail_outside { + // H := head + // L := last element // H L // [. . . o o o o o o o . . . . . . ] // L H @@ -932,6 +944,8 @@ impl VecDeque { self.copy_nonoverlapping(target_cap, 0, len); } } else if self.head >= target_cap { + // H := head + // L := last element // L H // [o o o o o . . . . . . . . . o o ] // L H @@ -998,11 +1012,6 @@ impl VecDeque { return; } - if !mem::needs_drop::() { - self.len = len; - return; - } - let (front, back) = self.as_mut_slices(); if len > front.len() { let begin = len - front.len(); @@ -1102,18 +1111,10 @@ impl VecDeque { #[inline] #[stable(feature = "deque_extras_15", since = "1.5.0")] pub fn as_slices(&self) -> (&[T], &[T]) { - if self.is_contiguous() { - (unsafe { slice::from_raw_parts(self.ptr().add(self.head), self.len) }, &[]) - } else { - let head_len = self.capacity() - self.head; - let tail_len = self.len - head_len; - unsafe { - ( - slice::from_raw_parts(self.ptr().add(self.head), head_len), - slice::from_raw_parts(self.ptr(), tail_len), - ) - } - } + let (a_range, b_range) = self.slice_ranges(..); + // SAFETY: `slice_ranges` always returns valid ranges into + // the physical buffer. + unsafe { (&*self.buffer_range(a_range), &*self.buffer_range(b_range)) } } /// Returns a pair of slices which contain, in order, the contents of the @@ -1144,18 +1145,10 @@ impl VecDeque { #[inline] #[stable(feature = "deque_extras_15", since = "1.5.0")] pub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T]) { - if self.is_contiguous() { - (unsafe { slice::from_raw_parts_mut(self.ptr().add(self.head), self.len) }, &mut []) - } else { - let head_len = self.capacity() - self.head; - let tail_len = self.len - head_len; - unsafe { - ( - slice::from_raw_parts_mut(self.ptr().add(self.head), head_len), - slice::from_raw_parts_mut(self.ptr(), tail_len), - ) - } - } + let (a_range, b_range) = self.slice_ranges(..); + // SAFETY: `slice_ranges` always returns valid ranges into + // the physical buffer. + unsafe { (&mut *self.buffer_range(a_range), &mut *self.buffer_range(b_range)) } } /// Returns the number of elements in the deque. @@ -1192,18 +1185,37 @@ impl VecDeque { self.len == 0 } + /// Given a range into the logical buffer of the deque, this function + /// return two ranges into the physical buffer that correspond to + /// the given range. fn slice_ranges(&self, range: R) -> (Range, Range) where R: RangeBounds, { let Range { start, end } = slice::range(range, ..self.len); - let a_len = self.len.min(self.capacity() - self.head); - if end <= a_len { - (start..end, 0..0) - } else if start >= a_len { - (0..0, start - a_len..end - a_len) + let len = end - start; + + if len == 0 { + (0..0, 0..0) } else { - (start..a_len, 0..end - a_len) + // `slice::range` guarantees that `start <= end <= self.len`. + // because `len != 0`, we know that `start < end`, so `start < self.len` + // and the indexing is valid. + let wrapped_start = self.to_physical_idx(start); + + // this subtraction can never overflow because `wrapped_start` is + // at most `self.capacity()` (and if `self.capacity != 0`, then `wrapped_start` is strictly less + // than `self.capacity`). + let head_len = self.capacity() - wrapped_start; + + if head_len >= len { + // we know that `len + wrapped_start <= self.capacity <= usize::MAX`, so this addition can't overflow + (wrapped_start..wrapped_start + len, 0..0) + } else { + // can't overflow because of the if condition + let tail_len = len - head_len; + (wrapped_start..self.capacity(), 0..tail_len) + } } } @@ -1233,9 +1245,14 @@ impl VecDeque { where R: RangeBounds, { - let (a, b) = self.as_slices(); let (a_range, b_range) = self.slice_ranges(range); - Iter::new(a[a_range].iter(), b[b_range].iter()) + // SAFETY: The ranges returned by `slice_ranges` + // are valid ranges into the physical buffer, so + // it's ok to pass them to `buffer_range` and + // dereference the result. + let a = unsafe { &*self.buffer_range(a_range) }; + let b = unsafe { &*self.buffer_range(b_range) }; + Iter::new(a.iter(), b.iter()) } /// Creates an iterator that covers the specified mutable range in the deque. @@ -1269,8 +1286,13 @@ impl VecDeque { R: RangeBounds, { let (a_range, b_range) = self.slice_ranges(range); - let (a, b) = self.as_mut_slices(); - IterMut::new(a[a_range].iter_mut(), b[b_range].iter_mut()) + // SAFETY: The ranges returned by `slice_ranges` + // are valid ranges into the physical buffer, so + // it's ok to pass them to `buffer_range` and + // dereference the result. + let a = unsafe { &mut *self.buffer_range(a_range) }; + let b = unsafe { &mut *self.buffer_range(b_range) }; + IterMut::new(a.iter_mut(), b.iter_mut()) } /// Removes the specified range from the deque in bulk, returning all @@ -1509,7 +1531,7 @@ impl VecDeque { None } else { let old_head = self.head; - self.head = self.wrap_idx(1); + self.head = self.to_physical_idx(1); self.len -= 1; Some(unsafe { self.buffer_read(old_head) }) } @@ -1535,7 +1557,7 @@ impl VecDeque { None } else { self.len -= 1; - Some(unsafe { self.buffer_read(self.wrap_idx(self.len)) }) + Some(unsafe { self.buffer_read(self.to_physical_idx(self.len)) }) } } @@ -1583,7 +1605,7 @@ impl VecDeque { self.grow(); } - unsafe { self.buffer_write(self.wrap_idx(self.len), value) } + unsafe { self.buffer_write(self.to_physical_idx(self.len), value) } self.len += 1; } @@ -1700,8 +1722,8 @@ impl VecDeque { // and panicked. unsafe { // see `remove()` for explanation why this wrap_copy() call is safe. - self.wrap_copy(self.wrap_idx(index), self.wrap_idx(index + 1), k); - self.buffer_write(self.wrap_idx(index), value); + self.wrap_copy(self.to_physical_idx(index), self.to_physical_idx(index + 1), k); + self.buffer_write(self.to_physical_idx(index), value); self.len += 1; } } else { @@ -1709,7 +1731,7 @@ impl VecDeque { self.head = self.wrap_sub(self.head, 1); unsafe { self.wrap_copy(old_head, self.head, index); - self.buffer_write(self.wrap_idx(index), value); + self.buffer_write(self.to_physical_idx(index), value); self.len += 1; } } @@ -1742,7 +1764,7 @@ impl VecDeque { return None; } - let wrapped_idx = self.wrap_idx(index); + let wrapped_idx = self.to_physical_idx(index); let elem = unsafe { Some(self.buffer_read(wrapped_idx)) }; @@ -1755,7 +1777,7 @@ impl VecDeque { self.len -= 1; } else { let old_head = self.head; - self.head = self.wrap_idx(1); + self.head = self.to_physical_idx(1); unsafe { self.wrap_copy(old_head, self.head, index) }; self.len -= 1; } @@ -1866,14 +1888,14 @@ impl VecDeque { self.reserve(other.len); unsafe { let (left, right) = other.as_slices(); - self.copy_slice(self.wrap_idx(self.len), left); + self.copy_slice(self.to_physical_idx(self.len), left); // no overflow, because self.capacity() >= old_cap + left.len() >= self.len + left.len() - self.copy_slice(self.wrap_idx(self.len + left.len()), right); + self.copy_slice(self.to_physical_idx(self.len + left.len()), right); } // SAFETY: Update pointers after copying to avoid leaving doppelganger // in case of panics. self.len += other.len; - // Silently drop values in `other`. + // Now that we own its values, forget everything in `other`. other.len = 0; other.head = 0; } @@ -1982,8 +2004,7 @@ impl VecDeque { // buffer without it being full emerge debug_assert!(self.is_full()); let old_cap = self.capacity(); - // if the cap was 0, we need to reserve at least 1. - self.buf.reserve(old_cap, old_cap.max(1)); + self.buf.reserve_for_push(old_cap); unsafe { self.handle_capacity_increase(old_cap); } @@ -2265,16 +2286,16 @@ impl VecDeque { unsafe fn rotate_left_inner(&mut self, mid: usize) { debug_assert!(mid * 2 <= self.len()); unsafe { - self.wrap_copy(self.head, self.wrap_idx(self.len), mid); + self.wrap_copy(self.head, self.to_physical_idx(self.len), mid); } - self.head = self.wrap_idx(mid); + self.head = self.to_physical_idx(mid); } unsafe fn rotate_right_inner(&mut self, k: usize) { debug_assert!(k * 2 <= self.len()); self.head = self.wrap_sub(self.head, k); unsafe { - self.wrap_copy(self.wrap_idx(self.len), self.head, k); + self.wrap_copy(self.to_physical_idx(self.len), self.head, k); } } @@ -2532,8 +2553,13 @@ impl VecDeque { /// Returns the index in the underlying buffer for a given logical element index. #[inline] -fn wrap_index(index: usize, size: usize) -> usize { - if index >= size { index - size } else { index } +fn wrap_index(logical_index: usize, capacity: usize) -> usize { + debug_assert!( + (logical_index == 0 && capacity == 0) + || logical_index < capacity + || (logical_index - capacity) < capacity + ); + if logical_index >= capacity { logical_index - capacity } else { logical_index } } #[stable(feature = "rust1", since = "1.0.0")] diff --git a/library/alloc/src/collections/vec_deque/spec_extend.rs b/library/alloc/src/collections/vec_deque/spec_extend.rs index 0f722ceb0823b..adcc8862a8c31 100644 --- a/library/alloc/src/collections/vec_deque/spec_extend.rs +++ b/library/alloc/src/collections/vec_deque/spec_extend.rs @@ -38,7 +38,7 @@ where // and `room == self.capacity() - self.len` // => `self.len + room <= self.capacity()` self.write_iter_wrapping( - self.wrap_idx(self.len), + self.to_physical_idx(self.len), ByRefSized(&mut iter).take(room), room, ); @@ -63,8 +63,9 @@ where ); self.reserve(additional); - let written = - unsafe { self.write_iter_wrapping(self.wrap_idx(self.len), iter, additional) }; + let written = unsafe { + self.write_iter_wrapping(self.to_physical_idx(self.len), iter, additional) + }; debug_assert_eq!( additional, written, @@ -87,7 +88,7 @@ impl SpecExtend> for VecDeque { self.reserve(slice.len()); unsafe { - self.copy_slice(self.wrap_idx(self.len), slice); + self.copy_slice(self.to_physical_idx(self.len), slice); self.len += slice.len(); } iterator.forget_remaining_elements(); @@ -113,7 +114,7 @@ where self.reserve(slice.len()); unsafe { - self.copy_slice(self.wrap_idx(self.len), slice); + self.copy_slice(self.to_physical_idx(self.len), slice); self.len += slice.len(); } } diff --git a/library/alloc/src/collections/vec_deque/tests.rs b/library/alloc/src/collections/vec_deque/tests.rs index 6995c5b47dbf8..553f8da3e2d01 100644 --- a/library/alloc/src/collections/vec_deque/tests.rs +++ b/library/alloc/src/collections/vec_deque/tests.rs @@ -257,13 +257,14 @@ fn test_swap_panic() { #[test] fn test_reserve_exact() { let mut tester: VecDeque = VecDeque::with_capacity(1); - assert!(tester.capacity() == 1); + assert_eq!(tester.capacity(), 1); tester.reserve_exact(50); - assert!(tester.capacity() >= 50); + assert_eq!(tester.capacity(), 50); tester.reserve_exact(40); - assert!(tester.capacity() >= 40); + // reserving won't shrink the buffer + assert_eq!(tester.capacity(), 50); tester.reserve_exact(200); - assert!(tester.capacity() >= 200); + assert_eq!(tester.capacity(), 200); } #[test] @@ -479,7 +480,7 @@ fn make_contiguous_big_tail() { assert_eq!(tester.capacity(), 15); assert_eq!((&[9, 8, 7, 6, 5, 4, 3] as &[_], &[0, 1, 2] as &[_]), tester.as_slices()); - let expected_start = tester.wrap_idx(tester.len); + let expected_start = tester.to_physical_idx(tester.len); tester.make_contiguous(); assert_eq!(tester.head, expected_start); assert_eq!((&[9, 8, 7, 6, 5, 4, 3, 0, 1, 2] as &[_], &[] as &[_]), tester.as_slices()); @@ -679,7 +680,7 @@ fn test_drain() { let cap = tester.capacity(); for len in 0..=cap { - for head in 0..=cap { + for head in 0..cap { for drain_start in 0..=len { for drain_end in drain_start..=len { tester.head = head; From 53f78ae0f3ee454b5178aaaedf620d961dbbbb25 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Fri, 18 Nov 2022 21:29:26 +0000 Subject: [PATCH 056/244] Simplify a bunch of trait ref obligation creations --- clippy_lints/src/ptr.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clippy_lints/src/ptr.rs b/clippy_lints/src/ptr.rs index 5420a0e782ea8..8c4cff66f554b 100644 --- a/clippy_lints/src/ptr.rs +++ b/clippy_lints/src/ptr.rs @@ -698,9 +698,8 @@ fn matches_preds<'tcx>( cx.tcx, ObligationCause::dummy(), cx.param_env, - cx.tcx.mk_predicate(Binder::bind_with_vars( + cx.tcx.mk_predicate(Binder::dummy( PredicateKind::Projection(p.with_self_ty(cx.tcx, ty)), - List::empty(), )), )), ExistentialPredicate::AutoTrait(p) => infcx From 3f059a49a483c3ae31e378e5d5993b4215ee9ce9 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 24 Nov 2022 18:14:58 -0300 Subject: [PATCH 057/244] Introduce PredicateKind::Clause --- clippy_lints/src/dereference.rs | 8 ++++---- clippy_lints/src/derive.rs | 8 ++++---- clippy_lints/src/future_not_send.rs | 4 ++-- clippy_lints/src/methods/unnecessary_to_owned.rs | 8 ++++---- clippy_lints/src/needless_pass_by_value.rs | 2 +- clippy_lints/src/ptr.rs | 4 ++-- clippy_lints/src/unit_return_expecting_ord.rs | 6 +++--- clippy_utils/src/eager_or_lazy.rs | 2 +- clippy_utils/src/qualify_min_const_fn.rs | 8 ++++---- clippy_utils/src/ty.rs | 16 ++++++++-------- 10 files changed, 33 insertions(+), 33 deletions(-) diff --git a/clippy_lints/src/dereference.rs b/clippy_lints/src/dereference.rs index 18e742a85b346..47ea98956be24 100644 --- a/clippy_lints/src/dereference.rs +++ b/clippy_lints/src/dereference.rs @@ -25,7 +25,7 @@ use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::mir::{Rvalue, StatementKind}; use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability}; use rustc_middle::ty::{ - self, Binder, BoundVariableKind, EarlyBinder, FnSig, GenericArgKind, List, ParamTy, PredicateKind, + self, Binder, BoundVariableKind, Clause, EarlyBinder, FnSig, GenericArgKind, List, ParamTy, PredicateKind, ProjectionPredicate, Ty, TyCtxt, TypeVisitable, TypeckResults, }; use rustc_semver::RustcVersion; @@ -1097,7 +1097,7 @@ fn needless_borrow_impl_arg_position<'tcx>( let projection_predicates = predicates .iter() .filter_map(|predicate| { - if let PredicateKind::Projection(projection_predicate) = predicate.kind().skip_binder() { + if let PredicateKind::Clause(Clause::Projection(projection_predicate)) = predicate.kind().skip_binder() { Some(projection_predicate) } else { None @@ -1111,7 +1111,7 @@ fn needless_borrow_impl_arg_position<'tcx>( if predicates .iter() .filter_map(|predicate| { - if let PredicateKind::Trait(trait_predicate) = predicate.kind().skip_binder() + if let PredicateKind::Clause(Clause::Trait(trait_predicate)) = predicate.kind().skip_binder() && trait_predicate.trait_ref.self_ty() == param_ty.to_ty(cx.tcx) { Some(trait_predicate.trait_ref.def_id) @@ -1173,7 +1173,7 @@ fn needless_borrow_impl_arg_position<'tcx>( } predicates.iter().all(|predicate| { - if let PredicateKind::Trait(trait_predicate) = predicate.kind().skip_binder() + if let PredicateKind::Clause(Clause::Trait(trait_predicate)) = predicate.kind().skip_binder() && cx.tcx.is_diagnostic_item(sym::IntoIterator, trait_predicate.trait_ref.def_id) && let ty::Param(param_ty) = trait_predicate.self_ty().kind() && let GenericArgKind::Type(ty) = substs_with_referent_ty[param_ty.index as usize].unpack() diff --git a/clippy_lints/src/derive.rs b/clippy_lints/src/derive.rs index 1d9af7cdbd358..d870e0ceef471 100644 --- a/clippy_lints/src/derive.rs +++ b/clippy_lints/src/derive.rs @@ -14,7 +14,7 @@ use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::hir::nested_filter; use rustc_middle::traits::Reveal; use rustc_middle::ty::{ - self, Binder, BoundConstness, GenericParamDefKind, ImplPolarity, ParamEnv, PredicateKind, TraitPredicate, TraitRef, + self, Binder, BoundConstness, Clause, GenericParamDefKind, ImplPolarity, ParamEnv, PredicateKind, TraitPredicate, TraitRef, Ty, TyCtxt, }; use rustc_session::{declare_lint_pass, declare_tool_lint}; @@ -499,7 +499,7 @@ fn param_env_for_derived_eq(tcx: TyCtxt<'_>, did: DefId, eq_trait_id: DefId) -> let ty_predicates = tcx.predicates_of(did).predicates; for (p, _) in ty_predicates { - if let PredicateKind::Trait(p) = p.kind().skip_binder() + if let PredicateKind::Clause(Clause::Trait(p)) = p.kind().skip_binder() && p.trait_ref.def_id == eq_trait_id && let ty::Param(self_ty) = p.trait_ref.self_ty().kind() && p.constness == BoundConstness::NotConst @@ -512,14 +512,14 @@ fn param_env_for_derived_eq(tcx: TyCtxt<'_>, did: DefId, eq_trait_id: DefId) -> ParamEnv::new( tcx.mk_predicates(ty_predicates.iter().map(|&(p, _)| p).chain( params.iter().filter(|&&(_, needs_eq)| needs_eq).map(|&(param, _)| { - tcx.mk_predicate(Binder::dummy(PredicateKind::Trait(TraitPredicate { + tcx.mk_predicate(Binder::dummy(PredicateKind::Clause(Clause::Trait(TraitPredicate { trait_ref: TraitRef::new( eq_trait_id, tcx.mk_substs(std::iter::once(tcx.mk_param_from_def(param))), ), constness: BoundConstness::NotConst, polarity: ImplPolarity::Positive, - }))) + })))) }), )), Reveal::UserFacing, diff --git a/clippy_lints/src/future_not_send.rs b/clippy_lints/src/future_not_send.rs index 0519f9ac24682..a9425a40f885e 100644 --- a/clippy_lints/src/future_not_send.rs +++ b/clippy_lints/src/future_not_send.rs @@ -4,7 +4,7 @@ use rustc_hir::intravisit::FnKind; use rustc_hir::{Body, FnDecl, HirId}; use rustc_infer::infer::TyCtxtInferExt; use rustc_lint::{LateContext, LateLintPass}; -use rustc_middle::ty::{EarlyBinder, Opaque, PredicateKind::Trait}; +use rustc_middle::ty::{Clause, EarlyBinder, Opaque, PredicateKind}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::{sym, Span}; use rustc_trait_selection::traits::error_reporting::suggestions::TypeErrCtxtExt; @@ -91,7 +91,7 @@ impl<'tcx> LateLintPass<'tcx> for FutureNotSend { infcx .err_ctxt() .maybe_note_obligation_cause_for_async_await(db, &obligation); - if let Trait(trait_pred) = obligation.predicate.kind().skip_binder() { + if let PredicateKind::Clause(Clause::Trait(trait_pred)) = obligation.predicate.kind().skip_binder() { db.note(&format!( "`{}` doesn't implement `{}`", trait_pred.self_ty(), diff --git a/clippy_lints/src/methods/unnecessary_to_owned.rs b/clippy_lints/src/methods/unnecessary_to_owned.rs index 8b000cd754cd1..7ff13b95626ba 100644 --- a/clippy_lints/src/methods/unnecessary_to_owned.rs +++ b/clippy_lints/src/methods/unnecessary_to_owned.rs @@ -17,7 +17,7 @@ use rustc_middle::mir::Mutability; use rustc_middle::ty::adjustment::{Adjust, Adjustment, OverloadedDeref}; use rustc_middle::ty::subst::{GenericArg, GenericArgKind, SubstsRef}; use rustc_middle::ty::EarlyBinder; -use rustc_middle::ty::{self, ParamTy, PredicateKind, ProjectionPredicate, TraitPredicate, Ty}; +use rustc_middle::ty::{self, Clause, ParamTy, PredicateKind, ProjectionPredicate, TraitPredicate, Ty}; use rustc_semver::RustcVersion; use rustc_span::{sym, Symbol}; use rustc_trait_selection::traits::{ @@ -341,12 +341,12 @@ fn get_input_traits_and_projections<'tcx>( let mut projection_predicates = Vec::new(); for predicate in cx.tcx.param_env(callee_def_id).caller_bounds() { match predicate.kind().skip_binder() { - PredicateKind::Trait(trait_predicate) => { + PredicateKind::Clause(Clause::Trait(trait_predicate)) => { if trait_predicate.trait_ref.self_ty() == input { trait_predicates.push(trait_predicate); } } - PredicateKind::Projection(projection_predicate) => { + PredicateKind::Clause(Clause::Projection(projection_predicate)) => { if projection_predicate.projection_ty.self_ty() == input { projection_predicates.push(projection_predicate); } @@ -403,7 +403,7 @@ fn can_change_type<'a>(cx: &LateContext<'a>, mut expr: &'a Expr<'a>, mut ty: Ty< let mut trait_predicates = cx.tcx.param_env(callee_def_id) .caller_bounds().iter().filter(|predicate| { - if let PredicateKind::Trait(trait_predicate) = predicate.kind().skip_binder() + if let PredicateKind::Clause(Clause::Trait(trait_predicate)) = predicate.kind().skip_binder() && trait_predicate.trait_ref.self_ty() == *param_ty { true } else { diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index 55599cd03ebe1..75e12715458f4 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -124,7 +124,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue { .filter_map(|obligation| { // Note that we do not want to deal with qualified predicates here. match obligation.predicate.kind().no_bound_vars() { - Some(ty::PredicateKind::Trait(pred)) if pred.def_id() != sized_trait => Some(pred), + Some(ty::PredicateKind::Clause(ty::Clause::Trait(pred))) if pred.def_id() != sized_trait => Some(pred), _ => None, } }) diff --git a/clippy_lints/src/ptr.rs b/clippy_lints/src/ptr.rs index 8c4cff66f554b..d28e97b794352 100644 --- a/clippy_lints/src/ptr.rs +++ b/clippy_lints/src/ptr.rs @@ -19,7 +19,7 @@ use rustc_infer::infer::TyCtxtInferExt; use rustc_infer::traits::{Obligation, ObligationCause}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::hir::nested_filter; -use rustc_middle::ty::{self, Binder, ExistentialPredicate, List, PredicateKind, Ty}; +use rustc_middle::ty::{self, Binder, Clause, ExistentialPredicate, List, PredicateKind, Ty}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::Span; use rustc_span::sym; @@ -699,7 +699,7 @@ fn matches_preds<'tcx>( ObligationCause::dummy(), cx.param_env, cx.tcx.mk_predicate(Binder::dummy( - PredicateKind::Projection(p.with_self_ty(cx.tcx, ty)), + PredicateKind::Clause(Clause::Projection(p.with_self_ty(cx.tcx, ty))), )), )), ExistentialPredicate::AutoTrait(p) => infcx diff --git a/clippy_lints/src/unit_return_expecting_ord.rs b/clippy_lints/src/unit_return_expecting_ord.rs index 1307288623f95..a138a4baa9b31 100644 --- a/clippy_lints/src/unit_return_expecting_ord.rs +++ b/clippy_lints/src/unit_return_expecting_ord.rs @@ -4,7 +4,7 @@ use rustc_hir::def_id::DefId; use rustc_hir::{Closure, Expr, ExprKind, StmtKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty; -use rustc_middle::ty::{GenericPredicates, PredicateKind, ProjectionPredicate, TraitPredicate}; +use rustc_middle::ty::{Clause, GenericPredicates, PredicateKind, ProjectionPredicate, TraitPredicate}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::{sym, BytePos, Span}; @@ -45,7 +45,7 @@ fn get_trait_predicates_for_trait_id<'tcx>( let mut preds = Vec::new(); for (pred, _) in generics.predicates { if_chain! { - if let PredicateKind::Trait(poly_trait_pred) = pred.kind().skip_binder(); + if let PredicateKind::Clause(Clause::Trait(poly_trait_pred)) = pred.kind().skip_binder(); let trait_pred = cx.tcx.erase_late_bound_regions(pred.kind().rebind(poly_trait_pred)); if let Some(trait_def_id) = trait_id; if trait_def_id == trait_pred.trait_ref.def_id; @@ -63,7 +63,7 @@ fn get_projection_pred<'tcx>( trait_pred: TraitPredicate<'tcx>, ) -> Option> { generics.predicates.iter().find_map(|(proj_pred, _)| { - if let ty::PredicateKind::Projection(pred) = proj_pred.kind().skip_binder() { + if let ty::PredicateKind::Clause(Clause::Projection(pred)) = proj_pred.kind().skip_binder() { let projection_pred = cx.tcx.erase_late_bound_regions(proj_pred.kind().rebind(pred)); if projection_pred.projection_ty.substs == trait_pred.trait_ref.substs { return Some(projection_pred); diff --git a/clippy_utils/src/eager_or_lazy.rs b/clippy_utils/src/eager_or_lazy.rs index 95b3e651e2b53..f74f7dadfa908 100644 --- a/clippy_utils/src/eager_or_lazy.rs +++ b/clippy_utils/src/eager_or_lazy.rs @@ -73,7 +73,7 @@ fn fn_eagerness(cx: &LateContext<'_>, fn_id: DefId, name: Symbol, have_one_arg: .flat_map(|v| v.fields.iter()) .any(|x| matches!(cx.tcx.type_of(x.did).peel_refs().kind(), ty::Param(_))) && all_predicates_of(cx.tcx, fn_id).all(|(pred, _)| match pred.kind().skip_binder() { - PredicateKind::Trait(pred) => cx.tcx.trait_def(pred.trait_ref.def_id).is_marker, + PredicateKind::Clause(ty::Clause::Trait(pred)) => cx.tcx.trait_def(pred.trait_ref.def_id).is_marker, _ => true, }) && subs.types().all(|x| matches!(x.peel_refs().kind(), ty::Param(_))) diff --git a/clippy_utils/src/qualify_min_const_fn.rs b/clippy_utils/src/qualify_min_const_fn.rs index d183e28f667c0..b8c2dd5ab9ea1 100644 --- a/clippy_utils/src/qualify_min_const_fn.rs +++ b/clippy_utils/src/qualify_min_const_fn.rs @@ -25,13 +25,13 @@ pub fn is_min_const_fn<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, msrv: Option< let predicates = tcx.predicates_of(current); for (predicate, _) in predicates.predicates { match predicate.kind().skip_binder() { - ty::PredicateKind::RegionOutlives(_) - | ty::PredicateKind::TypeOutlives(_) + ty::PredicateKind::Clause(ty::Clause::RegionOutlives(_)) + | ty::PredicateKind::Clause(ty::Clause::TypeOutlives(_)) | ty::PredicateKind::WellFormed(_) - | ty::PredicateKind::Projection(_) + | ty::PredicateKind::Clause(ty::Clause::Projection(_)) | ty::PredicateKind::ConstEvaluatable(..) | ty::PredicateKind::ConstEquate(..) - | ty::PredicateKind::Trait(..) + | ty::PredicateKind::Clause(ty::Clause::Trait(..)) | ty::PredicateKind::TypeWellFormedFromEnv(..) => continue, ty::PredicateKind::ObjectSafe(_) => panic!("object safe predicate on function: {predicate:#?}"), ty::PredicateKind::ClosureKind(..) => panic!("closure kind predicate on function: {predicate:#?}"), diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index 8284dc5c28c0b..f4459e3e6633a 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -81,7 +81,7 @@ pub fn contains_ty_adt_constructor_opaque<'tcx>(cx: &LateContext<'tcx>, ty: Ty<' match predicate.kind().skip_binder() { // For `impl Trait`, it will register a predicate of `T: Trait`, so we go through // and check substituions to find `U`. - ty::PredicateKind::Trait(trait_predicate) => { + ty::PredicateKind::Clause(ty::Clause::Trait(trait_predicate)) => { if trait_predicate .trait_ref .substs @@ -94,7 +94,7 @@ pub fn contains_ty_adt_constructor_opaque<'tcx>(cx: &LateContext<'tcx>, ty: Ty<' }, // For `impl Trait`, it will register a predicate of `::Assoc = U`, // so we check the term for `U`. - ty::PredicateKind::Projection(projection_predicate) => { + ty::PredicateKind::Clause(ty::Clause::Projection(projection_predicate)) => { if let ty::TermKind::Ty(ty) = projection_predicate.term.unpack() { if contains_ty_adt_constructor_opaque(cx, ty, needle) { return true; @@ -239,7 +239,7 @@ pub fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool { ty::Tuple(substs) => substs.iter().any(|ty| is_must_use_ty(cx, ty)), ty::Opaque(def_id, _) => { for (predicate, _) in cx.tcx.explicit_item_bounds(*def_id) { - if let ty::PredicateKind::Trait(trait_predicate) = predicate.kind().skip_binder() { + if let ty::PredicateKind::Clause(ty::Clause::Trait(trait_predicate)) = predicate.kind().skip_binder() { if cx.tcx.has_attr(trait_predicate.trait_ref.def_id, sym::must_use) { return true; } @@ -658,7 +658,7 @@ fn sig_from_bounds<'tcx>( for pred in predicates { match pred.kind().skip_binder() { - PredicateKind::Trait(p) + PredicateKind::Clause(ty::Clause::Trait(p)) if (lang_items.fn_trait() == Some(p.def_id()) || lang_items.fn_mut_trait() == Some(p.def_id()) || lang_items.fn_once_trait() == Some(p.def_id())) @@ -671,7 +671,7 @@ fn sig_from_bounds<'tcx>( } inputs = Some(i); }, - PredicateKind::Projection(p) + PredicateKind::Clause(ty::Clause::Projection(p)) if Some(p.projection_ty.item_def_id) == lang_items.fn_once_output() && p.projection_ty.self_ty() == ty => { @@ -699,7 +699,7 @@ fn sig_for_projection<'tcx>(cx: &LateContext<'tcx>, ty: ProjectionTy<'tcx>) -> O .subst_iter_copied(cx.tcx, ty.substs) { match pred.kind().skip_binder() { - PredicateKind::Trait(p) + PredicateKind::Clause(ty::Clause::Trait(p)) if (lang_items.fn_trait() == Some(p.def_id()) || lang_items.fn_mut_trait() == Some(p.def_id()) || lang_items.fn_once_trait() == Some(p.def_id())) => @@ -712,7 +712,7 @@ fn sig_for_projection<'tcx>(cx: &LateContext<'tcx>, ty: ProjectionTy<'tcx>) -> O } inputs = Some(i); }, - PredicateKind::Projection(p) if Some(p.projection_ty.item_def_id) == lang_items.fn_once_output() => { + PredicateKind::Clause(ty::Clause::Projection(p)) if Some(p.projection_ty.item_def_id) == lang_items.fn_once_output() => { if output.is_some() { // Multiple different fn trait impls. Is this even allowed? return None; @@ -887,7 +887,7 @@ pub fn ty_is_fn_once_param<'tcx>(tcx: TyCtxt<'_>, ty: Ty<'tcx>, predicates: &'tc predicates .iter() .try_fold(false, |found, p| { - if let PredicateKind::Trait(p) = p.kind().skip_binder() + if let PredicateKind::Clause(ty::Clause::Trait(p)) = p.kind().skip_binder() && let ty::Param(self_ty) = p.trait_ref.self_ty().kind() && ty.index == self_ty.index { From db760f67908d977639fee048de9344504d0a5d86 Mon Sep 17 00:00:00 2001 From: Pratush Rai Date: Fri, 25 Nov 2022 10:52:22 +0530 Subject: [PATCH 058/244] bootstrap --- src/bootstrap/lib.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index f4fa556b97450..108de217d7bfa 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -647,9 +647,11 @@ impl Build { if !update(true).status().map_or(false, |status| status.success()) { self.run(&mut update(false)); } - + // + self.run(Command::new("git").args(&["stash", "push"]).current_dir(&absolute_path)); self.run(Command::new("git").args(&["reset", "-q", "--hard"]).current_dir(&absolute_path)); - self.run(Command::new("git").args(&["clean", "-qdfx"]).current_dir(absolute_path)); + self.run(Command::new("git").args(&["clean", "-qdfx"]).current_dir(&absolute_path)); + self.run(Command::new("git").args(&["stash", "pop"]).current_dir(absolute_path)) } /// If any submodule has been initialized already, sync it unconditionally. From 424ae2395864051743d0fdd7bffafd0a75b5ac0f Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 25 Nov 2022 08:47:59 +0100 Subject: [PATCH 059/244] RefCell::get_mut: fix typo and fix the same typo in a bunch of other places --- clippy_dev/src/setup/git_hook.rs | 2 +- clippy_utils/src/hir_utils.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/clippy_dev/src/setup/git_hook.rs b/clippy_dev/src/setup/git_hook.rs index 1de5b1940bae1..c7c53bc69d0b8 100644 --- a/clippy_dev/src/setup/git_hook.rs +++ b/clippy_dev/src/setup/git_hook.rs @@ -6,7 +6,7 @@ use super::verify_inside_clippy_dir; /// Rusts setup uses `git rev-parse --git-common-dir` to get the root directory of the repo. /// I've decided against this for the sake of simplicity and to make sure that it doesn't install /// the hook if `clippy_dev` would be used in the rust tree. The hook also references this tool -/// for formatting and should therefor only be used in a normal clone of clippy +/// for formatting and should therefore only be used in a normal clone of clippy const REPO_GIT_DIR: &str = ".git"; const HOOK_SOURCE_FILE: &str = "util/etc/pre-commit.sh"; const HOOK_TARGET_FILE: &str = ".git/hooks/pre-commit"; diff --git a/clippy_utils/src/hir_utils.rs b/clippy_utils/src/hir_utils.rs index cf24ec8b67b90..abe10f3c81e51 100644 --- a/clippy_utils/src/hir_utils.rs +++ b/clippy_utils/src/hir_utils.rs @@ -113,7 +113,7 @@ impl HirEqInterExpr<'_, '_, '_> { } } - // eq_pat adds the HirIds to the locals map. We therefor call it last to make sure that + // eq_pat adds the HirIds to the locals map. We therefore call it last to make sure that // these only get added if the init and type is equal. both(&l.init, &r.init, |l, r| self.eq_expr(l, r)) && both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r)) From a116b9bdba6c3c00f4544a937af690bd462ca428 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 14 Nov 2022 13:42:47 +0100 Subject: [PATCH 060/244] Lint unnecessary safety comments on items --- CHANGELOG.md | 1 + clippy_lints/src/declared_lints.rs | 1 + .../src/undocumented_unsafe_blocks.rs | 240 +++++++++++++----- tests/ui/undocumented_unsafe_blocks.rs | 2 +- tests/ui/undocumented_unsafe_blocks.stderr | 15 +- 5 files changed, 200 insertions(+), 59 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b6b12c623af1..23912bb3ed6b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4451,6 +4451,7 @@ Released 2018-09-13 [`unnecessary_mut_passed`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_mut_passed [`unnecessary_operation`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation [`unnecessary_owned_empty_strings`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_owned_empty_strings +[`unnecessary_safety_comment`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_safety_comment [`unnecessary_safety_doc`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_safety_doc [`unnecessary_self_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_self_imports [`unnecessary_sort_by`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_sort_by diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index eb3210946f119..e4d76f07d6b48 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -584,6 +584,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::types::TYPE_COMPLEXITY_INFO, crate::types::VEC_BOX_INFO, crate::undocumented_unsafe_blocks::UNDOCUMENTED_UNSAFE_BLOCKS_INFO, + crate::undocumented_unsafe_blocks::UNNECESSARY_SAFETY_COMMENT_INFO, crate::unicode::INVISIBLE_CHARACTERS_INFO, crate::unicode::NON_ASCII_LITERAL_INFO, crate::unicode::UNICODE_NOT_NFC_INFO, diff --git a/clippy_lints/src/undocumented_unsafe_blocks.rs b/clippy_lints/src/undocumented_unsafe_blocks.rs index e8f15a4447352..57ee2735aa033 100644 --- a/clippy_lints/src/undocumented_unsafe_blocks.rs +++ b/clippy_lints/src/undocumented_unsafe_blocks.rs @@ -59,8 +59,36 @@ declare_clippy_lint! { restriction, "creating an unsafe block without explaining why it is safe" } +declare_clippy_lint! { + /// ### What it does + /// Checks for `// SAFETY: ` comments on safe code. + /// + /// ### Why is this bad? + /// Safe code has no safety requirements, so there is no need to + /// describe safety invariants. + /// + /// ### Example + /// ```rust + /// use std::ptr::NonNull; + /// let a = &mut 42; + /// + /// // SAFETY: references are guaranteed to be non-null. + /// let ptr = NonNull::new(a).unwrap(); + /// ``` + /// Use instead: + /// ```rust + /// use std::ptr::NonNull; + /// let a = &mut 42; + /// + /// let ptr = NonNull::new(a).unwrap(); + /// ``` + #[clippy::version = "1.66.0"] + pub UNNECESSARY_SAFETY_COMMENT, + restriction, + "creating an unsafe block without explaining why it is safe" +} -declare_lint_pass!(UndocumentedUnsafeBlocks => [UNDOCUMENTED_UNSAFE_BLOCKS]); +declare_lint_pass!(UndocumentedUnsafeBlocks => [UNDOCUMENTED_UNSAFE_BLOCKS, UNNECESSARY_SAFETY_COMMENT]); impl LateLintPass<'_> for UndocumentedUnsafeBlocks { fn check_block(&mut self, cx: &LateContext<'_>, block: &'_ Block<'_>) { @@ -90,28 +118,95 @@ impl LateLintPass<'_> for UndocumentedUnsafeBlocks { } fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) { - if let hir::ItemKind::Impl(imple) = item.kind - && imple.unsafety == hir::Unsafety::Unsafe - && !in_external_macro(cx.tcx.sess, item.span) - && !is_lint_allowed(cx, UNDOCUMENTED_UNSAFE_BLOCKS, item.hir_id()) - && !is_unsafe_from_proc_macro(cx, item.span) - && !item_has_safety_comment(cx, item) - { + if in_external_macro(cx.tcx.sess, item.span) { + return; + } + + let mk_spans = |pos: BytePos| { let source_map = cx.tcx.sess.source_map(); + let span = Span::new(pos, pos, SyntaxContext::root(), None); + let help_span = source_map.span_extend_to_next_char(span, '\n', true); let span = if source_map.is_multiline(item.span) { source_map.span_until_char(item.span, '\n') } else { item.span }; + (span, help_span) + }; - span_lint_and_help( - cx, - UNDOCUMENTED_UNSAFE_BLOCKS, - span, - "unsafe impl missing a safety comment", - None, - "consider adding a safety comment on the preceding line", - ); + let item_has_safety_comment = item_has_safety_comment(cx, item); + match (&item.kind, item_has_safety_comment) { + (hir::ItemKind::Impl(impl_), HasSafetyComment::No) if impl_.unsafety == hir::Unsafety::Unsafe => { + if !is_lint_allowed(cx, UNDOCUMENTED_UNSAFE_BLOCKS, item.hir_id()) + && !is_unsafe_from_proc_macro(cx, item.span) + { + let source_map = cx.tcx.sess.source_map(); + let span = if source_map.is_multiline(item.span) { + source_map.span_until_char(item.span, '\n') + } else { + item.span + }; + + span_lint_and_help( + cx, + UNDOCUMENTED_UNSAFE_BLOCKS, + span, + "unsafe impl missing a safety comment", + None, + "consider adding a safety comment on the preceding line", + ); + } + }, + (hir::ItemKind::Impl(impl_), HasSafetyComment::Yes(pos)) if impl_.unsafety == hir::Unsafety::Normal => { + if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, item.hir_id()) { + let (span, help_span) = mk_spans(pos); + + span_lint_and_help( + cx, + UNNECESSARY_SAFETY_COMMENT, + span, + "impl has unnecessary safety comment", + Some(help_span), + "consider removing the safety comment", + ); + } + }, + (hir::ItemKind::Impl(_), _) => {}, + (&hir::ItemKind::Const(.., body) | &hir::ItemKind::Static(.., body), HasSafetyComment::Yes(pos)) => { + if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, body.hir_id) { + let body = cx.tcx.hir().body(body); + if !matches!( + body.value.kind, hir::ExprKind::Block(block, _) + if block.rules == BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided) + ) { + let (span, help_span) = mk_spans(pos); + + span_lint_and_help( + cx, + UNNECESSARY_SAFETY_COMMENT, + span, + &format!("{} has unnecessary safety comment", item.kind.descr()), + Some(help_span), + "consider removing the safety comment", + ); + } + } + }, + (_, HasSafetyComment::Yes(pos)) => { + if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, item.hir_id()) { + let (span, help_span) = mk_spans(pos); + + span_lint_and_help( + cx, + UNNECESSARY_SAFETY_COMMENT, + span, + &format!("{} has unnecessary safety comment", item.kind.descr()), + Some(help_span), + "consider removing the safety comment", + ); + } + }, + _ => (), } } } @@ -170,28 +265,38 @@ fn block_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool { // won't work. This is to avoid dealing with where such a comment should be place relative to // attributes and doc comments. - span_from_macro_expansion_has_safety_comment(cx, span) || span_in_body_has_safety_comment(cx, span) + matches!( + span_from_macro_expansion_has_safety_comment(cx, span), + HasSafetyComment::Yes(_) + ) || span_in_body_has_safety_comment(cx, span) +} + +enum HasSafetyComment { + Yes(BytePos), + No, + Maybe, } /// Checks if the lines immediately preceding the item contain a safety comment. #[allow(clippy::collapsible_match)] -fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> bool { - if span_from_macro_expansion_has_safety_comment(cx, item.span) { - return true; +fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSafetyComment { + match span_from_macro_expansion_has_safety_comment(cx, item.span) { + HasSafetyComment::Maybe => (), + has_safety_comment => return has_safety_comment, } if item.span.ctxt() == SyntaxContext::root() { if let Some(parent_node) = get_parent_node(cx.tcx, item.hir_id()) { let comment_start = match parent_node { Node::Crate(parent_mod) => { - comment_start_before_impl_in_mod(cx, parent_mod, parent_mod.spans.inner_span, item) + comment_start_before_item_in_mod(cx, parent_mod, parent_mod.spans.inner_span, item) }, Node::Item(parent_item) => { if let ItemKind::Mod(parent_mod) = &parent_item.kind { - comment_start_before_impl_in_mod(cx, parent_mod, parent_item.span, item) + comment_start_before_item_in_mod(cx, parent_mod, parent_item.span, item) } else { // Doesn't support impls in this position. Pretend a comment was found. - return true; + return HasSafetyComment::Maybe; } }, Node::Stmt(stmt) => { @@ -200,17 +305,17 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> bool { Node::Block(block) => walk_span_to_context(block.span, SyntaxContext::root()).map(Span::lo), _ => { // Doesn't support impls in this position. Pretend a comment was found. - return true; + return HasSafetyComment::Maybe; }, } } else { // Problem getting the parent node. Pretend a comment was found. - return true; + return HasSafetyComment::Maybe; } }, _ => { // Doesn't support impls in this position. Pretend a comment was found. - return true; + return HasSafetyComment::Maybe; }, }; @@ -222,33 +327,40 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> bool { && let Some(src) = unsafe_line.sf.src.as_deref() { unsafe_line.sf.lines(|lines| { - comment_start_line.line < unsafe_line.line && text_has_safety_comment( - src, - &lines[comment_start_line.line + 1..=unsafe_line.line], - unsafe_line.sf.start_pos.to_usize(), - ) + if comment_start_line.line >= unsafe_line.line { + HasSafetyComment::No + } else { + match text_has_safety_comment( + src, + &lines[comment_start_line.line + 1..=unsafe_line.line], + unsafe_line.sf.start_pos.to_usize(), + ) { + Some(b) => HasSafetyComment::Yes(b), + None => HasSafetyComment::No, + } + } }) } else { // Problem getting source text. Pretend a comment was found. - true + HasSafetyComment::Maybe } } else { // No parent node. Pretend a comment was found. - true + HasSafetyComment::Maybe } } else { - false + HasSafetyComment::No } } -fn comment_start_before_impl_in_mod( +fn comment_start_before_item_in_mod( cx: &LateContext<'_>, parent_mod: &hir::Mod<'_>, parent_mod_span: Span, - imple: &hir::Item<'_>, + item: &hir::Item<'_>, ) -> Option { parent_mod.item_ids.iter().enumerate().find_map(|(idx, item_id)| { - if *item_id == imple.item_id() { + if *item_id == item.item_id() { if idx == 0 { // mod A { /* comment */ unsafe impl T {} ... } // ^------------------------------------------^ returns the start of this span @@ -270,11 +382,11 @@ fn comment_start_before_impl_in_mod( }) } -fn span_from_macro_expansion_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool { +fn span_from_macro_expansion_has_safety_comment(cx: &LateContext<'_>, span: Span) -> HasSafetyComment { let source_map = cx.sess().source_map(); let ctxt = span.ctxt(); if ctxt == SyntaxContext::root() { - false + HasSafetyComment::Maybe } else { // From a macro expansion. Get the text from the start of the macro declaration to start of the // unsafe block. @@ -286,15 +398,22 @@ fn span_from_macro_expansion_has_safety_comment(cx: &LateContext<'_>, span: Span && let Some(src) = unsafe_line.sf.src.as_deref() { unsafe_line.sf.lines(|lines| { - macro_line.line < unsafe_line.line && text_has_safety_comment( - src, - &lines[macro_line.line + 1..=unsafe_line.line], - unsafe_line.sf.start_pos.to_usize(), - ) + if macro_line.line < unsafe_line.line { + match text_has_safety_comment( + src, + &lines[macro_line.line + 1..=unsafe_line.line], + unsafe_line.sf.start_pos.to_usize(), + ) { + Some(b) => HasSafetyComment::Yes(b), + None => HasSafetyComment::No, + } + } else { + HasSafetyComment::No + } }) } else { // Problem getting source text. Pretend a comment was found. - true + HasSafetyComment::Maybe } } } @@ -333,7 +452,7 @@ fn span_in_body_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool { src, &lines[body_line.line + 1..=unsafe_line.line], unsafe_line.sf.start_pos.to_usize(), - ) + ).is_some() }) } else { // Problem getting source text. Pretend a comment was found. @@ -345,30 +464,34 @@ fn span_in_body_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool { } /// Checks if the given text has a safety comment for the immediately proceeding line. -fn text_has_safety_comment(src: &str, line_starts: &[BytePos], offset: usize) -> bool { +fn text_has_safety_comment(src: &str, line_starts: &[BytePos], offset: usize) -> Option { let mut lines = line_starts .array_windows::<2>() .rev() .map_while(|[start, end]| { let start = start.to_usize() - offset; let end = end.to_usize() - offset; - src.get(start..end).map(|text| (start, text.trim_start())) + let text = src.get(start..end)?; + let trimmed = text.trim_start(); + Some((start + (text.len() - trimmed.len()), trimmed)) }) .filter(|(_, text)| !text.is_empty()); let Some((line_start, line)) = lines.next() else { - return false; + return None; }; // Check for a sequence of line comments. if line.starts_with("//") { - let mut line = line; + let (mut line, mut line_start) = (line, line_start); loop { if line.to_ascii_uppercase().contains("SAFETY:") { - return true; + return Some(BytePos( + u32::try_from(line_start).unwrap() + u32::try_from(offset).unwrap(), + )); } match lines.next() { - Some((_, x)) if x.starts_with("//") => line = x, - _ => return false, + Some((s, x)) if x.starts_with("//") => (line, line_start) = (x, s), + _ => return None, } } } @@ -377,16 +500,19 @@ fn text_has_safety_comment(src: &str, line_starts: &[BytePos], offset: usize) -> let (mut line_start, mut line) = (line_start, line); loop { if line.starts_with("/*") { - let src = src[line_start..line_starts.last().unwrap().to_usize() - offset].trim_start(); + let src = &src[line_start..line_starts.last().unwrap().to_usize() - offset]; let mut tokens = tokenize(src); - return src[..tokens.next().unwrap().len as usize] + return (src[..tokens.next().unwrap().len as usize] .to_ascii_uppercase() .contains("SAFETY:") - && tokens.all(|t| t.kind == TokenKind::Whitespace); + && tokens.all(|t| t.kind == TokenKind::Whitespace)) + .then_some(BytePos( + u32::try_from(line_start).unwrap() + u32::try_from(offset).unwrap(), + )); } match lines.next() { Some(x) => (line_start, line) = x, - None => return false, + None => return None, } } } diff --git a/tests/ui/undocumented_unsafe_blocks.rs b/tests/ui/undocumented_unsafe_blocks.rs index cbc6768033ec8..c05eb447b2ebd 100644 --- a/tests/ui/undocumented_unsafe_blocks.rs +++ b/tests/ui/undocumented_unsafe_blocks.rs @@ -1,6 +1,6 @@ // aux-build:proc_macro_unsafe.rs -#![warn(clippy::undocumented_unsafe_blocks)] +#![warn(clippy::undocumented_unsafe_blocks, clippy::unnecessary_safety_comment)] #![allow(clippy::let_unit_value, clippy::missing_safety_doc)] extern crate proc_macro_unsafe; diff --git a/tests/ui/undocumented_unsafe_blocks.stderr b/tests/ui/undocumented_unsafe_blocks.stderr index ba4de9806d175..4c6f6cd18c518 100644 --- a/tests/ui/undocumented_unsafe_blocks.stderr +++ b/tests/ui/undocumented_unsafe_blocks.stderr @@ -239,6 +239,19 @@ LL | unsafe impl TrailingComment for () {} // SAFETY: | = help: consider adding a safety comment on the preceding line +error: constant item has unnecessary safety comment + --> $DIR/undocumented_unsafe_blocks.rs:471:5 + | +LL | const BIG_NUMBER: i32 = 1000000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/undocumented_unsafe_blocks.rs:470:5 + | +LL | // SAFETY: + | ^^^^^^^^^^ + = note: `-D clippy::unnecessary-safety-comment` implied by `-D warnings` + error: unsafe impl missing a safety comment --> $DIR/undocumented_unsafe_blocks.rs:472:5 | @@ -287,5 +300,5 @@ LL | let bar = unsafe {}; | = help: consider adding a safety comment on the preceding line -error: aborting due to 34 previous errors +error: aborting due to 35 previous errors From b8c3f64cee8b07470572f274712dc9ab2634221b Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 17 Nov 2022 11:00:51 +0100 Subject: [PATCH 061/244] Add some more test cases for undocumented_unsafe_blocks --- .../src/undocumented_unsafe_blocks.rs | 7 +- tests/ui/undocumented_unsafe_blocks.rs | 13 ++++ tests/ui/undocumented_unsafe_blocks.stderr | 72 +++++++++++++++++-- 3 files changed, 85 insertions(+), 7 deletions(-) diff --git a/clippy_lints/src/undocumented_unsafe_blocks.rs b/clippy_lints/src/undocumented_unsafe_blocks.rs index 57ee2735aa033..080a481e87f5d 100644 --- a/clippy_lints/src/undocumented_unsafe_blocks.rs +++ b/clippy_lints/src/undocumented_unsafe_blocks.rs @@ -82,7 +82,7 @@ declare_clippy_lint! { /// /// let ptr = NonNull::new(a).unwrap(); /// ``` - #[clippy::version = "1.66.0"] + #[clippy::version = "1.67.0"] pub UNNECESSARY_SAFETY_COMMENT, restriction, "creating an unsafe block without explaining why it is safe" @@ -136,6 +136,7 @@ impl LateLintPass<'_> for UndocumentedUnsafeBlocks { let item_has_safety_comment = item_has_safety_comment(cx, item); match (&item.kind, item_has_safety_comment) { + // lint unsafe impl without safety comment (hir::ItemKind::Impl(impl_), HasSafetyComment::No) if impl_.unsafety == hir::Unsafety::Unsafe => { if !is_lint_allowed(cx, UNDOCUMENTED_UNSAFE_BLOCKS, item.hir_id()) && !is_unsafe_from_proc_macro(cx, item.span) @@ -157,6 +158,7 @@ impl LateLintPass<'_> for UndocumentedUnsafeBlocks { ); } }, + // lint safe impl with unnecessary safety comment (hir::ItemKind::Impl(impl_), HasSafetyComment::Yes(pos)) if impl_.unsafety == hir::Unsafety::Normal => { if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, item.hir_id()) { let (span, help_span) = mk_spans(pos); @@ -172,6 +174,7 @@ impl LateLintPass<'_> for UndocumentedUnsafeBlocks { } }, (hir::ItemKind::Impl(_), _) => {}, + // const and static items only need a safety comment if their body is an unsafe block, lint otherwise (&hir::ItemKind::Const(.., body) | &hir::ItemKind::Static(.., body), HasSafetyComment::Yes(pos)) => { if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, body.hir_id) { let body = cx.tcx.hir().body(body); @@ -192,6 +195,8 @@ impl LateLintPass<'_> for UndocumentedUnsafeBlocks { } } }, + // Aside from unsafe impls and consts/statics with an unsafe block, items in general + // do not have safety invariants that need to be documented, so lint those. (_, HasSafetyComment::Yes(pos)) => { if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, item.hir_id()) { let (span, help_span) = mk_spans(pos); diff --git a/tests/ui/undocumented_unsafe_blocks.rs b/tests/ui/undocumented_unsafe_blocks.rs index c05eb447b2ebd..f68e0b4915eab 100644 --- a/tests/ui/undocumented_unsafe_blocks.rs +++ b/tests/ui/undocumented_unsafe_blocks.rs @@ -472,6 +472,19 @@ mod unsafe_impl_invalid_comment { unsafe impl Interference for () {} } +mod unsafe_items_invalid_comment { + // SAFETY: + const CONST: u32 = 0; + // SAFETY: + static STATIC: u32 = 0; + // SAFETY: + struct Struct; + // SAFETY: + enum Enum {} + // SAFETY: + mod module {} +} + unsafe trait ImplInFn {} fn impl_in_fn() { diff --git a/tests/ui/undocumented_unsafe_blocks.stderr b/tests/ui/undocumented_unsafe_blocks.stderr index 4c6f6cd18c518..becad4f61a92d 100644 --- a/tests/ui/undocumented_unsafe_blocks.stderr +++ b/tests/ui/undocumented_unsafe_blocks.stderr @@ -260,16 +260,76 @@ LL | unsafe impl Interference for () {} | = help: consider adding a safety comment on the preceding line -error: unsafe impl missing a safety comment +error: constant item has unnecessary safety comment + --> $DIR/undocumented_unsafe_blocks.rs:477:5 + | +LL | const CONST: u32 = 0; + | ^^^^^^^^^^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/undocumented_unsafe_blocks.rs:476:5 + | +LL | // SAFETY: + | ^^^^^^^^^^ + +error: static item has unnecessary safety comment --> $DIR/undocumented_unsafe_blocks.rs:479:5 | +LL | static STATIC: u32 = 0; + | ^^^^^^^^^^^^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/undocumented_unsafe_blocks.rs:478:5 + | +LL | // SAFETY: + | ^^^^^^^^^^ + +error: struct has unnecessary safety comment + --> $DIR/undocumented_unsafe_blocks.rs:481:5 + | +LL | struct Struct; + | ^^^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/undocumented_unsafe_blocks.rs:480:5 + | +LL | // SAFETY: + | ^^^^^^^^^^ + +error: enum has unnecessary safety comment + --> $DIR/undocumented_unsafe_blocks.rs:483:5 + | +LL | enum Enum {} + | ^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/undocumented_unsafe_blocks.rs:482:5 + | +LL | // SAFETY: + | ^^^^^^^^^^ + +error: module has unnecessary safety comment + --> $DIR/undocumented_unsafe_blocks.rs:485:5 + | +LL | mod module {} + | ^^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/undocumented_unsafe_blocks.rs:484:5 + | +LL | // SAFETY: + | ^^^^^^^^^^ + +error: unsafe impl missing a safety comment + --> $DIR/undocumented_unsafe_blocks.rs:492:5 + | LL | unsafe impl ImplInFn for () {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: consider adding a safety comment on the preceding line error: unsafe impl missing a safety comment - --> $DIR/undocumented_unsafe_blocks.rs:488:1 + --> $DIR/undocumented_unsafe_blocks.rs:501:1 | LL | unsafe impl CrateRoot for () {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -277,7 +337,7 @@ LL | unsafe impl CrateRoot for () {} = help: consider adding a safety comment on the preceding line error: unsafe block missing a safety comment - --> $DIR/undocumented_unsafe_blocks.rs:498:9 + --> $DIR/undocumented_unsafe_blocks.rs:511:9 | LL | unsafe {}; | ^^^^^^^^^ @@ -285,7 +345,7 @@ LL | unsafe {}; = help: consider adding a safety comment on the preceding line error: unsafe block missing a safety comment - --> $DIR/undocumented_unsafe_blocks.rs:502:12 + --> $DIR/undocumented_unsafe_blocks.rs:515:12 | LL | if unsafe { true } { | ^^^^^^^^^^^^^^^ @@ -293,12 +353,12 @@ LL | if unsafe { true } { = help: consider adding a safety comment on the preceding line error: unsafe block missing a safety comment - --> $DIR/undocumented_unsafe_blocks.rs:505:23 + --> $DIR/undocumented_unsafe_blocks.rs:518:23 | LL | let bar = unsafe {}; | ^^^^^^^^^ | = help: consider adding a safety comment on the preceding line -error: aborting due to 35 previous errors +error: aborting due to 40 previous errors From 4fa57575302a5931517590daf8df2ef8a93f7a7b Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 17 Nov 2022 15:14:03 +0100 Subject: [PATCH 062/244] Lint unnecessary safety comments on statements and block tail expressions --- .../src/undocumented_unsafe_blocks.rs | 126 +++++++++++++++++- clippy_utils/src/visitors.rs | 12 +- tests/ui/undocumented_unsafe_blocks.rs | 31 +++++ tests/ui/undocumented_unsafe_blocks.stderr | 60 ++++++++- 4 files changed, 220 insertions(+), 9 deletions(-) diff --git a/clippy_lints/src/undocumented_unsafe_blocks.rs b/clippy_lints/src/undocumented_unsafe_blocks.rs index 080a481e87f5d..c60144df757c2 100644 --- a/clippy_lints/src/undocumented_unsafe_blocks.rs +++ b/clippy_lints/src/undocumented_unsafe_blocks.rs @@ -1,6 +1,10 @@ +use std::ops::ControlFlow; + use clippy_utils::diagnostics::span_lint_and_help; use clippy_utils::source::walk_span_to_context; +use clippy_utils::visitors::{for_each_expr_with_closures, Descend}; use clippy_utils::{get_parent_node, is_lint_allowed}; +use hir::HirId; use rustc_data_structures::sync::Lrc; use rustc_hir as hir; use rustc_hir::{Block, BlockCheckMode, ItemKind, Node, UnsafeSource}; @@ -90,8 +94,8 @@ declare_clippy_lint! { declare_lint_pass!(UndocumentedUnsafeBlocks => [UNDOCUMENTED_UNSAFE_BLOCKS, UNNECESSARY_SAFETY_COMMENT]); -impl LateLintPass<'_> for UndocumentedUnsafeBlocks { - fn check_block(&mut self, cx: &LateContext<'_>, block: &'_ Block<'_>) { +impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks { + fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) { if block.rules == BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided) && !in_external_macro(cx.tcx.sess, block.span) && !is_lint_allowed(cx, UNDOCUMENTED_UNSAFE_BLOCKS, block.hir_id) @@ -115,6 +119,45 @@ impl LateLintPass<'_> for UndocumentedUnsafeBlocks { "consider adding a safety comment on the preceding line", ); } + + if let Some(tail) = block.expr + && !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, tail.hir_id) + && !in_external_macro(cx.tcx.sess, tail.span) + && let HasSafetyComment::Yes(pos) = stmt_has_safety_comment(cx, tail.span, tail.hir_id) + && let Some(help_span) = expr_has_unnecessary_safety_comment(cx, tail, pos) + { + span_lint_and_help( + cx, + UNNECESSARY_SAFETY_COMMENT, + tail.span, + "expression has unnecessary safety comment", + Some(help_span), + "consider removing the safety comment", + ); + } + } + + fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &hir::Stmt<'tcx>) { + let expr = match stmt.kind { + hir::StmtKind::Local(&hir::Local { init: Some(expr), .. }) + | hir::StmtKind::Expr(expr) + | hir::StmtKind::Semi(expr) => expr, + _ => return, + }; + if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, stmt.hir_id) + && !in_external_macro(cx.tcx.sess, stmt.span) + && let HasSafetyComment::Yes(pos) = stmt_has_safety_comment(cx, stmt.span, stmt.hir_id) + && let Some(help_span) = expr_has_unnecessary_safety_comment(cx, expr, pos) + { + span_lint_and_help( + cx, + UNNECESSARY_SAFETY_COMMENT, + stmt.span, + "statement has unnecessary safety comment", + Some(help_span), + "consider removing the safety comment", + ); + } } fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) { @@ -216,6 +259,36 @@ impl LateLintPass<'_> for UndocumentedUnsafeBlocks { } } +fn expr_has_unnecessary_safety_comment<'tcx>( + cx: &LateContext<'tcx>, + expr: &'tcx hir::Expr<'tcx>, + comment_pos: BytePos, +) -> Option { + // this should roughly be the reverse of `block_parents_have_safety_comment` + if for_each_expr_with_closures(cx, expr, |expr| match expr.kind { + hir::ExprKind::Block( + Block { + rules: BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided), + .. + }, + _, + ) => ControlFlow::Break(()), + // statements will be handled by check_stmt itself again + hir::ExprKind::Block(..) => ControlFlow::Continue(Descend::No), + _ => ControlFlow::Continue(Descend::Yes), + }) + .is_some() + { + return None; + } + + let source_map = cx.tcx.sess.source_map(); + let span = Span::new(comment_pos, comment_pos, SyntaxContext::root(), None); + let help_span = source_map.span_extend_to_next_char(span, '\n', true); + + Some(help_span) +} + fn is_unsafe_from_proc_macro(cx: &LateContext<'_>, span: Span) -> bool { let source_map = cx.sess().source_map(); let file_pos = source_map.lookup_byte_offset(span.lo()); @@ -358,6 +431,55 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSaf } } +/// Checks if the lines immediately preceding the item contain a safety comment. +#[allow(clippy::collapsible_match)] +fn stmt_has_safety_comment(cx: &LateContext<'_>, span: Span, hir_id: HirId) -> HasSafetyComment { + match span_from_macro_expansion_has_safety_comment(cx, span) { + HasSafetyComment::Maybe => (), + has_safety_comment => return has_safety_comment, + } + + if span.ctxt() == SyntaxContext::root() { + if let Some(parent_node) = get_parent_node(cx.tcx, hir_id) { + let comment_start = match parent_node { + Node::Block(block) => walk_span_to_context(block.span, SyntaxContext::root()).map(Span::lo), + _ => return HasSafetyComment::Maybe, + }; + + let source_map = cx.sess().source_map(); + if let Some(comment_start) = comment_start + && let Ok(unsafe_line) = source_map.lookup_line(span.lo()) + && let Ok(comment_start_line) = source_map.lookup_line(comment_start) + && Lrc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf) + && let Some(src) = unsafe_line.sf.src.as_deref() + { + unsafe_line.sf.lines(|lines| { + if comment_start_line.line >= unsafe_line.line { + HasSafetyComment::No + } else { + match text_has_safety_comment( + src, + &lines[comment_start_line.line + 1..=unsafe_line.line], + unsafe_line.sf.start_pos.to_usize(), + ) { + Some(b) => HasSafetyComment::Yes(b), + None => HasSafetyComment::No, + } + } + }) + } else { + // Problem getting source text. Pretend a comment was found. + HasSafetyComment::Maybe + } + } else { + // No parent node. Pretend a comment was found. + HasSafetyComment::Maybe + } + } else { + HasSafetyComment::No + } +} + fn comment_start_before_item_in_mod( cx: &LateContext<'_>, parent_mod: &hir::Mod<'_>, diff --git a/clippy_utils/src/visitors.rs b/clippy_utils/src/visitors.rs index d4294f18fd501..863fb60fcfca1 100644 --- a/clippy_utils/src/visitors.rs +++ b/clippy_utils/src/visitors.rs @@ -170,22 +170,22 @@ where cb: F, } - struct WithStmtGuarg<'a, F> { + struct WithStmtGuard<'a, F> { val: &'a mut RetFinder, prev_in_stmt: bool, } impl RetFinder { - fn inside_stmt(&mut self, in_stmt: bool) -> WithStmtGuarg<'_, F> { + fn inside_stmt(&mut self, in_stmt: bool) -> WithStmtGuard<'_, F> { let prev_in_stmt = std::mem::replace(&mut self.in_stmt, in_stmt); - WithStmtGuarg { + WithStmtGuard { val: self, prev_in_stmt, } } } - impl std::ops::Deref for WithStmtGuarg<'_, F> { + impl std::ops::Deref for WithStmtGuard<'_, F> { type Target = RetFinder; fn deref(&self) -> &Self::Target { @@ -193,13 +193,13 @@ where } } - impl std::ops::DerefMut for WithStmtGuarg<'_, F> { + impl std::ops::DerefMut for WithStmtGuard<'_, F> { fn deref_mut(&mut self) -> &mut Self::Target { self.val } } - impl Drop for WithStmtGuarg<'_, F> { + impl Drop for WithStmtGuard<'_, F> { fn drop(&mut self) { self.val.in_stmt = self.prev_in_stmt; } diff --git a/tests/ui/undocumented_unsafe_blocks.rs b/tests/ui/undocumented_unsafe_blocks.rs index f68e0b4915eab..cb99ce0d4214a 100644 --- a/tests/ui/undocumented_unsafe_blocks.rs +++ b/tests/ui/undocumented_unsafe_blocks.rs @@ -522,4 +522,35 @@ fn issue_9142() { }; } +mod unnecessary_from_macro { + trait T {} + + macro_rules! no_safety_comment { + ($t:ty) => { + impl T for $t {} + }; + } + + // FIXME: This is not caught + // Safety: unnecessary + no_safety_comment!(()); + + macro_rules! with_safety_comment { + ($t:ty) => { + // Safety: unnecessary + impl T for $t {} + }; + } + + with_safety_comment!(i32); +} + +fn unnecessary_on_stmt_and_expr() -> u32 { + // SAFETY: unnecessary + let num = 42; + + // SAFETY: unnecessary + 24 +} + fn main() {} diff --git a/tests/ui/undocumented_unsafe_blocks.stderr b/tests/ui/undocumented_unsafe_blocks.stderr index becad4f61a92d..919fd51351cb1 100644 --- a/tests/ui/undocumented_unsafe_blocks.stderr +++ b/tests/ui/undocumented_unsafe_blocks.stderr @@ -344,6 +344,24 @@ LL | unsafe {}; | = help: consider adding a safety comment on the preceding line +error: statement has unnecessary safety comment + --> $DIR/undocumented_unsafe_blocks.rs:514:5 + | +LL | / let _ = { +LL | | if unsafe { true } { +LL | | todo!(); +LL | | } else { +... | +LL | | } +LL | | }; + | |______^ + | +help: consider removing the safety comment + --> $DIR/undocumented_unsafe_blocks.rs:513:5 + | +LL | // SAFETY: this is more than one level away, so it should warn + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + error: unsafe block missing a safety comment --> $DIR/undocumented_unsafe_blocks.rs:515:12 | @@ -360,5 +378,45 @@ LL | let bar = unsafe {}; | = help: consider adding a safety comment on the preceding line -error: aborting due to 40 previous errors +error: impl has unnecessary safety comment + --> $DIR/undocumented_unsafe_blocks.rs:541:13 + | +LL | impl T for $t {} + | ^^^^^^^^^^^^^^^^ +... +LL | with_safety_comment!(i32); + | ------------------------- in this macro invocation + | +help: consider removing the safety comment + --> $DIR/undocumented_unsafe_blocks.rs:540:13 + | +LL | // Safety: unnecessary + | ^^^^^^^^^^^^^^^^^^^^^^ + = note: this error originates in the macro `with_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: expression has unnecessary safety comment + --> $DIR/undocumented_unsafe_blocks.rs:553:5 + | +LL | 24 + | ^^ + | +help: consider removing the safety comment + --> $DIR/undocumented_unsafe_blocks.rs:552:5 + | +LL | // SAFETY: unnecessary + | ^^^^^^^^^^^^^^^^^^^^^^ + +error: statement has unnecessary safety comment + --> $DIR/undocumented_unsafe_blocks.rs:550:5 + | +LL | let num = 42; + | ^^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/undocumented_unsafe_blocks.rs:549:5 + | +LL | // SAFETY: unnecessary + | ^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 44 previous errors From 9c69e1cc893878987991d77b8fb54d1f6de29733 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 17 Nov 2022 15:17:28 +0100 Subject: [PATCH 063/244] Simplify --- .../src/undocumented_unsafe_blocks.rs | 173 ++++++++---------- 1 file changed, 78 insertions(+), 95 deletions(-) diff --git a/clippy_lints/src/undocumented_unsafe_blocks.rs b/clippy_lints/src/undocumented_unsafe_blocks.rs index c60144df757c2..d7e4834230649 100644 --- a/clippy_lints/src/undocumented_unsafe_blocks.rs +++ b/clippy_lints/src/undocumented_unsafe_blocks.rs @@ -363,72 +363,60 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSaf has_safety_comment => return has_safety_comment, } - if item.span.ctxt() == SyntaxContext::root() { - if let Some(parent_node) = get_parent_node(cx.tcx, item.hir_id()) { - let comment_start = match parent_node { - Node::Crate(parent_mod) => { - comment_start_before_item_in_mod(cx, parent_mod, parent_mod.spans.inner_span, item) - }, - Node::Item(parent_item) => { - if let ItemKind::Mod(parent_mod) = &parent_item.kind { - comment_start_before_item_in_mod(cx, parent_mod, parent_item.span, item) - } else { - // Doesn't support impls in this position. Pretend a comment was found. - return HasSafetyComment::Maybe; - } - }, - Node::Stmt(stmt) => { - if let Some(stmt_parent) = get_parent_node(cx.tcx, stmt.hir_id) { - match stmt_parent { - Node::Block(block) => walk_span_to_context(block.span, SyntaxContext::root()).map(Span::lo), - _ => { - // Doesn't support impls in this position. Pretend a comment was found. - return HasSafetyComment::Maybe; - }, - } - } else { - // Problem getting the parent node. Pretend a comment was found. - return HasSafetyComment::Maybe; - } - }, - _ => { + if item.span.ctxt() != SyntaxContext::root() { + return HasSafetyComment::No; + } + if let Some(parent_node) = get_parent_node(cx.tcx, item.hir_id()) { + let comment_start = match parent_node { + Node::Crate(parent_mod) => { + comment_start_before_item_in_mod(cx, parent_mod, parent_mod.spans.inner_span, item) + }, + Node::Item(parent_item) => { + if let ItemKind::Mod(parent_mod) = &parent_item.kind { + comment_start_before_item_in_mod(cx, parent_mod, parent_item.span, item) + } else { // Doesn't support impls in this position. Pretend a comment was found. return HasSafetyComment::Maybe; - }, - }; + } + }, + Node::Stmt(stmt) => { + if let Some(Node::Block(block)) = get_parent_node(cx.tcx, stmt.hir_id) { + walk_span_to_context(block.span, SyntaxContext::root()).map(Span::lo) + } else { + // Problem getting the parent node. Pretend a comment was found. + return HasSafetyComment::Maybe; + } + }, + _ => { + // Doesn't support impls in this position. Pretend a comment was found. + return HasSafetyComment::Maybe; + }, + }; - let source_map = cx.sess().source_map(); - if let Some(comment_start) = comment_start - && let Ok(unsafe_line) = source_map.lookup_line(item.span.lo()) - && let Ok(comment_start_line) = source_map.lookup_line(comment_start) - && Lrc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf) - && let Some(src) = unsafe_line.sf.src.as_deref() - { - unsafe_line.sf.lines(|lines| { - if comment_start_line.line >= unsafe_line.line { - HasSafetyComment::No - } else { - match text_has_safety_comment( - src, - &lines[comment_start_line.line + 1..=unsafe_line.line], - unsafe_line.sf.start_pos.to_usize(), - ) { - Some(b) => HasSafetyComment::Yes(b), - None => HasSafetyComment::No, - } + let source_map = cx.sess().source_map(); + if let Some(comment_start) = comment_start + && let Ok(unsafe_line) = source_map.lookup_line(item.span.lo()) + && let Ok(comment_start_line) = source_map.lookup_line(comment_start) + && Lrc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf) + && let Some(src) = unsafe_line.sf.src.as_deref() + { + return unsafe_line.sf.lines(|lines| { + if comment_start_line.line >= unsafe_line.line { + HasSafetyComment::No + } else { + match text_has_safety_comment( + src, + &lines[comment_start_line.line + 1..=unsafe_line.line], + unsafe_line.sf.start_pos.to_usize(), + ) { + Some(b) => HasSafetyComment::Yes(b), + None => HasSafetyComment::No, } - }) - } else { - // Problem getting source text. Pretend a comment was found. - HasSafetyComment::Maybe - } - } else { - // No parent node. Pretend a comment was found. - HasSafetyComment::Maybe + } + }); } - } else { - HasSafetyComment::No } + HasSafetyComment::Maybe } /// Checks if the lines immediately preceding the item contain a safety comment. @@ -439,45 +427,40 @@ fn stmt_has_safety_comment(cx: &LateContext<'_>, span: Span, hir_id: HirId) -> H has_safety_comment => return has_safety_comment, } - if span.ctxt() == SyntaxContext::root() { - if let Some(parent_node) = get_parent_node(cx.tcx, hir_id) { - let comment_start = match parent_node { - Node::Block(block) => walk_span_to_context(block.span, SyntaxContext::root()).map(Span::lo), - _ => return HasSafetyComment::Maybe, - }; + if span.ctxt() != SyntaxContext::root() { + return HasSafetyComment::No; + } - let source_map = cx.sess().source_map(); - if let Some(comment_start) = comment_start - && let Ok(unsafe_line) = source_map.lookup_line(span.lo()) - && let Ok(comment_start_line) = source_map.lookup_line(comment_start) - && Lrc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf) - && let Some(src) = unsafe_line.sf.src.as_deref() - { - unsafe_line.sf.lines(|lines| { - if comment_start_line.line >= unsafe_line.line { - HasSafetyComment::No - } else { - match text_has_safety_comment( - src, - &lines[comment_start_line.line + 1..=unsafe_line.line], - unsafe_line.sf.start_pos.to_usize(), - ) { - Some(b) => HasSafetyComment::Yes(b), - None => HasSafetyComment::No, - } + if let Some(parent_node) = get_parent_node(cx.tcx, hir_id) { + let comment_start = match parent_node { + Node::Block(block) => walk_span_to_context(block.span, SyntaxContext::root()).map(Span::lo), + _ => return HasSafetyComment::Maybe, + }; + + let source_map = cx.sess().source_map(); + if let Some(comment_start) = comment_start + && let Ok(unsafe_line) = source_map.lookup_line(span.lo()) + && let Ok(comment_start_line) = source_map.lookup_line(comment_start) + && Lrc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf) + && let Some(src) = unsafe_line.sf.src.as_deref() + { + return unsafe_line.sf.lines(|lines| { + if comment_start_line.line >= unsafe_line.line { + HasSafetyComment::No + } else { + match text_has_safety_comment( + src, + &lines[comment_start_line.line + 1..=unsafe_line.line], + unsafe_line.sf.start_pos.to_usize(), + ) { + Some(b) => HasSafetyComment::Yes(b), + None => HasSafetyComment::No, } - }) - } else { - // Problem getting source text. Pretend a comment was found. - HasSafetyComment::Maybe - } - } else { - // No parent node. Pretend a comment was found. - HasSafetyComment::Maybe + } + }); } - } else { - HasSafetyComment::No } + HasSafetyComment::Maybe } fn comment_start_before_item_in_mod( From f96dd383188e9f24e5b401e664cca97b8bd73825 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 24 Nov 2022 09:47:50 +0100 Subject: [PATCH 064/244] Address reviews --- .../src/undocumented_unsafe_blocks.rs | 9 +- tests/ui/undocumented_unsafe_blocks.rs | 44 ------- tests/ui/undocumented_unsafe_blocks.stderr | 116 ++---------------- tests/ui/unnecessary_safety_comment.rs | 51 ++++++++ tests/ui/unnecessary_safety_comment.stderr | 115 +++++++++++++++++ 5 files changed, 178 insertions(+), 157 deletions(-) create mode 100644 tests/ui/unnecessary_safety_comment.rs create mode 100644 tests/ui/unnecessary_safety_comment.stderr diff --git a/clippy_lints/src/undocumented_unsafe_blocks.rs b/clippy_lints/src/undocumented_unsafe_blocks.rs index d7e4834230649..2e1b6d8d4ea7f 100644 --- a/clippy_lints/src/undocumented_unsafe_blocks.rs +++ b/clippy_lints/src/undocumented_unsafe_blocks.rs @@ -89,7 +89,7 @@ declare_clippy_lint! { #[clippy::version = "1.67.0"] pub UNNECESSARY_SAFETY_COMMENT, restriction, - "creating an unsafe block without explaining why it is safe" + "annotating safe code with a safety comment" } declare_lint_pass!(UndocumentedUnsafeBlocks => [UNDOCUMENTED_UNSAFE_BLOCKS, UNNECESSARY_SAFETY_COMMENT]); @@ -138,12 +138,11 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks { } fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &hir::Stmt<'tcx>) { - let expr = match stmt.kind { + let ( hir::StmtKind::Local(&hir::Local { init: Some(expr), .. }) | hir::StmtKind::Expr(expr) - | hir::StmtKind::Semi(expr) => expr, - _ => return, - }; + | hir::StmtKind::Semi(expr) + ) = stmt.kind else { return }; if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, stmt.hir_id) && !in_external_macro(cx.tcx.sess, stmt.span) && let HasSafetyComment::Yes(pos) = stmt_has_safety_comment(cx, stmt.span, stmt.hir_id) diff --git a/tests/ui/undocumented_unsafe_blocks.rs b/tests/ui/undocumented_unsafe_blocks.rs index cb99ce0d4214a..c05eb447b2ebd 100644 --- a/tests/ui/undocumented_unsafe_blocks.rs +++ b/tests/ui/undocumented_unsafe_blocks.rs @@ -472,19 +472,6 @@ mod unsafe_impl_invalid_comment { unsafe impl Interference for () {} } -mod unsafe_items_invalid_comment { - // SAFETY: - const CONST: u32 = 0; - // SAFETY: - static STATIC: u32 = 0; - // SAFETY: - struct Struct; - // SAFETY: - enum Enum {} - // SAFETY: - mod module {} -} - unsafe trait ImplInFn {} fn impl_in_fn() { @@ -522,35 +509,4 @@ fn issue_9142() { }; } -mod unnecessary_from_macro { - trait T {} - - macro_rules! no_safety_comment { - ($t:ty) => { - impl T for $t {} - }; - } - - // FIXME: This is not caught - // Safety: unnecessary - no_safety_comment!(()); - - macro_rules! with_safety_comment { - ($t:ty) => { - // Safety: unnecessary - impl T for $t {} - }; - } - - with_safety_comment!(i32); -} - -fn unnecessary_on_stmt_and_expr() -> u32 { - // SAFETY: unnecessary - let num = 42; - - // SAFETY: unnecessary - 24 -} - fn main() {} diff --git a/tests/ui/undocumented_unsafe_blocks.stderr b/tests/ui/undocumented_unsafe_blocks.stderr index 919fd51351cb1..d1c1bb5ffeac8 100644 --- a/tests/ui/undocumented_unsafe_blocks.stderr +++ b/tests/ui/undocumented_unsafe_blocks.stderr @@ -260,68 +260,8 @@ LL | unsafe impl Interference for () {} | = help: consider adding a safety comment on the preceding line -error: constant item has unnecessary safety comment - --> $DIR/undocumented_unsafe_blocks.rs:477:5 - | -LL | const CONST: u32 = 0; - | ^^^^^^^^^^^^^^^^^^^^^ - | -help: consider removing the safety comment - --> $DIR/undocumented_unsafe_blocks.rs:476:5 - | -LL | // SAFETY: - | ^^^^^^^^^^ - -error: static item has unnecessary safety comment - --> $DIR/undocumented_unsafe_blocks.rs:479:5 - | -LL | static STATIC: u32 = 0; - | ^^^^^^^^^^^^^^^^^^^^^^^ - | -help: consider removing the safety comment - --> $DIR/undocumented_unsafe_blocks.rs:478:5 - | -LL | // SAFETY: - | ^^^^^^^^^^ - -error: struct has unnecessary safety comment - --> $DIR/undocumented_unsafe_blocks.rs:481:5 - | -LL | struct Struct; - | ^^^^^^^^^^^^^^ - | -help: consider removing the safety comment - --> $DIR/undocumented_unsafe_blocks.rs:480:5 - | -LL | // SAFETY: - | ^^^^^^^^^^ - -error: enum has unnecessary safety comment - --> $DIR/undocumented_unsafe_blocks.rs:483:5 - | -LL | enum Enum {} - | ^^^^^^^^^^^^ - | -help: consider removing the safety comment - --> $DIR/undocumented_unsafe_blocks.rs:482:5 - | -LL | // SAFETY: - | ^^^^^^^^^^ - -error: module has unnecessary safety comment - --> $DIR/undocumented_unsafe_blocks.rs:485:5 - | -LL | mod module {} - | ^^^^^^^^^^^^^ - | -help: consider removing the safety comment - --> $DIR/undocumented_unsafe_blocks.rs:484:5 - | -LL | // SAFETY: - | ^^^^^^^^^^ - error: unsafe impl missing a safety comment - --> $DIR/undocumented_unsafe_blocks.rs:492:5 + --> $DIR/undocumented_unsafe_blocks.rs:479:5 | LL | unsafe impl ImplInFn for () {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -329,7 +269,7 @@ LL | unsafe impl ImplInFn for () {} = help: consider adding a safety comment on the preceding line error: unsafe impl missing a safety comment - --> $DIR/undocumented_unsafe_blocks.rs:501:1 + --> $DIR/undocumented_unsafe_blocks.rs:488:1 | LL | unsafe impl CrateRoot for () {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -337,7 +277,7 @@ LL | unsafe impl CrateRoot for () {} = help: consider adding a safety comment on the preceding line error: unsafe block missing a safety comment - --> $DIR/undocumented_unsafe_blocks.rs:511:9 + --> $DIR/undocumented_unsafe_blocks.rs:498:9 | LL | unsafe {}; | ^^^^^^^^^ @@ -345,7 +285,7 @@ LL | unsafe {}; = help: consider adding a safety comment on the preceding line error: statement has unnecessary safety comment - --> $DIR/undocumented_unsafe_blocks.rs:514:5 + --> $DIR/undocumented_unsafe_blocks.rs:501:5 | LL | / let _ = { LL | | if unsafe { true } { @@ -357,13 +297,13 @@ LL | | }; | |______^ | help: consider removing the safety comment - --> $DIR/undocumented_unsafe_blocks.rs:513:5 + --> $DIR/undocumented_unsafe_blocks.rs:500:5 | LL | // SAFETY: this is more than one level away, so it should warn | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: unsafe block missing a safety comment - --> $DIR/undocumented_unsafe_blocks.rs:515:12 + --> $DIR/undocumented_unsafe_blocks.rs:502:12 | LL | if unsafe { true } { | ^^^^^^^^^^^^^^^ @@ -371,52 +311,12 @@ LL | if unsafe { true } { = help: consider adding a safety comment on the preceding line error: unsafe block missing a safety comment - --> $DIR/undocumented_unsafe_blocks.rs:518:23 + --> $DIR/undocumented_unsafe_blocks.rs:505:23 | LL | let bar = unsafe {}; | ^^^^^^^^^ | = help: consider adding a safety comment on the preceding line -error: impl has unnecessary safety comment - --> $DIR/undocumented_unsafe_blocks.rs:541:13 - | -LL | impl T for $t {} - | ^^^^^^^^^^^^^^^^ -... -LL | with_safety_comment!(i32); - | ------------------------- in this macro invocation - | -help: consider removing the safety comment - --> $DIR/undocumented_unsafe_blocks.rs:540:13 - | -LL | // Safety: unnecessary - | ^^^^^^^^^^^^^^^^^^^^^^ - = note: this error originates in the macro `with_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info) - -error: expression has unnecessary safety comment - --> $DIR/undocumented_unsafe_blocks.rs:553:5 - | -LL | 24 - | ^^ - | -help: consider removing the safety comment - --> $DIR/undocumented_unsafe_blocks.rs:552:5 - | -LL | // SAFETY: unnecessary - | ^^^^^^^^^^^^^^^^^^^^^^ - -error: statement has unnecessary safety comment - --> $DIR/undocumented_unsafe_blocks.rs:550:5 - | -LL | let num = 42; - | ^^^^^^^^^^^^^ - | -help: consider removing the safety comment - --> $DIR/undocumented_unsafe_blocks.rs:549:5 - | -LL | // SAFETY: unnecessary - | ^^^^^^^^^^^^^^^^^^^^^^ - -error: aborting due to 44 previous errors +error: aborting due to 36 previous errors diff --git a/tests/ui/unnecessary_safety_comment.rs b/tests/ui/unnecessary_safety_comment.rs new file mode 100644 index 0000000000000..7fefea7051d69 --- /dev/null +++ b/tests/ui/unnecessary_safety_comment.rs @@ -0,0 +1,51 @@ +#![warn(clippy::undocumented_unsafe_blocks, clippy::unnecessary_safety_comment)] +#![allow(clippy::let_unit_value, clippy::missing_safety_doc)] + +mod unsafe_items_invalid_comment { + // SAFETY: + const CONST: u32 = 0; + // SAFETY: + static STATIC: u32 = 0; + // SAFETY: + struct Struct; + // SAFETY: + enum Enum {} + // SAFETY: + mod module {} +} + +mod unnecessary_from_macro { + trait T {} + + macro_rules! no_safety_comment { + ($t:ty) => { + impl T for $t {} + }; + } + + // FIXME: This is not caught + // Safety: unnecessary + no_safety_comment!(()); + + macro_rules! with_safety_comment { + ($t:ty) => { + // Safety: unnecessary + impl T for $t {} + }; + } + + with_safety_comment!(i32); +} + +fn unnecessary_on_stmt_and_expr() -> u32 { + // SAFETY: unnecessary + let num = 42; + + // SAFETY: unnecessary + if num > 24 {} + + // SAFETY: unnecessary + 24 +} + +fn main() {} diff --git a/tests/ui/unnecessary_safety_comment.stderr b/tests/ui/unnecessary_safety_comment.stderr new file mode 100644 index 0000000000000..7b2af67d64c7b --- /dev/null +++ b/tests/ui/unnecessary_safety_comment.stderr @@ -0,0 +1,115 @@ +error: constant item has unnecessary safety comment + --> $DIR/unnecessary_safety_comment.rs:6:5 + | +LL | const CONST: u32 = 0; + | ^^^^^^^^^^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/unnecessary_safety_comment.rs:5:5 + | +LL | // SAFETY: + | ^^^^^^^^^^ + = note: `-D clippy::unnecessary-safety-comment` implied by `-D warnings` + +error: static item has unnecessary safety comment + --> $DIR/unnecessary_safety_comment.rs:8:5 + | +LL | static STATIC: u32 = 0; + | ^^^^^^^^^^^^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/unnecessary_safety_comment.rs:7:5 + | +LL | // SAFETY: + | ^^^^^^^^^^ + +error: struct has unnecessary safety comment + --> $DIR/unnecessary_safety_comment.rs:10:5 + | +LL | struct Struct; + | ^^^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/unnecessary_safety_comment.rs:9:5 + | +LL | // SAFETY: + | ^^^^^^^^^^ + +error: enum has unnecessary safety comment + --> $DIR/unnecessary_safety_comment.rs:12:5 + | +LL | enum Enum {} + | ^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/unnecessary_safety_comment.rs:11:5 + | +LL | // SAFETY: + | ^^^^^^^^^^ + +error: module has unnecessary safety comment + --> $DIR/unnecessary_safety_comment.rs:14:5 + | +LL | mod module {} + | ^^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/unnecessary_safety_comment.rs:13:5 + | +LL | // SAFETY: + | ^^^^^^^^^^ + +error: impl has unnecessary safety comment + --> $DIR/unnecessary_safety_comment.rs:33:13 + | +LL | impl T for $t {} + | ^^^^^^^^^^^^^^^^ +... +LL | with_safety_comment!(i32); + | ------------------------- in this macro invocation + | +help: consider removing the safety comment + --> $DIR/unnecessary_safety_comment.rs:32:13 + | +LL | // Safety: unnecessary + | ^^^^^^^^^^^^^^^^^^^^^^ + = note: this error originates in the macro `with_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: expression has unnecessary safety comment + --> $DIR/unnecessary_safety_comment.rs:48:5 + | +LL | 24 + | ^^ + | +help: consider removing the safety comment + --> $DIR/unnecessary_safety_comment.rs:47:5 + | +LL | // SAFETY: unnecessary + | ^^^^^^^^^^^^^^^^^^^^^^ + +error: statement has unnecessary safety comment + --> $DIR/unnecessary_safety_comment.rs:42:5 + | +LL | let num = 42; + | ^^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/unnecessary_safety_comment.rs:41:5 + | +LL | // SAFETY: unnecessary + | ^^^^^^^^^^^^^^^^^^^^^^ + +error: statement has unnecessary safety comment + --> $DIR/unnecessary_safety_comment.rs:45:5 + | +LL | if num > 24 {} + | ^^^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/unnecessary_safety_comment.rs:44:5 + | +LL | // SAFETY: unnecessary + | ^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 9 previous errors + From bbcc260b6feecdeadd2b366cc8f021cc3404c9ec Mon Sep 17 00:00:00 2001 From: dswij Date: Fri, 25 Nov 2022 18:04:17 +0800 Subject: [PATCH 065/244] `manual_let_else`: keep macro call on suggestion blocks --- clippy_lints/src/manual_let_else.rs | 30 ++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/clippy_lints/src/manual_let_else.rs b/clippy_lints/src/manual_let_else.rs index 20f06830952cf..91f0dc2a7170c 100644 --- a/clippy_lints/src/manual_let_else.rs +++ b/clippy_lints/src/manual_let_else.rs @@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::higher::IfLetOrMatch; use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::peel_blocks; -use clippy_utils::source::snippet_opt; +use clippy_utils::source::{snippet, snippet_with_macro_callsite}; use clippy_utils::ty::is_type_diagnostic_item; use clippy_utils::visitors::{for_each_expr, Descend}; use if_chain::if_chain; @@ -143,18 +143,22 @@ fn emit_manual_let_else(cx: &LateContext<'_>, span: Span, expr: &Expr<'_>, pat: // for this to be machine applicable. let app = Applicability::HasPlaceholders; - if let Some(sn_pat) = snippet_opt(cx, pat.span) && - let Some(sn_expr) = snippet_opt(cx, expr.span) && - let Some(sn_else) = snippet_opt(cx, else_body.span) - { - let else_bl = if matches!(else_body.kind, ExprKind::Block(..)) { - sn_else - } else { - format!("{{ {sn_else} }}") - }; - let sugg = format!("let {sn_pat} = {sn_expr} else {else_bl};"); - diag.span_suggestion(span, "consider writing", sugg, app); - } + let snippet_fn = if span.from_expansion() { + snippet + } else { + snippet_with_macro_callsite + }; + let sn_pat = snippet_fn(cx, pat.span, ""); + let sn_expr = snippet_fn(cx, expr.span, ""); + let sn_else = snippet_fn(cx, else_body.span, ""); + + let else_bl = if matches!(else_body.kind, ExprKind::Block(..)) { + sn_else.into_owned() + } else { + format!("{{ {sn_else} }}") + }; + let sugg = format!("let {sn_pat} = {sn_expr} else {else_bl};"); + diag.span_suggestion(span, "consider writing", sugg, app); }, ); } From a4b53c9c14705475b6e9041b56c441c981e86423 Mon Sep 17 00:00:00 2001 From: dswij Date: Fri, 25 Nov 2022 18:04:31 +0800 Subject: [PATCH 066/244] `manual_let_else`: Add test with expanded macros --- tests/ui/manual_let_else.rs | 14 ++++++++++++++ tests/ui/manual_let_else.stderr | 11 ++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/ui/manual_let_else.rs b/tests/ui/manual_let_else.rs index 2ef40e5911af4..48a162c13602c 100644 --- a/tests/ui/manual_let_else.rs +++ b/tests/ui/manual_let_else.rs @@ -234,4 +234,18 @@ fn not_fire() { // If a type annotation is present, don't lint as // expressing the type might be too hard let v: () = if let Some(v_some) = g() { v_some } else { panic!() }; + + // Issue 9940 + // Suggestion should not expand macros + macro_rules! macro_call { + () => { + return () + }; + } + + let ff = Some(1); + let _ = match ff { + Some(value) => value, + _ => macro_call!(), + }; } diff --git a/tests/ui/manual_let_else.stderr b/tests/ui/manual_let_else.stderr index 453b68b8bd003..52aac6bc673d1 100644 --- a/tests/ui/manual_let_else.stderr +++ b/tests/ui/manual_let_else.stderr @@ -259,5 +259,14 @@ LL | create_binding_if_some!(w, g()); | = note: this error originates in the macro `create_binding_if_some` (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to 17 previous errors +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:247:5 + | +LL | / let _ = match ff { +LL | | Some(value) => value, +LL | | _ => macro_call!(), +LL | | }; + | |______^ help: consider writing: `let Some(value) = ff else { macro_call!() };` + +error: aborting due to 18 previous errors From 4faf11a102756ff81e4bf7105650c4429397911b Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Fri, 25 Nov 2022 11:39:04 +0100 Subject: [PATCH 067/244] Move syntax tree patterns RFC to the book --- book/src/SUMMARY.md | 1 + .../src/development/proposals/syntax-tree-patterns.md | 0 2 files changed, 1 insertion(+) rename rfcs/0001-syntax-tree-patterns.md => book/src/development/proposals/syntax-tree-patterns.md (100%) diff --git a/book/src/SUMMARY.md b/book/src/SUMMARY.md index 0b945faf9b78e..1f0b8db28a152 100644 --- a/book/src/SUMMARY.md +++ b/book/src/SUMMARY.md @@ -21,3 +21,4 @@ - [The Clippy Book](development/infrastructure/book.md) - [Proposals](development/proposals/README.md) - [Roadmap 2021](development/proposals/roadmap-2021.md) + - [Syntax Tree Patterns](development/proposals/syntax-tree-patterns.md) diff --git a/rfcs/0001-syntax-tree-patterns.md b/book/src/development/proposals/syntax-tree-patterns.md similarity index 100% rename from rfcs/0001-syntax-tree-patterns.md rename to book/src/development/proposals/syntax-tree-patterns.md From c6a1184e4d4ca23fcbc6656913ba5a5a21d54368 Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Fri, 25 Nov 2022 11:39:36 +0100 Subject: [PATCH 068/244] Book: Format syntax tree pattern proposal --- .../proposals/syntax-tree-patterns.md | 575 ++++++++++++------ 1 file changed, 386 insertions(+), 189 deletions(-) diff --git a/book/src/development/proposals/syntax-tree-patterns.md b/book/src/development/proposals/syntax-tree-patterns.md index 9161986a7b774..c5587c4bf9089 100644 --- a/book/src/development/proposals/syntax-tree-patterns.md +++ b/book/src/development/proposals/syntax-tree-patterns.md @@ -1,23 +1,22 @@ - - - Feature Name: syntax-tree-patterns - Start Date: 2019-03-12 -- RFC PR: (leave this empty) -- Rust Issue: (leave this empty) - -> Note: This project is part of my Master's Thesis (supervised by [@oli-obk](https://github.com/oli-obk)) +- RFC PR: [#3875](https://github.com/rust-lang/rust-clippy/pull/3875) # Summary -Introduce a domain-specific language (similar to regular expressions) that allows to describe lints using *syntax tree patterns*. +Introduce a domain-specific language (similar to regular expressions) that +allows to describe lints using *syntax tree patterns*. # Motivation +Finding parts of a syntax tree (AST, HIR, ...) that have certain properties +(e.g. "*an if that has a block as its condition*") is a major task when writing +lints. For non-trivial lints, it often requires nested pattern matching of AST / +HIR nodes. For example, testing that an expression is a boolean literal requires +the following checks: -Finding parts of a syntax tree (AST, HIR, ...) that have certain properties (e.g. "*an if that has a block as its condition*") is a major task when writing lints. For non-trivial lints, it often requires nested pattern matching of AST / HIR nodes. For example, testing that an expression is a boolean literal requires the following checks: - -``` +```rust if let ast::ExprKind::Lit(lit) = &expr.node { if let ast::LitKind::Bool(_) = &lit.node { ... @@ -25,9 +24,11 @@ if let ast::ExprKind::Lit(lit) = &expr.node { } ``` -Writing this kind of matching code quickly becomes a complex task and the resulting code is often hard to comprehend. The code below shows a simplified version of the pattern matching required by the `collapsible_if` lint: +Writing this kind of matching code quickly becomes a complex task and the +resulting code is often hard to comprehend. The code below shows a simplified +version of the pattern matching required by the `collapsible_if` lint: -``` +```rust // simplified version of the collapsible_if lint if let ast::ExprKind::If(check, then, None) = &expr.node { if then.stmts.len() == 1 { @@ -40,9 +41,10 @@ if let ast::ExprKind::If(check, then, None) = &expr.node { } ``` -The `if_chain` macro can improve readability by flattening the nested if statements, but the resulting code is still quite hard to read: +The `if_chain` macro can improve readability by flattening the nested if +statements, but the resulting code is still quite hard to read: -``` +```rust // simplified version of the collapsible_if lint if_chain! { if let ast::ExprKind::If(check, then, None) = &expr.node; @@ -55,39 +57,66 @@ if_chain! { } ``` -The code above matches if expressions that contain only another if expression (where both ifs don't have an else branch). While it's easy to explain what the lint does, it's hard to see that from looking at the code samples above. +The code above matches if expressions that contain only another if expression +(where both ifs don't have an else branch). While it's easy to explain what the +lint does, it's hard to see that from looking at the code samples above. -Following the motivation above, the first goal this RFC is to **simplify writing and reading lints**. +Following the motivation above, the first goal this RFC is to **simplify writing +and reading lints**. -The second part of the motivation is clippy's dependence on unstable compiler-internal data structures. Clippy lints are currently written against the compiler's AST / HIR which means that even small changes in these data structures might break a lot of lints. The second goal of this RFC is to **make lints independant of the compiler's AST / HIR data structures**. +The second part of the motivation is clippy's dependence on unstable +compiler-internal data structures. Clippy lints are currently written against +the compiler's AST / HIR which means that even small changes in these data +structures might break a lot of lints. The second goal of this RFC is to **make +lints independant of the compiler's AST / HIR data structures**. # Approach -A lot of complexity in writing lints currently seems to come from having to manually implement the matching logic (see code samples above). It's an imparative style that describes *how* to match a syntax tree node instead of specifying *what* should be matched against declaratively. In other areas, it's common to use declarative patterns to describe desired information and let the implementation do the actual matching. A well-known example of this approach are [regular expressions](https://en.wikipedia.org/wiki/Regular_expression). Instead of writing code that detects certain character sequences, one can describe a search pattern using a domain-specific language and search for matches using that pattern. The advantage of using a declarative domain-specific language is that its limited domain (e.g. matching character sequences in the case of regular expressions) allows to express entities in that domain in a very natural and expressive way. - -While regular expressions are very useful when searching for patterns in flat character sequences, they cannot easily be applied to hierarchical data structures like syntax trees. This RFC therefore proposes a pattern matching system that is inspired by regular expressions and designed for hierarchical syntax trees. +A lot of complexity in writing lints currently seems to come from having to +manually implement the matching logic (see code samples above). It's an +imparative style that describes *how* to match a syntax tree node instead of +specifying *what* should be matched against declaratively. In other areas, it's +common to use declarative patterns to describe desired information and let the +implementation do the actual matching. A well-known example of this approach are +[regular expressions](https://en.wikipedia.org/wiki/Regular_expression). Instead +of writing code that detects certain character sequences, one can describe a +search pattern using a domain-specific language and search for matches using +that pattern. The advantage of using a declarative domain-specific language is +that its limited domain (e.g. matching character sequences in the case of +regular expressions) allows to express entities in that domain in a very natural +and expressive way. + +While regular expressions are very useful when searching for patterns in flat +character sequences, they cannot easily be applied to hierarchical data +structures like syntax trees. This RFC therefore proposes a pattern matching +system that is inspired by regular expressions and designed for hierarchical +syntax trees. # Guide-level explanation -This proposal adds a `pattern!` macro that can be used to specify a syntax tree pattern to search for. A simple pattern is shown below: +This proposal adds a `pattern!` macro that can be used to specify a syntax tree +pattern to search for. A simple pattern is shown below: -``` +```rust pattern!{ - my_pattern: Expr = + my_pattern: Expr = Lit(Bool(false)) } ``` -This macro call defines a pattern named `my_pattern` that can be matched against an `Expr` syntax tree node. The actual pattern (`Lit(Bool(false))` in this case) defines which syntax trees should match the pattern. This pattern matches expressions that are boolean literals with value `false`. +This macro call defines a pattern named `my_pattern` that can be matched against +an `Expr` syntax tree node. The actual pattern (`Lit(Bool(false))` in this case) +defines which syntax trees should match the pattern. This pattern matches +expressions that are boolean literals with value `false`. The pattern can then be used to implement lints in the following way: -``` +```rust ... impl EarlyLintPass for MyAwesomeLint { fn check_expr(&mut self, cx: &EarlyContext, expr: &syntax::ast::Expr) { - + if my_pattern(expr).is_some() { cx.span_lint( MY_AWESOME_LINT, @@ -95,14 +124,18 @@ impl EarlyLintPass for MyAwesomeLint { "This is a match for a simple pattern. Well done!", ); } - + } } ``` -The `pattern!` macro call expands to a function `my_pattern` that expects a syntax tree expression as its argument and returns an `Option` that indicates whether the pattern matched. +The `pattern!` macro call expands to a function `my_pattern` that expects a +syntax tree expression as its argument and returns an `Option` that indicates +whether the pattern matched. -> Note: The result type is explained in more detail in [a later section](#the-result-type). For now, it's enough to know that the result is `Some` if the pattern matched and `None` otherwise. +> Note: The result type is explained in more detail in [a later +> section](#the-result-type). For now, it's enough to know that the result is +> `Some` if the pattern matched and `None` otherwise. ## Pattern syntax @@ -111,38 +144,43 @@ The following examples demonstate the pattern syntax: #### Any (`_`) -The simplest pattern is the any pattern. It matches anything and is therefore similar to regex's `*`. +The simplest pattern is the any pattern. It matches anything and is therefore +similar to regex's `*`. -``` +```rust pattern!{ // matches any expression - my_pattern: Expr = + my_pattern: Expr = _ } ``` #### Node (`()`) -Nodes are used to match a specific variant of an AST node. A node has a name and a number of arguments that depends on the node type. For example, the `Lit` node has a single argument that describes the type of the literal. As another example, the `If` node has three arguments describing the if's condition, then block and else block. +Nodes are used to match a specific variant of an AST node. A node has a name and +a number of arguments that depends on the node type. For example, the `Lit` node +has a single argument that describes the type of the literal. As another +example, the `If` node has three arguments describing the if's condition, then +block and else block. -``` +```rust pattern!{ // matches any expression that is a literal - my_pattern: Expr = + my_pattern: Expr = Lit(_) } pattern!{ // matches any expression that is a boolean literal - my_pattern: Expr = + my_pattern: Expr = Lit(Bool(_)) } pattern!{ // matches if expressions that have a boolean literal in their condition - // Note: The `_?` syntax here means that the else branch is optional and can be anything. + // Note: The `_?` syntax here means that the else branch is optional and can be anything. // This is discussed in more detail in the section `Repetition`. - my_pattern: Expr = + my_pattern: Expr = If( Lit(Bool(_)) , _, _?) } ``` @@ -152,69 +190,71 @@ pattern!{ A pattern can also contain Rust literals. These literals match themselves. -``` +```rust pattern!{ // matches the boolean literal false - my_pattern: Expr = + my_pattern: Expr = Lit(Bool(false)) } pattern!{ // matches the character literal 'x' - my_pattern: Expr = + my_pattern: Expr = Lit(Char('x')) } ``` #### Alternations (`a | b`) -``` +```rust pattern!{ // matches if the literal is a boolean or integer literal - my_pattern: Lit = + my_pattern: Lit = Bool(_) | Int(_) } pattern!{ // matches if the expression is a char literal with value 'x' or 'y' - my_pattern: Expr = + my_pattern: Expr = Lit( Char('x' | 'y') ) } ``` #### Empty (`()`) -The empty pattern represents an empty sequence or the `None` variant of an optional. +The empty pattern represents an empty sequence or the `None` variant of an +optional. -``` +```rust pattern!{ // matches if the expression is an empty array - my_pattern: Expr = + my_pattern: Expr = Array( () ) } pattern!{ // matches if expressions that don't have an else clause - my_pattern: Expr = + my_pattern: Expr = If(_, _, ()) } ``` #### Sequence (` `) -``` +```rust pattern!{ // matches the array [true, false] - my_pattern: Expr = + my_pattern: Expr = Array( Lit(Bool(true)) Lit(Bool(false)) ) } ``` #### Repetition (`*`, `+`, `?`, `{n}`, `{n,m}`, `{n,}`) -Elements may be repeated. The syntax for specifying repetitions is identical to [regex's syntax](https://docs.rs/regex/1.1.2/regex/#repetitions). +Elements may be repeated. The syntax for specifying repetitions is identical to +[regex's syntax](https://docs.rs/regex/1.1.2/regex/#repetitions). -``` +```rust pattern!{ // matches arrays that contain 2 'x's as their last or second-last elements // Examples: @@ -222,7 +262,7 @@ pattern!{ // ['x', 'x', 'y'] match // ['a', 'b', 'c', 'x', 'x', 'y'] match // ['x', 'x', 'y', 'z'] no match - my_pattern: Expr = + my_pattern: Expr = Array( _* Lit(Char('x')){2} _? ) } @@ -234,34 +274,35 @@ pattern!{ // If(_, _, _) | match | no match // If(_, _, _?) | match | match // If(_, _, ()) | no match | match - my_pattern: Expr = + my_pattern: Expr = If(_, _, _?) } ``` #### Named submatch (`#`) -``` +```rust pattern!{ // matches character literals and gives the literal the name foo - my_pattern: Expr = + my_pattern: Expr = Lit(Char(_)#foo) } pattern!{ // matches character literals and gives the char the name bar - my_pattern: Expr = + my_pattern: Expr = Lit(Char(_#bar)) } pattern!{ // matches character literals and gives the expression the name baz - my_pattern: Expr = + my_pattern: Expr = Lit(Char(_))#baz } ``` -The reason for using named submatches is described in the section [The result type](#the-result-type). +The reason for using named submatches is described in the section [The result +type](#the-result-type). ### Summary @@ -281,21 +322,31 @@ The following table gives an summary of the pattern syntax: ## The result type -A lot of lints require checks that go beyond what the pattern syntax described above can express. For example, a lint might want to check whether a node was created as part of a macro expansion or whether there's no comment above a node. Another example would be a lint that wants to match two nodes that have the same value (as needed by lints like `almost_swapped`). Instead of allowing users to write these checks into the pattern directly (which might make patterns hard to read), the proposed solution allows users to assign names to parts of a pattern expression. When matching a pattern against a syntax tree node, the return value will contain references to all nodes that were matched by these named subpatterns. This is similar to capture groups in regular expressions. +A lot of lints require checks that go beyond what the pattern syntax described +above can express. For example, a lint might want to check whether a node was +created as part of a macro expansion or whether there's no comment above a node. +Another example would be a lint that wants to match two nodes that have the same +value (as needed by lints like `almost_swapped`). Instead of allowing users to +write these checks into the pattern directly (which might make patterns hard to +read), the proposed solution allows users to assign names to parts of a pattern +expression. When matching a pattern against a syntax tree node, the return value +will contain references to all nodes that were matched by these named +subpatterns. This is similar to capture groups in regular expressions. For example, given the following pattern -``` +```rust pattern!{ // matches character literals - my_pattern: Expr = + my_pattern: Expr = Lit(Char(_#val_inner)#val)#val_outer } ``` -one could get references to the nodes that matched the subpatterns in the following way: +one could get references to the nodes that matched the subpatterns in the +following way: -``` +```rust ... fn check_expr(expr: &syntax::ast::Expr) { if let Some(result) = my_pattern(expr) { @@ -306,50 +357,57 @@ fn check_expr(expr: &syntax::ast::Expr) { } ``` -The types in the `result` struct depend on the pattern. For example, the following pattern +The types in the `result` struct depend on the pattern. For example, the +following pattern -``` +```rust pattern!{ // matches arrays of character literals - my_pattern_seq: Expr = + my_pattern_seq: Expr = Array( Lit(_)*#foo ) } ``` -matches arrays that consist of any number of literal expressions. Because those expressions are named `foo`, the result struct contains a `foo` attribute which is a vector of expressions: +matches arrays that consist of any number of literal expressions. Because those +expressions are named `foo`, the result struct contains a `foo` attribute which +is a vector of expressions: -``` +```rust ... if let Some(result) = my_pattern_seq(expr) { result.foo // type: Vec<&syntax::ast::Expr> } ``` -Another result type occurs when a name is only defined in one branch of an alternation: +Another result type occurs when a name is only defined in one branch of an +alternation: -``` +```rust pattern!{ // matches if expression is a boolean or integer literal - my_pattern_alt: Expr = + my_pattern_alt: Expr = Lit( Bool(_#bar) | Int(_) ) } ``` -In the pattern above, the `bar` name is only defined if the pattern matches a boolean literal. If it matches an integer literal, the name isn't set. To account fot this, the result struct's `bar` attribute is an option type: +In the pattern above, the `bar` name is only defined if the pattern matches a +boolean literal. If it matches an integer literal, the name isn't set. To +account for this, the result struct's `bar` attribute is an option type: -``` +```rust ... if let Some(result) = my_pattern_alt(expr) { result.bar // type: Option<&bool> } ``` -It's also possible to use a name in multiple alternation branches if they have compatible types: +It's also possible to use a name in multiple alternation branches if they have +compatible types: -``` +```rust pattern!{ // matches if expression is a boolean or integer literal - my_pattern_mult: Expr = + my_pattern_mult: Expr = Lit(_#baz) | Array( Lit(_#baz) ) } ... @@ -358,60 +416,77 @@ if let Some(result) = my_pattern_mult(expr) { } ``` -Named submatches are a **flat** namespace and this is intended. In the example above, two different sub-structures are assigned to a flat name. I expect that for most lints, a flat namespace is sufficient and easier to work with than a hierarchical one. +Named submatches are a **flat** namespace and this is intended. In the example +above, two different sub-structures are assigned to a flat name. I expect that +for most lints, a flat namespace is sufficient and easier to work with than a +hierarchical one. #### Two stages -Using named subpatterns, users can write lints in two stages. First, a coarse selection of possible matches is produced by the pattern syntax. In the second stage, the named subpattern references can be used to do additional tests like asserting that a node hasn't been created as part of a macro expansion. +Using named subpatterns, users can write lints in two stages. First, a coarse +selection of possible matches is produced by the pattern syntax. In the second +stage, the named subpattern references can be used to do additional tests like +asserting that a node hasn't been created as part of a macro expansion. ## Implementing clippy lints using patterns -As a "real-world" example, I re-implemented the `collapsible_if` lint using patterns. The code can be found [here](https://github.com/fkohlgrueber/rust-clippy-pattern/blob/039b07ecccaf96d6aa7504f5126720d2c9cceddd/clippy_lints/src/collapsible_if.rs#L88-L163). The pattern-based version passes all test cases that were written for `collapsible_if`. +As a "real-world" example, I re-implemented the `collapsible_if` lint using +patterns. The code can be found +[here](https://github.com/fkohlgrueber/rust-clippy-pattern/blob/039b07ecccaf96d6aa7504f5126720d2c9cceddd/clippy_lints/src/collapsible_if.rs#L88-L163). +The pattern-based version passes all test cases that were written for +`collapsible_if`. # Reference-level explanation ## Overview -The following diagram shows the dependencies between the main parts of the proposed solution: +The following diagram shows the dependencies between the main parts of the +proposed solution: ``` - Pattern syntax - | - | parsing / lowering - v - PatternTree - ^ - | - | - IsMatch trait - | - | - +---------------+-----------+---------+ - | | | | - v v v v - syntax::ast rustc::hir syn ... + Pattern syntax + | + | parsing / lowering + v + PatternTree + ^ + | + | + IsMatch trait + | + | + +---------------+-----------+---------+ + | | | | + v v v v + syntax::ast rustc::hir syn ... ``` -The pattern syntax described in the previous section is parsed / lowered into the so-called *PatternTree* data structure that represents a valid syntax tree pattern. Matching a *PatternTree* against an actual syntax tree (e.g. rust ast / hir or the syn ast, ...) is done using the *IsMatch* trait. +The pattern syntax described in the previous section is parsed / lowered into +the so-called *PatternTree* data structure that represents a valid syntax tree +pattern. Matching a *PatternTree* against an actual syntax tree (e.g. rust ast / +hir or the syn ast, ...) is done using the *IsMatch* trait. -The *PatternTree* and the *IsMatch* trait are introduced in more detail in the following sections. +The *PatternTree* and the *IsMatch* trait are introduced in more detail in the +following sections. ## PatternTree -The core data structure of this RFC is the **PatternTree**. +The core data structure of this RFC is the **PatternTree**. -It's a data structure similar to rust's AST / HIR, but with the following differences: +It's a data structure similar to rust's AST / HIR, but with the following +differences: - The PatternTree doesn't contain parsing information like `Span`s - The PatternTree can represent alternatives, sequences and optionals The code below shows a simplified version of the current PatternTree: -> Note: The current implementation can be found [here](https://github.com/fkohlgrueber/pattern-matching/blob/dfb3bc9fbab69cec7c91e72564a63ebaa2ede638/pattern-match/src/pattern_tree.rs#L50-L96). +> Note: The current implementation can be found +> [here](https://github.com/fkohlgrueber/pattern-matching/blob/dfb3bc9fbab69cec7c91e72564a63ebaa2ede638/pattern-match/src/pattern_tree.rs#L50-L96). -``` +```rust pub enum Expr { Lit(Alt), Array(Seq), @@ -441,9 +516,10 @@ pub enum BlockType { The `Alt`, `Seq` and `Opt` structs look like these: -> Note: The current implementation can be found [here](https://github.com/fkohlgrueber/pattern-matching/blob/dfb3bc9fbab69cec7c91e72564a63ebaa2ede638/pattern-match/src/matchers.rs#L35-L60). +> Note: The current implementation can be found +> [here](https://github.com/fkohlgrueber/pattern-matching/blob/dfb3bc9fbab69cec7c91e72564a63ebaa2ede638/pattern-match/src/matchers.rs#L35-L60). -``` +```rust pub enum Alt { Any, Elmt(Box), @@ -477,27 +553,45 @@ pub struct RepeatRange { ## Parsing / Lowering -The input of a `pattern!` macro call is parsed into a `ParseTree` first and then lowered to a `PatternTree`. +The input of a `pattern!` macro call is parsed into a `ParseTree` first and then +lowered to a `PatternTree`. -Valid patterns depend on the *PatternTree* definitions. For example, the pattern `Lit(Bool(_)*)` isn't valid because the parameter type of the `Lit` variant of the `Expr` enum is `Any` and therefore doesn't support repetition (`*`). As another example, `Array( Lit(_)* )` is a valid pattern because the parameter of `Array` is of type `Seq` which allows sequences and repetitions. +Valid patterns depend on the *PatternTree* definitions. For example, the pattern +`Lit(Bool(_)*)` isn't valid because the parameter type of the `Lit` variant of +the `Expr` enum is `Any` and therefore doesn't support repetition (`*`). As +another example, `Array( Lit(_)* )` is a valid pattern because the parameter of +`Array` is of type `Seq` which allows sequences and repetitions. -> Note: names in the pattern syntax correspond to *PatternTree* enum **variants**. For example, the `Lit` in the pattern above refers to the `Lit` variant of the `Expr` enum (`Expr::Lit`), not the `Lit` enum. +> Note: names in the pattern syntax correspond to *PatternTree* enum +> **variants**. For example, the `Lit` in the pattern above refers to the `Lit` +> variant of the `Expr` enum (`Expr::Lit`), not the `Lit` enum. ## The IsMatch Trait -The pattern syntax and the *PatternTree* are independant of specific syntax tree implementations (rust ast / hir, syn, ...). When looking at the different pattern examples in the previous sections, it can be seen that the patterns don't contain any information specific to a certain syntax tree implementation. In contrast, clippy lints currently match against ast / hir syntax tree nodes and therefore directly depend on their implementation. +The pattern syntax and the *PatternTree* are independant of specific syntax tree +implementations (rust ast / hir, syn, ...). When looking at the different +pattern examples in the previous sections, it can be seen that the patterns +don't contain any information specific to a certain syntax tree implementation. +In contrast, clippy lints currently match against ast / hir syntax tree nodes +and therefore directly depend on their implementation. -The connection between the *PatternTree* and specific syntax tree implementations is the `IsMatch` trait. It defines how to match *PatternTree* nodes against specific syntax tree nodes. A simplified implementation of the `IsMatch` trait is shown below: +The connection between the *PatternTree* and specific syntax tree +implementations is the `IsMatch` trait. It defines how to match *PatternTree* +nodes against specific syntax tree nodes. A simplified implementation of the +`IsMatch` trait is shown below: -``` +```rust pub trait IsMatch { fn is_match(&self, other: &'o O) -> bool; } ``` -This trait needs to be implemented on each enum of the *PatternTree* (for the corresponding syntax tree types). For example, the `IsMatch` implementation for matching `ast::LitKind` against the *PatternTree's* `Lit` enum might look like this: +This trait needs to be implemented on each enum of the *PatternTree* (for the +corresponding syntax tree types). For example, the `IsMatch` implementation for +matching `ast::LitKind` against the *PatternTree's* `Lit` enum might look like +this: -``` +```rust impl IsMatch for Lit { fn is_match(&self, other: &ast::LitKind) -> bool { match (self, other) { @@ -510,25 +604,30 @@ impl IsMatch for Lit { } ``` -All `IsMatch` implementations for matching the current *PatternTree* against `syntax::ast` can be found [here](https://github.com/fkohlgrueber/pattern-matching/blob/dfb3bc9fbab69cec7c91e72564a63ebaa2ede638/pattern-match/src/ast_match.rs). +All `IsMatch` implementations for matching the current *PatternTree* against +`syntax::ast` can be found +[here](https://github.com/fkohlgrueber/pattern-matching/blob/dfb3bc9fbab69cec7c91e72564a63ebaa2ede638/pattern-match/src/ast_match.rs). # Drawbacks #### Performance -The pattern matching code is currently not optimized for performance, so it might be slower than hand-written matching code. -Additionally, the two-stage approach (matching against the coarse pattern first and checking for additional properties later) might be slower than the current practice of checking for structure and additional properties in one pass. For example, the following lint +The pattern matching code is currently not optimized for performance, so it +might be slower than hand-written matching code. Additionally, the two-stage +approach (matching against the coarse pattern first and checking for additional +properties later) might be slower than the current practice of checking for +structure and additional properties in one pass. For example, the following lint -``` +```rust pattern!{ - pat_if_without_else: Expr = + pat_if_without_else: Expr = If( _, Block( Expr( If(_, _, ())#inner ) - | Semi( If(_, _, ())#inner ) - )#then, + | Semi( If(_, _, ())#inner ) + )#then, () ) } @@ -541,9 +640,11 @@ fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) { } ``` -first matches against the pattern and then checks that the `then` block doesn't start with a comment. Using clippy's current approach, it's possible to check for these conditions earlier: +first matches against the pattern and then checks that the `then` block doesn't +start with a comment. Using clippy's current approach, it's possible to check +for these conditions earlier: -``` +```rust fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) { if_chain! { if let ast::ExprKind::If(ref check, ref then, None) = expr.node; @@ -557,30 +658,57 @@ fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) { } ``` -Whether or not this causes performance regressions depends on actual patterns. If it turns out to be a problem, the pattern matching algorithms could be extended to allow "early filtering" (see the [Early Filtering](#early-filtering) section in Future Possibilities). +Whether or not this causes performance regressions depends on actual patterns. +If it turns out to be a problem, the pattern matching algorithms could be +extended to allow "early filtering" (see the [Early Filtering](#early-filtering) +section in Future Possibilities). -That being said, I don't see any conceptual limitations regarding pattern matching performance. +That being said, I don't see any conceptual limitations regarding pattern +matching performance. #### Applicability -Even though I'd expect that a lot of lints can be written using the proposed pattern syntax, it's unlikely that all lints can be expressed using patterns. I suspect that there will still be lints that need to be implemented by writing custom pattern matching code. This would lead to mix within clippy's codebase where some lints are implemented using patterns and others aren't. This inconsistency might be considered a drawback. +Even though I'd expect that a lot of lints can be written using the proposed +pattern syntax, it's unlikely that all lints can be expressed using patterns. I +suspect that there will still be lints that need to be implemented by writing +custom pattern matching code. This would lead to mix within clippy's codebase +where some lints are implemented using patterns and others aren't. This +inconsistency might be considered a drawback. # Rationale and alternatives -Specifying lints using syntax tree patterns has a couple of advantages compared to the current approach of manually writing matching code. First, syntax tree patterns allow users to describe patterns in a simple and expressive way. This makes it easier to write new lints for both novices and experts and also makes reading / modifying existing lints simpler. - -Another advantage is that lints are independent of specific syntax tree implementations (e.g. AST / HIR, ...). When these syntax tree implementations change, only the `IsMatch` trait implementations need to be adapted and existing lints can remain unchanged. This also means that if the `IsMatch` trait implementations were integrated into the compiler, updating the `IsMatch` implementations would be required for the compiler to compile successfully. This could reduce the number of times clippy breaks because of changes in the compiler. Another advantage of the pattern's independence is that converting an `EarlyLintPass` lint into a `LatePassLint` wouldn't require rewriting the whole pattern matching code. In fact, the pattern might work just fine without any adaptions. - +Specifying lints using syntax tree patterns has a couple of advantages compared +to the current approach of manually writing matching code. First, syntax tree +patterns allow users to describe patterns in a simple and expressive way. This +makes it easier to write new lints for both novices and experts and also makes +reading / modifying existing lints simpler. + +Another advantage is that lints are independent of specific syntax tree +implementations (e.g. AST / HIR, ...). When these syntax tree implementations +change, only the `IsMatch` trait implementations need to be adapted and existing +lints can remain unchanged. This also means that if the `IsMatch` trait +implementations were integrated into the compiler, updating the `IsMatch` +implementations would be required for the compiler to compile successfully. This +could reduce the number of times clippy breaks because of changes in the +compiler. Another advantage of the pattern's independence is that converting an +`EarlyLintPass` lint into a `LatePassLint` wouldn't require rewriting the whole +pattern matching code. In fact, the pattern might work just fine without any +adaptions. ## Alternatives ### Rust-like pattern syntax -The proposed pattern syntax requires users to know the structure of the `PatternTree` (which is very similar to the AST's / HIR's structure) and also the pattern syntax. An alternative would be to introduce a pattern syntax that is similar to actual Rust syntax (probably like the `quote!` macro). For example, a pattern that matches `if` expressions that have `false` in their condition could look like this: +The proposed pattern syntax requires users to know the structure of the +`PatternTree` (which is very similar to the AST's / HIR's structure) and also +the pattern syntax. An alternative would be to introduce a pattern syntax that +is similar to actual Rust syntax (probably like the `quote!` macro). For +example, a pattern that matches `if` expressions that have `false` in their +condition could look like this: -``` +```rust if false { #[*] } @@ -588,9 +716,13 @@ if false { #### Problems -Extending Rust syntax (which is quite complex by itself) with additional syntax needed for specifying patterns (alternations, sequences, repetisions, named submatches, ...) might become difficult to read and really hard to parse properly. +Extending Rust syntax (which is quite complex by itself) with additional syntax +needed for specifying patterns (alternations, sequences, repetisions, named +submatches, ...) might become difficult to read and really hard to parse +properly. -For example, a pattern that matches a binary operation that has `0` on both sides might look like this: +For example, a pattern that matches a binary operation that has `0` on both +sides might look like this: ``` 0 #[*:BinOpKind] 0 @@ -602,74 +734,116 @@ Now consider this slightly more complex example: 1 + 0 #[*:BinOpKind] 0 ``` -The parser would need to know the precedence of `#[*:BinOpKind]` because it affects the structure of the resulting AST. `1 + 0 + 0` is parsed as `(1 + 0) + 0` while `1 + 0 * 0` is parsed as `1 + (0 * 0)`. Since the pattern could be any `BinOpKind`, the precedence cannot be known in advance. +The parser would need to know the precedence of `#[*:BinOpKind]` because it +affects the structure of the resulting AST. `1 + 0 + 0` is parsed as `(1 + 0) + +0` while `1 + 0 * 0` is parsed as `1 + (0 * 0)`. Since the pattern could be any +`BinOpKind`, the precedence cannot be known in advance. -Another example of a problem would be named submatches. Take a look at this pattern: +Another example of a problem would be named submatches. Take a look at this +pattern: -``` +```rust fn test() { 1 #foo } ``` -Which node is `#foo` referring to? `int`, `ast::Lit`, `ast::Expr`, `ast::Stmt`? Naming subpatterns in a rust-like syntax is difficult because a lot of AST nodes don't have a syntactic element that can be used to put the name tag on. In these situations, the only sensible option would be to assign the name tag to the outermost node (`ast::Stmt` in the example above), because the information of all child nodes can be retrieved through the outermost node. The problem with this then would be that accessing inner nodes (like `ast::Lit`) would again require manual pattern matching. +Which node is `#foo` referring to? `int`, `ast::Lit`, `ast::Expr`, `ast::Stmt`? +Naming subpatterns in a rust-like syntax is difficult because a lot of AST nodes +don't have a syntactic element that can be used to put the name tag on. In these +situations, the only sensible option would be to assign the name tag to the +outermost node (`ast::Stmt` in the example above), because the information of +all child nodes can be retrieved through the outermost node. The problem with +this then would be that accessing inner nodes (like `ast::Lit`) would again +require manual pattern matching. -In general, Rust syntax contains a lot of code structure implicitly. This structure is reconstructed during parsing (e.g. binary operations are reconstructed using operator precedence and left-to-right) and is one of the reasons why parsing is a complex task. The advantage of this approach is that writing code is simpler for users. +In general, Rust syntax contains a lot of code structure implicitly. This +structure is reconstructed during parsing (e.g. binary operations are +reconstructed using operator precedence and left-to-right) and is one of the +reasons why parsing is a complex task. The advantage of this approach is that +writing code is simpler for users. -When writing *syntax tree patterns*, each element of the hierarchy might have alternatives, repetitions, etc.. Respecting that while still allowing human-friendly syntax that contains structure implicitly seems to be really complex, if not impossible. +When writing *syntax tree patterns*, each element of the hierarchy might have +alternatives, repetitions, etc.. Respecting that while still allowing +human-friendly syntax that contains structure implicitly seems to be really +complex, if not impossible. -Developing such a syntax would also require to maintain a custom parser that is at least as complex as the Rust parser itself. Additionally, future changes in the Rust syntax might be incompatible with such a syntax. +Developing such a syntax would also require to maintain a custom parser that is +at least as complex as the Rust parser itself. Additionally, future changes in +the Rust syntax might be incompatible with such a syntax. -In summary, I think that developing such a syntax would introduce a lot of complexity to solve a relatively minor problem. +In summary, I think that developing such a syntax would introduce a lot of +complexity to solve a relatively minor problem. -The issue of users not knowing about the *PatternTree* structure could be solved by a tool that, given a rust program, generates a pattern that matches only this program (similar to the clippy author lint). +The issue of users not knowing about the *PatternTree* structure could be solved +by a tool that, given a rust program, generates a pattern that matches only this +program (similar to the clippy author lint). -For some simple cases (like the first example above), it might be possible to successfully mix Rust and pattern syntax. This space could be further explored in a future extension. +For some simple cases (like the first example above), it might be possible to +successfully mix Rust and pattern syntax. This space could be further explored +in a future extension. # Prior art -The pattern syntax is heavily inspired by regular expressions (repetitions, alternatives, sequences, ...). +The pattern syntax is heavily inspired by regular expressions (repetitions, +alternatives, sequences, ...). -From what I've seen until now, other linters also implement lints that directly work on syntax tree data structures, just like clippy does currently. I would therefore consider the pattern syntax to be *new*, but please correct me if I'm wrong. +From what I've seen until now, other linters also implement lints that directly +work on syntax tree data structures, just like clippy does currently. I would +therefore consider the pattern syntax to be *new*, but please correct me if I'm +wrong. # Unresolved questions #### How to handle multiple matches? -When matching a syntax tree node against a pattern, there are possibly multiple ways in which the pattern can be matched. A simple example of this would be the following pattern: +When matching a syntax tree node against a pattern, there are possibly multiple +ways in which the pattern can be matched. A simple example of this would be the +following pattern: -``` +```rust pattern!{ - my_pattern: Expr = + my_pattern: Expr = Array( _* Lit(_)+#literals) } ``` -This pattern matches arrays that end with at least one literal. Now given the array `[x, 1, 2]`, should `1` be matched as part of the `_*` or the `Lit(_)+` part of the pattern? The difference is important because the named submatch `#literals` would contain 1 or 2 elements depending how the pattern is matched. In regular expressions, this problem is solved by matching "greedy" by default and "non-greedy" optionally. +This pattern matches arrays that end with at least one literal. Now given the +array `[x, 1, 2]`, should `1` be matched as part of the `_*` or the `Lit(_)+` +part of the pattern? The difference is important because the named submatch +`#literals` would contain 1 or 2 elements depending how the pattern is matched. +In regular expressions, this problem is solved by matching "greedy" by default +and "non-greedy" optionally. -I haven't looked much into this yet because I don't know how relevant it is for most lints. The current implementation simply returns the first match it finds. +I haven't looked much into this yet because I don't know how relevant it is for +most lints. The current implementation simply returns the first match it finds. # Future possibilities #### Implement rest of Rust Syntax -The current project only implements a small part of the Rust syntax. In the future, this should incrementally be extended to more syntax to allow implementing more lints. Implementing more of the Rust syntax requires extending the `PatternTree` and `IsMatch` implementations, but should be relatively straight-forward. +The current project only implements a small part of the Rust syntax. In the +future, this should incrementally be extended to more syntax to allow +implementing more lints. Implementing more of the Rust syntax requires extending +the `PatternTree` and `IsMatch` implementations, but should be relatively +straight-forward. #### Early filtering -As described in the *Drawbacks/Performance* section, allowing additional checks during the pattern matching might be beneficial. +As described in the *Drawbacks/Performance* section, allowing additional checks +during the pattern matching might be beneficial. The pattern below shows how this could look like: -``` +```rust pattern!{ - pat_if_without_else: Expr = + pat_if_without_else: Expr = If( _, Block( Expr( If(_, _, ())#inner ) - | Semi( If(_, _, ())#inner ) - )#then, + | Semi( If(_, _, ())#inner ) + )#then, () ) where @@ -677,47 +851,64 @@ pattern!{ } ``` -The difference compared to the currently proposed two-stage filtering is that using early filtering, the condition (`!in_macro(#then.span)` in this case) would be evaluated as soon as the `Block(_)#then` was matched. +The difference compared to the currently proposed two-stage filtering is that +using early filtering, the condition (`!in_macro(#then.span)` in this case) +would be evaluated as soon as the `Block(_)#then` was matched. -Another idea in this area would be to introduce a syntax for backreferences. They could be used to require that multiple parts of a pattern should match the same value. For example, the `assign_op_pattern` lint that searches for `a = a op b` and recommends changing it to `a op= b` requires that both occurrances of `a` are the same. Using `=#...` as syntax for backreferences, the lint could be implemented like this: +Another idea in this area would be to introduce a syntax for backreferences. +They could be used to require that multiple parts of a pattern should match the +same value. For example, the `assign_op_pattern` lint that searches for `a = a +op b` and recommends changing it to `a op= b` requires that both occurrances of +`a` are the same. Using `=#...` as syntax for backreferences, the lint could be +implemented like this: -``` +```rust pattern!{ - assign_op_pattern: Expr = + assign_op_pattern: Expr = Assign(_#target, Binary(_, =#target, _) } ``` #### Match descendant -A lot of lints currently implement custom visitors that check whether any subtree (which might not be a direct descendant) of the current node matches some properties. This cannot be expressed with the proposed pattern syntax. Extending the pattern syntax to allow patterns like "a function that contains at least two return statements" could be a practical addition. +A lot of lints currently implement custom visitors that check whether any +subtree (which might not be a direct descendant) of the current node matches +some properties. This cannot be expressed with the proposed pattern syntax. +Extending the pattern syntax to allow patterns like "a function that contains at +least two return statements" could be a practical addition. #### Negation operator for alternatives -For patterns like "a literal that is not a boolean literal" one currently needs to list all alternatives except the boolean case. Introducing a negation operator that allows to write `Lit(!Bool(_))` might be a good idea. This pattern would be eqivalent to `Lit( Char(_) | Int(_) )` (given that currently only three literal types are implemented). +For patterns like "a literal that is not a boolean literal" one currently needs +to list all alternatives except the boolean case. Introducing a negation +operator that allows to write `Lit(!Bool(_))` might be a good idea. This pattern +would be eqivalent to `Lit( Char(_) | Int(_) )` (given that currently only three +literal types are implemented). #### Functional composition -Patterns currently don't have any concept of composition. This leads to repetitions within patterns. For example, one of the collapsible-if patterns currently has to be written like this: +Patterns currently don't have any concept of composition. This leads to +repetitions within patterns. For example, one of the collapsible-if patterns +currently has to be written like this: -``` +```rust pattern!{ - pat_if_else: Expr = + pat_if_else: Expr = If( - _, - _, + _, + _, Block_( Block( - Expr((If(_, _, _?) | IfLet(_, _?))#else_) | + Expr((If(_, _, _?) | IfLet(_, _?))#else_) | Semi((If(_, _, _?) | IfLet(_, _?))#else_) )#block_inner )#block ) | IfLet( - _, + _, Block_( Block( - Expr((If(_, _, _?) | IfLet(_, _?))#else_) | + Expr((If(_, _, _?) | IfLet(_, _?))#else_) | Semi((If(_, _, _?) | IfLet(_, _?))#else_) )#block_inner )#block @@ -725,9 +916,10 @@ pattern!{ } ``` -If patterns supported defining functions of subpatterns, the code could be simplified as follows: +If patterns supported defining functions of subpatterns, the code could be +simplified as follows: -``` +```rust pattern!{ fn expr_or_semi(expr: Expr) -> Stmt { Expr(expr) | Semi(expr) @@ -735,7 +927,7 @@ pattern!{ fn if_or_if_let(then: Block, else: Opt) -> Expr { If(_, then, else) | IfLet(then, else) } - pat_if_else: Expr = + pat_if_else: Expr = if_or_if_let( _, Block_( @@ -747,43 +939,48 @@ pattern!{ } ``` -Additionally, common patterns like `expr_or_semi` could be shared between different lints. +Additionally, common patterns like `expr_or_semi` could be shared between +different lints. #### Clippy Pattern Author -Another improvement could be to create a tool that, given some valid Rust syntax, generates a pattern that matches this syntax exactly. This would make starting to write a pattern easier. A user could take a look at the patterns generated for a couple of Rust code examples and use that information to write a pattern that matches all of them. +Another improvement could be to create a tool that, given some valid Rust +syntax, generates a pattern that matches this syntax exactly. This would make +starting to write a pattern easier. A user could take a look at the patterns +generated for a couple of Rust code examples and use that information to write a +pattern that matches all of them. This is similar to clippy's author lint. #### Supporting other syntaxes -Most of the proposed system is language-agnostic. For example, the pattern syntax could also be used to describe patterns for other programming languages. +Most of the proposed system is language-agnostic. For example, the pattern +syntax could also be used to describe patterns for other programming languages. -In order to support other languages' syntaxes, one would need to implement another `PatternTree` that sufficiently describes the languages' AST and implement `IsMatch` for this `PatternTree` and the languages' AST. +In order to support other languages' syntaxes, one would need to implement +another `PatternTree` that sufficiently describes the languages' AST and +implement `IsMatch` for this `PatternTree` and the languages' AST. -One aspect of this is that it would even be possible to write lints that work on the pattern syntax itself. For example, when writing the following pattern +One aspect of this is that it would even be possible to write lints that work on +the pattern syntax itself. For example, when writing the following pattern -``` +```rust pattern!{ - my_pattern: Expr = + my_pattern: Expr = Array( Lit(Bool(false)) Lit(Bool(false)) ) } ``` -a lint that works on the pattern syntax's AST could suggest using this pattern instead: +a lint that works on the pattern syntax's AST could suggest using this pattern +instead: -``` +```rust pattern!{ - my_pattern: Expr = + my_pattern: Expr = Array( Lit(Bool(false)){2} ) } ``` -In the future, clippy could use this system to also provide lints for custom syntaxes like those found in macros. - -# Final Note - -This is the first RFC I've ever written and it might be a little rough around the edges. - -I'm looking forward to hearing your feedback and discussing the proposal. +In the future, clippy could use this system to also provide lints for custom +syntaxes like those found in macros. From 68d953014e8c1c8f529379acf0a97ed7ce8ac1ae Mon Sep 17 00:00:00 2001 From: lcnr Date: Fri, 25 Nov 2022 14:26:10 +0100 Subject: [PATCH 069/244] notify lcnr on changes to `ObligationCtxt` --- triagebot.toml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/triagebot.toml b/triagebot.toml index 985e065652d62..a3c5f0dde0720 100644 --- a/triagebot.toml +++ b/triagebot.toml @@ -334,6 +334,13 @@ cc = ["@rust-lang/wg-mir-opt"] message = "Some changes occurred in const_evaluatable.rs" cc = ["@lcnr"] +[mentions."compiler/rustc_trait_selection/src/traits/engine.rs"] +message = """ +Some changes occurred in engine.rs, potentially modifying the public API \ +of `ObligationCtxt`. +""" +cc = ["@lcnr"] + [mentions."compiler/rustc_error_codes/src/error_codes.rs"] message = "Some changes occurred in diagnostic error codes" cc = ["@GuillaumeGomez"] From 40a053361a2871e286cf5cbc83cc2ed3926e1b4b Mon Sep 17 00:00:00 2001 From: lcnr Date: Fri, 25 Nov 2022 15:51:46 +0100 Subject: [PATCH 070/244] small method code cleanup --- compiler/rustc_hir_typeck/src/callee.rs | 16 ++++++++++------ compiler/rustc_hir_typeck/src/coercion.rs | 7 +++---- compiler/rustc_hir_typeck/src/method/probe.rs | 9 ++++----- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/callee.rs b/compiler/rustc_hir_typeck/src/callee.rs index 25b6cf4ef2e3e..cd1322d1a3bcc 100644 --- a/compiler/rustc_hir_typeck/src/callee.rs +++ b/compiler/rustc_hir_typeck/src/callee.rs @@ -179,12 +179,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // Hack: we know that there are traits implementing Fn for &F // where F:Fn and so forth. In the particular case of types - // like `x: &mut FnMut()`, if there is a call `x()`, we would - // normally translate to `FnMut::call_mut(&mut x, ())`, but - // that winds up requiring `mut x: &mut FnMut()`. A little - // over the top. The simplest fix by far is to just ignore - // this case and deref again, so we wind up with - // `FnMut::call_mut(&mut *x, ())`. + // like `f: &mut FnMut()`, if there is a call `f()`, we would + // normally translate to `FnMut::call_mut(&mut f, ())`, but + // that winds up requiring the user to potentially mark their + // variable as `mut` which feels unnecessary and unexpected. + // + // fn foo(f: &mut impl FnMut()) { f() } + // ^ without this hack `f` would have to be declared as mutable + // + // The simplest fix by far is to just ignore this case and deref again, + // so we wind up with `FnMut::call_mut(&mut *f, ())`. ty::Ref(..) if autoderef.step_count() == 0 => { return None; } diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index 82784bb8a661a..f6377f9f49403 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -1112,15 +1112,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // Special-case that coercion alone cannot handle: // Function items or non-capturing closures of differing IDs or InternalSubsts. let (a_sig, b_sig) = { - #[allow(rustc::usage_of_ty_tykind)] - let is_capturing_closure = |ty: &ty::TyKind<'tcx>| { - if let &ty::Closure(closure_def_id, _substs) = ty { + let is_capturing_closure = |ty: Ty<'tcx>| { + if let &ty::Closure(closure_def_id, _substs) = ty.kind() { self.tcx.upvars_mentioned(closure_def_id.expect_local()).is_some() } else { false } }; - if is_capturing_closure(prev_ty.kind()) || is_capturing_closure(new_ty.kind()) { + if is_capturing_closure(prev_ty) || is_capturing_closure(new_ty) { (None, None) } else { match (prev_ty.kind(), new_ty.kind()) { diff --git a/compiler/rustc_hir_typeck/src/method/probe.rs b/compiler/rustc_hir_typeck/src/method/probe.rs index 44c3edf06a883..37f9cdfeebc60 100644 --- a/compiler/rustc_hir_typeck/src/method/probe.rs +++ b/compiler/rustc_hir_typeck/src/method/probe.rs @@ -342,10 +342,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { &mut orig_values, ); - let steps = if mode == Mode::MethodCall { - self.tcx.method_autoderef_steps(param_env_and_self_ty) - } else { - self.probe(|_| { + let steps = match mode { + Mode::MethodCall => self.tcx.method_autoderef_steps(param_env_and_self_ty), + Mode::Path => self.probe(|_| { // Mode::Path - the deref steps is "trivial". This turns // our CanonicalQuery into a "trivial" QueryResponse. This // is a bit inefficient, but I don't think that writing @@ -374,7 +373,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { opt_bad_ty: None, reached_recursion_limit: false, } - }) + }), }; // If our autoderef loop had reached the recursion limit, From 815d3703469194355d6fae6bc5844a1f7bbae4e1 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Fri, 25 Nov 2022 15:02:53 +0000 Subject: [PATCH 071/244] Add documentation for `has_escaping_bound_vars` --- compiler/rustc_middle/src/ty/visit.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_middle/src/ty/visit.rs b/compiler/rustc_middle/src/ty/visit.rs index b04afe549ac55..4cdfd9e594042 100644 --- a/compiler/rustc_middle/src/ty/visit.rs +++ b/compiler/rustc_middle/src/ty/visit.rs @@ -72,12 +72,18 @@ pub trait TypeVisitable<'tcx>: fmt::Debug + Clone { self.visit_with(&mut HasEscapingVarsVisitor { outer_index: binder }).is_break() } - /// Returns `true` if this `self` has any regions that escape `binder` (and + /// Returns `true` if this type has any regions that escape `binder` (and /// hence are not bound by it). fn has_vars_bound_above(&self, binder: ty::DebruijnIndex) -> bool { self.has_vars_bound_at_or_above(binder.shifted_in(1)) } + /// Return `true` if this type has regions that are not a part of the type. + /// For example, `for<'a> fn(&'a i32)` return `false`, while `fn(&'a i32)` + /// would return `true`. The latter can occur when traversing through the + /// former. + /// + /// See [`HasEscapingVarsVisitor`] for more information. fn has_escaping_bound_vars(&self) -> bool { self.has_vars_bound_at_or_above(ty::INNERMOST) } From 5610d22c8d58d12748308efb6e331e65618d11dc Mon Sep 17 00:00:00 2001 From: kraktus Date: Fri, 25 Nov 2022 16:36:22 +0100 Subject: [PATCH 072/244] Re-enable `uninlined_format_args` on multiline `format!` But do not display the code suggestion which can be sometimes completely broken (fortunately when applied it's valid) --- clippy_lints/src/format_args.rs | 19 ++++++++++------ tests/ui/uninlined_format_args.fixed | 11 +++------- tests/ui/uninlined_format_args.stderr | 31 ++++++++++++++++++++++++++- 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/clippy_lints/src/format_args.rs b/clippy_lints/src/format_args.rs index ec45be558f14b..fd3ce2f3d6cd6 100644 --- a/clippy_lints/src/format_args.rs +++ b/clippy_lints/src/format_args.rs @@ -9,7 +9,10 @@ use clippy_utils::source::snippet_opt; use clippy_utils::ty::{implements_trait, is_type_diagnostic_item}; use if_chain::if_chain; use itertools::Itertools; -use rustc_errors::Applicability; +use rustc_errors::{ + Applicability, + SuggestionStyle::{CompletelyHidden, ShowCode}, +}; use rustc_hir::{Expr, ExprKind, HirId, QPath}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::ty::adjustment::{Adjust, Adjustment}; @@ -286,10 +289,9 @@ fn check_uninlined_args(cx: &LateContext<'_>, args: &FormatArgsExpn<'_>, call_si return; } - // Temporarily ignore multiline spans: https://github.com/rust-lang/rust/pull/102729#discussion_r988704308 - if fixes.iter().any(|(span, _)| cx.sess().source_map().is_multiline(*span)) { - return; - } + // multiline span display suggestion is sometimes broken: https://github.com/rust-lang/rust/pull/102729#discussion_r988704308 + // in those cases, make the code suggestion hidden + let multiline_fix = fixes.iter().any(|(span, _)| cx.sess().source_map().is_multiline(*span)); span_lint_and_then( cx, @@ -297,7 +299,12 @@ fn check_uninlined_args(cx: &LateContext<'_>, args: &FormatArgsExpn<'_>, call_si call_site, "variables can be used directly in the `format!` string", |diag| { - diag.multipart_suggestion("change this to", fixes, Applicability::MachineApplicable); + diag.multipart_suggestion_with_style( + "change this to", + fixes, + Applicability::MachineApplicable, + if multiline_fix { CompletelyHidden } else { ShowCode }, + ); }, ); } diff --git a/tests/ui/uninlined_format_args.fixed b/tests/ui/uninlined_format_args.fixed index 106274479751d..ca56c95c23f40 100644 --- a/tests/ui/uninlined_format_args.fixed +++ b/tests/ui/uninlined_format_args.fixed @@ -44,9 +44,7 @@ fn tester(fn_arg: i32) { println!("val='{local_i32}'"); // space+tab println!("val='{local_i32}'"); // tab+space println!( - "val='{ - }'", - local_i32 + "val='{local_i32}'" ); println!("{local_i32}"); println!("{fn_arg}"); @@ -110,8 +108,7 @@ fn tester(fn_arg: i32) { println!("{local_f64:width$.prec$}"); println!("{local_f64:width$.prec$} {local_f64} {width} {prec}"); println!( - "{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$}", - local_i32, width, prec, + "{local_i32:width$.prec$} {local_i32:prec$.width$} {width:local_i32$.prec$} {width:prec$.local_i32$} {prec:local_i32$.width$} {prec:width$.local_i32$}", ); println!( "{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$} {3}", @@ -142,9 +139,7 @@ fn tester(fn_arg: i32) { println!(no_param_str!(), local_i32); println!( - "{}", - // comment with a comma , in it - val, + "{val}", ); println!("{val}"); diff --git a/tests/ui/uninlined_format_args.stderr b/tests/ui/uninlined_format_args.stderr index 2ce3b7fa960c6..1182d57ce9b7e 100644 --- a/tests/ui/uninlined_format_args.stderr +++ b/tests/ui/uninlined_format_args.stderr @@ -59,6 +59,16 @@ LL - println!("val='{ }'", local_i32); // tab+space LL + println!("val='{local_i32}'"); // tab+space | +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:46:5 + | +LL | / println!( +LL | | "val='{ +LL | | }'", +LL | | local_i32 +LL | | ); + | |_____^ + error: variables can be used directly in the `format!` string --> $DIR/uninlined_format_args.rs:51:5 | @@ -767,6 +777,15 @@ LL - println!("{:1$.2$} {0} {1} {2}", local_f64, width, prec); LL + println!("{local_f64:width$.prec$} {local_f64} {width} {prec}"); | +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:112:5 + | +LL | / println!( +LL | | "{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$}", +LL | | local_i32, width, prec, +LL | | ); + | |_____^ + error: variables can be used directly in the `format!` string --> $DIR/uninlined_format_args.rs:123:5 | @@ -815,6 +834,16 @@ LL - println!("{}", format!("{}", local_i32)); LL + println!("{}", format!("{local_i32}")); | +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:144:5 + | +LL | / println!( +LL | | "{}", +LL | | // comment with a comma , in it +LL | | val, +LL | | ); + | |_____^ + error: variables can be used directly in the `format!` string --> $DIR/uninlined_format_args.rs:149:5 | @@ -875,5 +904,5 @@ LL - println!("expand='{}'", local_i32); LL + println!("expand='{local_i32}'"); | -error: aborting due to 73 previous errors +error: aborting due to 76 previous errors From a215b7b4dfef1d8f4161416838a0cd6abb348216 Mon Sep 17 00:00:00 2001 From: lcnr Date: Fri, 25 Nov 2022 16:21:42 +0100 Subject: [PATCH 073/244] remove `TypeError::ObjectUnsafeCoercion` --- compiler/rustc_hir_typeck/src/cast.rs | 12 ---- compiler/rustc_hir_typeck/src/coercion.rs | 4 -- .../src/infer/error_reporting/mod.rs | 62 ++++++++----------- compiler/rustc_middle/src/ty/error.rs | 5 +- 4 files changed, 27 insertions(+), 56 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/cast.rs b/compiler/rustc_hir_typeck/src/cast.rs index 5e1e44dcb6d15..6f8743ed32ab6 100644 --- a/compiler/rustc_hir_typeck/src/cast.rs +++ b/compiler/rustc_hir_typeck/src/cast.rs @@ -46,7 +46,6 @@ use rustc_span::def_id::{DefId, LOCAL_CRATE}; use rustc_span::symbol::sym; use rustc_span::Span; use rustc_trait_selection::infer::InferCtxtExt; -use rustc_trait_selection::traits::error_reporting::report_object_safety_error; /// Reifies a cast check to be checked once we have full type information for /// a function context. @@ -727,9 +726,6 @@ impl<'a, 'tcx> CastCheck<'tcx> { debug!(" -> CoercionCast"); fcx.typeck_results.borrow_mut().set_coercion_cast(self.expr.hir_id.local_id); } - Err(ty::error::TypeError::ObjectUnsafeCoercion(did)) => { - self.report_object_unsafe_cast(&fcx, did); - } Err(_) => { match self.do_check(fcx) { Ok(k) => { @@ -741,14 +737,6 @@ impl<'a, 'tcx> CastCheck<'tcx> { }; } } - - fn report_object_unsafe_cast(&self, fcx: &FnCtxt<'a, 'tcx>, did: DefId) { - let violations = fcx.tcx.object_safety_violations(did); - let mut err = report_object_safety_error(fcx.tcx, self.cast_span, did, violations); - err.note(&format!("required by cast to type '{}'", fcx.ty_to_string(self.cast_ty))); - err.emit(); - } - /// Checks a cast, and report an error if one exists. In some cases, this /// can return Ok and create type errors in the fcx rather than returning /// directly. coercion-cast is handled in check instead of here. diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index f6377f9f49403..121dd4a1be1ca 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -199,10 +199,6 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { debug!("coerce: unsize successful"); return unsize; } - Err(TypeError::ObjectUnsafeCoercion(did)) => { - debug!("coerce: unsize not object safe"); - return Err(TypeError::ObjectUnsafeCoercion(did)); - } Err(error) => { debug!(?error, "coerce: unsize failed"); } diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 0dee3be705474..5c0a460e5d637 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -1677,40 +1677,34 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { } }; - match terr { - // Ignore msg for object safe coercion - // since E0038 message will be printed - TypeError::ObjectUnsafeCoercion(_) => {} - _ => { - let mut label_or_note = |span: Span, msg: &str| { - if (prefer_label && is_simple_error) || &[span] == diag.span.primary_spans() { - diag.span_label(span, msg); - } else { - diag.span_note(span, msg); - } - }; - if let Some((sp, msg)) = secondary_span { - if swap_secondary_and_primary { - let terr = if let Some(infer::ValuePairs::Terms(infer::ExpectedFound { - expected, - .. - })) = values - { - format!("expected this to be `{}`", expected) - } else { - terr.to_string() - }; - label_or_note(sp, &terr); - label_or_note(span, &msg); - } else { - label_or_note(span, &terr.to_string()); - label_or_note(sp, &msg); - } - } else { - label_or_note(span, &terr.to_string()); - } + let mut label_or_note = |span: Span, msg: &str| { + if (prefer_label && is_simple_error) || &[span] == diag.span.primary_spans() { + diag.span_label(span, msg); + } else { + diag.span_note(span, msg); } }; + if let Some((sp, msg)) = secondary_span { + if swap_secondary_and_primary { + let terr = if let Some(infer::ValuePairs::Terms(infer::ExpectedFound { + expected, + .. + })) = values + { + format!("expected this to be `{}`", expected) + } else { + terr.to_string() + }; + label_or_note(sp, &terr); + label_or_note(span, &msg); + } else { + label_or_note(span, &terr.to_string()); + label_or_note(sp, &msg); + } + } else { + label_or_note(span, &terr.to_string()); + } + if let Some((expected, found)) = expected_found { let (expected_label, found_label, exp_found) = match exp_found { Mismatch::Variable(ef) => ( @@ -1880,9 +1874,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { ); } } - TypeError::ObjectUnsafeCoercion(_) => { - diag.note_unsuccessful_coercion(found, expected); - } _ => { debug!( "note_type_err: exp_found={:?}, expected={:?} found={:?}", @@ -3127,7 +3118,6 @@ impl<'tcx> ObligationCauseExt<'tcx> for ObligationCause<'tcx> { TypeError::IntrinsicCast => { Error0308("cannot coerce intrinsics to function pointers") } - TypeError::ObjectUnsafeCoercion(did) => Error0038(did), _ => Error0308("mismatched types"), }, } diff --git a/compiler/rustc_middle/src/ty/error.rs b/compiler/rustc_middle/src/ty/error.rs index a61f41b9c580f..d83e17574a094 100644 --- a/compiler/rustc_middle/src/ty/error.rs +++ b/compiler/rustc_middle/src/ty/error.rs @@ -70,7 +70,6 @@ pub enum TypeError<'tcx> { CyclicConst(ty::Const<'tcx>), ProjectionMismatched(ExpectedFound), ExistentialMismatch(ExpectedFound<&'tcx ty::List>>), - ObjectUnsafeCoercion(DefId), ConstMismatch(ExpectedFound>), IntrinsicCast, @@ -222,7 +221,6 @@ impl<'tcx> fmt::Display for TypeError<'tcx> { f, "cannot coerce functions with `#[target_feature]` to safe function pointers" ), - ObjectUnsafeCoercion(_) => write!(f, "coercion to object-unsafe trait object"), } } } @@ -249,8 +247,7 @@ impl<'tcx> TypeError<'tcx> { | ProjectionMismatched(_) | ExistentialMismatch(_) | ConstMismatch(_) - | IntrinsicCast - | ObjectUnsafeCoercion(_) => true, + | IntrinsicCast => true, } } } From 4f07008419667eb7d9feea5ec391eda19d6d9338 Mon Sep 17 00:00:00 2001 From: lcnr Date: Fri, 25 Nov 2022 16:35:10 +0100 Subject: [PATCH 074/244] remove confusing comment `?0: CoerceUnsized` can definitely apply because `?0` matches any type, same for `SomeTy: CoerceUnsized` --- compiler/rustc_hir_typeck/src/coercion.rs | 24 +++-------------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index 121dd4a1be1ca..e949f7ec34f8a 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -503,27 +503,9 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { target = self.shallow_resolve(target); debug!(?source, ?target); - // These 'if' statements require some explanation. - // The `CoerceUnsized` trait is special - it is only - // possible to write `impl CoerceUnsized for A` where - // A and B have 'matching' fields. This rules out the following - // two types of blanket impls: - // - // `impl CoerceUnsized for SomeType` - // `impl CoerceUnsized for T` - // - // Both of these trigger a special `CoerceUnsized`-related error (E0376) - // - // We can take advantage of this fact to avoid performing unnecessary work. - // If either `source` or `target` is a type variable, then any applicable impl - // would need to be generic over the self-type (`impl CoerceUnsized for T`) - // or generic over the `CoerceUnsized` type parameter (`impl CoerceUnsized for - // SomeType`). - // - // However, these are exactly the kinds of impls which are forbidden by - // the compiler! Therefore, we can be sure that coercion will always fail - // when either the source or target type is a type variable. This allows us - // to skip performing any trait selection, and immediately bail out. + // We don't apply any coercions incase either the source or target + // aren't sufficiently well known but tend to instead just equate + // them both. if source.is_ty_var() { debug!("coerce_unsized: source is a TyVar, bailing out"); return Err(TypeError::Mismatch); From 2fd10bc59b2c4c39691a1a9ec9de318a01cbf60c Mon Sep 17 00:00:00 2001 From: kraktus Date: Fri, 25 Nov 2022 16:41:08 +0100 Subject: [PATCH 075/244] dogfood with expanded `uninlined_format_args` --- clippy_utils/src/ty.rs | 15 +++++---------- lintcheck/src/main.rs | 15 +++++++-------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index 897edfc5495f4..09967f317f891 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -1005,14 +1005,12 @@ pub fn make_projection<'tcx>( debug_assert!( generic_count == substs.len(), - "wrong number of substs for `{:?}`: found `{}` expected `{}`.\n\ + "wrong number of substs for `{:?}`: found `{}` expected `{generic_count}`.\n\ note: the expected parameters are: {:#?}\n\ - the given arguments are: `{:#?}`", + the given arguments are: `{substs:#?}`", assoc_item.def_id, substs.len(), - generic_count, params.map(GenericParamDefKind::descr).collect::>(), - substs, ); if let Some((idx, (param, arg))) = params @@ -1030,14 +1028,11 @@ pub fn make_projection<'tcx>( { debug_assert!( false, - "mismatched subst type at index {}: expected a {}, found `{:?}`\n\ + "mismatched subst type at index {idx}: expected a {}, found `{arg:?}`\n\ note: the expected parameters are {:#?}\n\ - the given arguments are {:#?}", - idx, + the given arguments are {substs:#?}", param.descr(), - arg, - params.map(GenericParamDefKind::descr).collect::>(), - substs, + params.map(GenericParamDefKind::descr).collect::>() ); } } diff --git a/lintcheck/src/main.rs b/lintcheck/src/main.rs index ee8ab7c1d7cbb..bd49f09607263 100644 --- a/lintcheck/src/main.rs +++ b/lintcheck/src/main.rs @@ -120,8 +120,8 @@ impl ClippyWarning { format!("$CARGO_HOME/{}", stripped.display()) } else { format!( - "target/lintcheck/sources/{}-{}/{}", - crate_name, crate_version, span.file_name + "target/lintcheck/sources/{crate_name}-{crate_version}/{}", + span.file_name ) }; @@ -322,13 +322,13 @@ impl Crate { if config.max_jobs == 1 { println!( - "{}/{} {}% Linting {} {}", - index, total_crates_to_lint, perc, &self.name, &self.version + "{index}/{total_crates_to_lint} {perc}% Linting {} {}", + &self.name, &self.version ); } else { println!( - "{}/{} {}% Linting {} {} in target dir {:?}", - index, total_crates_to_lint, perc, &self.name, &self.version, thread_index + "{index}/{total_crates_to_lint} {perc}% Linting {} {} in target dir {thread_index:?}", + &self.name, &self.version ); } @@ -398,8 +398,7 @@ impl Crate { .output() .unwrap_or_else(|error| { panic!( - "Encountered error:\n{:?}\ncargo_clippy_path: {}\ncrate path:{}\n", - error, + "Encountered error:\n{error:?}\ncargo_clippy_path: {}\ncrate path:{}\n", &cargo_clippy_path.display(), &self.path.display() ); From 8582f9644b01bb7d395dd6f206fca17145d6bed7 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 14 Mar 2022 16:54:31 +0000 Subject: [PATCH 076/244] Cache Predicates' hash within themselves --- compiler/rustc_middle/src/arena.rs | 2 +- compiler/rustc_middle/src/ty/context.rs | 77 +++++++++++++++++-------- compiler/rustc_middle/src/ty/mod.rs | 8 +-- 3 files changed, 59 insertions(+), 28 deletions(-) diff --git a/compiler/rustc_middle/src/arena.rs b/compiler/rustc_middle/src/arena.rs index 7bd4b6c0c2767..e83106b1ee515 100644 --- a/compiler/rustc_middle/src/arena.rs +++ b/compiler/rustc_middle/src/arena.rs @@ -89,7 +89,7 @@ macro_rules! arena_types { // Interned types [] tys: rustc_data_structures::intern::WithStableHash>, - [] predicates: rustc_middle::ty::PredicateS<'tcx>, + [] predicates: rustc_data_structures::intern::WithStableHash>, [] consts: rustc_middle::ty::ConstS<'tcx>, // Note that this deliberately duplicates items in the `rustc_hir::arena`, diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index b5327ad0cecc6..b9deaed94088d 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -141,7 +141,7 @@ pub struct CtxtInterners<'tcx> { canonical_var_infos: InternedSet<'tcx, List>>, region: InternedSet<'tcx, RegionKind<'tcx>>, poly_existential_predicates: InternedSet<'tcx, List>>, - predicate: InternedSet<'tcx, PredicateS<'tcx>>, + predicate: InternedSet<'tcx, WithStableHash>>, predicates: InternedSet<'tcx, List>>, projs: InternedSet<'tcx, List>, place_elems: InternedSet<'tcx, List>>, @@ -188,20 +188,8 @@ impl<'tcx> CtxtInterners<'tcx> { self.type_ .intern(kind, |kind| { let flags = super::flags::FlagComputation::for_kind(&kind); - - // It's impossible to hash inference variables (and will ICE), so we don't need to try to cache them. - // Without incremental, we rarely stable-hash types, so let's not do it proactively. - let stable_hash = if flags.flags.intersects(TypeFlags::NEEDS_INFER) - || sess.opts.incremental.is_none() - { - Fingerprint::ZERO - } else { - let mut hasher = StableHasher::new(); - let mut hcx = - StableHashingContext::new(sess, definitions, cstore, source_span); - kind.hash_stable(&mut hcx, &mut hasher); - hasher.finish() - }; + let stable_hash = + self.stable_hash(&flags, sess, definitions, cstore, source_span, &kind); let ty_struct = TyS { kind, @@ -217,20 +205,54 @@ impl<'tcx> CtxtInterners<'tcx> { )) } + fn stable_hash<'a, T: HashStable>>( + &self, + flags: &ty::flags::FlagComputation, + sess: &'a Session, + definitions: &'a rustc_hir::definitions::Definitions, + cstore: &'a CrateStoreDyn, + source_span: &'a IndexVec, + val: &T, + ) -> Fingerprint { + // It's impossible to hash inference variables (and will ICE), so we don't need to try to cache them. + // Without incremental, we rarely stable-hash types, so let's not do it proactively. + if flags.flags.intersects(TypeFlags::NEEDS_INFER) || sess.opts.incremental.is_none() { + Fingerprint::ZERO + } else { + let mut hasher = StableHasher::new(); + let mut hcx = StableHashingContext::new(sess, definitions, cstore, source_span); + val.hash_stable(&mut hcx, &mut hasher); + hasher.finish() + } + } + #[inline(never)] - fn intern_predicate(&self, kind: Binder<'tcx, PredicateKind<'tcx>>) -> Predicate<'tcx> { + fn intern_predicate( + &self, + kind: Binder<'tcx, PredicateKind<'tcx>>, + sess: &Session, + definitions: &rustc_hir::definitions::Definitions, + cstore: &CrateStoreDyn, + source_span: &IndexVec, + ) -> Predicate<'tcx> { Predicate(Interned::new_unchecked( self.predicate .intern(kind, |kind| { let flags = super::flags::FlagComputation::for_predicate(kind); + let stable_hash = + self.stable_hash(&flags, sess, definitions, cstore, source_span, &kind); + let predicate_struct = PredicateS { kind, flags: flags.flags, outer_exclusive_binder: flags.outer_exclusive_binder, }; - InternedInSet(self.arena.alloc(predicate_struct)) + InternedInSet( + self.arena + .alloc(WithStableHash { internee: predicate_struct, stable_hash }), + ) }) .0, )) @@ -2158,23 +2180,25 @@ impl<'tcx> Hash for InternedInSet<'tcx, WithStableHash>> { } } -impl<'tcx> Borrow>> for InternedInSet<'tcx, PredicateS<'tcx>> { +impl<'tcx> Borrow>> + for InternedInSet<'tcx, WithStableHash>> +{ fn borrow<'a>(&'a self) -> &'a Binder<'tcx, PredicateKind<'tcx>> { &self.0.kind } } -impl<'tcx> PartialEq for InternedInSet<'tcx, PredicateS<'tcx>> { - fn eq(&self, other: &InternedInSet<'tcx, PredicateS<'tcx>>) -> bool { +impl<'tcx> PartialEq for InternedInSet<'tcx, WithStableHash>> { + fn eq(&self, other: &InternedInSet<'tcx, WithStableHash>>) -> bool { // The `Borrow` trait requires that `x.borrow() == y.borrow()` equals // `x == y`. self.0.kind == other.0.kind } } -impl<'tcx> Eq for InternedInSet<'tcx, PredicateS<'tcx>> {} +impl<'tcx> Eq for InternedInSet<'tcx, WithStableHash>> {} -impl<'tcx> Hash for InternedInSet<'tcx, PredicateS<'tcx>> { +impl<'tcx> Hash for InternedInSet<'tcx, WithStableHash>> { fn hash(&self, s: &mut H) { // The `Borrow` trait requires that `x.borrow().hash(s) == x.hash(s)`. self.0.kind.hash(s) @@ -2373,7 +2397,14 @@ impl<'tcx> TyCtxt<'tcx> { #[inline] pub fn mk_predicate(self, binder: Binder<'tcx, PredicateKind<'tcx>>) -> Predicate<'tcx> { - self.interners.intern_predicate(binder) + self.interners.intern_predicate( + binder, + self.sess, + &self.definitions.read(), + &*self.untracked_resolutions.cstore, + // This is only used to create a stable hashing context. + &self.untracked_resolutions.source_span, + ) } #[inline] diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 9d778ff2fb6e3..a4c67ad0f3d4d 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -546,9 +546,9 @@ pub(crate) struct PredicateS<'tcx> { } /// Use this rather than `PredicateS`, whenever possible. -#[derive(Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Clone, Copy, PartialEq, Eq, Hash, HashStable)] #[rustc_pass_by_value] -pub struct Predicate<'tcx>(Interned<'tcx, PredicateS<'tcx>>); +pub struct Predicate<'tcx>(Interned<'tcx, WithStableHash>>); impl<'tcx> Predicate<'tcx> { /// Gets the inner `Binder<'tcx, PredicateKind<'tcx>>`. @@ -627,7 +627,7 @@ impl<'tcx> Predicate<'tcx> { } } -impl<'a, 'tcx> HashStable> for Predicate<'tcx> { +impl<'a, 'tcx> HashStable> for PredicateS<'tcx> { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { let PredicateS { ref kind, @@ -636,7 +636,7 @@ impl<'a, 'tcx> HashStable> for Predicate<'tcx> { // also contained in `kind`, so no need to hash them. flags: _, outer_exclusive_binder: _, - } = self.0.0; + } = self; kind.hash_stable(hcx, hasher); } From 6436c348dbaa0eb8f2853dfb64cbf8b30f5119f2 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 25 Nov 2022 23:31:28 +0000 Subject: [PATCH 077/244] Remove SelectionContext::infcx() in favor of field access --- .../src/traits/auto_trait.rs | 31 +++++------ .../src/traits/coherence.rs | 14 ++--- .../src/traits/fulfill.rs | 31 ++++++----- .../src/traits/project.rs | 54 +++++++++---------- .../src/traits/select/candidate_assembly.rs | 10 ++-- .../src/traits/select/confirmation.rs | 4 +- .../src/traits/select/mod.rs | 19 +++---- .../rustc_trait_selection/src/traits/util.rs | 4 +- 8 files changed, 75 insertions(+), 92 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/auto_trait.rs b/compiler/rustc_trait_selection/src/traits/auto_trait.rs index 52552ff3f8674..3bc248bb0b274 100644 --- a/compiler/rustc_trait_selection/src/traits/auto_trait.rs +++ b/compiler/rustc_trait_selection/src/traits/auto_trait.rs @@ -599,17 +599,17 @@ impl<'tcx> AutoTraitFinder<'tcx> { computed_preds: &mut FxIndexSet>, fresh_preds: &mut FxHashSet>, predicates: &mut VecDeque>, - select: &mut SelectionContext<'_, 'tcx>, + selcx: &mut SelectionContext<'_, 'tcx>, only_projections: bool, ) -> bool { let dummy_cause = ObligationCause::dummy(); for obligation in nested { let is_new_pred = - fresh_preds.insert(self.clean_pred(select.infcx(), obligation.predicate)); + fresh_preds.insert(self.clean_pred(selcx.infcx, obligation.predicate)); // Resolve any inference variables that we can, to help selection succeed - let predicate = select.infcx().resolve_vars_if_possible(obligation.predicate); + let predicate = selcx.infcx.resolve_vars_if_possible(obligation.predicate); // We only add a predicate as a user-displayable bound if // it involves a generic parameter, and doesn't contain @@ -717,10 +717,8 @@ impl<'tcx> AutoTraitFinder<'tcx> { // and turn them into an explicit negative impl for our type. debug!("Projecting and unifying projection predicate {:?}", predicate); - match project::poly_project_and_unify_type( - select, - &obligation.with(self.tcx, p), - ) { + match project::poly_project_and_unify_type(selcx, &obligation.with(self.tcx, p)) + { ProjectAndUnifyResult::MismatchedProjectionTypes(e) => { debug!( "evaluate_nested_obligations: Unable to unify predicate \ @@ -745,7 +743,7 @@ impl<'tcx> AutoTraitFinder<'tcx> { computed_preds, fresh_preds, predicates, - select, + selcx, only_projections, ) { return false; @@ -768,7 +766,7 @@ impl<'tcx> AutoTraitFinder<'tcx> { } ty::PredicateKind::Clause(ty::Clause::RegionOutlives(binder)) => { let binder = bound_predicate.rebind(binder); - select.infcx().region_outlives_predicate(&dummy_cause, binder) + selcx.infcx.region_outlives_predicate(&dummy_cause, binder) } ty::PredicateKind::Clause(ty::Clause::TypeOutlives(binder)) => { let binder = bound_predicate.rebind(binder); @@ -777,14 +775,14 @@ impl<'tcx> AutoTraitFinder<'tcx> { binder.map_bound_ref(|pred| pred.0).no_bound_vars(), ) { (None, Some(t_a)) => { - select.infcx().register_region_obligation_with_cause( + selcx.infcx.register_region_obligation_with_cause( t_a, - select.infcx().tcx.lifetimes.re_static, + selcx.infcx.tcx.lifetimes.re_static, &dummy_cause, ); } (Some(ty::OutlivesPredicate(t_a, r_b)), _) => { - select.infcx().register_region_obligation_with_cause( + selcx.infcx.register_region_obligation_with_cause( t_a, r_b, &dummy_cause, @@ -796,13 +794,13 @@ impl<'tcx> AutoTraitFinder<'tcx> { ty::PredicateKind::ConstEquate(c1, c2) => { let evaluate = |c: ty::Const<'tcx>| { if let ty::ConstKind::Unevaluated(unevaluated) = c.kind() { - match select.infcx().const_eval_resolve( + match selcx.infcx.const_eval_resolve( obligation.param_env, unevaluated, Some(obligation.cause.span), ) { Ok(Some(valtree)) => { - Ok(ty::Const::from_value(select.tcx(), valtree, c.ty())) + Ok(ty::Const::from_value(selcx.tcx(), valtree, c.ty())) } Ok(None) => { let tcx = self.tcx; @@ -823,10 +821,7 @@ impl<'tcx> AutoTraitFinder<'tcx> { match (evaluate(c1), evaluate(c2)) { (Ok(c1), Ok(c2)) => { - match select - .infcx() - .at(&obligation.cause, obligation.param_env) - .eq(c1, c2) + match selcx.infcx.at(&obligation.cause, obligation.param_env).eq(c1, c2) { Ok(_) => (), Err(_) => return false, diff --git a/compiler/rustc_trait_selection/src/traits/coherence.rs b/compiler/rustc_trait_selection/src/traits/coherence.rs index 741bf206d037e..99724fb28db11 100644 --- a/compiler/rustc_trait_selection/src/traits/coherence.rs +++ b/compiler/rustc_trait_selection/src/traits/coherence.rs @@ -119,7 +119,7 @@ fn with_fresh_ty_vars<'cx, 'tcx>( impl_def_id: DefId, ) -> ty::ImplHeader<'tcx> { let tcx = selcx.tcx(); - let impl_substs = selcx.infcx().fresh_substs_for_item(DUMMY_SP, impl_def_id); + let impl_substs = selcx.infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id); let header = ty::ImplHeader { impl_def_id, @@ -149,7 +149,7 @@ fn overlap<'cx, 'tcx>( impl1_def_id, impl2_def_id, overlap_mode ); - selcx.infcx().probe_maybe_skip_leak_check(skip_leak_check.is_yes(), |snapshot| { + selcx.infcx.probe_maybe_skip_leak_check(skip_leak_check.is_yes(), |snapshot| { overlap_within_probe(selcx, impl1_def_id, impl2_def_id, overlap_mode, snapshot) }) } @@ -161,7 +161,7 @@ fn overlap_within_probe<'cx, 'tcx>( overlap_mode: OverlapMode, snapshot: &CombinedSnapshot<'tcx>, ) -> Option> { - let infcx = selcx.infcx(); + let infcx = selcx.infcx; if overlap_mode.use_negative_impl() { if negative_impl(infcx.tcx, impl1_def_id, impl2_def_id) @@ -200,9 +200,9 @@ fn overlap_within_probe<'cx, 'tcx>( debug!("overlap: intercrate_ambiguity_causes={:#?}", intercrate_ambiguity_causes); let involves_placeholder = - matches!(selcx.infcx().region_constraints_added_in_snapshot(snapshot), Some(true)); + matches!(selcx.infcx.region_constraints_added_in_snapshot(snapshot), Some(true)); - let impl_header = selcx.infcx().resolve_vars_if_possible(impl1_header); + let impl_header = selcx.infcx.resolve_vars_if_possible(impl1_header); Some(OverlapResult { impl_header, intercrate_ambiguity_causes, involves_placeholder }) } @@ -214,7 +214,7 @@ fn equate_impl_headers<'cx, 'tcx>( // Do `a` and `b` unify? If not, no overlap. debug!("equate_impl_headers(impl1_header={:?}, impl2_header={:?}", impl1_header, impl2_header); selcx - .infcx() + .infcx .at(&ObligationCause::dummy(), ty::ParamEnv::empty()) .eq_impl_headers(impl1_header, impl2_header) .map(|infer_ok| infer_ok.obligations) @@ -255,7 +255,7 @@ fn implicit_negative<'cx, 'tcx>( "implicit_negative(impl1_header={:?}, impl2_header={:?}, obligations={:?})", impl1_header, impl2_header, obligations ); - let infcx = selcx.infcx(); + let infcx = selcx.infcx; let opt_failing_obligation = impl1_header .predicates .iter() diff --git a/compiler/rustc_trait_selection/src/traits/fulfill.rs b/compiler/rustc_trait_selection/src/traits/fulfill.rs index b0d0d1a7cba44..19c384671b3b9 100644 --- a/compiler/rustc_trait_selection/src/traits/fulfill.rs +++ b/compiler/rustc_trait_selection/src/traits/fulfill.rs @@ -199,7 +199,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> { // code is so hot. 1 and 0 dominate; 2+ is fairly rare. 1 => { let infer_var = pending_obligation.stalled_on[0]; - self.selcx.infcx().ty_or_const_infer_var_changed(infer_var) + self.selcx.infcx.ty_or_const_infer_var_changed(infer_var) } 0 => { // In this case we haven't changed, but wish to make a change. @@ -210,7 +210,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> { // form was a perf win. See #64545 for details. (|| { for &infer_var in &pending_obligation.stalled_on { - if self.selcx.infcx().ty_or_const_infer_var_changed(infer_var) { + if self.selcx.infcx.ty_or_const_infer_var_changed(infer_var) { return true; } } @@ -240,13 +240,12 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> { debug!(?obligation, "pre-resolve"); if obligation.predicate.has_non_region_infer() { - obligation.predicate = - self.selcx.infcx().resolve_vars_if_possible(obligation.predicate); + obligation.predicate = self.selcx.infcx.resolve_vars_if_possible(obligation.predicate); } let obligation = &pending_obligation.obligation; - let infcx = self.selcx.infcx(); + let infcx = self.selcx.infcx; if obligation.predicate.has_projections() { let mut obligations = Vec::new(); @@ -353,7 +352,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> { } ty::PredicateKind::ClosureKind(_, closure_substs, kind) => { - match self.selcx.infcx().closure_kind(closure_substs) { + match self.selcx.infcx.closure_kind(closure_substs) { Some(closure_kind) => { if closure_kind.extends(kind) { ProcessResult::Changed(vec![]) @@ -367,7 +366,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> { ty::PredicateKind::WellFormed(arg) => { match wf::obligations( - self.selcx.infcx(), + self.selcx.infcx, obligation.param_env, obligation.cause.body_id, obligation.recursion_depth + 1, @@ -384,7 +383,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> { } ty::PredicateKind::Subtype(subtype) => { - match self.selcx.infcx().subtype_predicate( + match self.selcx.infcx.subtype_predicate( &obligation.cause, obligation.param_env, Binder::dummy(subtype), @@ -408,7 +407,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> { } ty::PredicateKind::Coerce(coerce) => { - match self.selcx.infcx().coerce_predicate( + match self.selcx.infcx.coerce_predicate( &obligation.cause, obligation.param_env, Binder::dummy(coerce), @@ -432,7 +431,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> { ty::PredicateKind::ConstEvaluatable(uv) => { match const_evaluatable::is_const_evaluatable( - self.selcx.infcx(), + self.selcx.infcx, uv, obligation.param_env, obligation.cause.span, @@ -476,7 +475,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> { let mut evaluate = |c: Const<'tcx>| { if let ty::ConstKind::Unevaluated(unevaluated) = c.kind() { - match self.selcx.infcx().try_const_eval_resolve( + match self.selcx.infcx.try_const_eval_resolve( obligation.param_env, unevaluated, c.ty(), @@ -504,7 +503,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> { (Ok(c1), Ok(c2)) => { match self .selcx - .infcx() + .infcx .at(&obligation.cause, obligation.param_env) .eq(c1, c2) { @@ -572,7 +571,7 @@ impl<'a, 'tcx> FulfillProcessor<'a, 'tcx> { trait_obligation: TraitObligation<'tcx>, stalled_on: &mut Vec>, ) -> ProcessResult, FulfillmentErrorCode<'tcx>> { - let infcx = self.selcx.infcx(); + let infcx = self.selcx.infcx; if obligation.predicate.is_global() { // no type variables present, can use evaluation for better caching. // FIXME: consider caching errors too. @@ -630,7 +629,7 @@ impl<'a, 'tcx> FulfillProcessor<'a, 'tcx> { if obligation.predicate.is_global() { // no type variables present, can use evaluation for better caching. // FIXME: consider caching errors too. - if self.selcx.infcx().predicate_must_hold_considering_regions(obligation) { + if self.selcx.infcx.predicate_must_hold_considering_regions(obligation) { if let Some(key) = ProjectionCacheKey::from_poly_projection_predicate( &mut self.selcx, project_obligation.predicate, @@ -639,7 +638,7 @@ impl<'a, 'tcx> FulfillProcessor<'a, 'tcx> { // evaluated all sub-obligations. We can therefore mark the 'root' // obligation as complete, and skip evaluating sub-obligations. self.selcx - .infcx() + .infcx .inner .borrow_mut() .projection_cache() @@ -678,7 +677,7 @@ fn substs_infer_vars<'a, 'tcx>( substs: ty::Binder<'tcx, SubstsRef<'tcx>>, ) -> impl Iterator> { selcx - .infcx() + .infcx .resolve_vars_if_possible(substs) .skip_binder() // ok because this check doesn't care about regions .iter() diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index ae6fa841856cb..f358687402f47 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -194,7 +194,7 @@ pub(super) fn poly_project_and_unify_type<'cx, 'tcx>( selcx: &mut SelectionContext<'cx, 'tcx>, obligation: &PolyProjectionObligation<'tcx>, ) -> ProjectAndUnifyResult<'tcx> { - let infcx = selcx.infcx(); + let infcx = selcx.infcx; let r = infcx.commit_if_ok(|_snapshot| { let old_universe = infcx.universe(); let placeholder_predicate = @@ -250,7 +250,7 @@ fn project_and_unify_type<'cx, 'tcx>( ) -> ProjectAndUnifyResult<'tcx> { let mut obligations = vec![]; - let infcx = selcx.infcx(); + let infcx = selcx.infcx; let normalized = match opt_normalize_projection_type( selcx, obligation.param_env, @@ -269,7 +269,7 @@ fn project_and_unify_type<'cx, 'tcx>( // This allows users to omit re-mentioning all bounds on an associated type and just use an // `impl Trait` for the assoc type to add more bounds. let InferOk { value: actual, obligations: new } = - selcx.infcx().replace_opaque_types_with_inference_vars( + selcx.infcx.replace_opaque_types_with_inference_vars( actual, obligation.cause.body_id, obligation.cause.span, @@ -445,7 +445,7 @@ impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> { } fn fold>(&mut self, value: T) -> T { - let value = self.selcx.infcx().resolve_vars_if_possible(value); + let value = self.selcx.infcx.resolve_vars_if_possible(value); debug!(?value); assert!( @@ -524,7 +524,7 @@ impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> { self.param_env, ty, ); - self.selcx.infcx().err_ctxt().report_overflow_error(&obligation, true); + self.selcx.infcx.err_ctxt().report_overflow_error(&obligation, true); } let substs = substs.fold_with(self); @@ -590,7 +590,7 @@ impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> { // want to figure out how to register obligations with escaping vars // or handle this some other way. - let infcx = self.selcx.infcx(); + let infcx = self.selcx.infcx; let (data, mapped_regions, mapped_types, mapped_consts) = BoundVarReplacer::replace_bound_vars(infcx, &mut self.universes, data); let data = data.fold_with(self); @@ -640,7 +640,7 @@ impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> { let constant = constant.super_fold_with(self); debug!(?constant, ?self.param_env); with_replaced_escaping_bound_vars( - self.selcx.infcx(), + self.selcx.infcx, &mut self.universes, constant, |constant| constant.eval(tcx, self.param_env), @@ -992,10 +992,7 @@ pub fn normalize_projection_type<'a, 'b, 'tcx>( // and a deferred predicate to resolve this when more type // information is available. - selcx - .infcx() - .infer_projection(param_env, projection_ty, cause, depth + 1, obligations) - .into() + selcx.infcx.infer_projection(param_env, projection_ty, cause, depth + 1, obligations).into() }) } @@ -1018,7 +1015,7 @@ fn opt_normalize_projection_type<'a, 'b, 'tcx>( depth: usize, obligations: &mut Vec>, ) -> Result>, InProgress> { - let infcx = selcx.infcx(); + let infcx = selcx.infcx; // Don't use the projection cache in intercrate mode - // the `infcx` may be re-used between intercrate in non-intercrate // mode, which could lead to using incorrect cache results. @@ -1110,7 +1107,7 @@ fn opt_normalize_projection_type<'a, 'b, 'tcx>( // an impl, where-clause etc) and hence we must // re-normalize it - let projected_term = selcx.infcx().resolve_vars_if_possible(projected_term); + let projected_term = selcx.infcx.resolve_vars_if_possible(projected_term); let mut result = if projected_term.has_projections() { let mut normalizer = AssocTypeNormalizer::new( @@ -1206,9 +1203,9 @@ fn normalize_to_error<'a, 'tcx>( param_env, predicate: trait_ref.without_const().to_predicate(selcx.tcx()), }; - let tcx = selcx.infcx().tcx; + let tcx = selcx.infcx.tcx; let def_id = projection_ty.item_def_id; - let new_value = selcx.infcx().next_ty_var(TypeVariableOrigin { + let new_value = selcx.infcx.next_ty_var(TypeVariableOrigin { kind: TypeVariableOriginKind::NormalizeProjectionType, span: tcx.def_span(def_id), }); @@ -1330,7 +1327,7 @@ fn assemble_candidate_for_impl_trait_in_trait<'cx, 'tcx>( let trait_predicate = ty::Binder::dummy(ty::TraitRef { def_id: trait_def_id, substs: trait_substs }); - let _ = selcx.infcx().commit_if_ok(|_| { + let _ = selcx.infcx.commit_if_ok(|_| { match selcx.select(&obligation.with(tcx, trait_predicate)) { Ok(Some(super::ImplSource::UserDefined(data))) => { candidate_set.push_candidate(ProjectionCandidate::ImplTraitInTrait( @@ -1435,7 +1432,7 @@ fn assemble_candidates_from_object_ty<'cx, 'tcx>( let tcx = selcx.tcx(); let self_ty = obligation.predicate.self_ty(); - let object_ty = selcx.infcx().shallow_resolve(self_ty); + let object_ty = selcx.infcx.shallow_resolve(self_ty); let data = match object_ty.kind() { ty::Dynamic(data, ..) => data, ty::Infer(ty::TyVar(_)) => { @@ -1473,7 +1470,7 @@ fn assemble_candidates_from_predicates<'cx, 'tcx>( env_predicates: impl Iterator>, potentially_unnormalized_candidates: bool, ) { - let infcx = selcx.infcx(); + let infcx = selcx.infcx; for predicate in env_predicates { let bound_predicate = predicate.kind(); if let ty::PredicateKind::Clause(ty::Clause::Projection(data)) = @@ -1529,7 +1526,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>( // start out by selecting the predicate `T as TraitRef<...>`: let poly_trait_ref = ty::Binder::dummy(obligation.predicate.trait_ref(selcx.tcx())); let trait_obligation = obligation.with(selcx.tcx(), poly_trait_ref); - let _ = selcx.infcx().commit_if_ok(|_| { + let _ = selcx.infcx.commit_if_ok(|_| { let impl_source = match selcx.select(&trait_obligation) { Ok(Some(impl_source)) => impl_source, Ok(None) => { @@ -1587,7 +1584,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>( if obligation.param_env.reveal() == Reveal::All { // NOTE(eddyb) inference variables can resolve to parameters, so // assume `poly_trait_ref` isn't monomorphic, if it contains any. - let poly_trait_ref = selcx.infcx().resolve_vars_if_possible(poly_trait_ref); + let poly_trait_ref = selcx.infcx.resolve_vars_if_possible(poly_trait_ref); !poly_trait_ref.still_further_specializable() } else { debug!( @@ -1603,7 +1600,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>( // While a builtin impl may be known to exist, the associated type may not yet // be known. Any type with multiple potential associated types is therefore // not eligible. - let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty()); + let self_ty = selcx.infcx.shallow_resolve(obligation.predicate.self_ty()); let lang_items = selcx.tcx().lang_items(); if lang_items.discriminant_kind_trait() == Some(poly_trait_ref.def_id()) { @@ -1690,7 +1687,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>( // type parameters, opaques, and unnormalized projections have pointer // metadata if they're known (e.g. by the param_env) to be sized ty::Param(_) | ty::Projection(..) | ty::Opaque(..) - if selcx.infcx().predicate_must_hold_modulo_regions( + if selcx.infcx.predicate_must_hold_modulo_regions( &obligation.with( selcx.tcx(), ty::Binder::dummy( @@ -1818,8 +1815,7 @@ fn confirm_candidate<'cx, 'tcx>( // when possible for this to work. See `auto-trait-projection-recursion.rs` // for a case where this matters. if progress.term.has_infer_regions() { - progress.term = - progress.term.fold_with(&mut OpportunisticRegionResolver::new(selcx.infcx())); + progress.term = progress.term.fold_with(&mut OpportunisticRegionResolver::new(selcx.infcx)); } progress } @@ -2000,7 +1996,7 @@ fn confirm_fn_pointer_candidate<'cx, 'tcx>( obligation: &ProjectionTyObligation<'tcx>, fn_pointer_impl_source: ImplSourceFnPointerData<'tcx, PredicateObligation<'tcx>>, ) -> Progress<'tcx> { - let fn_type = selcx.infcx().shallow_resolve(fn_pointer_impl_source.fn_ty); + let fn_type = selcx.infcx.shallow_resolve(fn_pointer_impl_source.fn_ty); let sig = fn_type.fn_sig(selcx.tcx()); let Normalized { value: sig, obligations } = normalize_with_depth( selcx, @@ -2073,7 +2069,7 @@ fn confirm_param_env_candidate<'cx, 'tcx>( poly_cache_entry: ty::PolyProjectionPredicate<'tcx>, potentially_unnormalized_candidate: bool, ) -> Progress<'tcx> { - let infcx = selcx.infcx(); + let infcx = selcx.infcx; let cause = &obligation.cause; let param_env = obligation.param_env; @@ -2168,7 +2164,7 @@ fn confirm_impl_candidate<'cx, 'tcx>( // * `substs` ends up as `[u32, S]` let substs = obligation.predicate.substs.rebase_onto(tcx, trait_def_id, substs); let substs = - translate_substs(selcx.infcx(), param_env, impl_def_id, substs, assoc_ty.defining_node); + translate_substs(selcx.infcx, param_env, impl_def_id, substs, assoc_ty.defining_node); let ty = tcx.bound_type_of(assoc_ty.item.def_id); let is_const = matches!(tcx.def_kind(assoc_ty.item.def_id), DefKind::AssocConst); let term: ty::EarlyBinder> = if is_const { @@ -2264,7 +2260,7 @@ fn confirm_impl_trait_in_trait_candidate<'tcx>( let impl_fn_substs = obligation.predicate.substs.rebase_onto(tcx, tcx.parent(trait_fn_def_id), data.substs); let impl_fn_substs = translate_substs( - selcx.infcx(), + selcx.infcx, obligation.param_env, data.impl_def_id, impl_fn_substs, @@ -2424,7 +2420,7 @@ impl<'cx, 'tcx> ProjectionCacheKeyExt<'cx, 'tcx> for ProjectionCacheKey<'tcx> { selcx: &mut SelectionContext<'cx, 'tcx>, predicate: ty::PolyProjectionPredicate<'tcx>, ) -> Option { - let infcx = selcx.infcx(); + let infcx = selcx.infcx; // We don't do cross-snapshot caching of obligations with escaping regions, // so there's no cache key to use predicate.no_bound_vars().map(|predicate| { diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs index f8c7a896b530b..fa076765ad991 100644 --- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs +++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs @@ -238,7 +238,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { param_env: obligation.param_env, cause: obligation.cause.clone(), recursion_depth: obligation.recursion_depth, - predicate: self.infcx().resolve_vars_if_possible(obligation.predicate), + predicate: self.infcx.resolve_vars_if_possible(obligation.predicate), }; if obligation.predicate.skip_binder().self_ty().is_ty_var() { @@ -689,9 +689,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { debug!(?poly_trait_ref, "assemble_candidates_from_object_ty"); - let poly_trait_predicate = self.infcx().resolve_vars_if_possible(obligation.predicate); + let poly_trait_predicate = self.infcx.resolve_vars_if_possible(obligation.predicate); let placeholder_trait_predicate = - self.infcx().replace_bound_vars_with_placeholders(poly_trait_predicate); + self.infcx.replace_bound_vars_with_placeholders(poly_trait_predicate); // Count only those upcast versions that match the trait-ref // we are looking for. Specifically, do not only check for the @@ -940,7 +940,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { return; } - let self_ty = self.infcx().shallow_resolve(obligation.self_ty()); + let self_ty = self.infcx.shallow_resolve(obligation.self_ty()); match self_ty.skip_binder().kind() { ty::Opaque(..) | ty::Dynamic(..) @@ -1007,7 +1007,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { obligation: &TraitObligation<'tcx>, candidates: &mut SelectionCandidateSet<'tcx>, ) { - let self_ty = self.infcx().shallow_resolve(obligation.self_ty().skip_binder()); + let self_ty = self.infcx.shallow_resolve(obligation.self_ty().skip_binder()); match self_ty.kind() { ty::Tuple(_) => { candidates.vec.push(BuiltinCandidate { has_nested: false }); diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index 8c589aa8cd1de..86fd69c159e57 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -147,7 +147,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { let trait_predicate = self.infcx.shallow_resolve(obligation.predicate); let placeholder_trait_predicate = - self.infcx().replace_bound_vars_with_placeholders(trait_predicate).trait_ref; + self.infcx.replace_bound_vars_with_placeholders(trait_predicate).trait_ref; let placeholder_self_ty = placeholder_trait_predicate.self_ty(); let placeholder_trait_predicate = ty::Binder::dummy(placeholder_trait_predicate); let (def_id, substs) = match *placeholder_self_ty.kind() { @@ -639,7 +639,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { debug!(?obligation, "confirm_trait_alias_candidate"); let alias_def_id = obligation.predicate.def_id(); - let predicate = self.infcx().replace_bound_vars_with_placeholders(obligation.predicate); + let predicate = self.infcx.replace_bound_vars_with_placeholders(obligation.predicate); let trait_ref = predicate.trait_ref; let trait_def_id = trait_ref.def_id; let substs = trait_ref.substs; diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 3c8f8b5642c9e..7088016e6e6a5 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -101,7 +101,7 @@ impl IntercrateAmbiguityCause { } pub struct SelectionContext<'cx, 'tcx> { - infcx: &'cx InferCtxt<'tcx>, + pub infcx: &'cx InferCtxt<'tcx>, /// Freshener used specifically for entries on the obligation /// stack. This ensures that all entries on the stack at one time @@ -237,10 +237,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { self.intercrate_ambiguity_causes.take().unwrap_or_default() } - pub fn infcx(&self) -> &'cx InferCtxt<'tcx> { - self.infcx - } - pub fn tcx(&self) -> TyCtxt<'tcx> { self.infcx.tcx } @@ -693,10 +689,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { match (evaluate(c1), evaluate(c2)) { (Ok(c1), Ok(c2)) => { - match self - .infcx() - .at(&obligation.cause, obligation.param_env) - .eq(c1, c2) + match self.infcx.at(&obligation.cause, obligation.param_env).eq(c1, c2) { Ok(_) => Ok(EvaluatedToOk), Err(_) => Ok(EvaluatedToErr), @@ -1212,7 +1205,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } let obligation = &stack.obligation; - let predicate = self.infcx().resolve_vars_if_possible(obligation.predicate); + let predicate = self.infcx.resolve_vars_if_possible(obligation.predicate); // Okay to skip binder because of the nature of the // trait-ref-is-knowable check, which does not care about @@ -1349,9 +1342,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { &mut self, obligation: &TraitObligation<'tcx>, ) -> smallvec::SmallVec<[(usize, ty::BoundConstness); 2]> { - let poly_trait_predicate = self.infcx().resolve_vars_if_possible(obligation.predicate); + let poly_trait_predicate = self.infcx.resolve_vars_if_possible(obligation.predicate); let placeholder_trait_predicate = - self.infcx().replace_bound_vars_with_placeholders(poly_trait_predicate); + self.infcx.replace_bound_vars_with_placeholders(poly_trait_predicate); debug!(?placeholder_trait_predicate); let tcx = self.infcx.tcx; @@ -2131,7 +2124,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { obligation: &TraitObligation<'tcx>, ) -> Result>, ()> { let placeholder_obligation = - self.infcx().replace_bound_vars_with_placeholders(obligation.predicate); + self.infcx.replace_bound_vars_with_placeholders(obligation.predicate); let placeholder_obligation_trait_ref = placeholder_obligation.trait_ref; let impl_substs = self.infcx.fresh_substs_for_item(obligation.cause.span, impl_def_id); diff --git a/compiler/rustc_trait_selection/src/traits/util.rs b/compiler/rustc_trait_selection/src/traits/util.rs index dae7d589d5cca..a06db4c274831 100644 --- a/compiler/rustc_trait_selection/src/traits/util.rs +++ b/compiler/rustc_trait_selection/src/traits/util.rs @@ -203,13 +203,13 @@ pub fn impl_subject_and_oblig<'a, 'tcx>( let subject = selcx.tcx().bound_impl_subject(impl_def_id); let subject = subject.subst(selcx.tcx(), impl_substs); let InferOk { value: subject, obligations: normalization_obligations1 } = selcx - .infcx() + .infcx .partially_normalize_associated_types_in(ObligationCause::dummy(), param_env, subject); let predicates = selcx.tcx().predicates_of(impl_def_id); let predicates = predicates.instantiate(selcx.tcx(), impl_substs); let InferOk { value: predicates, obligations: normalization_obligations2 } = selcx - .infcx() + .infcx .partially_normalize_associated_types_in(ObligationCause::dummy(), param_env, predicates); let impl_obligations = super::predicates_for_generics(|_, _| ObligationCause::dummy(), param_env, predicates); From f6f25983c623e7a503df3afc643b846905a37412 Mon Sep 17 00:00:00 2001 From: Markus Everling Date: Fri, 25 Nov 2022 14:47:58 +0100 Subject: [PATCH 078/244] Don't use `Take` in `SpecExtend` impl --- .../src/collections/vec_deque/spec_extend.rs | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/spec_extend.rs b/library/alloc/src/collections/vec_deque/spec_extend.rs index adcc8862a8c31..dccf40ccb38aa 100644 --- a/library/alloc/src/collections/vec_deque/spec_extend.rs +++ b/library/alloc/src/collections/vec_deque/spec_extend.rs @@ -1,6 +1,6 @@ use crate::alloc::Allocator; use crate::vec; -use core::iter::{ByRefSized, TrustedLen}; +use core::iter::TrustedLen; use core::slice; use super::VecDeque; @@ -20,28 +20,30 @@ where // for item in iter { // self.push_back(item); // } - loop { - let lower_bound = iter.size_hint().0; - if lower_bound != 0 { - self.reserve(lower_bound); - } - match iter.next() { - Some(val) => self.push_back(val), - None => break, - } + // May only be called if `deque.len() < deque.capacity()` + unsafe fn push_unchecked(deque: &mut VecDeque, element: T) { + // SAFETY: Because of the precondition, it's guaranteed that there is space + // in the logical array after the last element. + unsafe { deque.buffer_write(deque.to_physical_idx(deque.len), element) }; + // This can't overflow because `deque.len() < deque.capacity() <= usize::MAX`. + deque.len += 1; + } + + while let Some(element) = iter.next() { + let (lower, _) = iter.size_hint(); + self.reserve(lower.saturating_add(1)); + + // SAFETY: We just reserved space for at least one element. + unsafe { push_unchecked(self, element) }; - let room = self.capacity() - self.len; - unsafe { - // Safety: - // The iter is at most `room` items long, - // and `room == self.capacity() - self.len` - // => `self.len + room <= self.capacity()` - self.write_iter_wrapping( - self.to_physical_idx(self.len), - ByRefSized(&mut iter).take(room), - room, - ); + // Inner loop to avoid repeatedly calling `reserve`. + while self.len < self.capacity() { + let Some(element) = iter.next() else { + return; + }; + // SAFETY: The loop condition guarantees that `self.len() < self.capacity()`. + unsafe { push_unchecked(self, element) }; } } } From 1dc88ffb3f1b51bbede0c5056a2861833b889e3c Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 25 Nov 2022 16:06:52 -0800 Subject: [PATCH 079/244] Allow non-org members to label `requires-debug-assertions` jruderman tried to add this in #104916, for example. I think I've seen this happen before as well. --- triagebot.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/triagebot.toml b/triagebot.toml index 985e065652d62..5efe65864ab24 100644 --- a/triagebot.toml +++ b/triagebot.toml @@ -15,7 +15,7 @@ allow-unauthenticated = [ "llvm-main", "needs-fcp", "relnotes", - "requires-nightly", + "requires-*", "regression-*", "perf-*", "AsyncAwait-OnDeck", From 61e185b5d08d0db64e0fa331b64ac7283968f8a0 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 26 Nov 2022 05:11:06 +0000 Subject: [PATCH 080/244] simplify some binder shifting logic --- compiler/rustc_middle/src/ty/fold.rs | 47 ++++++++++++++-------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_middle/src/ty/fold.rs b/compiler/rustc_middle/src/ty/fold.rs index 2842b3c3102d2..d431d008ddf06 100644 --- a/compiler/rustc_middle/src/ty/fold.rs +++ b/compiler/rustc_middle/src/ty/fold.rs @@ -407,6 +407,7 @@ where match *t.kind() { ty::Bound(debruijn, bound_ty) if debruijn == self.current_index => { let ty = self.delegate.replace_ty(bound_ty); + debug_assert!(!ty.has_vars_bound_above(ty::INNERMOST)); ty::fold::shift_vars(self.tcx, ty, self.current_index.as_u32()) } _ if t.has_vars_bound_at_or_above(self.current_index) => t.super_fold_with(self), @@ -437,6 +438,7 @@ where match ct.kind() { ty::ConstKind::Bound(debruijn, bound_const) if debruijn == self.current_index => { let ct = self.delegate.replace_const(bound_const, ct.ty()); + debug_assert!(!ct.has_vars_bound_above(ty::INNERMOST)); ty::fold::shift_vars(self.tcx, ct, self.current_index.as_u32()) } _ => ct.super_fold_with(self), @@ -697,14 +699,10 @@ impl<'tcx> TypeFolder<'tcx> for Shifter<'tcx> { fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { match *r { - ty::ReLateBound(debruijn, br) => { - if self.amount == 0 || debruijn < self.current_index { - r - } else { - let debruijn = debruijn.shifted_in(self.amount); - let shifted = ty::ReLateBound(debruijn, br); - self.tcx.mk_region(shifted) - } + ty::ReLateBound(debruijn, br) if debruijn >= self.current_index => { + let debruijn = debruijn.shifted_in(self.amount); + let shifted = ty::ReLateBound(debruijn, br); + self.tcx.mk_region(shifted) } _ => r, } @@ -712,31 +710,30 @@ impl<'tcx> TypeFolder<'tcx> for Shifter<'tcx> { fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> { match *ty.kind() { - ty::Bound(debruijn, bound_ty) => { - if self.amount == 0 || debruijn < self.current_index { - ty - } else { - let debruijn = debruijn.shifted_in(self.amount); - self.tcx.mk_ty(ty::Bound(debruijn, bound_ty)) - } + ty::Bound(debruijn, bound_ty) if debruijn >= self.current_index => { + let debruijn = debruijn.shifted_in(self.amount); + self.tcx.mk_ty(ty::Bound(debruijn, bound_ty)) } - _ => ty.super_fold_with(self), + _ if ty.has_vars_bound_at_or_above(self.current_index) => ty.super_fold_with(self), + _ => ty, } } fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> { - if let ty::ConstKind::Bound(debruijn, bound_ct) = ct.kind() { - if self.amount == 0 || debruijn < self.current_index { - ct - } else { - let debruijn = debruijn.shifted_in(self.amount); - self.tcx.mk_const(ty::ConstKind::Bound(debruijn, bound_ct), ct.ty()) - } + if let ty::ConstKind::Bound(debruijn, bound_ct) = ct.kind() + && debruijn >= self.current_index + { + let debruijn = debruijn.shifted_in(self.amount); + self.tcx.mk_const(ty::ConstKind::Bound(debruijn, bound_ct), ct.ty()) } else { ct.super_fold_with(self) } } + + fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> { + if p.has_vars_bound_at_or_above(self.current_index) { p.super_fold_with(self) } else { p } + } } pub fn shift_region<'tcx>( @@ -758,5 +755,9 @@ where { debug!("shift_vars(value={:?}, amount={})", value, amount); + if amount == 0 || !value.has_escaping_bound_vars() { + return value; + } + value.fold_with(&mut Shifter::new(tcx, amount)) } From 77071f7e3ac76ae6734aa3ce7cfc964b3da4939f Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 26 Nov 2022 10:10:34 +0100 Subject: [PATCH 081/244] interpret: remove PartialOrd from a bunch of types that do not have or need a sensible order --- compiler/rustc_middle/src/mir/interpret/allocation.rs | 4 ++-- .../rustc_middle/src/mir/interpret/allocation/init_mask.rs | 2 +- .../src/mir/interpret/allocation/provenance_map.rs | 2 +- compiler/rustc_middle/src/mir/interpret/pointer.rs | 2 +- compiler/rustc_middle/src/mir/interpret/value.rs | 4 ++-- src/tools/miri/src/concurrency/data_race.rs | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_middle/src/mir/interpret/allocation.rs b/compiler/rustc_middle/src/mir/interpret/allocation.rs index f8a69f3c7d53f..5f911d5884a33 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation.rs @@ -36,7 +36,7 @@ pub use init_mask::{InitChunk, InitChunkIter}; /// module provides higher-level access. // Note: for performance reasons when interning, some of the `Allocation` fields can be partially // hashed. (see the `Hash` impl below for more details), so the impl is not derived. -#[derive(Clone, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)] +#[derive(Clone, Eq, PartialEq, TyEncodable, TyDecodable)] #[derive(HashStable)] pub struct Allocation { /// The actual bytes of the allocation. @@ -108,7 +108,7 @@ impl hash::Hash for Allocation { /// Here things are different because only const allocations are interned. This /// means that both the inner type (`Allocation`) and the outer type /// (`ConstAllocation`) are used quite a bit. -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable)] +#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable)] #[rustc_pass_by_value] pub struct ConstAllocation<'tcx>(pub Interned<'tcx, Allocation>); diff --git a/compiler/rustc_middle/src/mir/interpret/allocation/init_mask.rs b/compiler/rustc_middle/src/mir/interpret/allocation/init_mask.rs index d88a0c19e5937..82e9a961a2bfb 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation/init_mask.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation/init_mask.rs @@ -12,7 +12,7 @@ type Block = u64; /// is initialized. If it is `false` the byte is uninitialized. // Note: for performance reasons when interning, some of the `InitMask` fields can be partially // hashed. (see the `Hash` impl below for more details), so the impl is not derived. -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)] +#[derive(Clone, Debug, Eq, PartialEq, TyEncodable, TyDecodable)] #[derive(HashStable)] pub struct InitMask { blocks: Vec, diff --git a/compiler/rustc_middle/src/mir/interpret/allocation/provenance_map.rs b/compiler/rustc_middle/src/mir/interpret/allocation/provenance_map.rs index 19ee209e552d2..ddd3f394358a3 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation/provenance_map.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation/provenance_map.rs @@ -10,7 +10,7 @@ use super::{alloc_range, AllocError, AllocId, AllocRange, AllocResult, Provenanc use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; /// Stores the provenance information of pointers stored in memory. -#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[derive(Clone, PartialEq, Eq, Hash, Debug)] #[derive(HashStable)] pub struct ProvenanceMap { /// Provenance in this map applies from the given offset for an entire pointer-size worth of diff --git a/compiler/rustc_middle/src/mir/interpret/pointer.rs b/compiler/rustc_middle/src/mir/interpret/pointer.rs index 4e59f1b248216..9c270ba1ec179 100644 --- a/compiler/rustc_middle/src/mir/interpret/pointer.rs +++ b/compiler/rustc_middle/src/mir/interpret/pointer.rs @@ -173,7 +173,7 @@ impl Provenance for AllocId { /// Represents a pointer in the Miri engine. /// /// Pointers are "tagged" with provenance information; typically the `AllocId` they belong to. -#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, TyEncodable, TyDecodable, Hash)] +#[derive(Copy, Clone, Eq, PartialEq, TyEncodable, TyDecodable, Hash)] #[derive(HashStable)] pub struct Pointer { pub(super) offset: Size, // kept private to avoid accidental misinterpretation (meaning depends on `Prov` type) diff --git a/compiler/rustc_middle/src/mir/interpret/value.rs b/compiler/rustc_middle/src/mir/interpret/value.rs index 770c3ed05e8d2..48a137162952e 100644 --- a/compiler/rustc_middle/src/mir/interpret/value.rs +++ b/compiler/rustc_middle/src/mir/interpret/value.rs @@ -28,7 +28,7 @@ pub struct ConstAlloc<'tcx> { /// Represents a constant value in Rust. `Scalar` and `Slice` are optimizations for /// array length computations, enum discriminants and the pattern matching logic. -#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Hash)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, TyEncodable, TyDecodable, Hash)] #[derive(HashStable, Lift)] pub enum ConstValue<'tcx> { /// Used only for types with `layout::abi::Scalar` ABI. @@ -110,7 +110,7 @@ impl<'tcx> ConstValue<'tcx> { /// /// These variants would be private if there was a convenient way to achieve that in Rust. /// Do *not* match on a `Scalar`! Use the various `to_*` methods instead. -#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, TyEncodable, TyDecodable, Hash)] +#[derive(Clone, Copy, Eq, PartialEq, TyEncodable, TyDecodable, Hash)] #[derive(HashStable)] pub enum Scalar { /// The raw bytes of a simple value. diff --git a/src/tools/miri/src/concurrency/data_race.rs b/src/tools/miri/src/concurrency/data_race.rs index b0f766462127f..ebfd3bca736cf 100644 --- a/src/tools/miri/src/concurrency/data_race.rs +++ b/src/tools/miri/src/concurrency/data_race.rs @@ -158,7 +158,7 @@ impl ThreadClockSet { /// Error returned by finding a data race /// should be elaborated upon. -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] pub struct DataRace; /// Externally stored memory cell clocks From c256bd29087ae9403064d7fd8aa8178570b0de80 Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Sat, 26 Nov 2022 09:31:40 +0000 Subject: [PATCH 082/244] Remove redundant `all` in cfg --- library/std/src/sys/common/alloc.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/library/std/src/sys/common/alloc.rs b/library/std/src/sys/common/alloc.rs index e8e7c51cb9ba6..3edbe7280774d 100644 --- a/library/std/src/sys/common/alloc.rs +++ b/library/std/src/sys/common/alloc.rs @@ -4,7 +4,7 @@ use crate::ptr; // The minimum alignment guaranteed by the architecture. This value is used to // add fast paths for low alignment values. -#[cfg(all(any( +#[cfg(any( target_arch = "x86", target_arch = "arm", target_arch = "mips", @@ -16,9 +16,9 @@ use crate::ptr; target_arch = "hexagon", all(target_arch = "riscv32", not(target_os = "espidf")), all(target_arch = "xtensa", not(target_os = "espidf")), -)))] +))] pub const MIN_ALIGN: usize = 8; -#[cfg(all(any( +#[cfg(any( target_arch = "x86_64", target_arch = "aarch64", target_arch = "mips64", @@ -26,13 +26,13 @@ pub const MIN_ALIGN: usize = 8; target_arch = "sparc64", target_arch = "riscv64", target_arch = "wasm64", -)))] +))] pub const MIN_ALIGN: usize = 16; // The allocator on the esp-idf platform guarantees 4 byte alignment. -#[cfg(all(any( +#[cfg(any( all(target_arch = "riscv32", target_os = "espidf"), all(target_arch = "xtensa", target_os = "espidf"), -)))] +))] pub const MIN_ALIGN: usize = 4; pub unsafe fn realloc_fallback( From 89afda781149e5779c38d1ee1d5bf4d633a27d61 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 26 Nov 2022 09:46:05 +0000 Subject: [PATCH 083/244] Ignore bivariant parameters in test_type_match. --- .../src/infer/outlives/test_type_match.rs | 7 +- src/test/ui/impl-trait/issues/issue-104815.rs | 66 +++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/impl-trait/issues/issue-104815.rs diff --git a/compiler/rustc_infer/src/infer/outlives/test_type_match.rs b/compiler/rustc_infer/src/infer/outlives/test_type_match.rs index 5d204dd70ed0c..10b474efd5aeb 100644 --- a/compiler/rustc_infer/src/infer/outlives/test_type_match.rs +++ b/compiler/rustc_infer/src/infer/outlives/test_type_match.rs @@ -155,14 +155,17 @@ impl<'tcx> TypeRelation<'tcx> for Match<'tcx> { bug!() } + #[instrument(level = "trace", skip(self))] fn relate_with_variance>( &mut self, - _: ty::Variance, + variance: ty::Variance, _: ty::VarianceDiagInfo<'tcx>, a: T, b: T, ) -> RelateResult<'tcx, T> { - self.relate(a, b) + // Opaque types substs have lifetime parameters. + // We must not check them to be equal, as we never insert anything to make them so. + if variance != ty::Bivariant { self.relate(a, b) } else { Ok(a) } } #[instrument(skip(self), level = "debug")] diff --git a/src/test/ui/impl-trait/issues/issue-104815.rs b/src/test/ui/impl-trait/issues/issue-104815.rs new file mode 100644 index 0000000000000..7a9826a8dff91 --- /dev/null +++ b/src/test/ui/impl-trait/issues/issue-104815.rs @@ -0,0 +1,66 @@ +// check-pass + +struct It; + +struct Data { + items: Vec, +} + +impl Data { + fn new() -> Self { + Self { + items: vec![It, It], + } + } + + fn content(&self) -> impl Iterator { + self.items.iter() + } +} + +struct Container<'a> { + name: String, + resolver: Box, +} + +impl<'a> Container<'a> { + fn new(name: &str, resolver: R) -> Self { + Self { + name: name.to_owned(), + resolver: Box::new(resolver), + } + } +} + +trait Resolver {} + +impl Resolver for &R {} + +impl Resolver for It {} + +fn get<'a>(mut items: impl Iterator) -> impl Resolver + 'a { + items.next().unwrap() +} + +fn get2<'a, 'b: 'b>(mut items: impl Iterator) -> impl Resolver + 'a { + items.next().unwrap() +} + +fn main() { + let data = Data::new(); + let resolver = get(data.content()); + + let _ = ["a", "b"] + .iter() + .map(|&n| Container::new(n, &resolver)) + .map(|c| c.name) + .collect::>(); + + let resolver = get2(data.content()); + + let _ = ["a", "b"] + .iter() + .map(|&n| Container::new(n, &resolver)) + .map(|c| c.name) + .collect::>(); +} From 2ab3f76442f1acdf8c8229164f27b66b1a822993 Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Sat, 26 Nov 2022 09:54:54 +0000 Subject: [PATCH 084/244] Remove more redundant `all`s --- compiler/rustc_codegen_gcc/example/alloc_system.rs | 8 ++++---- compiler/rustc_span/src/analyze_source_file.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_codegen_gcc/example/alloc_system.rs b/compiler/rustc_codegen_gcc/example/alloc_system.rs index 89661918d05a5..fd01fcf1fc82c 100644 --- a/compiler/rustc_codegen_gcc/example/alloc_system.rs +++ b/compiler/rustc_codegen_gcc/example/alloc_system.rs @@ -13,17 +13,17 @@ // The minimum alignment guaranteed by the architecture. This value is used to // add fast paths for low alignment values. -#[cfg(all(any(target_arch = "x86", +#[cfg(any(target_arch = "x86", target_arch = "arm", target_arch = "mips", target_arch = "powerpc", - target_arch = "powerpc64")))] + target_arch = "powerpc64"))] const MIN_ALIGN: usize = 8; -#[cfg(all(any(target_arch = "x86_64", +#[cfg(any(target_arch = "x86_64", target_arch = "aarch64", target_arch = "mips64", target_arch = "s390x", - target_arch = "sparc64")))] + target_arch = "sparc64"))] const MIN_ALIGN: usize = 16; pub struct System; diff --git a/compiler/rustc_span/src/analyze_source_file.rs b/compiler/rustc_span/src/analyze_source_file.rs index 5987fb2a19897..47aa4dfba42bb 100644 --- a/compiler/rustc_span/src/analyze_source_file.rs +++ b/compiler/rustc_span/src/analyze_source_file.rs @@ -41,7 +41,7 @@ pub fn analyze_source_file( } cfg_if::cfg_if! { - if #[cfg(all(any(target_arch = "x86", target_arch = "x86_64")))] { + if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] { fn analyze_source_file_dispatch(src: &str, source_file_start_pos: BytePos, lines: &mut Vec, From 459621d31be6f9b177372feae3a37a872a630f58 Mon Sep 17 00:00:00 2001 From: xFrednet Date: Fri, 25 Nov 2022 21:50:38 +0100 Subject: [PATCH 085/244] Improve `EXIT` lint docs --- clippy_lints/src/exit.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/exit.rs b/clippy_lints/src/exit.rs index 407dd1b39575f..9c8b0d076dfd7 100644 --- a/clippy_lints/src/exit.rs +++ b/clippy_lints/src/exit.rs @@ -7,21 +7,34 @@ use rustc_session::{declare_lint_pass, declare_tool_lint}; declare_clippy_lint! { /// ### What it does - /// `exit()` terminates the program and doesn't provide a - /// stack trace. + /// Detects calls to the `exit()` function which terminates the program. /// /// ### Why is this bad? - /// Ideally a program is terminated by finishing + /// Exit terminates the program at the location it is called. For unrecoverable + /// errors `panics` should be used to provide a stacktrace and potentualy other + /// information. A normal termination or one with an error code should happen in /// the main function. /// /// ### Example - /// ```ignore + /// ``` /// std::process::exit(0) /// ``` + /// + /// Use instead: + /// + /// ```ignore + /// // To provide a stacktrace and additional information + /// panic!("message"); + /// + /// // or a main method with a return + /// fn main() -> Result<(), i32> { + /// Ok(()) + /// } + /// ``` #[clippy::version = "1.41.0"] pub EXIT, restriction, - "`std::process::exit` is called, terminating the program" + "detects `std::process::exit` calls" } declare_lint_pass!(Exit => [EXIT]); From e06b61c8f9994eba138b42e50e59c316134ffddc Mon Sep 17 00:00:00 2001 From: Lukas Markeffsky <@> Date: Fri, 25 Nov 2022 16:13:52 +0100 Subject: [PATCH 086/244] explain how to get the discriminant out of a `#[repr(T)] enum` --- library/core/src/mem/mod.rs | 61 ++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/library/core/src/mem/mod.rs b/library/core/src/mem/mod.rs index ec35914f1e3b5..723988b33ab5c 100644 --- a/library/core/src/mem/mod.rs +++ b/library/core/src/mem/mod.rs @@ -1113,7 +1113,10 @@ impl fmt::Debug for Discriminant { /// # Stability /// /// The discriminant of an enum variant may change if the enum definition changes. A discriminant -/// of some variant will not change between compilations with the same compiler. +/// of some variant will not change between compilations with the same compiler. See the [Reference] +/// for more information. +/// +/// [Reference]: ../../reference/items/enumerations.html#discriminants /// /// # Examples /// @@ -1129,6 +1132,62 @@ impl fmt::Debug for Discriminant { /// assert_eq!(mem::discriminant(&Foo::B(1)), mem::discriminant(&Foo::B(2))); /// assert_ne!(mem::discriminant(&Foo::B(3)), mem::discriminant(&Foo::C(3))); /// ``` +/// +/// ## Accessing the numeric value of the discriminant +/// +/// Note that it is *undefined behavior* to [`transmute`] from [`Discriminant`] to a primitive! +/// +/// If an enum has only unit variants, then the numeric value of the discriminant can be accessed +/// with an [`as`] cast: +/// +/// ``` +/// enum Enum { +/// Foo, +/// Bar, +/// Baz, +/// } +/// +/// assert_eq!(0, Enum::Foo as isize); +/// assert_eq!(1, Enum::Bar as isize); +/// assert_eq!(2, Enum::Baz as isize); +/// ``` +/// +/// If an enum has opted-in to having a [primitive representation] for its discriminant, +/// then it's possible to use pointers to read the memory location storing the discriminant. +/// That **cannot** be done for enums using the [default representation], however, as it's +/// undefined what layout the discriminant has and where it's stored — it might not even be +/// stored at all! +/// +/// [`as`]: ../../std/keyword.as.html +/// [primitive representation]: ../../reference/type-layout.html#primitive-representations +/// [default representation]: ../../reference/type-layout.html#the-default-representation +/// ``` +/// #[repr(u8)] +/// enum Enum { +/// Unit, +/// Tuple(bool), +/// Struct { a: bool }, +/// } +/// +/// impl Enum { +/// fn discriminant(&self) -> u8 { +/// // SAFETY: Because `Self` is marked `repr(u8)`, its layout is a `repr(C)` `union` +/// // between `repr(C)` structs, each of which has the `u8` discriminant as its first +/// // field, so we can read the discriminant without offsetting the pointer. +/// unsafe { *<*const _>::from(self).cast::() } +/// } +/// } +/// +/// let unit_like = Enum::Unit; +/// let tuple_like = Enum::Tuple(true); +/// let struct_like = Enum::Struct { a: false }; +/// assert_eq!(0, unit_like.discriminant()); +/// assert_eq!(1, tuple_like.discriminant()); +/// assert_eq!(2, struct_like.discriminant()); +/// +/// // ⚠️ This is undefined behavior. Don't do this. ⚠️ +/// // assert_eq!(0, unsafe { std::mem::transmute::<_, u8>(std::mem::discriminant(&unit_like)) }); +/// ``` #[stable(feature = "discriminant_value", since = "1.21.0")] #[rustc_const_unstable(feature = "const_discriminant", issue = "69821")] #[cfg_attr(not(test), rustc_diagnostic_item = "mem_discriminant")] From 4b2a1eb775d7a9ff37b5b4dc990c287f427c2c80 Mon Sep 17 00:00:00 2001 From: Nixon Enraght-Moony Date: Sat, 26 Nov 2022 15:32:49 +0000 Subject: [PATCH 087/244] Support unit tests for jsondoclint --- src/bootstrap/builder.rs | 1 + src/bootstrap/test.rs | 36 ++++++++++++++++++++ src/tools/jsondoclint/src/json_find.rs | 3 ++ src/tools/jsondoclint/src/json_find/tests.rs | 4 +++ 4 files changed, 44 insertions(+) create mode 100644 src/tools/jsondoclint/src/json_find/tests.rs diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index 251431a15eb89..cff5fd8c5b02c 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -644,6 +644,7 @@ impl<'a> Builder<'a> { test::CrateLibrustc, test::CrateRustdoc, test::CrateRustdocJsonTypes, + test::CrateJsonDocLint, test::Linkcheck, test::TierCheck, test::ReplacePlaceholderTest, diff --git a/src/bootstrap/test.rs b/src/bootstrap/test.rs index a69979d7f073a..39cedfdac5f77 100644 --- a/src/bootstrap/test.rs +++ b/src/bootstrap/test.rs @@ -90,6 +90,42 @@ fn try_run_quiet(builder: &Builder<'_>, cmd: &mut Command) -> bool { true } +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +pub struct CrateJsonDocLint { + host: TargetSelection, +} + +impl Step for CrateJsonDocLint { + type Output = (); + const ONLY_HOSTS: bool = true; + const DEFAULT: bool = true; + + fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { + run.path("src/tools/jsondoclint") + } + + fn make_run(run: RunConfig<'_>) { + run.builder.ensure(CrateJsonDocLint { host: run.target }); + } + + fn run(self, builder: &Builder<'_>) { + let bootstrap_host = builder.config.build; + let compiler = builder.compiler(0, bootstrap_host); + + let cargo = tool::prepare_tool_cargo( + builder, + compiler, + Mode::ToolBootstrap, + bootstrap_host, + "test", + "src/tools/jsondoclint", + SourceType::InTree, + &[], + ); + try_run(builder, &mut cargo.into()); + } +} + #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub struct Linkcheck { host: TargetSelection, diff --git a/src/tools/jsondoclint/src/json_find.rs b/src/tools/jsondoclint/src/json_find.rs index 95ea8866609d0..0fbffc0a00550 100644 --- a/src/tools/jsondoclint/src/json_find.rs +++ b/src/tools/jsondoclint/src/json_find.rs @@ -72,3 +72,6 @@ fn find_selector_recursive( } } } + +#[cfg(test)] +mod tests; diff --git a/src/tools/jsondoclint/src/json_find/tests.rs b/src/tools/jsondoclint/src/json_find/tests.rs new file mode 100644 index 0000000000000..b3e009729b130 --- /dev/null +++ b/src/tools/jsondoclint/src/json_find/tests.rs @@ -0,0 +1,4 @@ +#[test] +fn should_fail() { + assert_eq!(true, false); +} From 946d51e8ba2501aa24256317d8798e475dc862b5 Mon Sep 17 00:00:00 2001 From: Lukas Markeffsky <@> Date: Sat, 26 Nov 2022 16:56:29 +0100 Subject: [PATCH 088/244] fix broken link fragment --- library/core/src/mem/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/mem/mod.rs b/library/core/src/mem/mod.rs index 723988b33ab5c..383bdc7b6e2e9 100644 --- a/library/core/src/mem/mod.rs +++ b/library/core/src/mem/mod.rs @@ -1116,7 +1116,7 @@ impl fmt::Debug for Discriminant { /// of some variant will not change between compilations with the same compiler. See the [Reference] /// for more information. /// -/// [Reference]: ../../reference/items/enumerations.html#discriminants +/// [Reference]: ../../reference/items/enumerations.html#custom-discriminant-values-for-fieldless-enumerations /// /// # Examples /// From 09818a8ccaec19d96ec4d364e668742cf7b10522 Mon Sep 17 00:00:00 2001 From: Nixon Enraght-Moony Date: Sat, 26 Nov 2022 16:24:43 +0000 Subject: [PATCH 089/244] Add a test that makes sense --- src/tools/jsondoclint/src/json_find.rs | 2 +- src/tools/jsondoclint/src/json_find/tests.rs | 27 ++++++++++++++++++-- src/tools/jsondoclint/src/main.rs | 4 +-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/tools/jsondoclint/src/json_find.rs b/src/tools/jsondoclint/src/json_find.rs index 0fbffc0a00550..70e7440f73085 100644 --- a/src/tools/jsondoclint/src/json_find.rs +++ b/src/tools/jsondoclint/src/json_find.rs @@ -2,7 +2,7 @@ use std::fmt::Write; use serde_json::Value; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum SelectorPart { Field(String), Index(usize), diff --git a/src/tools/jsondoclint/src/json_find/tests.rs b/src/tools/jsondoclint/src/json_find/tests.rs index b3e009729b130..2a53353071469 100644 --- a/src/tools/jsondoclint/src/json_find/tests.rs +++ b/src/tools/jsondoclint/src/json_find/tests.rs @@ -1,4 +1,27 @@ +use super::*; + #[test] -fn should_fail() { - assert_eq!(true, false); +fn basic_find() { + use SelectorPart::*; + + let j = serde_json::json!({ + "index": { + "4": { + "inner": { + "items": ["1", "2", "3"] + } + } + } + }); + + let sel = find_selector(&j, &serde_json::json!("1")); + let exp: Vec> = vec![vec![ + Field("index".to_owned()), + Field("4".to_owned()), + Field("inner".to_owned()), + Field("items".to_owned()), + Index(0), + ]]; + + assert_eq!(exp, sel); } diff --git a/src/tools/jsondoclint/src/main.rs b/src/tools/jsondoclint/src/main.rs index 70d7a82a57605..fc54c421b4b22 100644 --- a/src/tools/jsondoclint/src/main.rs +++ b/src/tools/jsondoclint/src/main.rs @@ -9,13 +9,13 @@ pub(crate) mod item_kind; mod json_find; mod validator; -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] struct Error { kind: ErrorKind, id: Id, } -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] enum ErrorKind { NotFound, Custom(String), From 74de78a414043202ed70c239b2aca74dceb4c1a8 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Sat, 26 Nov 2022 09:52:58 -0700 Subject: [PATCH 090/244] rustdoc: improve popover focus handling JS This commit fixes a few inconsistencies and erratic behavior from the notable traits, settings, and sidebar popups: * It makes it so that pressing Escape closes the mobile sidebar. This is a bit difficult to do on iPhone, but on other setups like desktop tiling window managers, it's easy and makes sense. * It makes sure that pressing escape while a notable trait popover is open focuses the popover's toggle button, instead of leaving nothing focused, since that makes more sense with keyboard navigation. Clicking the settings, help, or sidebar buttons, however, will not focus the notable trait popover toggle button. * It ensures that notable trait and settings popovers are exclusive with the mobile sidebar. Nothing should ever overlap a popover, and there should never be more than one popover open at once. --- src/librustdoc/html/static/js/main.js | 24 ++++++++++++++++++---- src/librustdoc/html/static/js/settings.js | 2 +- src/test/rustdoc-gui/notable-trait.goml | 25 +++++++++++++++++++++++ src/test/rustdoc-gui/pocket-menu.goml | 21 +++++++++++++++++++ src/test/rustdoc-gui/sidebar-mobile.goml | 6 ++++++ 5 files changed, 73 insertions(+), 5 deletions(-) diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index 2a109ffbc608d..ef1ae68c59eb1 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -202,6 +202,7 @@ function loadCss(cssUrl) { if (event.ctrlKey || event.altKey || event.metaKey) { return; } + window.hideAllModals(); addClass(getSettingsButton(), "rotate"); event.preventDefault(); // Sending request for the CSS and the JS files at the same time so it will @@ -377,7 +378,10 @@ function loadCss(cssUrl) { } ev.preventDefault(); searchState.defocus(); - window.hidePopoverMenus(); + // If the notable traits popover is open, and the user presses Escape, + // reset focus back to the link. + hideNotable(true); + window.hideAllModals(); } function handleShortcut(ev) { @@ -767,6 +771,7 @@ function loadCss(cssUrl) { }; function showSidebar() { + window.hideAllModals(); window.rustdocMobileScrollLock(); const sidebar = document.getElementsByClassName("sidebar")[0]; addClass(sidebar, "shown"); @@ -843,7 +848,7 @@ function loadCss(cssUrl) { // Make this function idempotent. return; } - hideNotable(false); + window.hideAllModals(); const ty = e.getAttribute("data-ty"); const wrapper = document.createElement("div"); wrapper.innerHTML = "
" + window.NOTABLE_TRAITS[ty] + "
"; @@ -1049,6 +1054,18 @@ function loadCss(cssUrl) { return container; } + /** + * Hide popover menus, notable trait tooltips, and the sidebar (if applicable). + * + * This version does not do anything to tweak the focused element. The caller + * should make sure it behaves reasonably. + */ + window.hideAllModals = function() { + hideSidebar(); + window.hidePopoverMenus(); + hideNotable(false); + }; + /** * Hide all the popover menus. */ @@ -1056,7 +1073,6 @@ function loadCss(cssUrl) { onEachLazy(document.querySelectorAll(".search-form .popover"), elem => { elem.style.display = "none"; }); - hideNotable(false); }; /** @@ -1081,7 +1097,7 @@ function loadCss(cssUrl) { function showHelp() { const menu = getHelpMenu(true); if (menu.style.display === "none") { - window.hidePopoverMenus(); + window.hideAllModals(); menu.style.display = ""; } } diff --git a/src/librustdoc/html/static/js/settings.js b/src/librustdoc/html/static/js/settings.js index 5256ae916a771..589bfc79360ce 100644 --- a/src/librustdoc/html/static/js/settings.js +++ b/src/librustdoc/html/static/js/settings.js @@ -268,7 +268,7 @@ event.preventDefault(); const shouldDisplaySettings = settingsMenu.style.display === "none"; - window.hidePopoverMenus(); + window.hideAllModals(); if (shouldDisplaySettings) { displaySettings(); } diff --git a/src/test/rustdoc-gui/notable-trait.goml b/src/test/rustdoc-gui/notable-trait.goml index aab3b11433e93..7e24af47ee817 100644 --- a/src/test/rustdoc-gui/notable-trait.goml +++ b/src/test/rustdoc-gui/notable-trait.goml @@ -200,12 +200,14 @@ move-cursor-to: "//*[@class='notable popover']" assert-count: ("//*[@class='notable popover']", 1) press-key: "Escape" assert-count: ("//*[@class='notable popover']", 0) +assert: "#method\.create_an_iterator_from_read .notable-traits:focus" // Check that clicking outside works. click: "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']" assert-count: ("//*[@class='notable popover']", 1) click: ".search-input" assert-count: ("//*[@class='notable popover']", 0) +assert-false: "#method\.create_an_iterator_from_read .notable-traits:focus" // Check that pressing tab over and over works. click: "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']" @@ -249,3 +251,26 @@ click: "#settings-menu a" press-key: "Escape" // We ensure we didn't come back to the previous focused item. assert-window-property-false: {"scrollY": |scroll|} + +// Opening the mobile sidebar should close the popover. +size: (650, 600) +click: "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']" +assert-count: ("//*[@class='notable popover']", 1) +click: ".sidebar-menu-toggle" +assert: "//*[@class='sidebar shown']" +assert-count: ("//*[@class='notable popover']", 0) +assert-false: "#method\.create_an_iterator_from_read .notable-traits:focus" +// Clicking a notable popover should close the sidebar. +click: "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']" +assert-count: ("//*[@class='notable popover']", 1) +assert-false: "//*[@class='sidebar shown']" + +// Also check the focus handling for the help button. +size: (1100, 600) +reload: +assert-count: ("//*[@class='notable popover']", 0) +click: "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']" +assert-count: ("//*[@class='notable popover']", 1) +click: "#help-button a" +assert-count: ("//*[@class='notable popover']", 0) +assert-false: "#method\.create_an_iterator_from_read .notable-traits:focus" diff --git a/src/test/rustdoc-gui/pocket-menu.goml b/src/test/rustdoc-gui/pocket-menu.goml index fb63ea62a48af..c3649dc7bda20 100644 --- a/src/test/rustdoc-gui/pocket-menu.goml +++ b/src/test/rustdoc-gui/pocket-menu.goml @@ -75,3 +75,24 @@ assert-css: ( ) compare-elements-css: ("#help-button .popover", "#help-button .top", ["border-color"]) compare-elements-css: ("#help-button .popover", "#help-button .bottom", ["border-color"]) + +// Opening the mobile sidebar should close the settings popover. +size: (650, 600) +click: "#settings-menu a" +assert-css: ("#settings-menu .popover", {"display": "block"}) +click: ".sidebar-menu-toggle" +assert: "//*[@class='sidebar shown']" +assert-css: ("#settings-menu .popover", {"display": "none"}) +// Opening the settings popover should close the sidebar. +click: "#settings-menu a" +assert-css: ("#settings-menu .popover", {"display": "block"}) +assert-false: "//*[@class='sidebar shown']" + +// Opening the settings popover at start (which async loads stuff) should also close. +reload: +click: ".sidebar-menu-toggle" +assert: "//*[@class='sidebar shown']" +assert-false: "#settings-menu .popover" +click: "#settings-menu a" +assert-false: "//*[@class='sidebar shown']" +wait-for: "#settings-menu .popover" diff --git a/src/test/rustdoc-gui/sidebar-mobile.goml b/src/test/rustdoc-gui/sidebar-mobile.goml index 453873f1b81b6..840091799a778 100644 --- a/src/test/rustdoc-gui/sidebar-mobile.goml +++ b/src/test/rustdoc-gui/sidebar-mobile.goml @@ -32,6 +32,12 @@ assert-css: ("//nav[contains(@class, 'sidebar')]//h2/a[text()='In test_docs']/pa click: "body" assert-css: (".sidebar", {"display": "block", "left": "-1000px"}) +// Open the sidebar menu, and make sure pressing Escape closes it. +click: ".sidebar-menu-toggle" +assert-css: (".sidebar", {"left": "0px"}) +press-key: "Escape" +assert-css: (".sidebar", {"display": "block", "left": "-1000px"}) + // Check that the topbar is visible assert-property: (".mobile-topbar", {"clientHeight": "45"}) From b22418eac3be6a163d876c8bdb0d13663d592a22 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 26 Nov 2022 17:22:10 +0000 Subject: [PATCH 091/244] Verify that HIR parenting and Def parenting match. --- compiler/rustc_ast_lowering/src/index.rs | 2 +- compiler/rustc_middle/src/hir/mod.rs | 9 +-- compiler/rustc_passes/src/hir_id_validator.rs | 75 +++++++++++++------ 3 files changed, 54 insertions(+), 32 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/index.rs b/compiler/rustc_ast_lowering/src/index.rs index 695a698e0227a..267132b67cfa5 100644 --- a/compiler/rustc_ast_lowering/src/index.rs +++ b/compiler/rustc_ast_lowering/src/index.rs @@ -77,7 +77,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { if hir_id.owner != self.owner { span_bug!( span, - "inconsistent DepNode at `{:?}` for `{:?}`: \ + "inconsistent HirId at `{:?}` for `{:?}`: \ current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})", self.source_map.span_to_diagnostic_string(span), node, diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index 1c6264ad03654..02fd03c02839a 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -133,13 +133,8 @@ pub fn provide(providers: &mut Providers) { // Accessing the local_parent is ok since its value is hashed as part of `id`'s DefPathHash. tcx.opt_local_parent(id.def_id).map_or(CRATE_HIR_ID, |parent| { let mut parent_hir_id = tcx.hir().local_def_id_to_hir_id(parent); - if let Some(local_id) = tcx.hir_crate(()).owners[parent_hir_id.owner.def_id] - .unwrap() - .parenting - .get(&id.def_id) - { - parent_hir_id.local_id = *local_id; - } + parent_hir_id.local_id = + tcx.hir_crate(()).owners[parent_hir_id.owner.def_id].unwrap().parenting[&id.def_id]; parent_hir_id }) }; diff --git a/compiler/rustc_passes/src/hir_id_validator.rs b/compiler/rustc_passes/src/hir_id_validator.rs index 88bb39debb114..d143adb2eb941 100644 --- a/compiler/rustc_passes/src/hir_id_validator.rs +++ b/compiler/rustc_passes/src/hir_id_validator.rs @@ -1,11 +1,11 @@ use rustc_data_structures::sync::Lock; use rustc_hir as hir; +use rustc_hir::def_id::LocalDefId; use rustc_hir::intravisit; use rustc_hir::{HirId, ItemLocalId}; use rustc_index::bit_set::GrowableBitSet; -use rustc_middle::hir::map::Map; use rustc_middle::hir::nested_filter; -use rustc_middle::ty::TyCtxt; +use rustc_middle::ty::{DefIdTree, TyCtxt}; pub fn check_crate(tcx: TyCtxt<'_>) { tcx.dep_graph.assert_ignored(); @@ -17,11 +17,10 @@ pub fn check_crate(tcx: TyCtxt<'_>) { #[cfg(debug_assertions)] { let errors = Lock::new(Vec::new()); - let hir_map = tcx.hir(); - hir_map.par_for_each_module(|module_id| { + tcx.hir().par_for_each_module(|module_id| { let mut v = HirIdValidator { - hir_map, + tcx, owner: None, hir_ids_seen: Default::default(), errors: &errors, @@ -40,20 +39,15 @@ pub fn check_crate(tcx: TyCtxt<'_>) { } struct HirIdValidator<'a, 'hir> { - hir_map: Map<'hir>, + tcx: TyCtxt<'hir>, owner: Option, hir_ids_seen: GrowableBitSet, errors: &'a Lock>, } impl<'a, 'hir> HirIdValidator<'a, 'hir> { - fn new_visitor(&self, hir_map: Map<'hir>) -> HirIdValidator<'a, 'hir> { - HirIdValidator { - hir_map, - owner: None, - hir_ids_seen: Default::default(), - errors: self.errors, - } + fn new_visitor(&self, tcx: TyCtxt<'hir>) -> HirIdValidator<'a, 'hir> { + HirIdValidator { tcx, owner: None, hir_ids_seen: Default::default(), errors: self.errors } } #[cold] @@ -96,36 +90,69 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> { missing_items.push(format!( "[local_id: {}, owner: {}]", local_id, - self.hir_map.def_path(owner.def_id).to_string_no_crate_verbose() + self.tcx.hir().def_path(owner.def_id).to_string_no_crate_verbose() )); } self.error(|| { format!( "ItemLocalIds not assigned densely in {}. \ Max ItemLocalId = {}, missing IDs = {:#?}; seens IDs = {:#?}", - self.hir_map.def_path(owner.def_id).to_string_no_crate_verbose(), + self.tcx.hir().def_path(owner.def_id).to_string_no_crate_verbose(), max, missing_items, self.hir_ids_seen .iter() .map(|local_id| HirId { owner, local_id }) - .map(|h| format!("({:?} {})", h, self.hir_map.node_to_string(h))) + .map(|h| format!("({:?} {})", h, self.tcx.hir().node_to_string(h))) .collect::>() ) }); } } + + fn check_nested_id(&mut self, id: LocalDefId) { + let Some(owner) = self.owner else { return }; + let def_parent = self.tcx.local_parent(id); + let def_parent_hir_id = self.tcx.local_def_id_to_hir_id(def_parent); + if def_parent_hir_id.owner != owner { + self.error(|| { + format!( + "inconsistent Def parent at `{:?}` for `{:?}`:\nexpected={:?}\nfound={:?}", + self.tcx.def_span(id), + id, + owner, + def_parent_hir_id + ) + }); + } + } } impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> { type NestedFilter = nested_filter::OnlyBodies; fn nested_visit_map(&mut self) -> Self::Map { - self.hir_map + self.tcx.hir() + } + + fn visit_nested_item(&mut self, id: hir::ItemId) { + self.check_nested_id(id.owner_id.def_id); + } + + fn visit_nested_trait_item(&mut self, id: hir::TraitItemId) { + self.check_nested_id(id.owner_id.def_id); + } + + fn visit_nested_impl_item(&mut self, id: hir::ImplItemId) { + self.check_nested_id(id.owner_id.def_id); + } + + fn visit_nested_foreign_item(&mut self, id: hir::ForeignItemId) { + self.check_nested_id(id.owner_id.def_id); } fn visit_item(&mut self, i: &'hir hir::Item<'hir>) { - let mut inner_visitor = self.new_visitor(self.hir_map); + let mut inner_visitor = self.new_visitor(self.tcx); inner_visitor.check(i.owner_id, |this| intravisit::walk_item(this, i)); } @@ -136,9 +163,9 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> { self.error(|| { format!( "HirIdValidator: The recorded owner of {} is {} instead of {}", - self.hir_map.node_to_string(hir_id), - self.hir_map.def_path(hir_id.owner.def_id).to_string_no_crate_verbose(), - self.hir_map.def_path(owner.def_id).to_string_no_crate_verbose() + self.tcx.hir().node_to_string(hir_id), + self.tcx.hir().def_path(hir_id.owner.def_id).to_string_no_crate_verbose(), + self.tcx.hir().def_path(owner.def_id).to_string_no_crate_verbose() ) }); } @@ -147,17 +174,17 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> { } fn visit_foreign_item(&mut self, i: &'hir hir::ForeignItem<'hir>) { - let mut inner_visitor = self.new_visitor(self.hir_map); + let mut inner_visitor = self.new_visitor(self.tcx); inner_visitor.check(i.owner_id, |this| intravisit::walk_foreign_item(this, i)); } fn visit_trait_item(&mut self, i: &'hir hir::TraitItem<'hir>) { - let mut inner_visitor = self.new_visitor(self.hir_map); + let mut inner_visitor = self.new_visitor(self.tcx); inner_visitor.check(i.owner_id, |this| intravisit::walk_trait_item(this, i)); } fn visit_impl_item(&mut self, i: &'hir hir::ImplItem<'hir>) { - let mut inner_visitor = self.new_visitor(self.hir_map); + let mut inner_visitor = self.new_visitor(self.tcx); inner_visitor.check(i.owner_id, |this| intravisit::walk_impl_item(this, i)); } } From c96d888bdfb23122c7823445f471b3d284dd07e6 Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Fri, 25 Nov 2022 09:53:58 +0100 Subject: [PATCH 092/244] Pretty-print generators with their `generator_kind` After removing `GenFuture`, I special-cased async generators to pretty-print as `impl Future` mainly to avoid too much diagnostics changes originally. This now reverses that change so that async fn/blocks are pretty-printed as `[$movability `async` $something@$source-position]` in various diagnostics, and updates the tests that this touches. --- .../src/transform/check_consts/ops.rs | 2 +- compiler/rustc_hir/src/hir.rs | 6 +-- .../src/generator_interior/mod.rs | 3 +- compiler/rustc_middle/src/ty/print/pretty.rs | 25 ++++------ .../src/traits/error_reporting/suggestions.rs | 2 +- ...ync-block-control-flow-static-semantics.rs | 4 +- ...block-control-flow-static-semantics.stderr | 8 ++-- src/test/ui/async-await/generator-desc.stderr | 8 ++-- .../issue-67252-unnamed-future.stderr | 2 +- src/test/ui/async-await/issue-86507.stderr | 2 +- ...6-raw-ptr-not-send.no_drop_tracking.stderr | 2 +- src/test/ui/chalkify/bugs/async.stderr | 22 ++++----- src/test/ui/generator/clone-impl-async.rs | 27 +++++------ src/test/ui/generator/clone-impl-async.stderr | 48 +++++++++---------- src/test/ui/impl-trait/issue-55872-3.rs | 2 +- src/test/ui/impl-trait/issue-55872-3.stderr | 4 +- src/test/ui/impl-trait/issues/issue-78722.rs | 2 +- .../ui/impl-trait/issues/issue-78722.stderr | 2 +- .../pattern/non-structural-match-types.stderr | 2 +- .../expected-boxed-future-isnt-pinned.stderr | 2 +- 20 files changed, 85 insertions(+), 90 deletions(-) diff --git a/compiler/rustc_const_eval/src/transform/check_consts/ops.rs b/compiler/rustc_const_eval/src/transform/check_consts/ops.rs index c62c665158779..3d74d689db25d 100644 --- a/compiler/rustc_const_eval/src/transform/check_consts/ops.rs +++ b/compiler/rustc_const_eval/src/transform/check_consts/ops.rs @@ -381,7 +381,7 @@ impl<'tcx> NonConstOp<'tcx> for Generator { ccx: &ConstCx<'_, 'tcx>, span: Span, ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> { - let msg = format!("{}s are not allowed in {}s", self.0, ccx.const_kind()); + let msg = format!("{}s are not allowed in {}s", self.0.descr(), ccx.const_kind()); if let hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Block) = self.0 { ccx.tcx.sess.create_feature_err( UnallowedOpInConstContext { span, msg }, diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 473a04f33a9ad..3ed599fd2cc6f 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -1514,9 +1514,9 @@ pub enum AsyncGeneratorKind { impl fmt::Display for AsyncGeneratorKind { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(match self { - AsyncGeneratorKind::Block => "`async` block", - AsyncGeneratorKind::Closure => "`async` closure body", - AsyncGeneratorKind::Fn => "`async fn` body", + AsyncGeneratorKind::Block => "async block", + AsyncGeneratorKind::Closure => "async closure body", + AsyncGeneratorKind::Fn => "async fn body", }) } } diff --git a/compiler/rustc_hir_typeck/src/generator_interior/mod.rs b/compiler/rustc_hir_typeck/src/generator_interior/mod.rs index bd3589c6c43a9..1e6056101f191 100644 --- a/compiler/rustc_hir_typeck/src/generator_interior/mod.rs +++ b/compiler/rustc_hir_typeck/src/generator_interior/mod.rs @@ -118,7 +118,8 @@ impl<'a, 'tcx> InteriorVisitor<'a, 'tcx> { } else { let note = format!( "the type is part of the {} because of this {}", - self.kind, yield_data.source + self.kind.descr(), + yield_data.source ); self.fcx diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index c54edf10c2d8f..77f9cec511894 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -681,25 +681,20 @@ pub trait PrettyPrinter<'tcx>: } ty::Str => p!("str"), ty::Generator(did, substs, movability) => { - // FIXME(swatinem): async constructs used to be pretty printed - // as `impl Future` previously due to the `from_generator` wrapping. - // lets special case this here for now to avoid churn in diagnostics. - let generator_kind = self.tcx().generator_kind(did); - if matches!(generator_kind, Some(hir::GeneratorKind::Async(..))) { - let return_ty = substs.as_generator().return_ty(); - p!(write("impl Future", return_ty)); - - return Ok(self); - } - p!(write("[")); - match movability { - hir::Movability::Movable => {} - hir::Movability::Static => p!("static "), + let generator_kind = self.tcx().generator_kind(did).unwrap(); + let should_print_movability = + self.should_print_verbose() || generator_kind == hir::GeneratorKind::Gen; + + if should_print_movability { + match movability { + hir::Movability::Movable => {} + hir::Movability::Static => p!("static "), + } } if !self.should_print_verbose() { - p!("generator"); + p!(write("{}", generator_kind)); // FIXME(eddyb) should use `def_span`. if let Some(did) = did.as_local() { let span = self.tcx().def_span(did); diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 30207033236c5..482c6f06cd3e5 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -2677,7 +2677,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { let sp = self.tcx.def_span(def_id); // Special-case this to say "async block" instead of `[static generator]`. - let kind = tcx.generator_kind(def_id).unwrap(); + let kind = tcx.generator_kind(def_id).unwrap().descr(); err.span_note( sp, &format!("required because it's used within this {}", kind), diff --git a/src/test/ui/async-await/async-block-control-flow-static-semantics.rs b/src/test/ui/async-await/async-block-control-flow-static-semantics.rs index 446212ca767c4..bc9d127931d59 100644 --- a/src/test/ui/async-await/async-block-control-flow-static-semantics.rs +++ b/src/test/ui/async-await/async-block-control-flow-static-semantics.rs @@ -15,7 +15,7 @@ fn return_targets_async_block_not_fn() -> u8 { return 0u8; }; let _: &dyn Future = █ - //~^ ERROR expected `impl Future` to be a future that resolves to `()`, but it resolves to `u8` + //~^ ERROR to be a future that resolves to `()`, but it resolves to `u8` } async fn return_targets_async_block_not_async_fn() -> u8 { @@ -24,7 +24,7 @@ async fn return_targets_async_block_not_async_fn() -> u8 { return 0u8; }; let _: &dyn Future = █ - //~^ ERROR expected `impl Future` to be a future that resolves to `()`, but it resolves to `u8` + //~^ ERROR to be a future that resolves to `()`, but it resolves to `u8` } fn no_break_in_async_block() { diff --git a/src/test/ui/async-await/async-block-control-flow-static-semantics.stderr b/src/test/ui/async-await/async-block-control-flow-static-semantics.stderr index b8ca64fae8319..c4487eb840abc 100644 --- a/src/test/ui/async-await/async-block-control-flow-static-semantics.stderr +++ b/src/test/ui/async-await/async-block-control-flow-static-semantics.stderr @@ -29,13 +29,13 @@ LL | | LL | | } | |_^ expected `u8`, found `()` -error[E0271]: expected `impl Future` to be a future that resolves to `()`, but it resolves to `u8` +error[E0271]: expected `[async block@$DIR/async-block-control-flow-static-semantics.rs:23:17: 25:6]` to be a future that resolves to `()`, but it resolves to `u8` --> $DIR/async-block-control-flow-static-semantics.rs:26:39 | LL | let _: &dyn Future = █ | ^^^^^^ expected `()`, found `u8` | - = note: required for the cast from `impl Future` to the object type `dyn Future` + = note: required for the cast from `[async block@$DIR/async-block-control-flow-static-semantics.rs:23:17: 25:6]` to the object type `dyn Future` error[E0308]: mismatched types --> $DIR/async-block-control-flow-static-semantics.rs:12:43 @@ -45,13 +45,13 @@ LL | fn return_targets_async_block_not_fn() -> u8 { | | | implicitly returns `()` as its body has no tail or `return` expression -error[E0271]: expected `impl Future` to be a future that resolves to `()`, but it resolves to `u8` +error[E0271]: expected `[async block@$DIR/async-block-control-flow-static-semantics.rs:14:17: 16:6]` to be a future that resolves to `()`, but it resolves to `u8` --> $DIR/async-block-control-flow-static-semantics.rs:17:39 | LL | let _: &dyn Future = █ | ^^^^^^ expected `()`, found `u8` | - = note: required for the cast from `impl Future` to the object type `dyn Future` + = note: required for the cast from `[async block@$DIR/async-block-control-flow-static-semantics.rs:14:17: 16:6]` to the object type `dyn Future` error[E0308]: mismatched types --> $DIR/async-block-control-flow-static-semantics.rs:49:44 diff --git a/src/test/ui/async-await/generator-desc.stderr b/src/test/ui/async-await/generator-desc.stderr index 774c97966b18e..1686153acf9a6 100644 --- a/src/test/ui/async-await/generator-desc.stderr +++ b/src/test/ui/async-await/generator-desc.stderr @@ -8,8 +8,8 @@ LL | fun(async {}, async {}); | | arguments to this function are incorrect | the expected `async` block | - = note: expected `async` block `impl Future` (`async` block) - found `async` block `impl Future` (`async` block) + = note: expected `async` block `[async block@$DIR/generator-desc.rs:10:9: 10:17]` + found `async` block `[async block@$DIR/generator-desc.rs:10:19: 10:27]` note: function defined here --> $SRC_DIR/core/src/future/mod.rs:LL:COL | @@ -53,8 +53,8 @@ LL | fun((async || {})(), (async || {})()); | | the expected `async` closure body | arguments to this function are incorrect | - = note: expected `async` closure body `impl Future` (`async` closure body) - found `async` closure body `impl Future` (`async` closure body) + = note: expected `async` closure body `[async closure body@$DIR/generator-desc.rs:14:19: 14:21]` + found `async` closure body `[async closure body@$DIR/generator-desc.rs:14:36: 14:38]` note: function defined here --> $DIR/generator-desc.rs:8:4 | diff --git a/src/test/ui/async-await/issue-67252-unnamed-future.stderr b/src/test/ui/async-await/issue-67252-unnamed-future.stderr index af99b608ca14d..fcba4410ba9a5 100644 --- a/src/test/ui/async-await/issue-67252-unnamed-future.stderr +++ b/src/test/ui/async-await/issue-67252-unnamed-future.stderr @@ -8,7 +8,7 @@ LL | | AFuture.await; LL | | }); | |_____^ future created by async block is not `Send` | - = help: within `impl Future`, the trait `Send` is not implemented for `*mut ()` + = help: within `[async block@$DIR/issue-67252-unnamed-future.rs:18:11: 21:6]`, the trait `Send` is not implemented for `*mut ()` note: future is not `Send` as this value is used across an await --> $DIR/issue-67252-unnamed-future.rs:20:16 | diff --git a/src/test/ui/async-await/issue-86507.stderr b/src/test/ui/async-await/issue-86507.stderr index 0e21dba980deb..8c2c06da25cc4 100644 --- a/src/test/ui/async-await/issue-86507.stderr +++ b/src/test/ui/async-await/issue-86507.stderr @@ -13,7 +13,7 @@ note: captured value is not `Send` because `&` references cannot be sent unless | LL | let x = x; | ^ has type `&T` which is not `Send`, because `T` is not `Sync` - = note: required for the cast from `impl Future` to the object type `dyn Future + Send` + = note: required for the cast from `[async block@$DIR/issue-86507.rs:18:17: 20:18]` to the object type `dyn Future + Send` help: consider further restricting this bound | LL | fn bar<'me, 'async_trait, T: Send + std::marker::Sync>(x: &'me T) diff --git a/src/test/ui/async-await/issues/issue-65436-raw-ptr-not-send.no_drop_tracking.stderr b/src/test/ui/async-await/issues/issue-65436-raw-ptr-not-send.no_drop_tracking.stderr index a723503776b9d..ab196dca20cc4 100644 --- a/src/test/ui/async-await/issues/issue-65436-raw-ptr-not-send.no_drop_tracking.stderr +++ b/src/test/ui/async-await/issues/issue-65436-raw-ptr-not-send.no_drop_tracking.stderr @@ -8,7 +8,7 @@ LL | | bar(Foo(std::ptr::null())).await; LL | | }) | |_____^ future created by async block is not `Send` | - = help: within `impl Future`, the trait `Send` is not implemented for `*const u8` + = help: within `[async block@$DIR/issue-65436-raw-ptr-not-send.rs:16:17: 19:6]`, the trait `Send` is not implemented for `*const u8` note: future is not `Send` as this value is used across an await --> $DIR/issue-65436-raw-ptr-not-send.rs:18:35 | diff --git a/src/test/ui/chalkify/bugs/async.stderr b/src/test/ui/chalkify/bugs/async.stderr index 6f22d2c593a3b..4804df133401b 100644 --- a/src/test/ui/chalkify/bugs/async.stderr +++ b/src/test/ui/chalkify/bugs/async.stderr @@ -1,4 +1,4 @@ -error[E0277]: `impl Future` is not a future +error[E0277]: `[async fn body@$DIR/async.rs:7:29: 9:2]` is not a future --> $DIR/async.rs:7:29 | LL | async fn foo(x: u32) -> u32 { @@ -7,18 +7,18 @@ LL | | x LL | | } | | ^ | | | - | |_`impl Future` is not a future + | |_`[async fn body@$DIR/async.rs:7:29: 9:2]` is not a future | required by a bound introduced by this call | - = help: the trait `Future` is not implemented for `impl Future` - = note: impl Future must be a future or must implement `IntoFuture` to be awaited + = help: the trait `Future` is not implemented for `[async fn body@$DIR/async.rs:7:29: 9:2]` + = note: [async fn body@$DIR/async.rs:7:29: 9:2] must be a future or must implement `IntoFuture` to be awaited note: required by a bound in `identity_future` --> $SRC_DIR/core/src/future/mod.rs:LL:COL | LL | pub const fn identity_future>(f: Fut) -> Fut { | ^^^^^^^^^^^^^^^^^^ required by this bound in `identity_future` -error[E0277]: the size for values of type ` as Future>::Output` cannot be known at compilation time +error[E0277]: the size for values of type `<[async fn body@$DIR/async.rs:7:29: 9:2] as Future>::Output` cannot be known at compilation time --> $DIR/async.rs:7:29 | LL | async fn foo(x: u32) -> u32 { @@ -27,23 +27,23 @@ LL | | x LL | | } | |_^ doesn't have a size known at compile-time | - = help: the trait `Sized` is not implemented for ` as Future>::Output` + = help: the trait `Sized` is not implemented for `<[async fn body@$DIR/async.rs:7:29: 9:2] as Future>::Output` note: required by a bound in `identity_future` --> $SRC_DIR/core/src/future/mod.rs:LL:COL | LL | pub const fn identity_future>(f: Fut) -> Fut { | ^ required by this bound in `identity_future` -error[E0277]: `impl Future` is not a future +error[E0277]: `[async fn body@$DIR/async.rs:7:29: 9:2]` is not a future --> $DIR/async.rs:7:25 | LL | async fn foo(x: u32) -> u32 { - | ^^^ `impl Future` is not a future + | ^^^ `[async fn body@$DIR/async.rs:7:29: 9:2]` is not a future | - = help: the trait `Future` is not implemented for `impl Future` - = note: impl Future must be a future or must implement `IntoFuture` to be awaited + = help: the trait `Future` is not implemented for `[async fn body@$DIR/async.rs:7:29: 9:2]` + = note: [async fn body@$DIR/async.rs:7:29: 9:2] must be a future or must implement `IntoFuture` to be awaited -error[E0280]: the requirement ` as Future>::Output == u32` is not satisfied +error[E0280]: the requirement `<[async fn body@$DIR/async.rs:7:29: 9:2] as Future>::Output == u32` is not satisfied --> $DIR/async.rs:7:25 | LL | async fn foo(x: u32) -> u32 { diff --git a/src/test/ui/generator/clone-impl-async.rs b/src/test/ui/generator/clone-impl-async.rs index 83c51526b7b09..9e9b59d3633f2 100644 --- a/src/test/ui/generator/clone-impl-async.rs +++ b/src/test/ui/generator/clone-impl-async.rs @@ -15,42 +15,42 @@ fn main() { drop(non_clone); }; check_copy(&inner_non_clone); - //~^ ERROR the trait bound `impl Future: Copy` is not satisfied + //~^ ERROR : Copy` is not satisfied check_clone(&inner_non_clone); - //~^ ERROR the trait bound `impl Future: Clone` is not satisfied + //~^ ERROR : Clone` is not satisfied let non_clone = NonClone; let outer_non_clone = async move { drop(non_clone); }; check_copy(&outer_non_clone); - //~^ ERROR the trait bound `impl Future: Copy` is not satisfied + //~^ ERROR : Copy` is not satisfied check_clone(&outer_non_clone); - //~^ ERROR the trait bound `impl Future: Clone` is not satisfied + //~^ ERROR : Clone` is not satisfied let maybe_copy_clone = async move {}; check_copy(&maybe_copy_clone); - //~^ ERROR the trait bound `impl Future: Copy` is not satisfied + //~^ ERROR : Copy` is not satisfied check_clone(&maybe_copy_clone); - //~^ ERROR the trait bound `impl Future: Clone` is not satisfied + //~^ ERROR : Clone` is not satisfied let inner_non_clone_fn = the_inner_non_clone_fn(); check_copy(&inner_non_clone_fn); - //~^ ERROR the trait bound `impl Future: Copy` is not satisfied + //~^ ERROR : Copy` is not satisfied check_clone(&inner_non_clone_fn); - //~^ ERROR the trait bound `impl Future: Clone` is not satisfied + //~^ ERROR : Clone` is not satisfied let outer_non_clone_fn = the_outer_non_clone_fn(NonClone); check_copy(&outer_non_clone_fn); - //~^ ERROR the trait bound `impl Future: Copy` is not satisfied + //~^ ERROR : Copy` is not satisfied check_clone(&outer_non_clone_fn); - //~^ ERROR the trait bound `impl Future: Clone` is not satisfied + //~^ ERROR : Clone` is not satisfied let maybe_copy_clone_fn = the_maybe_copy_clone_fn(); check_copy(&maybe_copy_clone_fn); - //~^ ERROR the trait bound `impl Future: Copy` is not satisfied + //~^ ERROR : Copy` is not satisfied check_clone(&maybe_copy_clone_fn); - //~^ ERROR the trait bound `impl Future: Clone` is not satisfied + //~^ ERROR : Clone` is not satisfied } async fn the_inner_non_clone_fn() { @@ -64,8 +64,7 @@ async fn the_outer_non_clone_fn(non_clone: NonClone) { drop(non_clone); } -async fn the_maybe_copy_clone_fn() { -} +async fn the_maybe_copy_clone_fn() {} fn check_copy(_x: &T) {} fn check_clone(_x: &T) {} diff --git a/src/test/ui/generator/clone-impl-async.stderr b/src/test/ui/generator/clone-impl-async.stderr index cbb58d2af18e7..9854728876f64 100644 --- a/src/test/ui/generator/clone-impl-async.stderr +++ b/src/test/ui/generator/clone-impl-async.stderr @@ -1,83 +1,83 @@ -error[E0277]: the trait bound `impl Future: Copy` is not satisfied +error[E0277]: the trait bound `[async block@$DIR/clone-impl-async.rs:12:27: 16:6]: Copy` is not satisfied --> $DIR/clone-impl-async.rs:17:16 | LL | check_copy(&inner_non_clone); - | ---------- ^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `impl Future` + | ---------- ^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `[async block@$DIR/clone-impl-async.rs:12:27: 16:6]` | | | required by a bound introduced by this call | note: required by a bound in `check_copy` - --> $DIR/clone-impl-async.rs:70:18 + --> $DIR/clone-impl-async.rs:69:18 | LL | fn check_copy(_x: &T) {} | ^^^^ required by this bound in `check_copy` -error[E0277]: the trait bound `impl Future: Clone` is not satisfied +error[E0277]: the trait bound `[async block@$DIR/clone-impl-async.rs:12:27: 16:6]: Clone` is not satisfied --> $DIR/clone-impl-async.rs:19:17 | LL | check_clone(&inner_non_clone); - | ----------- ^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `impl Future` + | ----------- ^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `[async block@$DIR/clone-impl-async.rs:12:27: 16:6]` | | | required by a bound introduced by this call | note: required by a bound in `check_clone` - --> $DIR/clone-impl-async.rs:71:19 + --> $DIR/clone-impl-async.rs:70:19 | LL | fn check_clone(_x: &T) {} | ^^^^^ required by this bound in `check_clone` -error[E0277]: the trait bound `impl Future: Copy` is not satisfied +error[E0277]: the trait bound `[async block@$DIR/clone-impl-async.rs:23:27: 25:6]: Copy` is not satisfied --> $DIR/clone-impl-async.rs:26:16 | LL | check_copy(&outer_non_clone); - | ---------- ^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `impl Future` + | ---------- ^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `[async block@$DIR/clone-impl-async.rs:23:27: 25:6]` | | | required by a bound introduced by this call | note: required by a bound in `check_copy` - --> $DIR/clone-impl-async.rs:70:18 + --> $DIR/clone-impl-async.rs:69:18 | LL | fn check_copy(_x: &T) {} | ^^^^ required by this bound in `check_copy` -error[E0277]: the trait bound `impl Future: Clone` is not satisfied +error[E0277]: the trait bound `[async block@$DIR/clone-impl-async.rs:23:27: 25:6]: Clone` is not satisfied --> $DIR/clone-impl-async.rs:28:17 | LL | check_clone(&outer_non_clone); - | ----------- ^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `impl Future` + | ----------- ^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `[async block@$DIR/clone-impl-async.rs:23:27: 25:6]` | | | required by a bound introduced by this call | note: required by a bound in `check_clone` - --> $DIR/clone-impl-async.rs:71:19 + --> $DIR/clone-impl-async.rs:70:19 | LL | fn check_clone(_x: &T) {} | ^^^^^ required by this bound in `check_clone` -error[E0277]: the trait bound `impl Future: Copy` is not satisfied +error[E0277]: the trait bound `[async block@$DIR/clone-impl-async.rs:31:28: 31:41]: Copy` is not satisfied --> $DIR/clone-impl-async.rs:32:16 | LL | check_copy(&maybe_copy_clone); - | ---------- ^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `impl Future` + | ---------- ^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `[async block@$DIR/clone-impl-async.rs:31:28: 31:41]` | | | required by a bound introduced by this call | note: required by a bound in `check_copy` - --> $DIR/clone-impl-async.rs:70:18 + --> $DIR/clone-impl-async.rs:69:18 | LL | fn check_copy(_x: &T) {} | ^^^^ required by this bound in `check_copy` -error[E0277]: the trait bound `impl Future: Clone` is not satisfied +error[E0277]: the trait bound `[async block@$DIR/clone-impl-async.rs:31:28: 31:41]: Clone` is not satisfied --> $DIR/clone-impl-async.rs:34:17 | LL | check_clone(&maybe_copy_clone); - | ----------- ^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `impl Future` + | ----------- ^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `[async block@$DIR/clone-impl-async.rs:31:28: 31:41]` | | | required by a bound introduced by this call | note: required by a bound in `check_clone` - --> $DIR/clone-impl-async.rs:71:19 + --> $DIR/clone-impl-async.rs:70:19 | LL | fn check_clone(_x: &T) {} | ^^^^^ required by this bound in `check_clone` @@ -91,7 +91,7 @@ LL | check_copy(&inner_non_clone_fn); | required by a bound introduced by this call | note: required by a bound in `check_copy` - --> $DIR/clone-impl-async.rs:70:18 + --> $DIR/clone-impl-async.rs:69:18 | LL | fn check_copy(_x: &T) {} | ^^^^ required by this bound in `check_copy` @@ -105,7 +105,7 @@ LL | check_clone(&inner_non_clone_fn); | required by a bound introduced by this call | note: required by a bound in `check_clone` - --> $DIR/clone-impl-async.rs:71:19 + --> $DIR/clone-impl-async.rs:70:19 | LL | fn check_clone(_x: &T) {} | ^^^^^ required by this bound in `check_clone` @@ -119,7 +119,7 @@ LL | check_copy(&outer_non_clone_fn); | required by a bound introduced by this call | note: required by a bound in `check_copy` - --> $DIR/clone-impl-async.rs:70:18 + --> $DIR/clone-impl-async.rs:69:18 | LL | fn check_copy(_x: &T) {} | ^^^^ required by this bound in `check_copy` @@ -133,7 +133,7 @@ LL | check_clone(&outer_non_clone_fn); | required by a bound introduced by this call | note: required by a bound in `check_clone` - --> $DIR/clone-impl-async.rs:71:19 + --> $DIR/clone-impl-async.rs:70:19 | LL | fn check_clone(_x: &T) {} | ^^^^^ required by this bound in `check_clone` @@ -147,7 +147,7 @@ LL | check_copy(&maybe_copy_clone_fn); | required by a bound introduced by this call | note: required by a bound in `check_copy` - --> $DIR/clone-impl-async.rs:70:18 + --> $DIR/clone-impl-async.rs:69:18 | LL | fn check_copy(_x: &T) {} | ^^^^ required by this bound in `check_copy` @@ -161,7 +161,7 @@ LL | check_clone(&maybe_copy_clone_fn); | required by a bound introduced by this call | note: required by a bound in `check_clone` - --> $DIR/clone-impl-async.rs:71:19 + --> $DIR/clone-impl-async.rs:70:19 | LL | fn check_clone(_x: &T) {} | ^^^^^ required by this bound in `check_clone` diff --git a/src/test/ui/impl-trait/issue-55872-3.rs b/src/test/ui/impl-trait/issue-55872-3.rs index 3ffce85e61ba4..91811df93cd4a 100644 --- a/src/test/ui/impl-trait/issue-55872-3.rs +++ b/src/test/ui/impl-trait/issue-55872-3.rs @@ -12,7 +12,7 @@ pub trait Bar { impl Bar for S { type E = impl std::marker::Copy; fn foo() -> Self::E { - //~^ ERROR the trait bound `impl Future: Copy` is not satisfied [E0277] + //~^ ERROR : Copy` is not satisfied [E0277] async {} } } diff --git a/src/test/ui/impl-trait/issue-55872-3.stderr b/src/test/ui/impl-trait/issue-55872-3.stderr index 6ab540e875162..c6e10f0f3504f 100644 --- a/src/test/ui/impl-trait/issue-55872-3.stderr +++ b/src/test/ui/impl-trait/issue-55872-3.stderr @@ -1,8 +1,8 @@ -error[E0277]: the trait bound `impl Future: Copy` is not satisfied +error[E0277]: the trait bound `[async block@$DIR/issue-55872-3.rs:16:9: 16:17]: Copy` is not satisfied --> $DIR/issue-55872-3.rs:14:20 | LL | fn foo() -> Self::E { - | ^^^^^^^ the trait `Copy` is not implemented for `impl Future` + | ^^^^^^^ the trait `Copy` is not implemented for `[async block@$DIR/issue-55872-3.rs:16:9: 16:17]` error: aborting due to previous error diff --git a/src/test/ui/impl-trait/issues/issue-78722.rs b/src/test/ui/impl-trait/issues/issue-78722.rs index 9ee1ba3d3b486..78233f300bdd0 100644 --- a/src/test/ui/impl-trait/issues/issue-78722.rs +++ b/src/test/ui/impl-trait/issues/issue-78722.rs @@ -7,7 +7,7 @@ type F = impl core::future::Future; struct Bug { V1: [(); { fn concrete_use() -> F { - //~^ ERROR expected `impl Future` to be a future that resolves to `u8`, but it resolves to `()` + //~^ ERROR to be a future that resolves to `u8`, but it resolves to `()` async {} } let f: F = async { 1 }; diff --git a/src/test/ui/impl-trait/issues/issue-78722.stderr b/src/test/ui/impl-trait/issues/issue-78722.stderr index a96994f5a7fb8..c00df8087e895 100644 --- a/src/test/ui/impl-trait/issues/issue-78722.stderr +++ b/src/test/ui/impl-trait/issues/issue-78722.stderr @@ -16,7 +16,7 @@ LL | let f: F = async { 1 }; LL | }], | - value is dropped here -error[E0271]: expected `impl Future` to be a future that resolves to `u8`, but it resolves to `()` +error[E0271]: expected `[async block@$DIR/issue-78722.rs:11:13: 11:21]` to be a future that resolves to `u8`, but it resolves to `()` --> $DIR/issue-78722.rs:9:30 | LL | fn concrete_use() -> F { diff --git a/src/test/ui/pattern/non-structural-match-types.stderr b/src/test/ui/pattern/non-structural-match-types.stderr index 45e1626497388..dea7c4695cc18 100644 --- a/src/test/ui/pattern/non-structural-match-types.stderr +++ b/src/test/ui/pattern/non-structural-match-types.stderr @@ -4,7 +4,7 @@ error: `[closure@$DIR/non-structural-match-types.rs:9:17: 9:19]` cannot be used LL | const { || {} } => {}, | ^^^^^^^^^^^^^^^ -error: `impl Future` cannot be used in patterns +error: `[async block@$DIR/non-structural-match-types.rs:12:17: 12:25]` cannot be used in patterns --> $DIR/non-structural-match-types.rs:12:9 | LL | const { async {} } => {}, diff --git a/src/test/ui/suggestions/expected-boxed-future-isnt-pinned.stderr b/src/test/ui/suggestions/expected-boxed-future-isnt-pinned.stderr index 918d37e65594d..34ff59a9bb050 100644 --- a/src/test/ui/suggestions/expected-boxed-future-isnt-pinned.stderr +++ b/src/test/ui/suggestions/expected-boxed-future-isnt-pinned.stderr @@ -87,7 +87,7 @@ LL | | } | arguments to this function are incorrect | = note: expected struct `Pin + Send>>` - found `async` block `impl Future` + found `async` block `[async block@$DIR/expected-boxed-future-isnt-pinned.rs:28:5: 30:6]` note: function defined here --> $SRC_DIR/core/src/future/mod.rs:LL:COL | From c26074afde3dbe5873874647709ccd4ce4b64813 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Sat, 26 Nov 2022 14:32:57 -0700 Subject: [PATCH 093/244] rustdoc: pass "true" to reset focus for notable traits --- src/librustdoc/html/static/js/main.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index ef1ae68c59eb1..7230df36c075b 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -202,7 +202,7 @@ function loadCss(cssUrl) { if (event.ctrlKey || event.altKey || event.metaKey) { return; } - window.hideAllModals(); + window.hideAllModals(false); addClass(getSettingsButton(), "rotate"); event.preventDefault(); // Sending request for the CSS and the JS files at the same time so it will @@ -378,10 +378,7 @@ function loadCss(cssUrl) { } ev.preventDefault(); searchState.defocus(); - // If the notable traits popover is open, and the user presses Escape, - // reset focus back to the link. - hideNotable(true); - window.hideAllModals(); + window.hideAllModals(true); // true = reset focus for notable traits } function handleShortcut(ev) { @@ -771,7 +768,7 @@ function loadCss(cssUrl) { }; function showSidebar() { - window.hideAllModals(); + window.hideAllModals(false); window.rustdocMobileScrollLock(); const sidebar = document.getElementsByClassName("sidebar")[0]; addClass(sidebar, "shown"); @@ -848,7 +845,7 @@ function loadCss(cssUrl) { // Make this function idempotent. return; } - window.hideAllModals(); + window.hideAllModals(false); const ty = e.getAttribute("data-ty"); const wrapper = document.createElement("div"); wrapper.innerHTML = "
" + window.NOTABLE_TRAITS[ty] + "
"; @@ -1057,13 +1054,12 @@ function loadCss(cssUrl) { /** * Hide popover menus, notable trait tooltips, and the sidebar (if applicable). * - * This version does not do anything to tweak the focused element. The caller - * should make sure it behaves reasonably. + * Pass "true" to reset focus for notable traits. */ - window.hideAllModals = function() { + window.hideAllModals = function(switchFocus) { hideSidebar(); window.hidePopoverMenus(); - hideNotable(false); + hideNotable(switchFocus); }; /** From b19e0347453b9388266ecbcf6ec85d2c66e2f9f4 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 26 Nov 2022 22:35:05 +0100 Subject: [PATCH 094/244] Switch rustdoc-gui test to function call --- src/test/rustdoc-gui/sidebar-mobile.goml | 46 +++++++++++++++--------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/src/test/rustdoc-gui/sidebar-mobile.goml b/src/test/rustdoc-gui/sidebar-mobile.goml index 453873f1b81b6..fc36401733633 100644 --- a/src/test/rustdoc-gui/sidebar-mobile.goml +++ b/src/test/rustdoc-gui/sidebar-mobile.goml @@ -48,23 +48,35 @@ compare-elements-position-near: (".block.keyword li:nth-child(1)", ".mobile-topb // Now checking the background color of the sidebar. show-text: true -local-storage: {"rustdoc-use-system-theme": "false", "rustdoc-theme": "dark"} -reload: -// Open the sidebar menu. -click: ".sidebar-menu-toggle" -assert-css: (".sidebar", {"background-color": "rgb(80, 80, 80)", "color": "rgb(221, 221, 221)"}) - -local-storage: {"rustdoc-use-system-theme": "false", "rustdoc-theme": "ayu"} -reload: - -// Open the sidebar menu. -click: ".sidebar-menu-toggle" -assert-css: (".sidebar", {"background-color": "rgb(20, 25, 31)", "color": "rgb(197, 197, 197)"}) +define-function: ( + "check-colors", + (theme, color, background), + [ + ("local-storage", {"rustdoc-use-system-theme": "false", "rustdoc-theme": |theme|}), + ("reload"), -local-storage: {"rustdoc-use-system-theme": "false", "rustdoc-theme": "light"} -reload: + // Open the sidebar menu. + ("click", ".sidebar-menu-toggle"), + ("assert-css", (".sidebar", { + "background-color": |background|, + "color": |color|, + })), + ], +) -// Open the sidebar menu. -click: ".sidebar-menu-toggle" -assert-css: (".sidebar", {"background-color": "rgb(245, 245, 245)", "color": "rgb(0, 0, 0)"}) +call-function: ("check-colors", { + "theme": "ayu", + "color": "rgb(197, 197, 197)", + "background": "rgb(20, 25, 31)", +}) +call-function: ("check-colors", { + "theme": "dark", + "color": "rgb(221, 221, 221)", + "background": "rgb(80, 80, 80)", +}) +call-function: ("check-colors", { + "theme": "light", + "color": "rgb(0, 0, 0)", + "background": "rgb(245, 245, 245)", +}) From 451259811a7b0783ef413945e176a65d8b2162bb Mon Sep 17 00:00:00 2001 From: Markus Everling Date: Sat, 26 Nov 2022 22:48:20 +0100 Subject: [PATCH 095/244] Improve slow path in `make_contiguous` --- .../alloc/src/collections/vec_deque/mod.rs | 94 +++++++++++++------ .../alloc/src/collections/vec_deque/tests.rs | 18 ++-- 2 files changed, 76 insertions(+), 36 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 52b46e448c458..86d77182bccee 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -2152,37 +2152,77 @@ impl VecDeque { self.head = tail; } else { - // free is smaller than both head and tail, - // this means we have to slowly "swap" the tail and the head. + // ´free` is smaller than both `head_len` and `tail_len`. + // the general algorithm for this first moves the slices + // right next to each other and then uses `slice::rotate` + // to rotate them into place: // - // from: EFGHI...ABCD or HIJK.ABCDEFG - // to: ABCDEFGHI... or ABCDEFGHIJK. - let mut left_edge: usize = 0; - let mut right_edge: usize = head; - unsafe { - // The general problem looks like this - // GHIJKLM...ABCDEF - before any swaps - // ABCDEFM...GHIJKL - after 1 pass of swaps - // ABCDEFGHIJM...KL - swap until the left edge reaches the temp store - // - then restart the algorithm with a new (smaller) store - // Sometimes the temp store is reached when the right edge is at the end - // of the buffer - this means we've hit the right order with fewer swaps! - // E.g - // EF..ABCD - // ABCDEF.. - after four only swaps we've finished - while left_edge < len && right_edge != cap { - let mut right_offset = 0; - for i in left_edge..right_edge { - right_offset = (i - left_edge) % (cap - right_edge); - let src = right_edge + right_offset; - ptr::swap(ptr.add(i), ptr.add(src)); + // initially: HIJK..ABCDEFG + // step 1: ..HIJKABCDEFG + // step 2: ..ABCDEFGHIJK + // + // or: + // + // initially: FGHIJK..ABCDE + // step 1: FGHIJKABCDE.. + // step 2: ABCDEFGHIJK.. + + // pick the shorter of the 2 slices to reduce the amount + // of memory that needs to be moved around. + if head_len > tail_len { + // tail is shorter, so: + // 1. copy tail forwards + // 2. rotate used part of the buffer + // 3. update head to point to the new beginning (which is just `free`) + + unsafe { + // if there is no free space in the buffer, then the slices are already + // right next to each other and we don't need to move any memory. + if free != 0 { + // because we only move the tail forward as much as there's free space + // behind it, we don't overwrite any elements of the head slice, and + // the slices end up right next to each other. + self.copy(0, free, tail_len); } - let n_ops = right_edge - left_edge; - left_edge += n_ops; - right_edge += right_offset + 1; + + // We just copied the tail right next to the head slice, + // so all of the elements in the range are initialized + let slice = &mut *self.buffer_range(free..self.capacity()); + + // because the deque wasn't contiguous, we know that `tail_len < self.len == slice.len()`, + // so this will never panic. + slice.rotate_left(tail_len); + + // the used part of the buffer now is `free..self.capacity()`, so set + // `head` to the beginning of that range. + self.head = free; } + } else { + // head is shorter so: + // 1. copy head backwards + // 2. rotate used part of the buffer + // 3. update head to point to the new beginning (which is the beginning of the buffer) - self.head = 0; + unsafe { + // if there is no free space in the buffer, then the slices are already + // right next to each other and we don't need to move any memory. + if free != 0 { + // copy the head slice to lie right behind the tail slice. + self.copy(self.head, tail_len, head_len); + } + + // because we copied the head slice so that both slices lie right + // next to each other, all the elements in the range are initialized. + let slice = &mut *self.buffer_range(0..self.len); + + // because the deque wasn't contiguous, we know that `head_len < self.len == slice.len()` + // so this will never panic. + slice.rotate_right(head_len); + + // the used part of the buffer now is `0..self.len`, so set + // `head` to the beginning of that range. + self.head = 0; + } } } diff --git a/library/alloc/src/collections/vec_deque/tests.rs b/library/alloc/src/collections/vec_deque/tests.rs index 553f8da3e2d01..2515c57874ce8 100644 --- a/library/alloc/src/collections/vec_deque/tests.rs +++ b/library/alloc/src/collections/vec_deque/tests.rs @@ -465,7 +465,7 @@ fn test_binary_search_key() { } #[test] -fn make_contiguous_big_tail() { +fn make_contiguous_big_head() { let mut tester = VecDeque::with_capacity(15); for i in 0..3 { @@ -480,14 +480,14 @@ fn make_contiguous_big_tail() { assert_eq!(tester.capacity(), 15); assert_eq!((&[9, 8, 7, 6, 5, 4, 3] as &[_], &[0, 1, 2] as &[_]), tester.as_slices()); - let expected_start = tester.to_physical_idx(tester.len); + let expected_start = tester.as_slices().1.len(); tester.make_contiguous(); assert_eq!(tester.head, expected_start); assert_eq!((&[9, 8, 7, 6, 5, 4, 3, 0, 1, 2] as &[_], &[] as &[_]), tester.as_slices()); } #[test] -fn make_contiguous_big_head() { +fn make_contiguous_big_tail() { let mut tester = VecDeque::with_capacity(15); for i in 0..8 { @@ -507,13 +507,13 @@ fn make_contiguous_big_head() { #[test] fn make_contiguous_small_free() { - let mut tester = VecDeque::with_capacity(15); + let mut tester = VecDeque::with_capacity(16); - for i in 'A' as u8..'I' as u8 { + for i in b'A'..b'I' { tester.push_back(i as char); } - for i in 'I' as u8..'N' as u8 { + for i in b'I'..b'N' { tester.push_front(i as char); } @@ -529,16 +529,16 @@ fn make_contiguous_small_free() { ); tester.clear(); - for i in 'I' as u8..'N' as u8 { + for i in b'I'..b'N' { tester.push_back(i as char); } - for i in 'A' as u8..'I' as u8 { + for i in b'A'..b'I' { tester.push_front(i as char); } // IJKLM...HGFEDCBA - let expected_start = 0; + let expected_start = 3; tester.make_contiguous(); assert_eq!(tester.head, expected_start); assert_eq!( From 69a07f796c1b35875c94f5f6b4d635cb732db271 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 26 Nov 2022 21:57:04 +0000 Subject: [PATCH 096/244] Revert "Add fatal overflow test" This reverts commit 9decfff6f87d3e5760fd61375c3d27fa45a83e52. --- src/test/ui/typeck/hang-in-overflow.rs | 19 ------------------- src/test/ui/typeck/hang-in-overflow.stderr | 22 ---------------------- 2 files changed, 41 deletions(-) delete mode 100644 src/test/ui/typeck/hang-in-overflow.rs delete mode 100644 src/test/ui/typeck/hang-in-overflow.stderr diff --git a/src/test/ui/typeck/hang-in-overflow.rs b/src/test/ui/typeck/hang-in-overflow.rs deleted file mode 100644 index a8330c9b65c31..0000000000000 --- a/src/test/ui/typeck/hang-in-overflow.rs +++ /dev/null @@ -1,19 +0,0 @@ -// normalize-stderr-test "the requirement `.*`" -> "the requirement `...`" -// normalize-stderr-test "required for `.*` to implement `.*`" -> "required for `...` to implement `...`" -// normalize-stderr-test: ".*the full type name has been written to.*\n" -> "" - -// Currently this fatally aborts instead of hanging. -// Make sure at least that this doesn't turn into a hang. - -fn f() { - foo::<_>(); - //~^ ERROR overflow evaluating the requirement -} - -fn foo() -where - Vec<[[[B; 1]; 1]; 1]>: PartialEq, -{ -} - -fn main() {} diff --git a/src/test/ui/typeck/hang-in-overflow.stderr b/src/test/ui/typeck/hang-in-overflow.stderr deleted file mode 100644 index 7a7b85b19b4ee..0000000000000 --- a/src/test/ui/typeck/hang-in-overflow.stderr +++ /dev/null @@ -1,22 +0,0 @@ -error[E0275]: overflow evaluating the requirement `...` - --> $DIR/hang-in-overflow.rs:9:5 - | -LL | foo::<_>(); - | ^^^^^^^^ - | - = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`hang_in_overflow`) - = note: required for `...` to implement `...` - = note: 127 redundant requirements hidden - = note: required for `...` to implement `...` -note: required by a bound in `foo` - --> $DIR/hang-in-overflow.rs:15:28 - | -LL | fn foo() - | --- required by a bound in this -LL | where -LL | Vec<[[[B; 1]; 1]; 1]>: PartialEq, - | ^^^^^^^^^^^^ required by this bound in `foo` - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0275`. From acf95adfe2e1cee70ddb30abc42f1c47becbdc55 Mon Sep 17 00:00:00 2001 From: Markus Everling Date: Sat, 26 Nov 2022 23:08:57 +0100 Subject: [PATCH 097/244] Add second test case in `make_contiguous_head_to_end` --- .../alloc/src/collections/vec_deque/tests.rs | 59 +++++++++++++++---- 1 file changed, 49 insertions(+), 10 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/tests.rs b/library/alloc/src/collections/vec_deque/tests.rs index 2515c57874ce8..220ad71beabd4 100644 --- a/library/alloc/src/collections/vec_deque/tests.rs +++ b/library/alloc/src/collections/vec_deque/tests.rs @@ -549,16 +549,55 @@ fn make_contiguous_small_free() { #[test] fn make_contiguous_head_to_end() { - let mut dq = VecDeque::with_capacity(3); - dq.push_front('B'); - dq.push_front('A'); - dq.push_back('C'); - dq.make_contiguous(); - let expected_head = 0; - let expected_len = 3; - assert_eq!(expected_head, dq.head); - assert_eq!(expected_len, dq.len); - assert_eq!((&['A', 'B', 'C'] as &[_], &[] as &[_]), dq.as_slices()); + let mut tester = VecDeque::with_capacity(16); + + for i in b'A'..b'L' { + tester.push_back(i as char); + } + + for i in b'L'..b'Q' { + tester.push_front(i as char); + } + + assert_eq!( + tester, + ['P', 'O', 'N', 'M', 'L', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'] + ); + + // ABCDEFGHIJKPONML + let expected_start = 0; + tester.make_contiguous(); + assert_eq!(tester.head, expected_start); + assert_eq!( + ( + &['P', 'O', 'N', 'M', 'L', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'] + as &[_], + &[] as &[_] + ), + tester.as_slices() + ); + + tester.clear(); + for i in b'L'..b'Q' { + tester.push_back(i as char); + } + + for i in b'A'..b'L' { + tester.push_front(i as char); + } + + // LMNOPKJIHGFEDCBA + let expected_start = 0; + tester.make_contiguous(); + assert_eq!(tester.head, expected_start); + assert_eq!( + ( + &['K', 'J', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A', 'L', 'M', 'N', 'O', 'P'] + as &[_], + &[] as &[_] + ), + tester.as_slices() + ); } #[test] From c285218f43027a9257d619b42643a8a10ba6af7d Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 26 Nov 2022 22:15:43 +0000 Subject: [PATCH 098/244] Revert "Drive-by: Don't manually call evaluate_obligation_no_overflow" This reverts commit a884a9e634b827781e77bddf4082f1196301d86f. --- .../src/traits/error_reporting/suggestions.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index da6ca30cc9a32..50816d12025b7 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -1336,8 +1336,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { obligation.param_env, trait_pred_and_suggested_ty, ); - let suggested_ty_would_satisfy_obligation = - self.predicate_must_hold_modulo_regions(&new_obligation); + let suggested_ty_would_satisfy_obligation = self + .evaluate_obligation_no_overflow(&new_obligation) + .must_apply_modulo_regions(); if suggested_ty_would_satisfy_obligation { let sp = self .tcx From 4149923ff0a948161e85544c2f68dfd828ddb382 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 26 Nov 2022 22:15:51 +0000 Subject: [PATCH 099/244] Revert "Do not need to account for overflow in predicate_can_apply" This reverts commit cbe932801892da06688b53638622be1c8a1c8974. --- .../src/traits/error_reporting/mod.rs | 5 +---- .../ui/traits/predicate_can_apply-hang.rs | 6 ------ .../ui/traits/predicate_can_apply-hang.stderr | 21 ------------------- 3 files changed, 1 insertion(+), 31 deletions(-) delete mode 100644 src/test/ui/traits/predicate_can_apply-hang.rs delete mode 100644 src/test/ui/traits/predicate_can_apply-hang.stderr diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index 4ac53f6302fbf..e06ee85126402 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -2544,10 +2544,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> { let obligation = Obligation::new(self.tcx, ObligationCause::dummy(), param_env, cleaned_pred); - // We don't use `InferCtxt::predicate_may_hold` because that - // will re-run predicates that overflow locally, which ends up - // taking a really long time to compute. - self.evaluate_obligation(&obligation).map_or(false, |eval| eval.may_apply()) + self.predicate_may_hold(&obligation) }) } diff --git a/src/test/ui/traits/predicate_can_apply-hang.rs b/src/test/ui/traits/predicate_can_apply-hang.rs deleted file mode 100644 index 5f01645da5242..0000000000000 --- a/src/test/ui/traits/predicate_can_apply-hang.rs +++ /dev/null @@ -1,6 +0,0 @@ -fn f(x: Vec<[[[B; 1]; 1]; 1]>) -> impl PartialEq { - //~^ ERROR can't compare `Vec<[[[B; 1]; 1]; 1]>` with `B` - x -} - -fn main() {} diff --git a/src/test/ui/traits/predicate_can_apply-hang.stderr b/src/test/ui/traits/predicate_can_apply-hang.stderr deleted file mode 100644 index 49fe63b412ac9..0000000000000 --- a/src/test/ui/traits/predicate_can_apply-hang.stderr +++ /dev/null @@ -1,21 +0,0 @@ -error[E0277]: can't compare `Vec<[[[B; 1]; 1]; 1]>` with `B` - --> $DIR/predicate_can_apply-hang.rs:1:38 - | -LL | fn f(x: Vec<[[[B; 1]; 1]; 1]>) -> impl PartialEq { - | ^^^^^^^^^^^^^^^^^ no implementation for `Vec<[[[B; 1]; 1]; 1]> == B` -LL | -LL | x - | - return type was inferred to be `Vec<[[[B; 1]; 1]; 1]>` here - | - = help: the trait `PartialEq` is not implemented for `Vec<[[[B; 1]; 1]; 1]>` - = help: the following other types implement trait `PartialEq`: - as PartialEq>> - as PartialEq<&[U; N]>> - as PartialEq<&[U]>> - as PartialEq<&mut [U]>> - as PartialEq<[U; N]>> - as PartialEq<[U]>> - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0277`. From be188807454001cb8aacffcccfd46adc771268c7 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 26 Nov 2022 15:24:01 -0800 Subject: [PATCH 100/244] Assign myself for docs updates This sets myself as the reviewer for docs submodule updates. Now with https://github.com/rust-lang/triagebot/pull/1673 automating the process, this piece of the puzzle handles the assignment step. --- triagebot.toml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/triagebot.toml b/triagebot.toml index 985e065652d62..040274dd5701d 100644 --- a/triagebot.toml +++ b/triagebot.toml @@ -568,6 +568,13 @@ fallback = [ "/src/bootstrap" = ["bootstrap"] "/src/ci" = ["infra-ci"] "/src/doc" = ["docs"] +"/src/doc/book" = ["@ehuss"] +"/src/doc/edition-guide" = ["@ehuss"] +"/src/doc/embedded-book" = ["@ehuss"] +"/src/doc/nomicon" = ["@ehuss"] +"/src/doc/reference" = ["@ehuss"] +"/src/doc/rust-by-example" = ["@ehuss"] +"/src/doc/rustc-dev-guide" = ["@ehuss"] "/src/doc/rustdoc" = ["rustdoc"] "/src/etc" = ["@Mark-Simulacrum"] "/src/librustdoc" = ["rustdoc"] From 1e5dad0bef3e06fb96aae47d5fe21051dbda0a40 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Fri, 25 Nov 2022 22:37:04 +0000 Subject: [PATCH 101/244] Update cargo 5 commits in ba607b23db8398723d659249d9abf5536bc322e5..e027c4b5d25af2119b1956fac42863b9b3242744 2022-11-22 20:52:39 +0000 to 2022-11-25 19:44:46 +0000 - fix: Move off atty to resolve soundness issue (rust-lang/cargo#11420) - add newline char to `cargo install .` error message for easier reading. (rust-lang/cargo#11401) - chore: Upgrade to env_logger (rust-lang/cargo#11417) - Change rustdoc-scrape-examples to be a target-level configuration (rust-lang/cargo#10343) - temporarily disable test `lto::test_profile` (rust-lang/cargo#11419) --- Cargo.lock | 81 ++++++++++++++++++++++- src/tools/cargo | 2 +- src/tools/rustc-workspace-hack/Cargo.toml | 2 + 3 files changed, 82 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d8612b3a2561b..dbf1e06ee6e92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -288,7 +288,6 @@ name = "cargo" version = "0.68.0" dependencies = [ "anyhow", - "atty", "bytesize", "cargo-platform 0.1.2", "cargo-test-macro", @@ -298,7 +297,7 @@ dependencies = [ "crates-io", "curl", "curl-sys", - "env_logger 0.9.0", + "env_logger 0.10.0", "filetime", "flate2", "fwdansi", @@ -312,6 +311,7 @@ dependencies = [ "ignore", "im-rc", "indexmap", + "is-terminal", "itertools", "jobserver", "lazy_static", @@ -1213,6 +1213,40 @@ dependencies = [ "termcolor", ] +[[package]] +name = "env_logger" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0" +dependencies = [ + "humantime 2.0.1", + "is-terminal", + "log", + "regex", + "termcolor", +] + +[[package]] +name = "errno" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1" +dependencies = [ + "errno-dragonfly", + "libc", + "winapi", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "error_index_generator" version = "0.0.0" @@ -1907,6 +1941,28 @@ dependencies = [ "unic-langid", ] +[[package]] +name = "io-lifetimes" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e394faa0efb47f9f227f1cd89978f854542b318a6f64fa695489c9c993056656" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "is-terminal" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aae5bc6e2eb41c9def29a3e0f1306382807764b9b53112030eff57435667352d" +dependencies = [ + "hermit-abi 0.2.6", + "io-lifetimes", + "rustix", + "windows-sys", +] + [[package]] name = "itertools" version = "0.10.5" @@ -2116,6 +2172,12 @@ dependencies = [ "walkdir", ] +[[package]] +name = "linux-raw-sys" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f9f08d8963a6c613f4b1a78f4f4a4dbfadf8e6545b2d72861731e4858b8b47f" + [[package]] name = "litemap" version = "0.6.0" @@ -3193,6 +3255,7 @@ version = "1.0.0" dependencies = [ "bstr 0.2.17", "clap 3.2.20", + "libc", "libz-sys", "rand 0.8.5", "regex", @@ -4508,6 +4571,20 @@ dependencies = [ "unicode_categories", ] +[[package]] +name = "rustix" +version = "0.36.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b1fbb4dfc4eb1d390c02df47760bb19a84bb80b301ecc947ab5406394d8223e" +dependencies = [ + "bitflags", + "errno", + "io-lifetimes", + "libc", + "linux-raw-sys", + "windows-sys", +] + [[package]] name = "rustversion" version = "1.0.5" diff --git a/src/tools/cargo b/src/tools/cargo index ba607b23db839..e027c4b5d25af 160000 --- a/src/tools/cargo +++ b/src/tools/cargo @@ -1 +1 @@ -Subproject commit ba607b23db8398723d659249d9abf5536bc322e5 +Subproject commit e027c4b5d25af2119b1956fac42863b9b3242744 diff --git a/src/tools/rustc-workspace-hack/Cargo.toml b/src/tools/rustc-workspace-hack/Cargo.toml index 07cf89f7d338d..a5f0c0f320a8c 100644 --- a/src/tools/rustc-workspace-hack/Cargo.toml +++ b/src/tools/rustc-workspace-hack/Cargo.toml @@ -75,6 +75,8 @@ features = [ bstr = { version = "0.2.17", features = ["default"] } clap = { version = "3.1.1", features = ["derive", "clap_derive"]} curl-sys = { version = "0.4.13", features = ["http2", "libnghttp2-sys"], optional = true } +# Ensure `extra_traits` of libc, which is used transitively by Cargo. +libc = { version = "0.2", features = ["extra_traits"] } # Ensure default features of libz-sys, which are disabled in some scenarios. libz-sys = { version = "1.1.2" } # Ensure default features of regex, which are disabled in some scenarios. From 353cef9654444799ed03576acbcc3af26f9899fd Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Fri, 8 Apr 2022 22:28:33 -0700 Subject: [PATCH 102/244] Use target exe_suffix for doctests This will use rust_out.exe for doctests on Windows, rust_out.wasm for doctests in the wasm case, and also handles cross-compiling or user-provided targets. --- src/librustdoc/doctest.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index cb50c3ae829da..81d9c46447a37 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -19,7 +19,7 @@ use rustc_span::edition::Edition; use rustc_span::source_map::SourceMap; use rustc_span::symbol::sym; use rustc_span::{BytePos, FileName, Pos, Span, DUMMY_SP}; -use rustc_target::spec::TargetTriple; +use rustc_target::spec::{Target, TargetTriple}; use tempfile::Builder as TempFileBuilder; use std::env; @@ -293,6 +293,16 @@ struct UnusedExterns { unused_extern_names: Vec, } +fn add_exe_suffix(input: String, target: &TargetTriple) -> String { + let exe_suffix = match target { + TargetTriple::TargetTriple(_) => Target::expect_builtin(target).options.exe_suffix, + TargetTriple::TargetJson { contents, .. } => { + Target::from_json(contents.parse().unwrap()).unwrap().0.options.exe_suffix + } + }; + input + &exe_suffix +} + fn run_test( test: &str, crate_name: &str, @@ -313,7 +323,9 @@ fn run_test( let (test, line_offset, supports_color) = make_test(test, Some(crate_name), lang_string.test_harness, opts, edition, Some(test_id)); - let output_file = outdir.path().join("rust_out"); + // Make sure we emit well-formed executable names for our target. + let rust_out = add_exe_suffix("rust_out".to_owned(), &target); + let output_file = outdir.path().join(rust_out); let rustc_binary = rustdoc_options .test_builder From 47ddca6812300da235073c0cbef058ff3bd033bf Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Sat, 26 Nov 2022 18:47:52 -0800 Subject: [PATCH 103/244] Attempt to solve problem with globs --- src/test/run-make/coverage-reports/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/run-make/coverage-reports/Makefile b/src/test/run-make/coverage-reports/Makefile index 407992c9f43d8..436aebf1174dd 100644 --- a/src/test/run-make/coverage-reports/Makefile +++ b/src/test/run-make/coverage-reports/Makefile @@ -132,7 +132,7 @@ include clear_expected_if_blessed --instr-profile="$(TMPDIR)"/$@.profdata \ $(call BIN,"$(TMPDIR)"/$@) \ $$( \ - for file in $(TMPDIR)/rustdoc-$@/*/rust_out; do \ + for file in $(TMPDIR)/rustdoc-$@/*/rust_out*; do \ [ -x "$$file" ] && printf "%s %s " -object $$file; \ done \ ) \ From 8d148988caf8a42f38cccd88a75a02c1c4968ad4 Mon Sep 17 00:00:00 2001 From: Pratush Rai Date: Sun, 27 Nov 2022 10:05:19 +0530 Subject: [PATCH 104/244] suggested changes --- src/bootstrap/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index 108de217d7bfa..5411647c065b7 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -647,11 +647,11 @@ impl Build { if !update(true).status().map_or(false, |status| status.success()) { self.run(&mut update(false)); } - // + self.run(Command::new("git").args(&["stash", "push"]).current_dir(&absolute_path)); self.run(Command::new("git").args(&["reset", "-q", "--hard"]).current_dir(&absolute_path)); self.run(Command::new("git").args(&["clean", "-qdfx"]).current_dir(&absolute_path)); - self.run(Command::new("git").args(&["stash", "pop"]).current_dir(absolute_path)) + self.run(Command::new("git").args(&["stash", "pop"]).current_dir(absolute_path)); } /// If any submodule has been initialized already, sync it unconditionally. From 881862ecb79a447c9631becb7f35c1c47b406346 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Tue, 22 Nov 2022 18:12:12 +0000 Subject: [PATCH 105/244] Rename `fn_trait_kind_from_{from_lang=>def_id}` to better convey meaning --- compiler/rustc_hir_typeck/src/closure.rs | 6 +++--- .../error_reporting/nice_region_error/placeholder_error.rs | 2 +- compiler/rustc_middle/src/middle/lang_items.rs | 2 +- compiler/rustc_middle/src/ty/print/pretty.rs | 2 +- compiler/rustc_mir_transform/src/shim.rs | 2 +- .../rustc_trait_selection/src/traits/error_reporting/mod.rs | 2 +- .../src/traits/error_reporting/suggestions.rs | 2 +- .../src/traits/select/candidate_assembly.rs | 4 ++-- .../rustc_trait_selection/src/traits/select/confirmation.rs | 2 +- compiler/rustc_ty_utils/src/instance.rs | 2 +- src/librustdoc/clean/utils.rs | 2 +- 11 files changed, 14 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/closure.rs b/compiler/rustc_hir_typeck/src/closure.rs index e2fefd2724f7e..11a5f2b0d90e7 100644 --- a/compiler/rustc_hir_typeck/src/closure.rs +++ b/compiler/rustc_hir_typeck/src/closure.rs @@ -178,7 +178,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { }); let kind = object_type .principal_def_id() - .and_then(|did| self.tcx.fn_trait_kind_from_lang_item(did)); + .and_then(|did| self.tcx.fn_trait_kind_from_def_id(did)); (sig, kind) } ty::Infer(ty::TyVar(vid)) => self.deduce_signature_from_predicates( @@ -235,7 +235,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { _ => None, }; if let Some(closure_kind) = - trait_def_id.and_then(|def_id| self.tcx.fn_trait_kind_from_lang_item(def_id)) + trait_def_id.and_then(|def_id| self.tcx.fn_trait_kind_from_def_id(def_id)) { expected_kind = Some( expected_kind @@ -263,7 +263,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let trait_def_id = projection.trait_def_id(tcx); - let is_fn = tcx.fn_trait_kind_from_lang_item(trait_def_id).is_some(); + let is_fn = tcx.fn_trait_kind_from_def_id(trait_def_id).is_some(); let gen_trait = tcx.require_lang_item(LangItem::Generator, cause_span); let is_gen = gen_trait == trait_def_id; if !is_fn && !is_gen { diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs index a585168294a29..3a74ac51b2c27 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs @@ -401,7 +401,7 @@ impl<'tcx> NiceRegionError<'_, 'tcx> { if self_ty.value.is_closure() && self .tcx() - .fn_trait_kind_from_lang_item(expected_trait_ref.value.def_id) + .fn_trait_kind_from_def_id(expected_trait_ref.value.def_id) .is_some() { let closure_sig = self_ty.map(|closure| { diff --git a/compiler/rustc_middle/src/middle/lang_items.rs b/compiler/rustc_middle/src/middle/lang_items.rs index dd4332d0db6d2..e1bcb74b281c4 100644 --- a/compiler/rustc_middle/src/middle/lang_items.rs +++ b/compiler/rustc_middle/src/middle/lang_items.rs @@ -27,7 +27,7 @@ impl<'tcx> TyCtxt<'tcx> { }) } - pub fn fn_trait_kind_from_lang_item(self, id: DefId) -> Option { + pub fn fn_trait_kind_from_def_id(self, id: DefId) -> Option { let items = self.lang_items(); match Some(id) { x if x == items.fn_trait() => Some(ty::ClosureKind::Fn), diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index d5e196b2e9faa..6afb8bdc45743 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -1085,7 +1085,7 @@ pub trait PrettyPrinter<'tcx>: let mut resugared = false; // Special-case `Fn(...) -> ...` and re-sugar it. - let fn_trait_kind = cx.tcx().fn_trait_kind_from_lang_item(principal.def_id); + let fn_trait_kind = cx.tcx().fn_trait_kind_from_def_id(principal.def_id); if !cx.should_print_verbose() && fn_trait_kind.is_some() { if let ty::Tuple(tys) = principal.substs.type_at(0).kind() { let mut projections = predicates.projection_bounds(); diff --git a/compiler/rustc_mir_transform/src/shim.rs b/compiler/rustc_mir_transform/src/shim.rs index 68703eb0a232e..a115bb2831a4b 100644 --- a/compiler/rustc_mir_transform/src/shim.rs +++ b/compiler/rustc_mir_transform/src/shim.rs @@ -37,7 +37,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> Body<' } ty::InstanceDef::FnPtrShim(def_id, ty) => { let trait_ = tcx.trait_of_item(def_id).unwrap(); - let adjustment = match tcx.fn_trait_kind_from_lang_item(trait_) { + let adjustment = match tcx.fn_trait_kind_from_def_id(trait_) { Some(ty::ClosureKind::FnOnce) => Adjustment::Identity, Some(ty::ClosureKind::FnMut | ty::ClosureKind::Fn) => Adjustment::Deref, None => bug!("fn pointer {:?} is not an fn", ty), diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index 4ac53f6302fbf..c466e1e29ad80 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -2155,7 +2155,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> { if generics.params.iter().any(|p| p.name != kw::SelfUpper) && !snippet.ends_with('>') && !generics.has_impl_trait() - && !self.tcx.fn_trait_kind_from_lang_item(def_id).is_some() + && !self.tcx.fn_trait_kind_from_def_id(def_id).is_some() { // FIXME: To avoid spurious suggestions in functions where type arguments // where already supplied, we check the snippet to make sure it doesn't diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index da6ca30cc9a32..07ba4d0c3b856 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -1680,7 +1680,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { let inputs = trait_ref.skip_binder().substs.type_at(1); let sig = match inputs.kind() { ty::Tuple(inputs) - if infcx.tcx.fn_trait_kind_from_lang_item(trait_ref.def_id()).is_some() => + if infcx.tcx.fn_trait_kind_from_def_id(trait_ref.def_id()).is_some() => { infcx.tcx.mk_fn_sig( inputs.iter(), diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs index f8c7a896b530b..460ca923c6e29 100644 --- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs +++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs @@ -451,7 +451,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { obligation: &TraitObligation<'tcx>, candidates: &mut SelectionCandidateSet<'tcx>, ) { - let Some(kind) = self.tcx().fn_trait_kind_from_lang_item(obligation.predicate.def_id()) else { + let Some(kind) = self.tcx().fn_trait_kind_from_def_id(obligation.predicate.def_id()) else { return; }; @@ -489,7 +489,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { candidates: &mut SelectionCandidateSet<'tcx>, ) { // We provide impl of all fn traits for fn pointers. - if self.tcx().fn_trait_kind_from_lang_item(obligation.predicate.def_id()).is_none() { + if self.tcx().fn_trait_kind_from_def_id(obligation.predicate.def_id()).is_none() { return; } diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index 8c589aa8cd1de..28e742688a914 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -735,7 +735,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { ) -> Result>, SelectionError<'tcx>> { let kind = self .tcx() - .fn_trait_kind_from_lang_item(obligation.predicate.def_id()) + .fn_trait_kind_from_def_id(obligation.predicate.def_id()) .unwrap_or_else(|| bug!("closure candidate for non-fn trait {:?}", obligation)); // Okay to skip binder because the substs on closure types never diff --git a/compiler/rustc_ty_utils/src/instance.rs b/compiler/rustc_ty_utils/src/instance.rs index 4a887bc591827..c6f2b16ca2102 100644 --- a/compiler/rustc_ty_utils/src/instance.rs +++ b/compiler/rustc_ty_utils/src/instance.rs @@ -209,7 +209,7 @@ fn resolve_associated_item<'tcx>( substs: future_data.substs, }), traits::ImplSource::Closure(closure_data) => { - let trait_closure_kind = tcx.fn_trait_kind_from_lang_item(trait_id).unwrap(); + let trait_closure_kind = tcx.fn_trait_kind_from_def_id(trait_id).unwrap(); Instance::resolve_closure( tcx, closure_data.closure_def_id, diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 35e2720fdff89..d540d828d3b10 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -106,7 +106,7 @@ fn external_generic_args<'tcx>( ) -> GenericArgs { let args = substs_to_args(cx, substs, has_self); - if cx.tcx.fn_trait_kind_from_lang_item(did).is_some() { + if cx.tcx.fn_trait_kind_from_def_id(did).is_some() { let inputs = // The trait's first substitution is the one after self, if there is one. match substs.iter().nth(if has_self { 1 } else { 0 }).unwrap().expect_ty().kind() { From d0c7ed3beaaef613d2b0676c7808ee818cb8c8bc Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Tue, 22 Nov 2022 18:24:50 +0000 Subject: [PATCH 106/244] Remove `ty::ClosureKind::from_def_id` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …in favour of `TyCtxt::fn_trait_kind_from_def_id` --- compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs | 4 ++-- compiler/rustc_middle/src/ty/closure.rs | 15 +++------------ .../src/traits/error_reporting/mod.rs | 9 +++++---- .../src/traits/error_reporting/suggestions.rs | 4 ++-- 4 files changed, 12 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index ecf6f458ca3f7..5c7d21fc2e418 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -2089,7 +2089,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { && let maybe_trait_item_def_id = assoc_item.trait_item_def_id.unwrap_or(def_id) && let maybe_trait_def_id = self.tcx.parent(maybe_trait_item_def_id) // Just an easy way to check "trait_def_id == Fn/FnMut/FnOnce" - && let Some(call_kind) = ty::ClosureKind::from_def_id(self.tcx, maybe_trait_def_id) + && let Some(call_kind) = self.tcx.fn_trait_kind_from_def_id(maybe_trait_def_id) && let Some(callee_ty) = callee_ty { let callee_ty = callee_ty.peel_refs(); @@ -2115,7 +2115,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { { if let ty::PredicateKind::Clause(ty::Clause::Trait(pred)) = predicate.kind().skip_binder() && pred.self_ty().peel_refs() == callee_ty - && ty::ClosureKind::from_def_id(self.tcx, pred.def_id()).is_some() + && self.tcx.fn_trait_kind_from_def_id(pred.def_id()).is_some() { err.span_note(span, "callable defined here"); return; diff --git a/compiler/rustc_middle/src/ty/closure.rs b/compiler/rustc_middle/src/ty/closure.rs index 273a61c966c72..2653c8576de6c 100644 --- a/compiler/rustc_middle/src/ty/closure.rs +++ b/compiler/rustc_middle/src/ty/closure.rs @@ -118,18 +118,9 @@ impl<'tcx> ClosureKind { } } - pub fn from_def_id(tcx: TyCtxt<'_>, def_id: DefId) -> Option { - if Some(def_id) == tcx.lang_items().fn_once_trait() { - Some(ClosureKind::FnOnce) - } else if Some(def_id) == tcx.lang_items().fn_mut_trait() { - Some(ClosureKind::FnMut) - } else if Some(def_id) == tcx.lang_items().fn_trait() { - Some(ClosureKind::Fn) - } else { - None - } - } - + /// Converts `self` to a [`DefId`] of the corresponding trait. + /// + /// Note: the inverse of this function is [`TyCtxt::fn_trait_kind_from_def_id`] pub fn to_def_id(&self, tcx: TyCtxt<'_>) -> DefId { tcx.require_lang_item( match self { diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index c466e1e29ad80..de35fbfe6d0db 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -357,7 +357,8 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> { ocx.register_obligation(obligation); if ocx.select_all_or_error().is_empty() { return Ok(( - ty::ClosureKind::from_def_id(self.tcx, trait_def_id) + self.tcx + .fn_trait_kind_from_def_id(trait_def_id) .expect("expected to map DefId to ClosureKind"), ty.rebind(self.resolve_vars_if_possible(var)), )); @@ -686,7 +687,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { } ObligationCauseCode::BindingObligation(def_id, _) | ObligationCauseCode::ItemObligation(def_id) - if ty::ClosureKind::from_def_id(tcx, *def_id).is_some() => + if tcx.fn_trait_kind_from_def_id(*def_id).is_some() => { err.code(rustc_errors::error_code!(E0059)); err.set_primary_message(format!( @@ -847,7 +848,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { } let is_fn_trait = - ty::ClosureKind::from_def_id(tcx, trait_ref.def_id()).is_some(); + tcx.fn_trait_kind_from_def_id(trait_ref.def_id()).is_some(); let is_target_feature_fn = if let ty::FnDef(def_id, _) = *trait_ref.skip_binder().self_ty().kind() { @@ -877,7 +878,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { // Note if the `FnMut` or `FnOnce` is less general than the trait we're trying // to implement. let selected_kind = - ty::ClosureKind::from_def_id(self.tcx, trait_ref.def_id()) + self.tcx.fn_trait_kind_from_def_id(trait_ref.def_id()) .expect("expected to map DefId to ClosureKind"); if !implemented_kind.extends(selected_kind) { err.note( diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 07ba4d0c3b856..4ae9b22f22171 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -1752,7 +1752,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { && let predicates = self.tcx.predicates_of(def_id).instantiate_identity(self.tcx) && let Some(pred) = predicates.predicates.get(*idx) && let ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) = pred.kind().skip_binder() - && ty::ClosureKind::from_def_id(self.tcx, trait_pred.def_id()).is_some() + && self.tcx.fn_trait_kind_from_def_id(trait_pred.def_id()).is_some() { let expected_self = self.tcx.anonymize_late_bound_regions(pred.kind().rebind(trait_pred.self_ty())); @@ -1766,7 +1766,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { .enumerate() .find(|(other_idx, (pred, _))| match pred.kind().skip_binder() { ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) - if ty::ClosureKind::from_def_id(self.tcx, trait_pred.def_id()) + if self.tcx.fn_trait_kind_from_def_id(trait_pred.def_id()) .is_some() && other_idx != idx // Make sure that the self type matches From 4b6e1d1c5fc9eaedc119759d99b6105b4f7f5d1e Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Tue, 22 Nov 2022 18:31:23 +0000 Subject: [PATCH 107/244] Add `TyCtxt::is_fn_trait` --- compiler/rustc_hir_typeck/src/closure.rs | 2 +- compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs | 2 +- compiler/rustc_middle/src/middle/lang_items.rs | 8 ++++++++ .../src/traits/error_reporting/mod.rs | 7 +++---- .../src/traits/error_reporting/suggestions.rs | 6 ++---- .../src/traits/select/candidate_assembly.rs | 2 +- 6 files changed, 16 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/closure.rs b/compiler/rustc_hir_typeck/src/closure.rs index 11a5f2b0d90e7..e584d413c4190 100644 --- a/compiler/rustc_hir_typeck/src/closure.rs +++ b/compiler/rustc_hir_typeck/src/closure.rs @@ -263,7 +263,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let trait_def_id = projection.trait_def_id(tcx); - let is_fn = tcx.fn_trait_kind_from_def_id(trait_def_id).is_some(); + let is_fn = tcx.is_fn_trait(trait_def_id); let gen_trait = tcx.require_lang_item(LangItem::Generator, cause_span); let is_gen = gen_trait == trait_def_id; if !is_fn && !is_gen { diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 5c7d21fc2e418..86384c7b93e71 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -2115,7 +2115,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { { if let ty::PredicateKind::Clause(ty::Clause::Trait(pred)) = predicate.kind().skip_binder() && pred.self_ty().peel_refs() == callee_ty - && self.tcx.fn_trait_kind_from_def_id(pred.def_id()).is_some() + && self.tcx.is_fn_trait(pred.def_id()) { err.span_note(span, "callable defined here"); return; diff --git a/compiler/rustc_middle/src/middle/lang_items.rs b/compiler/rustc_middle/src/middle/lang_items.rs index e1bcb74b281c4..343ea1f00f58b 100644 --- a/compiler/rustc_middle/src/middle/lang_items.rs +++ b/compiler/rustc_middle/src/middle/lang_items.rs @@ -27,6 +27,9 @@ impl<'tcx> TyCtxt<'tcx> { }) } + /// Given a [`DefId`] of a [`Fn`], [`FnMut`] or [`FnOnce`] traits, + /// returns a corresponding [`ty::ClosureKind`]. + /// For any other [`DefId`] return `None`. pub fn fn_trait_kind_from_def_id(self, id: DefId) -> Option { let items = self.lang_items(); match Some(id) { @@ -36,6 +39,11 @@ impl<'tcx> TyCtxt<'tcx> { _ => None, } } + + /// Returns `true` if `id` is a `DefId` of [`Fn`], [`FnMut`] or [`FnOnce`] traits. + pub fn is_fn_trait(self, id: DefId) -> bool { + self.fn_trait_kind_from_def_id(id).is_some() + } } /// Returns `true` if the specified `lang_item` must be present for this diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index de35fbfe6d0db..e96b9b64e7874 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -687,7 +687,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { } ObligationCauseCode::BindingObligation(def_id, _) | ObligationCauseCode::ItemObligation(def_id) - if tcx.fn_trait_kind_from_def_id(*def_id).is_some() => + if tcx.is_fn_trait(*def_id) => { err.code(rustc_errors::error_code!(E0059)); err.set_primary_message(format!( @@ -847,8 +847,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { ); } - let is_fn_trait = - tcx.fn_trait_kind_from_def_id(trait_ref.def_id()).is_some(); + let is_fn_trait = tcx.is_fn_trait(trait_ref.def_id()); let is_target_feature_fn = if let ty::FnDef(def_id, _) = *trait_ref.skip_binder().self_ty().kind() { @@ -2156,7 +2155,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> { if generics.params.iter().any(|p| p.name != kw::SelfUpper) && !snippet.ends_with('>') && !generics.has_impl_trait() - && !self.tcx.fn_trait_kind_from_def_id(def_id).is_some() + && !self.tcx.is_fn_trait(def_id) { // FIXME: To avoid spurious suggestions in functions where type arguments // where already supplied, we check the snippet to make sure it doesn't diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 4ae9b22f22171..ddffe25bc263c 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -1679,9 +1679,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { ) -> Ty<'tcx> { let inputs = trait_ref.skip_binder().substs.type_at(1); let sig = match inputs.kind() { - ty::Tuple(inputs) - if infcx.tcx.fn_trait_kind_from_def_id(trait_ref.def_id()).is_some() => - { + ty::Tuple(inputs) if infcx.tcx.is_fn_trait(trait_ref.def_id()) => { infcx.tcx.mk_fn_sig( inputs.iter(), infcx.next_ty_var(TypeVariableOrigin { @@ -1752,7 +1750,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { && let predicates = self.tcx.predicates_of(def_id).instantiate_identity(self.tcx) && let Some(pred) = predicates.predicates.get(*idx) && let ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) = pred.kind().skip_binder() - && self.tcx.fn_trait_kind_from_def_id(trait_pred.def_id()).is_some() + && self.tcx.is_fn_trait(trait_pred.def_id()) { let expected_self = self.tcx.anonymize_late_bound_regions(pred.kind().rebind(trait_pred.self_ty())); diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs index 460ca923c6e29..adce549a858cb 100644 --- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs +++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs @@ -489,7 +489,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { candidates: &mut SelectionCandidateSet<'tcx>, ) { // We provide impl of all fn traits for fn pointers. - if self.tcx().fn_trait_kind_from_def_id(obligation.predicate.def_id()).is_none() { + if !self.tcx().is_fn_trait(obligation.predicate.def_id()) { return; } From fa89f533524ba41589b211845f19a75811330efc Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Tue, 22 Nov 2022 18:35:58 +0000 Subject: [PATCH 108/244] Simplify `ty::ClosureKind::extends` This is valid per the comment of the `ClosureKind` defition --- compiler/rustc_middle/src/ty/closure.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/compiler/rustc_middle/src/ty/closure.rs b/compiler/rustc_middle/src/ty/closure.rs index 2653c8576de6c..e0bd490981074 100644 --- a/compiler/rustc_middle/src/ty/closure.rs +++ b/compiler/rustc_middle/src/ty/closure.rs @@ -97,15 +97,7 @@ impl<'tcx> ClosureKind { /// Returns `true` if a type that impls this closure kind /// must also implement `other`. pub fn extends(self, other: ty::ClosureKind) -> bool { - matches!( - (self, other), - (ClosureKind::Fn, ClosureKind::Fn) - | (ClosureKind::Fn, ClosureKind::FnMut) - | (ClosureKind::Fn, ClosureKind::FnOnce) - | (ClosureKind::FnMut, ClosureKind::FnMut) - | (ClosureKind::FnMut, ClosureKind::FnOnce) - | (ClosureKind::FnOnce, ClosureKind::FnOnce) - ) + self <= other } /// Returns the representative scalar type for this closure kind. From ea447924cef96746d27c396d395cc5cfea738928 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Tue, 22 Nov 2022 18:52:07 +0000 Subject: [PATCH 109/244] micro doc fixes --- compiler/rustc_middle/src/ty/closure.rs | 22 +++++++++++----------- compiler/rustc_middle/src/ty/sty.rs | 3 +-- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_middle/src/ty/closure.rs b/compiler/rustc_middle/src/ty/closure.rs index e0bd490981074..286946d1d1f27 100644 --- a/compiler/rustc_middle/src/ty/closure.rs +++ b/compiler/rustc_middle/src/ty/closure.rs @@ -100,19 +100,9 @@ impl<'tcx> ClosureKind { self <= other } - /// Returns the representative scalar type for this closure kind. - /// See `Ty::to_opt_closure_kind` for more details. - pub fn to_ty(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> { - match self { - ClosureKind::Fn => tcx.types.i8, - ClosureKind::FnMut => tcx.types.i16, - ClosureKind::FnOnce => tcx.types.i32, - } - } - /// Converts `self` to a [`DefId`] of the corresponding trait. /// - /// Note: the inverse of this function is [`TyCtxt::fn_trait_kind_from_def_id`] + /// Note: the inverse of this function is [`TyCtxt::fn_trait_kind_from_def_id`]. pub fn to_def_id(&self, tcx: TyCtxt<'_>) -> DefId { tcx.require_lang_item( match self { @@ -123,6 +113,16 @@ impl<'tcx> ClosureKind { None, ) } + + /// Returns the representative scalar type for this closure kind. + /// See `Ty::to_opt_closure_kind` for more details. + pub fn to_ty(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> { + match self { + ClosureKind::Fn => tcx.types.i8, + ClosureKind::FnMut => tcx.types.i16, + ClosureKind::FnOnce => tcx.types.i32, + } + } } /// A composite describing a `Place` that is captured by a closure. diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index a53b275fb02cd..4fc97c0baaf53 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -2114,8 +2114,7 @@ impl<'tcx> Ty<'tcx> { /// parameter. This is kind of a phantom type, except that the /// most convenient thing for us to are the integral types. This /// function converts such a special type into the closure - /// kind. To go the other way, use - /// `tcx.closure_kind_ty(closure_kind)`. + /// kind. To go the other way, use `closure_kind.to_ty(tcx)`. /// /// Note that during type checking, we use an inference variable /// to represent the closure kind, because it has not yet been From 5ba0056346a745e61cb2cb90d69a3304a255ff42 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Tue, 22 Nov 2022 19:03:26 +0000 Subject: [PATCH 110/244] Use `TyCtxt::is_fn_trait` is a couple more places --- .../error_reporting/nice_region_error/placeholder_error.rs | 5 +---- .../src/traits/error_reporting/suggestions.rs | 3 +-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs index 3a74ac51b2c27..1f554c81eff8e 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs @@ -399,10 +399,7 @@ impl<'tcx> NiceRegionError<'_, 'tcx> { self_ty.highlight.maybe_highlighting_region(vid, actual_has_vid); if self_ty.value.is_closure() - && self - .tcx() - .fn_trait_kind_from_def_id(expected_trait_ref.value.def_id) - .is_some() + && self.tcx().is_fn_trait(expected_trait_ref.value.def_id) { let closure_sig = self_ty.map(|closure| { if let ty::Closure(_, substs) = closure.kind() { diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index ddffe25bc263c..2cb04b949546c 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -1764,8 +1764,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { .enumerate() .find(|(other_idx, (pred, _))| match pred.kind().skip_binder() { ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) - if self.tcx.fn_trait_kind_from_def_id(trait_pred.def_id()) - .is_some() + if self.tcx.is_fn_trait(trait_pred.def_id()) && other_idx != idx // Make sure that the self type matches // (i.e. constraining this closure) From ee6f18ef595df21cfe167834f59c768985d74d4c Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Sat, 26 Nov 2022 22:23:27 +0100 Subject: [PATCH 111/244] make simple check of prinf function. With this commit we start to make some simple check when the name resolution fails, and we generate some helper message in case the name is a C name like in the case of the `printf` and suggest the correct rust method. Signed-off-by: Vincenzo Palazzo --- compiler/rustc_resolve/src/late/diagnostics.rs | 8 ++++++++ .../ui/suggestions/seggest_print_over_printf.rs | 9 +++++++++ .../suggestions/seggest_print_over_printf.stderr | 14 ++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 src/test/ui/suggestions/seggest_print_over_printf.rs create mode 100644 src/test/ui/suggestions/seggest_print_over_printf.stderr diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 9c95adc628bc6..5b9513ebc0da4 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -282,6 +282,14 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { "you may want to use a bool value instead", format!("{}", item_typo), )) + // FIXME(vicnenzopalazzo): make the check smarter, + // and maybe expand with levenshtein distance checks + } else if item_str.as_str() == "printf" { + Some(( + item_span, + "you may have meant to use the `print` macro", + "print!".to_owned(), + )) } else { suggestion }; diff --git a/src/test/ui/suggestions/seggest_print_over_printf.rs b/src/test/ui/suggestions/seggest_print_over_printf.rs new file mode 100644 index 0000000000000..25566cd7f2aeb --- /dev/null +++ b/src/test/ui/suggestions/seggest_print_over_printf.rs @@ -0,0 +1,9 @@ +// Suggest to a user to use the print macros +// instead to use the printf. + +fn main() { + let x = 4; + printf("%d", x); + //~^ ERROR cannot find function `printf` in this scope + //~| HELP you may have meant to use the `print` macro +} diff --git a/src/test/ui/suggestions/seggest_print_over_printf.stderr b/src/test/ui/suggestions/seggest_print_over_printf.stderr new file mode 100644 index 0000000000000..7b1ce047a9274 --- /dev/null +++ b/src/test/ui/suggestions/seggest_print_over_printf.stderr @@ -0,0 +1,14 @@ +error[E0425]: cannot find function `printf` in this scope + --> $DIR/seggest_print_over_printf.rs:6:5 + | +LL | printf("%d", x); + | ^^^^^^ not found in this scope + | +help: you may have meant to use the `print` macro + | +LL | print!("%d", x); + | ~~~~~~ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0425`. From 1d42936b18d08ba414d9def35508d3baf2175c72 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 27 Nov 2022 11:15:06 +0000 Subject: [PATCH 112/244] Prefer doc comments over `//`-comments in compiler --- compiler/rustc_ast/src/ast.rs | 6 +- compiler/rustc_ast/src/attr/mod.rs | 12 ++-- compiler/rustc_ast/src/mut_visit.rs | 6 +- compiler/rustc_ast/src/token.rs | 20 +++--- compiler/rustc_ast/src/tokenstream.rs | 20 +++--- compiler/rustc_ast_pretty/src/helpers.rs | 4 +- .../rustc_ast_pretty/src/pprust/state/expr.rs | 8 +-- .../rustc_attr/src/session_diagnostics.rs | 4 +- .../rustc_borrowck/src/diagnostics/mod.rs | 4 +- compiler/rustc_borrowck/src/lib.rs | 2 +- .../src/type_check/liveness/polonius.rs | 4 +- .../src/deriving/generic/mod.rs | 12 ++-- .../rustc_builtin_macros/src/edition_panic.rs | 26 ++++---- .../rustc_builtin_macros/src/source_util.rs | 2 +- compiler/rustc_builtin_macros/src/test.rs | 14 ++-- .../rustc_builtin_macros/src/test_harness.rs | 4 +- .../src/value_and_place.rs | 4 +- compiler/rustc_codegen_gcc/src/context.rs | 4 +- .../src/coverageinfo/mod.rs | 2 +- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 2 +- compiler/rustc_codegen_llvm/src/llvm_util.rs | 8 +-- compiler/rustc_codegen_llvm/src/type_.rs | 2 +- compiler/rustc_codegen_ssa/src/back/link.rs | 2 +- compiler/rustc_codegen_ssa/src/back/linker.rs | 6 +- .../rustc_codegen_ssa/src/back/metadata.rs | 64 +++++++++---------- compiler/rustc_codegen_ssa/src/back/write.rs | 18 +++--- .../src/debuginfo/type_names.rs | 10 +-- compiler/rustc_codegen_ssa/src/mir/operand.rs | 4 +- .../rustc_const_eval/src/interpret/machine.rs | 4 +- .../src/interpret/projection.rs | 4 +- .../rustc_const_eval/src/interpret/visitor.rs | 2 +- .../src/transform/check_consts/mod.rs | 16 ++--- .../src/transform/check_consts/ops.rs | 2 +- .../rustc_const_eval/src/util/aggregate.rs | 3 +- compiler/rustc_driver/src/lib.rs | 4 +- compiler/rustc_expand/src/base.rs | 2 +- compiler/rustc_expand/src/build.rs | 2 +- compiler/rustc_expand/src/config.rs | 2 +- compiler/rustc_expand/src/expand.rs | 11 ++-- compiler/rustc_hir/src/hir.rs | 26 ++++---- .../rustc_hir_analysis/src/astconv/mod.rs | 4 +- .../rustc_hir_analysis/src/check/dropck.rs | 7 +- .../rustc_hir_analysis/src/variance/terms.rs | 14 ++-- compiler/rustc_hir_typeck/src/demand.rs | 4 +- compiler/rustc_hir_typeck/src/expectation.rs | 6 +- compiler/rustc_hir_typeck/src/expr.rs | 4 +- compiler/rustc_hir_typeck/src/inherited.rs | 20 +++--- .../src/persist/dirty_clean.rs | 6 +- .../rustc_infer/src/infer/free_regions.rs | 8 +-- .../rustc_infer/src/infer/opaque_types.rs | 26 ++++---- .../src/infer/opaque_types/table.rs | 8 +-- compiler/rustc_lint/src/context.rs | 2 +- compiler/rustc_metadata/src/rmeta/decoder.rs | 8 +-- .../rustc_middle/src/mir/interpret/value.rs | 4 +- compiler/rustc_middle/src/traits/mod.rs | 2 +- compiler/rustc_middle/src/ty/closure.rs | 6 +- compiler/rustc_middle/src/ty/context.rs | 38 +++++------ compiler/rustc_middle/src/ty/flags.rs | 2 +- compiler/rustc_middle/src/ty/mod.rs | 5 +- compiler/rustc_middle/src/ty/sty.rs | 2 +- compiler/rustc_middle/src/ty/trait_def.rs | 4 +- compiler/rustc_middle/src/ty/util.rs | 10 +-- .../src/build/expr/category.rs | 28 ++++---- compiler/rustc_mir_build/src/build/misc.rs | 4 +- compiler/rustc_mir_build/src/build/scope.rs | 8 ++- .../src/add_moves_for_packed_drops.rs | 61 +++++++++--------- .../src/elaborate_box_derefs.rs | 2 +- compiler/rustc_monomorphize/src/collector.rs | 6 +- .../rustc_parse/src/lexer/unicode_chars.rs | 4 +- .../rustc_parse/src/parser/attr_wrapper.rs | 2 +- .../rustc_parse/src/parser/diagnostics.rs | 6 +- .../rustc_query_system/src/dep_graph/graph.rs | 24 +++---- compiler/rustc_resolve/src/imports.rs | 8 +-- compiler/rustc_save_analysis/src/lib.rs | 4 +- compiler/rustc_serialize/src/leb128.rs | 2 +- compiler/rustc_serialize/src/opaque.rs | 26 ++++---- compiler/rustc_span/src/lib.rs | 14 ++-- compiler/rustc_span/src/source_map.rs | 22 +++---- compiler/rustc_span/src/symbol.rs | 4 +- compiler/rustc_target/src/spec/mod.rs | 14 ++-- .../src/traits/auto_trait.rs | 2 +- .../src/traits/project.rs | 2 +- .../src/traits/specialize/mod.rs | 2 +- 83 files changed, 400 insertions(+), 387 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index b48a7d29f5097..8bb4442d1bb27 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -111,8 +111,8 @@ impl HashStable for Path { } impl Path { - // Convert a span and an identifier to the corresponding - // one-segment path. + /// Convert a span and an identifier to the corresponding + /// one-segment path. pub fn from_ident(ident: Ident) -> Path { Path { segments: thin_vec![PathSegment::from_ident(ident)], span: ident.span, tokens: None } } @@ -1283,7 +1283,7 @@ impl Expr { ) } - // To a first-order approximation, is this a pattern + /// To a first-order approximation, is this a pattern? pub fn is_approximately_pattern(&self) -> bool { match &self.peel_parens().kind { ExprKind::Box(_) diff --git a/compiler/rustc_ast/src/attr/mod.rs b/compiler/rustc_ast/src/attr/mod.rs index 3e0129531150c..c948faeb35835 100644 --- a/compiler/rustc_ast/src/attr/mod.rs +++ b/compiler/rustc_ast/src/attr/mod.rs @@ -26,9 +26,9 @@ use thin_vec::thin_vec; pub struct MarkedAttrs(GrowableBitSet); impl MarkedAttrs { - // We have no idea how many attributes there will be, so just - // initiate the vectors with 0 bits. We'll grow them as necessary. pub fn new() -> Self { + // We have no idea how many attributes there will be, so just + // initiate the vectors with 0 bits. We'll grow them as necessary. MarkedAttrs(GrowableBitSet::new_empty()) } @@ -174,9 +174,11 @@ impl MetaItem { self.ident().unwrap_or_else(Ident::empty).name } - // Example: - // #[attribute(name = "value")] - // ^^^^^^^^^^^^^^ + /// ```text + /// Example: + /// #[attribute(name = "value")] + /// ^^^^^^^^^^^^^^ + /// ``` pub fn name_value_literal(&self) -> Option<&Lit> { match &self.kind { MetaItemKind::NameValue(v) => Some(v), diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index a5b24c403dd37..11def67c46365 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -725,10 +725,10 @@ pub fn visit_lazy_tts(lazy_tts: &mut Option, visit_lazy_tts_opt_mut(lazy_tts.as_mut(), vis); } +/// Applies ident visitor if it's an ident; applies other visits to interpolated nodes. +/// In practice the ident part is not actually used by specific visitors right now, +/// but there's a test below checking that it works. // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`. -// Applies ident visitor if it's an ident; applies other visits to interpolated nodes. -// In practice the ident part is not actually used by specific visitors right now, -// but there's a test below checking that it works. pub fn visit_token(t: &mut Token, vis: &mut T) { let Token { kind, span } = t; match kind { diff --git a/compiler/rustc_ast/src/token.rs b/compiler/rustc_ast/src/token.rs index cb32925584c58..c0cc4e79a3d53 100644 --- a/compiler/rustc_ast/src/token.rs +++ b/compiler/rustc_ast/src/token.rs @@ -302,9 +302,9 @@ impl TokenKind { Literal(Lit::new(kind, symbol, suffix)) } - // An approximation to proc-macro-style single-character operators used by rustc parser. - // If the operator token can be broken into two tokens, the first of which is single-character, - // then this function performs that operation, otherwise it returns `None`. + /// An approximation to proc-macro-style single-character operators used by rustc parser. + /// If the operator token can be broken into two tokens, the first of which is single-character, + /// then this function performs that operation, otherwise it returns `None`. pub fn break_two_token_op(&self) -> Option<(TokenKind, TokenKind)> { Some(match *self { Le => (Lt, Eq), @@ -538,10 +538,10 @@ impl Token { } } - // A convenience function for matching on identifiers during parsing. - // Turns interpolated identifier (`$i: ident`) or lifetime (`$l: lifetime`) token - // into the regular identifier or lifetime token it refers to, - // otherwise returns the original token. + /// A convenience function for matching on identifiers during parsing. + /// Turns interpolated identifier (`$i: ident`) or lifetime (`$l: lifetime`) token + /// into the regular identifier or lifetime token it refers to, + /// otherwise returns the original token. pub fn uninterpolate(&self) -> Cow<'_, Token> { match &self.kind { Interpolated(nt) => match **nt { @@ -621,7 +621,7 @@ impl Token { false } - // Is the token an interpolated block (`$b:block`)? + /// Is the token an interpolated block (`$b:block`)? pub fn is_whole_block(&self) -> bool { if let Interpolated(nt) = &self.kind && let NtBlock(..) = **nt { return true; @@ -665,8 +665,8 @@ impl Token { self.is_non_raw_ident_where(Ident::is_path_segment_keyword) } - // Returns true for reserved identifiers used internally for elided lifetimes, - // unnamed method parameters, crate root module, error recovery etc. + /// Returns true for reserved identifiers used internally for elided lifetimes, + /// unnamed method parameters, crate root module, error recovery etc. pub fn is_special_ident(&self) -> bool { self.is_non_raw_ident_where(Ident::is_special) } diff --git a/compiler/rustc_ast/src/tokenstream.rs b/compiler/rustc_ast/src/tokenstream.rs index 015f5c1ee8ae5..58c6d397ea270 100644 --- a/compiler/rustc_ast/src/tokenstream.rs +++ b/compiler/rustc_ast/src/tokenstream.rs @@ -86,12 +86,12 @@ impl TokenTree { } } - // Create a `TokenTree::Token` with alone spacing. + /// Create a `TokenTree::Token` with alone spacing. pub fn token_alone(kind: TokenKind, span: Span) -> TokenTree { TokenTree::Token(Token::new(kind, span), Spacing::Alone) } - // Create a `TokenTree::Token` with joint spacing. + /// Create a `TokenTree::Token` with joint spacing. pub fn token_joint(kind: TokenKind, span: Span) -> TokenTree { TokenTree::Token(Token::new(kind, span), Spacing::Joint) } @@ -413,17 +413,17 @@ impl TokenStream { TokenStream(Lrc::new(self.0.iter().enumerate().map(|(i, tree)| f(i, tree)).collect())) } - // Create a token stream containing a single token with alone spacing. + /// Create a token stream containing a single token with alone spacing. pub fn token_alone(kind: TokenKind, span: Span) -> TokenStream { TokenStream::new(vec![TokenTree::token_alone(kind, span)]) } - // Create a token stream containing a single token with joint spacing. + /// Create a token stream containing a single token with joint spacing. pub fn token_joint(kind: TokenKind, span: Span) -> TokenStream { TokenStream::new(vec![TokenTree::token_joint(kind, span)]) } - // Create a token stream containing a single `Delimited`. + /// Create a token stream containing a single `Delimited`. pub fn delimited(span: DelimSpan, delim: Delimiter, tts: TokenStream) -> TokenStream { TokenStream::new(vec![TokenTree::Delimited(span, delim, tts)]) } @@ -522,8 +522,8 @@ impl TokenStream { } } - // Push `tt` onto the end of the stream, possibly gluing it to the last - // token. Uses `make_mut` to maximize efficiency. + /// Push `tt` onto the end of the stream, possibly gluing it to the last + /// token. Uses `make_mut` to maximize efficiency. pub fn push_tree(&mut self, tt: TokenTree) { let vec_mut = Lrc::make_mut(&mut self.0); @@ -534,9 +534,9 @@ impl TokenStream { } } - // Push `stream` onto the end of the stream, possibly gluing the first - // token tree to the last token. (No other token trees will be glued.) - // Uses `make_mut` to maximize efficiency. + /// Push `stream` onto the end of the stream, possibly gluing the first + /// token tree to the last token. (No other token trees will be glued.) + /// Uses `make_mut` to maximize efficiency. pub fn push_stream(&mut self, stream: TokenStream) { let vec_mut = Lrc::make_mut(&mut self.0); diff --git a/compiler/rustc_ast_pretty/src/helpers.rs b/compiler/rustc_ast_pretty/src/helpers.rs index 5ec71cddf7de6..c3e0eccd3d404 100644 --- a/compiler/rustc_ast_pretty/src/helpers.rs +++ b/compiler/rustc_ast_pretty/src/helpers.rs @@ -36,8 +36,8 @@ impl Printer { self.nbsp() } - // Synthesizes a comment that was not textually present in the original - // source file. + /// Synthesizes a comment that was not textually present in the original + /// source file. pub fn synth_comment(&mut self, text: impl Into>) { self.word("/*"); self.space(); diff --git a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs index 4b37fa027f53b..949d98f96ab6a 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs @@ -58,10 +58,10 @@ impl<'a> State<'a> { self.print_expr_cond_paren(expr, Self::cond_needs_par(expr)) } - // Does `expr` need parentheses when printed in a condition position? - // - // These cases need parens due to the parse error observed in #26461: `if return {}` - // parses as the erroneous construct `if (return {})`, not `if (return) {}`. + /// Does `expr` need parentheses when printed in a condition position? + /// + /// These cases need parens due to the parse error observed in #26461: `if return {}` + /// parses as the erroneous construct `if (return {})`, not `if (return) {}`. pub(super) fn cond_needs_par(expr: &ast::Expr) -> bool { match expr.kind { ast::ExprKind::Break(..) diff --git a/compiler/rustc_attr/src/session_diagnostics.rs b/compiler/rustc_attr/src/session_diagnostics.rs index edccfa1c8ffa2..91c6bcb08a079 100644 --- a/compiler/rustc_attr/src/session_diagnostics.rs +++ b/compiler/rustc_attr/src/session_diagnostics.rs @@ -41,7 +41,7 @@ pub(crate) struct IncorrectMetaItem { pub span: Span, } -// Error code: E0541 +/// Error code: E0541 pub(crate) struct UnknownMetaItem<'a> { pub span: Span, pub item: String, @@ -200,7 +200,7 @@ pub(crate) struct InvalidReprHintNoValue { pub name: String, } -// Error code: E0565 +/// Error code: E0565 pub(crate) struct UnsupportedLiteral { pub span: Span, pub reason: UnsupportedLiteralReason, diff --git a/compiler/rustc_borrowck/src/diagnostics/mod.rs b/compiler/rustc_borrowck/src/diagnostics/mod.rs index c500cbc49e4a3..86c5d9cfa8121 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mod.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mod.rs @@ -590,7 +590,7 @@ impl UseSpans<'_> { } } - // Add a span label to the arguments of the closure, if it exists. + /// Add a span label to the arguments of the closure, if it exists. pub(super) fn args_span_label(self, err: &mut Diagnostic, message: impl Into) { if let UseSpans::ClosureUse { args_span, .. } = self { err.span_label(args_span, message); @@ -628,7 +628,7 @@ impl UseSpans<'_> { } } - // Add a span label to the use of the captured variable, if it exists. + /// Add a span label to the use of the captured variable, if it exists. pub(super) fn var_span_label( self, err: &mut Diagnostic, diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index 163170a1d1aa0..4d87ecf5e44be 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -83,7 +83,7 @@ mod type_check; mod universal_regions; mod used_muts; -// A public API provided for the Rust compiler consumers. +/// A public API provided for the Rust compiler consumers. pub mod consumers; use borrow_set::{BorrowData, BorrowSet}; diff --git a/compiler/rustc_borrowck/src/type_check/liveness/polonius.rs b/compiler/rustc_borrowck/src/type_check/liveness/polonius.rs index bc76a465e3c3a..b344ab46adbde 100644 --- a/compiler/rustc_borrowck/src/type_check/liveness/polonius.rs +++ b/compiler/rustc_borrowck/src/type_check/liveness/polonius.rs @@ -121,8 +121,8 @@ pub(super) fn populate_access_facts<'a, 'tcx>( } } -// For every potentially drop()-touched region `region` in `local`'s type -// (`kind`), emit a Polonius `use_of_var_derefs_origin(local, origin)` fact. +/// For every potentially drop()-touched region `region` in `local`'s type +/// (`kind`), emit a Polonius `use_of_var_derefs_origin(local, origin)` fact. pub(super) fn add_drop_of_var_derefs_origin<'tcx>( typeck: &mut TypeChecker<'_, 'tcx>, local: Local, diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index 3309fab224fb7..1467d4eaec068 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -300,12 +300,12 @@ struct TypeParameter { ty: P, } -// The code snippets built up for derived code are sometimes used as blocks -// (e.g. in a function body) and sometimes used as expressions (e.g. in a match -// arm). This structure avoids committing to either form until necessary, -// avoiding the insertion of any unnecessary blocks. -// -// The statements come before the expression. +/// The code snippets built up for derived code are sometimes used as blocks +/// (e.g. in a function body) and sometimes used as expressions (e.g. in a match +/// arm). This structure avoids committing to either form until necessary, +/// avoiding the insertion of any unnecessary blocks. +/// +/// The statements come before the expression. pub struct BlockOrExpr(Vec, Option>); impl BlockOrExpr { diff --git a/compiler/rustc_builtin_macros/src/edition_panic.rs b/compiler/rustc_builtin_macros/src/edition_panic.rs index cae648cd11aff..b2a21611db7f9 100644 --- a/compiler/rustc_builtin_macros/src/edition_panic.rs +++ b/compiler/rustc_builtin_macros/src/edition_panic.rs @@ -6,15 +6,15 @@ use rustc_span::edition::Edition; use rustc_span::symbol::sym; use rustc_span::Span; -// This expands to either -// - `$crate::panic::panic_2015!(...)` or -// - `$crate::panic::panic_2021!(...)` -// depending on the edition. -// -// This is used for both std::panic!() and core::panic!(). -// -// `$crate` will refer to either the `std` or `core` crate depending on which -// one we're expanding from. +/// This expands to either +/// - `$crate::panic::panic_2015!(...)` or +/// - `$crate::panic::panic_2021!(...)` +/// depending on the edition. +/// +/// This is used for both std::panic!() and core::panic!(). +/// +/// `$crate` will refer to either the `std` or `core` crate depending on which +/// one we're expanding from. pub fn expand_panic<'cx>( cx: &'cx mut ExtCtxt<'_>, sp: Span, @@ -24,10 +24,10 @@ pub fn expand_panic<'cx>( expand(mac, cx, sp, tts) } -// This expands to either -// - `$crate::panic::unreachable_2015!(...)` or -// - `$crate::panic::unreachable_2021!(...)` -// depending on the edition. +/// This expands to either +/// - `$crate::panic::unreachable_2015!(...)` or +/// - `$crate::panic::unreachable_2021!(...)` +/// depending on the edition. pub fn expand_unreachable<'cx>( cx: &'cx mut ExtCtxt<'_>, sp: Span, diff --git a/compiler/rustc_builtin_macros/src/source_util.rs b/compiler/rustc_builtin_macros/src/source_util.rs index 3411bd40c9de5..0b17e92efe936 100644 --- a/compiler/rustc_builtin_macros/src/source_util.rs +++ b/compiler/rustc_builtin_macros/src/source_util.rs @@ -164,7 +164,7 @@ pub fn expand_include<'cx>( Box::new(ExpandResult { p, node_id: cx.current_expansion.lint_node_id }) } -// include_str! : read the given file, insert it as a literal string expr +/// `include_str!`: read the given file, insert it as a literal string expr pub fn expand_include_str( cx: &mut ExtCtxt<'_>, sp: Span, diff --git a/compiler/rustc_builtin_macros/src/test.rs b/compiler/rustc_builtin_macros/src/test.rs index b62840d4bc822..82baf1da28f2f 100644 --- a/compiler/rustc_builtin_macros/src/test.rs +++ b/compiler/rustc_builtin_macros/src/test.rs @@ -13,13 +13,13 @@ use rustc_span::Span; use std::iter; use thin_vec::thin_vec; -// #[test_case] is used by custom test authors to mark tests -// When building for test, it needs to make the item public and gensym the name -// Otherwise, we'll omit the item. This behavior means that any item annotated -// with #[test_case] is never addressable. -// -// We mark item with an inert attribute "rustc_test_marker" which the test generation -// logic will pick up on. +/// #[test_case] is used by custom test authors to mark tests +/// When building for test, it needs to make the item public and gensym the name +/// Otherwise, we'll omit the item. This behavior means that any item annotated +/// with #[test_case] is never addressable. +/// +/// We mark item with an inert attribute "rustc_test_marker" which the test generation +/// logic will pick up on. pub fn expand_test_case( ecx: &mut ExtCtxt<'_>, attr_sp: Span, diff --git a/compiler/rustc_builtin_macros/src/test_harness.rs b/compiler/rustc_builtin_macros/src/test_harness.rs index b8b8351a36f41..3269f62b105b9 100644 --- a/compiler/rustc_builtin_macros/src/test_harness.rs +++ b/compiler/rustc_builtin_macros/src/test_harness.rs @@ -34,8 +34,8 @@ struct TestCtxt<'a> { test_runner: Option, } -// Traverse the crate, collecting all the test functions, eliding any -// existing main functions, and synthesizing a main test harness +/// Traverse the crate, collecting all the test functions, eliding any +/// existing main functions, and synthesizing a main test harness pub fn inject(sess: &Session, resolver: &mut dyn ResolverExpand, krate: &mut ast::Crate) { let span_diagnostic = sess.diagnostic(); let panic_strategy = sess.panic_strategy(); diff --git a/compiler/rustc_codegen_cranelift/src/value_and_place.rs b/compiler/rustc_codegen_cranelift/src/value_and_place.rs index c5bd574623df6..34746ff6b6645 100644 --- a/compiler/rustc_codegen_cranelift/src/value_and_place.rs +++ b/compiler/rustc_codegen_cranelift/src/value_and_place.rs @@ -108,8 +108,8 @@ impl<'tcx> CValue<'tcx> { } // FIXME remove - // Forces the data value of a dyn* value to the stack and returns a pointer to it as well as the - // vtable pointer. + /// Forces the data value of a dyn* value to the stack and returns a pointer to it as well as the + /// vtable pointer. pub(crate) fn dyn_star_force_data_on_stack( self, fx: &mut FunctionCx<'_, '_, 'tcx>, diff --git a/compiler/rustc_codegen_gcc/src/context.rs b/compiler/rustc_codegen_gcc/src/context.rs index 2e71c3665daa7..837708aeb0ea9 100644 --- a/compiler/rustc_codegen_gcc/src/context.rs +++ b/compiler/rustc_codegen_gcc/src/context.rs @@ -88,9 +88,9 @@ pub struct CodegenCx<'gcc, 'tcx> { pub vtables: RefCell, Option>), RValue<'gcc>>>, // TODO(antoyo): improve the SSA API to not require those. - // Mapping from function pointer type to indexes of on stack parameters. + /// Mapping from function pointer type to indexes of on stack parameters. pub on_stack_params: RefCell, FxHashSet>>, - // Mapping from function to indexes of on stack parameters. + /// Mapping from function to indexes of on stack parameters. pub on_stack_function_params: RefCell, FxHashSet>>, /// Cache of emitted const globals (value -> global) diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs index 964a632b6eedd..ace15cfb02477 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs @@ -37,7 +37,7 @@ const VAR_ALIGN_BYTES: usize = 8; /// A context object for maintaining all state needed by the coverageinfo module. pub struct CrateCoverageContext<'ll, 'tcx> { - // Coverage data for each instrumented function identified by DefId. + /// Coverage data for each instrumented function identified by DefId. pub(crate) function_coverage_map: RefCell, FunctionCoverage<'tcx>>>, pub(crate) pgo_func_name_var_map: RefCell, &'ll llvm::Value>>, } diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index f451984973048..c14e1656291e8 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -35,7 +35,7 @@ pub enum LLVMRustResult { pub struct LLVMRustCOFFShortExport { pub name: *const c_char, pub ordinal_present: bool, - // value of `ordinal` only important when `ordinal_present` is true + /// value of `ordinal` only important when `ordinal_present` is true pub ordinal: u16, } diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index 4af1aaec0a112..bc3a94a402706 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -194,8 +194,8 @@ pub fn to_llvm_features<'a>(sess: &Session, s: &'a str) -> SmallVec<[&'a str; 2] } } -// Given a map from target_features to whether they are enabled or disabled, -// ensure only valid combinations are allowed. +/// Given a map from target_features to whether they are enabled or disabled, +/// ensure only valid combinations are allowed. pub fn check_tied_features( sess: &Session, features: &FxHashMap<&str, bool>, @@ -213,8 +213,8 @@ pub fn check_tied_features( return None; } -// Used to generate cfg variables and apply features -// Must express features in the way Rust understands them +/// Used to generate cfg variables and apply features +/// Must express features in the way Rust understands them pub fn target_features(sess: &Session, allow_unstable: bool) -> Vec { let target_machine = create_informational_target_machine(sess); let mut features: Vec = supported_target_features(sess) diff --git a/compiler/rustc_codegen_llvm/src/type_.rs b/compiler/rustc_codegen_llvm/src/type_.rs index 5eec7dc613028..5772b7e1d812a 100644 --- a/compiler/rustc_codegen_llvm/src/type_.rs +++ b/compiler/rustc_codegen_llvm/src/type_.rs @@ -238,7 +238,7 @@ impl Type { unsafe { llvm::LLVMInt8TypeInContext(llcx) } } - // Creates an integer type with the given number of bits, e.g., i24 + /// Creates an integer type with the given number of bits, e.g., i24 pub fn ix_llcx(llcx: &llvm::Context, num_bits: u64) -> &Type { unsafe { llvm::LLVMIntTypeInContext(llcx, num_bits as c_uint) } } diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 762430c618721..39cd4a35f1778 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -1179,7 +1179,7 @@ pub fn ignored_for_lto(sess: &Session, info: &CrateInfo, cnum: CrateNum) -> bool && (info.compiler_builtins == Some(cnum) || info.is_no_builtins.contains(&cnum)) } -// This functions tries to determine the appropriate linker (and corresponding LinkerFlavor) to use +/// This functions tries to determine the appropriate linker (and corresponding LinkerFlavor) to use pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) { fn infer_from( sess: &Session, diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index 7f0c2861f7e29..ff0c1ac4916f2 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -34,9 +34,9 @@ pub fn disable_localization(linker: &mut Command) { linker.env("VSLANG", "1033"); } -// The third parameter is for env vars, used on windows to set up the -// path for MSVC to find its DLLs, and gcc to find its bundled -// toolchain +/// The third parameter is for env vars, used on windows to set up the +/// path for MSVC to find its DLLs, and gcc to find its bundled +/// toolchain pub fn get_linker<'a>( sess: &'a Session, linker: &Path, diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index 780a385003681..51c5c375d5191 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -191,38 +191,38 @@ pub enum MetadataPosition { Last, } -// For rlibs we "pack" rustc metadata into a dummy object file. -// -// Historically it was needed because rustc linked rlibs as whole-archive in some cases. -// In that case linkers try to include all files located in an archive, so if metadata is stored -// in an archive then it needs to be of a form that the linker is able to process. -// Now it's not clear whether metadata still needs to be wrapped into an object file or not. -// -// Note, though, that we don't actually want this metadata to show up in any -// final output of the compiler. Instead this is purely for rustc's own -// metadata tracking purposes. -// -// With the above in mind, each "flavor" of object format gets special -// handling here depending on the target: -// -// * MachO - macos-like targets will insert the metadata into a section that -// is sort of fake dwarf debug info. Inspecting the source of the macos -// linker this causes these sections to be skipped automatically because -// it's not in an allowlist of otherwise well known dwarf section names to -// go into the final artifact. -// -// * WebAssembly - we actually don't have any container format for this -// target. WebAssembly doesn't support the `dylib` crate type anyway so -// there's no need for us to support this at this time. Consequently the -// metadata bytes are simply stored as-is into an rlib. -// -// * COFF - Windows-like targets create an object with a section that has -// the `IMAGE_SCN_LNK_REMOVE` flag set which ensures that if the linker -// ever sees the section it doesn't process it and it's removed. -// -// * ELF - All other targets are similar to Windows in that there's a -// `SHF_EXCLUDE` flag we can set on sections in an object file to get -// automatically removed from the final output. +/// For rlibs we "pack" rustc metadata into a dummy object file. +/// +/// Historically it was needed because rustc linked rlibs as whole-archive in some cases. +/// In that case linkers try to include all files located in an archive, so if metadata is stored +/// in an archive then it needs to be of a form that the linker is able to process. +/// Now it's not clear whether metadata still needs to be wrapped into an object file or not. +/// +/// Note, though, that we don't actually want this metadata to show up in any +/// final output of the compiler. Instead this is purely for rustc's own +/// metadata tracking purposes. +/// +/// With the above in mind, each "flavor" of object format gets special +/// handling here depending on the target: +/// +/// * MachO - macos-like targets will insert the metadata into a section that +/// is sort of fake dwarf debug info. Inspecting the source of the macos +/// linker this causes these sections to be skipped automatically because +/// it's not in an allowlist of otherwise well known dwarf section names to +/// go into the final artifact. +/// +/// * WebAssembly - we actually don't have any container format for this +/// target. WebAssembly doesn't support the `dylib` crate type anyway so +/// there's no need for us to support this at this time. Consequently the +/// metadata bytes are simply stored as-is into an rlib. +/// +/// * COFF - Windows-like targets create an object with a section that has +/// the `IMAGE_SCN_LNK_REMOVE` flag set which ensures that if the linker +/// ever sees the section it doesn't process it and it's removed. +/// +/// * ELF - All other targets are similar to Windows in that there's a +/// `SHF_EXCLUDE` flag we can set on sections in an object file to get +/// automatically removed from the final output. pub fn create_wrapper_file( sess: &Session, section_name: Vec, diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index e3d28a1aca20e..12fca64968aac 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -340,20 +340,20 @@ pub struct CodegenContext { pub split_debuginfo: rustc_target::spec::SplitDebuginfo, pub split_dwarf_kind: rustc_session::config::SplitDwarfKind, - // Number of cgus excluding the allocator/metadata modules + /// Number of cgus excluding the allocator/metadata modules pub total_cgus: usize, - // Handler to use for diagnostics produced during codegen. + /// Handler to use for diagnostics produced during codegen. pub diag_emitter: SharedEmitter, - // LLVM optimizations for which we want to print remarks. + /// LLVM optimizations for which we want to print remarks. pub remark: Passes, - // Worker thread number + /// Worker thread number pub worker: usize, - // The incremental compilation session directory, or None if we are not - // compiling incrementally + /// The incremental compilation session directory, or None if we are not + /// compiling incrementally pub incr_comp_session_dir: Option, - // Used to update CGU re-use information during the thinlto phase. + /// Used to update CGU re-use information during the thinlto phase. pub cgu_reuse_tracker: CguReuseTracker, - // Channel back to the main control thread to send messages to + /// Channel back to the main control thread to send messages to pub coordinator_send: Sender>, } @@ -756,7 +756,7 @@ fn execute_work_item( } } -// Actual LTO type we end up choosing based on multiple factors. +/// Actual LTO type we end up choosing based on multiple factors. pub enum ComputedLtoType { No, Thin, diff --git a/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs b/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs index 8647fbace2a75..b004fbf85a97f 100644 --- a/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs +++ b/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs @@ -1,4 +1,4 @@ -// Type Names for Debug Info. +//! Type Names for Debug Info. // Notes on targeting MSVC: // In general, MSVC's debugger attempts to parse all arguments as C++ expressions, @@ -26,10 +26,10 @@ use std::fmt::Write; use crate::debuginfo::wants_c_like_enum_debuginfo; -// Compute the name of the type as it should be stored in debuginfo. Does not do -// any caching, i.e., calling the function twice with the same type will also do -// the work twice. The `qualified` parameter only affects the first level of the -// type name, further levels (i.e., type parameters) are always fully qualified. +/// Compute the name of the type as it should be stored in debuginfo. Does not do +/// any caching, i.e., calling the function twice with the same type will also do +/// the work twice. The `qualified` parameter only affects the first level of the +/// type name, further levels (i.e., type parameters) are always fully qualified. pub fn compute_debuginfo_type_name<'tcx>( tcx: TyCtxt<'tcx>, t: Ty<'tcx>, diff --git a/compiler/rustc_codegen_ssa/src/mir/operand.rs b/compiler/rustc_codegen_ssa/src/mir/operand.rs index e6ba642a7ed0e..34a5b638d7eba 100644 --- a/compiler/rustc_codegen_ssa/src/mir/operand.rs +++ b/compiler/rustc_codegen_ssa/src/mir/operand.rs @@ -40,10 +40,10 @@ pub enum OperandValue { /// instead. #[derive(Copy, Clone)] pub struct OperandRef<'tcx, V> { - // The value. + /// The value. pub val: OperandValue, - // The layout of value, based on its Rust type. + /// The layout of value, based on its Rust type. pub layout: TyAndLayout<'tcx>, } diff --git a/compiler/rustc_const_eval/src/interpret/machine.rs b/compiler/rustc_const_eval/src/interpret/machine.rs index 351152eba01f6..88d25be6bd861 100644 --- a/compiler/rustc_const_eval/src/interpret/machine.rs +++ b/compiler/rustc_const_eval/src/interpret/machine.rs @@ -417,8 +417,8 @@ pub trait Machine<'mir, 'tcx>: Sized { } } -// A lot of the flexibility above is just needed for `Miri`, but all "compile-time" machines -// (CTFE and ConstProp) use the same instance. Here, we share that code. +/// A lot of the flexibility above is just needed for `Miri`, but all "compile-time" machines +/// (CTFE and ConstProp) use the same instance. Here, we share that code. pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) { type Provenance = AllocId; type ProvenanceExtra = (); diff --git a/compiler/rustc_const_eval/src/interpret/projection.rs b/compiler/rustc_const_eval/src/interpret/projection.rs index 4966fd6ea80c1..2ffd73eef3ef8 100644 --- a/compiler/rustc_const_eval/src/interpret/projection.rs +++ b/compiler/rustc_const_eval/src/interpret/projection.rs @@ -206,8 +206,8 @@ where } } - // Iterates over all fields of an array. Much more efficient than doing the - // same by repeatedly calling `operand_index`. + /// Iterates over all fields of an array. Much more efficient than doing the + /// same by repeatedly calling `operand_index`. pub fn operand_array_fields<'a>( &self, base: &'a OpTy<'tcx, Prov>, diff --git a/compiler/rustc_const_eval/src/interpret/visitor.rs b/compiler/rustc_const_eval/src/interpret/visitor.rs index aee1f93b1a39c..1a10851a9f901 100644 --- a/compiler/rustc_const_eval/src/interpret/visitor.rs +++ b/compiler/rustc_const_eval/src/interpret/visitor.rs @@ -324,7 +324,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueMut<'mir, 'tcx, M> macro_rules! make_value_visitor { ($visitor_trait:ident, $value_trait:ident, $($mutability:ident)?) => { - // How to traverse a value and what to do when we are at the leaves. + /// How to traverse a value and what to do when we are at the leaves. pub trait $visitor_trait<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>>: Sized { type V: $value_trait<'mir, 'tcx, M>; diff --git a/compiler/rustc_const_eval/src/transform/check_consts/mod.rs b/compiler/rustc_const_eval/src/transform/check_consts/mod.rs index b90e0962ce6ad..655ec345ed377 100644 --- a/compiler/rustc_const_eval/src/transform/check_consts/mod.rs +++ b/compiler/rustc_const_eval/src/transform/check_consts/mod.rs @@ -75,14 +75,14 @@ pub fn rustc_allow_const_fn_unstable( attr::rustc_allow_const_fn_unstable(&tcx.sess, attrs).any(|name| name == feature_gate) } -// Returns `true` if the given `const fn` is "const-stable". -// -// Panics if the given `DefId` does not refer to a `const fn`. -// -// Const-stability is only relevant for `const fn` within a `staged_api` crate. Only "const-stable" -// functions can be called in a const-context by users of the stable compiler. "const-stable" -// functions are subject to more stringent restrictions than "const-unstable" functions: They -// cannot use unstable features and can only call other "const-stable" functions. +/// Returns `true` if the given `const fn` is "const-stable". +/// +/// Panics if the given `DefId` does not refer to a `const fn`. +/// +/// Const-stability is only relevant for `const fn` within a `staged_api` crate. Only "const-stable" +/// functions can be called in a const-context by users of the stable compiler. "const-stable" +/// functions are subject to more stringent restrictions than "const-unstable" functions: They +/// cannot use unstable features and can only call other "const-stable" functions. pub fn is_const_stable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool { // A default body in a `#[const_trait]` is not const-stable because const // trait fns currently cannot be const-stable. We shouldn't diff --git a/compiler/rustc_const_eval/src/transform/check_consts/ops.rs b/compiler/rustc_const_eval/src/transform/check_consts/ops.rs index bfc950eff5c06..27fb95dcd4031 100644 --- a/compiler/rustc_const_eval/src/transform/check_consts/ops.rs +++ b/compiler/rustc_const_eval/src/transform/check_consts/ops.rs @@ -686,7 +686,7 @@ impl<'tcx> NonConstOp<'tcx> for ThreadLocalAccess { } } -// Types that cannot appear in the signature or locals of a `const fn`. +/// Types that cannot appear in the signature or locals of a `const fn`. pub mod ty { use super::*; diff --git a/compiler/rustc_const_eval/src/util/aggregate.rs b/compiler/rustc_const_eval/src/util/aggregate.rs index 180a40043db07..c43de3368c62f 100644 --- a/compiler/rustc_const_eval/src/util/aggregate.rs +++ b/compiler/rustc_const_eval/src/util/aggregate.rs @@ -9,10 +9,11 @@ use std::iter::TrustedLen; /// Expand `lhs = Rvalue::Aggregate(kind, operands)` into assignments to the fields. /// /// Produces something like -/// +/// ```ignore (ilustrative) /// (lhs as Variant).field0 = arg0; // We only have a downcast if this is an enum /// (lhs as Variant).field1 = arg1; /// discriminant(lhs) = variant_index; // If lhs is an enum or generator. +/// ``` pub fn expand_aggregate<'tcx>( orig_lhs: Place<'tcx>, operands: impl Iterator, Ty<'tcx>)> + TrustedLen, diff --git a/compiler/rustc_driver/src/lib.rs b/compiler/rustc_driver/src/lib.rs index 8e176efb2a9ed..380fbd732d505 100644 --- a/compiler/rustc_driver/src/lib.rs +++ b/compiler/rustc_driver/src/lib.rs @@ -1336,8 +1336,8 @@ mod signal_handler { } } - // When an error signal (such as SIGABRT or SIGSEGV) is delivered to the - // process, print a stack trace and then exit. + /// When an error signal (such as SIGABRT or SIGSEGV) is delivered to the + /// process, print a stack trace and then exit. pub(super) fn install() { unsafe { const ALT_STACK_SIZE: usize = libc::MINSIGSTKSZ + 64 * 1024; diff --git a/compiler/rustc_expand/src/base.rs b/compiler/rustc_expand/src/base.rs index 8955abebf1e0f..9fe5d588b1f5d 100644 --- a/compiler/rustc_expand/src/base.rs +++ b/compiler/rustc_expand/src/base.rs @@ -242,8 +242,8 @@ pub enum ExpandResult { Retry(U), } -// `meta_item` is the attribute, and `item` is the item being modified. pub trait MultiItemModifier { + /// `meta_item` is the attribute, and `item` is the item being modified. fn expand( &self, ecx: &mut ExtCtxt<'_>, diff --git a/compiler/rustc_expand/src/build.rs b/compiler/rustc_expand/src/build.rs index e17cba1478ab6..234cf1b315a23 100644 --- a/compiler/rustc_expand/src/build.rs +++ b/compiler/rustc_expand/src/build.rs @@ -193,7 +193,7 @@ impl<'a> ExtCtxt<'a> { self.stmt_local(local, sp) } - // Generates `let _: Type;`, which is usually used for type assertions. + /// Generates `let _: Type;`, which is usually used for type assertions. pub fn stmt_let_type_only(&self, span: Span, ty: P) -> ast::Stmt { let local = P(ast::Local { pat: self.pat_wild(span), diff --git a/compiler/rustc_expand/src/config.rs b/compiler/rustc_expand/src/config.rs index 1d2b1298a68f6..2510795c2e3ed 100644 --- a/compiler/rustc_expand/src/config.rs +++ b/compiler/rustc_expand/src/config.rs @@ -200,7 +200,7 @@ fn get_features( features } -// `cfg_attr`-process the crate's attributes and compute the crate's features. +/// `cfg_attr`-process the crate's attributes and compute the crate's features. pub fn features( sess: &Session, mut krate: ast::Crate, diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs index c2b1b96cd6465..3e98b024c73b9 100644 --- a/compiler/rustc_expand/src/expand.rs +++ b/compiler/rustc_expand/src/expand.rs @@ -401,7 +401,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { krate } - // Recursively expand all macro invocations in this AST fragment. + /// Recursively expand all macro invocations in this AST fragment. pub fn fully_expand_fragment(&mut self, input_fragment: AstFragment) -> AstFragment { let orig_expansion_data = self.cx.current_expansion.clone(); let orig_force_mode = self.cx.force_mode; @@ -1931,9 +1931,12 @@ pub struct ExpansionConfig<'feat> { pub features: Option<&'feat Features>, pub recursion_limit: Limit, pub trace_mac: bool, - pub should_test: bool, // If false, strip `#[test]` nodes - pub span_debug: bool, // If true, use verbose debugging for `proc_macro::Span` - pub proc_macro_backtrace: bool, // If true, show backtraces for proc-macro panics + /// If false, strip `#[test]` nodes + pub should_test: bool, + /// If true, use verbose debugging for `proc_macro::Span` + pub span_debug: bool, + /// If true, show backtraces for proc-macro panics + pub proc_macro_backtrace: bool, } impl<'feat> ExpansionConfig<'feat> { diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 473a04f33a9ad..042e65fc4dff4 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -969,8 +969,8 @@ pub struct Pat<'hir> { pub hir_id: HirId, pub kind: PatKind<'hir>, pub span: Span, - // Whether to use default binding modes. - // At present, this is false only for destructuring assignment. + /// Whether to use default binding modes. + /// At present, this is false only for destructuring assignment. pub default_binding_modes: bool, } @@ -1078,7 +1078,7 @@ impl fmt::Display for RangeEnd { pub struct DotDotPos(u32); impl DotDotPos { - // Panics if n >= u32::MAX. + /// Panics if n >= u32::MAX. pub fn new(n: Option) -> Self { match n { Some(n) => { @@ -1682,10 +1682,10 @@ impl Expr<'_> { } } - // Whether this looks like a place expr, without checking for deref - // adjustments. - // This will return `true` in some potentially surprising cases such as - // `CONSTANT.field`. + /// Whether this looks like a place expr, without checking for deref + /// adjustments. + /// This will return `true` in some potentially surprising cases such as + /// `CONSTANT.field`. pub fn is_syntactic_place_expr(&self) -> bool { self.is_place_expr(|_| true) } @@ -1826,7 +1826,7 @@ impl Expr<'_> { } } - // To a first-order approximation, is this a pattern + /// To a first-order approximation, is this a pattern? pub fn is_approximately_pattern(&self) -> bool { match &self.kind { ExprKind::Box(_) @@ -2148,11 +2148,11 @@ impl fmt::Display for LoopIdError { #[derive(Copy, Clone, Encodable, Debug, HashStable_Generic)] pub struct Destination { - // This is `Some(_)` iff there is an explicit user-specified `label + /// This is `Some(_)` iff there is an explicit user-specified 'label pub label: Option
)` on an `Option` value. This can be done more directly by calling `map_or(, )` instead - --> $DIR/map_unwrap_or.rs:18:13 + --> $DIR/map_unwrap_or.rs:17:13 | LL | let _ = opt.map(|x| x + 1) | _____________^ @@ -15,7 +15,7 @@ LL + let _ = opt.map_or(0, |x| x + 1); | error: called `map().unwrap_or()` on an `Option` value. This can be done more directly by calling `map_or(, )` instead - --> $DIR/map_unwrap_or.rs:22:13 + --> $DIR/map_unwrap_or.rs:21:13 | LL | let _ = opt.map(|x| { | _____________^ @@ -33,7 +33,7 @@ LL ~ ); | error: called `map().unwrap_or()` on an `Option` value. This can be done more directly by calling `map_or(, )` instead - --> $DIR/map_unwrap_or.rs:26:13 + --> $DIR/map_unwrap_or.rs:25:13 | LL | let _ = opt.map(|x| x + 1) | _____________^ @@ -50,7 +50,7 @@ LL ~ }, |x| x + 1); | error: called `map().unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then()` instead - --> $DIR/map_unwrap_or.rs:31:13 + --> $DIR/map_unwrap_or.rs:30:13 | LL | let _ = opt.map(|x| Some(x + 1)).unwrap_or(None); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -62,7 +62,7 @@ LL + let _ = opt.and_then(|x| Some(x + 1)); | error: called `map().unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then()` instead - --> $DIR/map_unwrap_or.rs:33:13 + --> $DIR/map_unwrap_or.rs:32:13 | LL | let _ = opt.map(|x| { | _____________^ @@ -80,7 +80,7 @@ LL ~ ); | error: called `map().unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then()` instead - --> $DIR/map_unwrap_or.rs:37:13 + --> $DIR/map_unwrap_or.rs:36:13 | LL | let _ = opt | _____________^ @@ -95,7 +95,7 @@ LL + .and_then(|x| Some(x + 1)); | error: called `map().unwrap_or()` on an `Option` value. This can be done more directly by calling `map_or(, )` instead - --> $DIR/map_unwrap_or.rs:48:13 + --> $DIR/map_unwrap_or.rs:47:13 | LL | let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -107,7 +107,7 @@ LL + let _ = Some("prefix").map_or(id, |p| format!("{}.", p)); | error: called `map().unwrap_or_else()` on an `Option` value. This can be done more directly by calling `map_or_else(, )` instead - --> $DIR/map_unwrap_or.rs:52:13 + --> $DIR/map_unwrap_or.rs:51:13 | LL | let _ = opt.map(|x| { | _____________^ @@ -117,7 +117,7 @@ LL | | ).unwrap_or_else(|| 0); | |__________________________^ error: called `map().unwrap_or_else()` on an `Option` value. This can be done more directly by calling `map_or_else(, )` instead - --> $DIR/map_unwrap_or.rs:56:13 + --> $DIR/map_unwrap_or.rs:55:13 | LL | let _ = opt.map(|x| x + 1) | _____________^ @@ -127,7 +127,7 @@ LL | | ); | |_________^ error: called `map().unwrap_or_else()` on a `Result` value. This can be done more directly by calling `.map_or_else(, )` instead - --> $DIR/map_unwrap_or.rs:68:13 + --> $DIR/map_unwrap_or.rs:67:13 | LL | let _ = res.map(|x| { | _____________^ @@ -137,7 +137,7 @@ LL | | ).unwrap_or_else(|_e| 0); | |____________________________^ error: called `map().unwrap_or_else()` on a `Result` value. This can be done more directly by calling `.map_or_else(, )` instead - --> $DIR/map_unwrap_or.rs:72:13 + --> $DIR/map_unwrap_or.rs:71:13 | LL | let _ = res.map(|x| x + 1) | _____________^ @@ -147,7 +147,7 @@ LL | | }); | |__________^ error: called `map().unwrap_or_else()` on a `Result` value. This can be done more directly by calling `.map_or_else(, )` instead - --> $DIR/map_unwrap_or.rs:98:13 + --> $DIR/map_unwrap_or.rs:95:13 | LL | let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `res.map_or_else(|_e| 0, |x| x + 1)` diff --git a/tests/ui/match_expr_like_matches_macro.fixed b/tests/ui/match_expr_like_matches_macro.fixed index 968f462f8a029..55cd15bd5c385 100644 --- a/tests/ui/match_expr_like_matches_macro.fixed +++ b/tests/ui/match_expr_like_matches_macro.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::match_like_matches_macro)] #![allow( unreachable_patterns, @@ -200,17 +199,15 @@ fn main() { }; } +#[clippy::msrv = "1.41"] fn msrv_1_41() { - #![clippy::msrv = "1.41"] - let _y = match Some(5) { Some(0) => true, _ => false, }; } +#[clippy::msrv = "1.42"] fn msrv_1_42() { - #![clippy::msrv = "1.42"] - let _y = matches!(Some(5), Some(0)); } diff --git a/tests/ui/match_expr_like_matches_macro.rs b/tests/ui/match_expr_like_matches_macro.rs index c6b479e27c5ac..5d645e108e511 100644 --- a/tests/ui/match_expr_like_matches_macro.rs +++ b/tests/ui/match_expr_like_matches_macro.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::match_like_matches_macro)] #![allow( unreachable_patterns, @@ -241,18 +240,16 @@ fn main() { }; } +#[clippy::msrv = "1.41"] fn msrv_1_41() { - #![clippy::msrv = "1.41"] - let _y = match Some(5) { Some(0) => true, _ => false, }; } +#[clippy::msrv = "1.42"] fn msrv_1_42() { - #![clippy::msrv = "1.42"] - let _y = match Some(5) { Some(0) => true, _ => false, diff --git a/tests/ui/match_expr_like_matches_macro.stderr b/tests/ui/match_expr_like_matches_macro.stderr index a4df8008ac239..46f67ef4900f8 100644 --- a/tests/ui/match_expr_like_matches_macro.stderr +++ b/tests/ui/match_expr_like_matches_macro.stderr @@ -1,5 +1,5 @@ error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:16:14 + --> $DIR/match_expr_like_matches_macro.rs:15:14 | LL | let _y = match x { | ______________^ @@ -11,7 +11,7 @@ LL | | }; = note: `-D clippy::match-like-matches-macro` implied by `-D warnings` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:22:14 + --> $DIR/match_expr_like_matches_macro.rs:21:14 | LL | let _w = match x { | ______________^ @@ -21,7 +21,7 @@ LL | | }; | |_____^ help: try this: `matches!(x, Some(_))` error: redundant pattern matching, consider using `is_none()` - --> $DIR/match_expr_like_matches_macro.rs:28:14 + --> $DIR/match_expr_like_matches_macro.rs:27:14 | LL | let _z = match x { | ______________^ @@ -33,7 +33,7 @@ LL | | }; = note: `-D clippy::redundant-pattern-matching` implied by `-D warnings` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:34:15 + --> $DIR/match_expr_like_matches_macro.rs:33:15 | LL | let _zz = match x { | _______________^ @@ -43,13 +43,13 @@ LL | | }; | |_____^ help: try this: `!matches!(x, Some(r) if r == 0)` error: if let .. else expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:40:16 + --> $DIR/match_expr_like_matches_macro.rs:39:16 | LL | let _zzz = if let Some(5) = x { true } else { false }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `matches!(x, Some(5))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:64:20 + --> $DIR/match_expr_like_matches_macro.rs:63:20 | LL | let _ans = match x { | ____________________^ @@ -60,7 +60,7 @@ LL | | }; | |_________^ help: try this: `matches!(x, E::A(_) | E::B(_))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:74:20 + --> $DIR/match_expr_like_matches_macro.rs:73:20 | LL | let _ans = match x { | ____________________^ @@ -73,7 +73,7 @@ LL | | }; | |_________^ help: try this: `matches!(x, E::A(_) | E::B(_))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:84:20 + --> $DIR/match_expr_like_matches_macro.rs:83:20 | LL | let _ans = match x { | ____________________^ @@ -84,7 +84,7 @@ LL | | }; | |_________^ help: try this: `!matches!(x, E::B(_) | E::C)` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:144:18 + --> $DIR/match_expr_like_matches_macro.rs:143:18 | LL | let _z = match &z { | __________________^ @@ -94,7 +94,7 @@ LL | | }; | |_________^ help: try this: `matches!(z, Some(3))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:153:18 + --> $DIR/match_expr_like_matches_macro.rs:152:18 | LL | let _z = match &z { | __________________^ @@ -104,7 +104,7 @@ LL | | }; | |_________^ help: try this: `matches!(&z, Some(3))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:170:21 + --> $DIR/match_expr_like_matches_macro.rs:169:21 | LL | let _ = match &z { | _____________________^ @@ -114,7 +114,7 @@ LL | | }; | |_____________^ help: try this: `matches!(&z, AnEnum::X)` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:184:20 + --> $DIR/match_expr_like_matches_macro.rs:183:20 | LL | let _res = match &val { | ____________________^ @@ -124,7 +124,7 @@ LL | | }; | |_________^ help: try this: `matches!(&val, &Some(ref _a))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:196:20 + --> $DIR/match_expr_like_matches_macro.rs:195:20 | LL | let _res = match &val { | ____________________^ @@ -134,7 +134,7 @@ LL | | }; | |_________^ help: try this: `matches!(&val, &Some(ref _a))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:256:14 + --> $DIR/match_expr_like_matches_macro.rs:253:14 | LL | let _y = match Some(5) { | ______________^ diff --git a/tests/ui/mem_replace.fixed b/tests/ui/mem_replace.fixed index ae237395b95fc..874d558433034 100644 --- a/tests/ui/mem_replace.fixed +++ b/tests/ui/mem_replace.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![allow(unused)] #![warn( clippy::all, @@ -80,16 +79,14 @@ fn main() { dont_lint_primitive(); } +#[clippy::msrv = "1.39"] fn msrv_1_39() { - #![clippy::msrv = "1.39"] - let mut s = String::from("foo"); let _ = std::mem::replace(&mut s, String::default()); } +#[clippy::msrv = "1.40"] fn msrv_1_40() { - #![clippy::msrv = "1.40"] - let mut s = String::from("foo"); let _ = std::mem::take(&mut s); } diff --git a/tests/ui/mem_replace.rs b/tests/ui/mem_replace.rs index 3202e99e0be9e..f4f3bff514463 100644 --- a/tests/ui/mem_replace.rs +++ b/tests/ui/mem_replace.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![allow(unused)] #![warn( clippy::all, @@ -80,16 +79,14 @@ fn main() { dont_lint_primitive(); } +#[clippy::msrv = "1.39"] fn msrv_1_39() { - #![clippy::msrv = "1.39"] - let mut s = String::from("foo"); let _ = std::mem::replace(&mut s, String::default()); } +#[clippy::msrv = "1.40"] fn msrv_1_40() { - #![clippy::msrv = "1.40"] - let mut s = String::from("foo"); let _ = std::mem::replace(&mut s, String::default()); } diff --git a/tests/ui/mem_replace.stderr b/tests/ui/mem_replace.stderr index dd8a50dab9002..caa127f76eeff 100644 --- a/tests/ui/mem_replace.stderr +++ b/tests/ui/mem_replace.stderr @@ -1,5 +1,5 @@ error: replacing an `Option` with `None` - --> $DIR/mem_replace.rs:17:13 + --> $DIR/mem_replace.rs:16:13 | LL | let _ = mem::replace(&mut an_option, None); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()` @@ -7,13 +7,13 @@ LL | let _ = mem::replace(&mut an_option, None); = note: `-D clippy::mem-replace-option-with-none` implied by `-D warnings` error: replacing an `Option` with `None` - --> $DIR/mem_replace.rs:19:13 + --> $DIR/mem_replace.rs:18:13 | LL | let _ = mem::replace(an_option, None); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:24:13 + --> $DIR/mem_replace.rs:23:13 | LL | let _ = std::mem::replace(&mut s, String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut s)` @@ -21,103 +21,103 @@ LL | let _ = std::mem::replace(&mut s, String::default()); = note: `-D clippy::mem-replace-with-default` implied by `-D warnings` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:27:13 + --> $DIR/mem_replace.rs:26:13 | LL | let _ = std::mem::replace(s, String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(s)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:28:13 + --> $DIR/mem_replace.rs:27:13 | LL | let _ = std::mem::replace(s, Default::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(s)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:31:13 + --> $DIR/mem_replace.rs:30:13 | LL | let _ = std::mem::replace(&mut v, Vec::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut v)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:32:13 + --> $DIR/mem_replace.rs:31:13 | LL | let _ = std::mem::replace(&mut v, Default::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut v)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:33:13 + --> $DIR/mem_replace.rs:32:13 | LL | let _ = std::mem::replace(&mut v, Vec::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut v)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:34:13 + --> $DIR/mem_replace.rs:33:13 | LL | let _ = std::mem::replace(&mut v, vec![]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut v)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:37:13 + --> $DIR/mem_replace.rs:36:13 | LL | let _ = std::mem::replace(&mut hash_map, HashMap::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut hash_map)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:40:13 + --> $DIR/mem_replace.rs:39:13 | LL | let _ = std::mem::replace(&mut btree_map, BTreeMap::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut btree_map)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:43:13 + --> $DIR/mem_replace.rs:42:13 | LL | let _ = std::mem::replace(&mut vd, VecDeque::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut vd)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:46:13 + --> $DIR/mem_replace.rs:45:13 | LL | let _ = std::mem::replace(&mut hash_set, HashSet::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut hash_set)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:49:13 + --> $DIR/mem_replace.rs:48:13 | LL | let _ = std::mem::replace(&mut btree_set, BTreeSet::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut btree_set)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:52:13 + --> $DIR/mem_replace.rs:51:13 | LL | let _ = std::mem::replace(&mut list, LinkedList::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut list)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:55:13 + --> $DIR/mem_replace.rs:54:13 | LL | let _ = std::mem::replace(&mut binary_heap, BinaryHeap::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut binary_heap)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:58:13 + --> $DIR/mem_replace.rs:57:13 | LL | let _ = std::mem::replace(&mut tuple, (vec![], BinaryHeap::new())); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut tuple)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:61:13 + --> $DIR/mem_replace.rs:60:13 | LL | let _ = std::mem::replace(&mut refstr, ""); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut refstr)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:64:13 + --> $DIR/mem_replace.rs:63:13 | LL | let _ = std::mem::replace(&mut slice, &[]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut slice)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:94:13 + --> $DIR/mem_replace.rs:91:13 | LL | let _ = std::mem::replace(&mut s, String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut s)` diff --git a/tests/ui/min_rust_version_attr.rs b/tests/ui/min_rust_version_attr.rs index 28ab132394b82..955e7eb727634 100644 --- a/tests/ui/min_rust_version_attr.rs +++ b/tests/ui/min_rust_version_attr.rs @@ -3,27 +3,37 @@ fn main() {} +#[clippy::msrv = "1.42.0"] fn just_under_msrv() { - #![clippy::msrv = "1.42.0"] let log2_10 = 3.321928094887362; } +#[clippy::msrv = "1.43.0"] fn meets_msrv() { - #![clippy::msrv = "1.43.0"] let log2_10 = 3.321928094887362; } +#[clippy::msrv = "1.44.0"] fn just_above_msrv() { - #![clippy::msrv = "1.44.0"] let log2_10 = 3.321928094887362; } +#[clippy::msrv = "1.42"] fn no_patch_under() { - #![clippy::msrv = "1.42"] let log2_10 = 3.321928094887362; } +#[clippy::msrv = "1.43"] fn no_patch_meets() { + let log2_10 = 3.321928094887362; +} + +fn inner_attr_under() { + #![clippy::msrv = "1.42"] + let log2_10 = 3.321928094887362; +} + +fn inner_attr_meets() { #![clippy::msrv = "1.43"] let log2_10 = 3.321928094887362; } diff --git a/tests/ui/min_rust_version_attr.stderr b/tests/ui/min_rust_version_attr.stderr index 6174443372f87..7e2135584efde 100644 --- a/tests/ui/min_rust_version_attr.stderr +++ b/tests/ui/min_rust_version_attr.stderr @@ -32,12 +32,20 @@ LL | let log2_10 = 3.321928094887362; = help: consider using the constant directly error: approximate value of `f{32, 64}::consts::LOG2_10` found - --> $DIR/min_rust_version_attr.rs:45:27 + --> $DIR/min_rust_version_attr.rs:48:19 + | +LL | let log2_10 = 3.321928094887362; + | ^^^^^^^^^^^^^^^^^ + | + = help: consider using the constant directly + +error: approximate value of `f{32, 64}::consts::LOG2_10` found + --> $DIR/min_rust_version_attr.rs:55:27 | LL | let log2_10 = 3.321928094887362; | ^^^^^^^^^^^^^^^^^ | = help: consider using the constant directly -error: aborting due to 5 previous errors +error: aborting due to 6 previous errors diff --git a/tests/ui/min_rust_version_invalid_attr.stderr b/tests/ui/min_rust_version_invalid_attr.stderr index 93370a0fa9c91..675b780315251 100644 --- a/tests/ui/min_rust_version_invalid_attr.stderr +++ b/tests/ui/min_rust_version_invalid_attr.stderr @@ -4,7 +4,7 @@ error: `invalid.version` is not a valid Rust version LL | #![clippy::msrv = "invalid.version"] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: `msrv` cannot be an outer attribute +error: `invalid.version` is not a valid Rust version --> $DIR/min_rust_version_invalid_attr.rs:6:1 | LL | #[clippy::msrv = "invalid.version"] diff --git a/tests/ui/missing_const_for_fn/cant_be_const.rs b/tests/ui/missing_const_for_fn/cant_be_const.rs index b950248ef9420..75cace1816754 100644 --- a/tests/ui/missing_const_for_fn/cant_be_const.rs +++ b/tests/ui/missing_const_for_fn/cant_be_const.rs @@ -7,7 +7,6 @@ #![warn(clippy::missing_const_for_fn)] #![feature(start)] -#![feature(custom_inner_attributes)] extern crate helper; extern crate proc_macro_with_span; @@ -115,9 +114,8 @@ fn unstably_const_fn() { helper::unstably_const_fn() } +#[clippy::msrv = "1.46.0"] mod const_fn_stabilized_after_msrv { - #![clippy::msrv = "1.46.0"] - // Do not lint this because `u8::is_ascii_digit` is stabilized as a const function in 1.47.0. fn const_fn_stabilized_after_msrv(byte: u8) { byte.is_ascii_digit(); diff --git a/tests/ui/missing_const_for_fn/could_be_const.rs b/tests/ui/missing_const_for_fn/could_be_const.rs index b85e88784918d..0246c8622ed3a 100644 --- a/tests/ui/missing_const_for_fn/could_be_const.rs +++ b/tests/ui/missing_const_for_fn/could_be_const.rs @@ -1,6 +1,5 @@ #![warn(clippy::missing_const_for_fn)] #![allow(incomplete_features, clippy::let_and_return)] -#![feature(custom_inner_attributes)] use std::mem::transmute; @@ -68,24 +67,21 @@ mod with_drop { } } +#[clippy::msrv = "1.47.0"] mod const_fn_stabilized_before_msrv { - #![clippy::msrv = "1.47.0"] - // This could be const because `u8::is_ascii_digit` is a stable const function in 1.47. fn const_fn_stabilized_before_msrv(byte: u8) { byte.is_ascii_digit(); } } +#[clippy::msrv = "1.45"] fn msrv_1_45() -> i32 { - #![clippy::msrv = "1.45"] - 45 } +#[clippy::msrv = "1.46"] fn msrv_1_46() -> i32 { - #![clippy::msrv = "1.46"] - 46 } diff --git a/tests/ui/missing_const_for_fn/could_be_const.stderr b/tests/ui/missing_const_for_fn/could_be_const.stderr index f8e221c82f1a5..955e1ed263408 100644 --- a/tests/ui/missing_const_for_fn/could_be_const.stderr +++ b/tests/ui/missing_const_for_fn/could_be_const.stderr @@ -1,5 +1,5 @@ error: this could be a `const fn` - --> $DIR/could_be_const.rs:13:5 + --> $DIR/could_be_const.rs:12:5 | LL | / pub fn new() -> Self { LL | | Self { guess: 42 } @@ -9,7 +9,7 @@ LL | | } = note: `-D clippy::missing-const-for-fn` implied by `-D warnings` error: this could be a `const fn` - --> $DIR/could_be_const.rs:17:5 + --> $DIR/could_be_const.rs:16:5 | LL | / fn const_generic_params<'a, T, const N: usize>(&self, b: &'a [T; N]) -> &'a [T; N] { LL | | b @@ -17,7 +17,7 @@ LL | | } | |_____^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:23:1 + --> $DIR/could_be_const.rs:22:1 | LL | / fn one() -> i32 { LL | | 1 @@ -25,7 +25,7 @@ LL | | } | |_^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:28:1 + --> $DIR/could_be_const.rs:27:1 | LL | / fn two() -> i32 { LL | | let abc = 2; @@ -34,7 +34,7 @@ LL | | } | |_^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:34:1 + --> $DIR/could_be_const.rs:33:1 | LL | / fn string() -> String { LL | | String::new() @@ -42,7 +42,7 @@ LL | | } | |_^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:39:1 + --> $DIR/could_be_const.rs:38:1 | LL | / unsafe fn four() -> i32 { LL | | 4 @@ -50,7 +50,7 @@ LL | | } | |_^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:44:1 + --> $DIR/could_be_const.rs:43:1 | LL | / fn generic(t: T) -> T { LL | | t @@ -58,7 +58,7 @@ LL | | } | |_^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:52:1 + --> $DIR/could_be_const.rs:51:1 | LL | / fn generic_arr(t: [T; 1]) -> T { LL | | t[0] @@ -66,7 +66,7 @@ LL | | } | |_^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:65:9 + --> $DIR/could_be_const.rs:64:9 | LL | / pub fn b(self, a: &A) -> B { LL | | B @@ -74,7 +74,7 @@ LL | | } | |_________^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:75:5 + --> $DIR/could_be_const.rs:73:5 | LL | / fn const_fn_stabilized_before_msrv(byte: u8) { LL | | byte.is_ascii_digit(); @@ -82,11 +82,9 @@ LL | | } | |_____^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:86:1 + --> $DIR/could_be_const.rs:84:1 | LL | / fn msrv_1_46() -> i32 { -LL | | #![clippy::msrv = "1.46"] -LL | | LL | | 46 LL | | } | |_^ diff --git a/tests/ui/needless_borrow.fixed b/tests/ui/needless_borrow.fixed index 85b6b639d5549..4cb7f6b687f11 100644 --- a/tests/ui/needless_borrow.fixed +++ b/tests/ui/needless_borrow.fixed @@ -1,13 +1,13 @@ // run-rustfix -#![feature(custom_inner_attributes, lint_reasons)] - -#[warn(clippy::all, clippy::needless_borrow)] -#[allow(unused_variables)] -#[allow( +#![feature(lint_reasons)] +#![allow( + unused, clippy::uninlined_format_args, clippy::unnecessary_mut_passed, clippy::unnecessary_to_owned )] +#![warn(clippy::needless_borrow)] + fn main() { let a = 5; let ref_a = &a; @@ -171,14 +171,12 @@ impl<'a> Trait for &'a str {} fn h(_: &dyn Trait) {} -#[allow(dead_code)] fn check_expect_suppression() { let a = 5; #[expect(clippy::needless_borrow)] let _ = x(&&a); } -#[allow(dead_code)] mod issue9160 { pub struct S { f: F, @@ -267,7 +265,6 @@ where } // https://github.com/rust-lang/rust-clippy/pull/9136#pullrequestreview-1037379321 -#[allow(dead_code)] mod copyable_iterator { #[derive(Clone, Copy)] struct Iter; @@ -287,25 +284,20 @@ mod copyable_iterator { } } +#[clippy::msrv = "1.52.0"] mod under_msrv { - #![allow(dead_code)] - #![clippy::msrv = "1.52.0"] - fn foo() { let _ = std::process::Command::new("ls").args(&["-a", "-l"]).status().unwrap(); } } +#[clippy::msrv = "1.53.0"] mod meets_msrv { - #![allow(dead_code)] - #![clippy::msrv = "1.53.0"] - fn foo() { let _ = std::process::Command::new("ls").args(["-a", "-l"]).status().unwrap(); } } -#[allow(unused)] fn issue9383() { // Should not lint because unions need explicit deref when accessing field use std::mem::ManuallyDrop; @@ -334,7 +326,6 @@ fn issue9383() { } } -#[allow(dead_code)] fn closure_test() { let env = "env".to_owned(); let arg = "arg".to_owned(); @@ -348,7 +339,6 @@ fn closure_test() { f(arg); } -#[allow(dead_code)] mod significant_drop { #[derive(Debug)] struct X; @@ -368,7 +358,6 @@ mod significant_drop { fn debug(_: impl std::fmt::Debug) {} } -#[allow(dead_code)] mod used_exactly_once { fn foo(x: String) { use_x(x); @@ -376,7 +365,6 @@ mod used_exactly_once { fn use_x(_: impl AsRef) {} } -#[allow(dead_code)] mod used_more_than_once { fn foo(x: String) { use_x(&x); @@ -387,7 +375,6 @@ mod used_more_than_once { } // https://github.com/rust-lang/rust-clippy/issues/9111#issuecomment-1277114280 -#[allow(dead_code)] mod issue_9111 { struct A; @@ -409,7 +396,6 @@ mod issue_9111 { } } -#[allow(dead_code)] mod issue_9710 { fn main() { let string = String::new(); @@ -421,7 +407,6 @@ mod issue_9710 { fn f>(_: T) {} } -#[allow(dead_code)] mod issue_9739 { fn foo(_it: impl IntoIterator) {} @@ -434,7 +419,6 @@ mod issue_9739 { } } -#[allow(dead_code)] mod issue_9739_method_variant { struct S; @@ -451,7 +435,6 @@ mod issue_9739_method_variant { } } -#[allow(dead_code)] mod issue_9782 { fn foo>(t: T) { println!("{}", std::mem::size_of::()); @@ -475,7 +458,6 @@ mod issue_9782 { } } -#[allow(dead_code)] mod issue_9782_type_relative_variant { struct S; @@ -493,7 +475,6 @@ mod issue_9782_type_relative_variant { } } -#[allow(dead_code)] mod issue_9782_method_variant { struct S; diff --git a/tests/ui/needless_borrow.rs b/tests/ui/needless_borrow.rs index 7b97bcf3817ec..9a01190ed8dbd 100644 --- a/tests/ui/needless_borrow.rs +++ b/tests/ui/needless_borrow.rs @@ -1,13 +1,13 @@ // run-rustfix -#![feature(custom_inner_attributes, lint_reasons)] - -#[warn(clippy::all, clippy::needless_borrow)] -#[allow(unused_variables)] -#[allow( +#![feature(lint_reasons)] +#![allow( + unused, clippy::uninlined_format_args, clippy::unnecessary_mut_passed, clippy::unnecessary_to_owned )] +#![warn(clippy::needless_borrow)] + fn main() { let a = 5; let ref_a = &a; @@ -171,14 +171,12 @@ impl<'a> Trait for &'a str {} fn h(_: &dyn Trait) {} -#[allow(dead_code)] fn check_expect_suppression() { let a = 5; #[expect(clippy::needless_borrow)] let _ = x(&&a); } -#[allow(dead_code)] mod issue9160 { pub struct S { f: F, @@ -267,7 +265,6 @@ where } // https://github.com/rust-lang/rust-clippy/pull/9136#pullrequestreview-1037379321 -#[allow(dead_code)] mod copyable_iterator { #[derive(Clone, Copy)] struct Iter; @@ -287,25 +284,20 @@ mod copyable_iterator { } } +#[clippy::msrv = "1.52.0"] mod under_msrv { - #![allow(dead_code)] - #![clippy::msrv = "1.52.0"] - fn foo() { let _ = std::process::Command::new("ls").args(&["-a", "-l"]).status().unwrap(); } } +#[clippy::msrv = "1.53.0"] mod meets_msrv { - #![allow(dead_code)] - #![clippy::msrv = "1.53.0"] - fn foo() { let _ = std::process::Command::new("ls").args(&["-a", "-l"]).status().unwrap(); } } -#[allow(unused)] fn issue9383() { // Should not lint because unions need explicit deref when accessing field use std::mem::ManuallyDrop; @@ -334,7 +326,6 @@ fn issue9383() { } } -#[allow(dead_code)] fn closure_test() { let env = "env".to_owned(); let arg = "arg".to_owned(); @@ -348,7 +339,6 @@ fn closure_test() { f(arg); } -#[allow(dead_code)] mod significant_drop { #[derive(Debug)] struct X; @@ -368,7 +358,6 @@ mod significant_drop { fn debug(_: impl std::fmt::Debug) {} } -#[allow(dead_code)] mod used_exactly_once { fn foo(x: String) { use_x(&x); @@ -376,7 +365,6 @@ mod used_exactly_once { fn use_x(_: impl AsRef) {} } -#[allow(dead_code)] mod used_more_than_once { fn foo(x: String) { use_x(&x); @@ -387,7 +375,6 @@ mod used_more_than_once { } // https://github.com/rust-lang/rust-clippy/issues/9111#issuecomment-1277114280 -#[allow(dead_code)] mod issue_9111 { struct A; @@ -409,7 +396,6 @@ mod issue_9111 { } } -#[allow(dead_code)] mod issue_9710 { fn main() { let string = String::new(); @@ -421,7 +407,6 @@ mod issue_9710 { fn f>(_: T) {} } -#[allow(dead_code)] mod issue_9739 { fn foo(_it: impl IntoIterator) {} @@ -434,7 +419,6 @@ mod issue_9739 { } } -#[allow(dead_code)] mod issue_9739_method_variant { struct S; @@ -451,7 +435,6 @@ mod issue_9739_method_variant { } } -#[allow(dead_code)] mod issue_9782 { fn foo>(t: T) { println!("{}", std::mem::size_of::()); @@ -475,7 +458,6 @@ mod issue_9782 { } } -#[allow(dead_code)] mod issue_9782_type_relative_variant { struct S; @@ -493,7 +475,6 @@ mod issue_9782_type_relative_variant { } } -#[allow(dead_code)] mod issue_9782_method_variant { struct S; diff --git a/tests/ui/needless_borrow.stderr b/tests/ui/needless_borrow.stderr index 485e6b84c868b..d26c317124b8d 100644 --- a/tests/ui/needless_borrow.stderr +++ b/tests/ui/needless_borrow.stderr @@ -163,55 +163,55 @@ LL | let _ = std::fs::write("x", &"".to_string()); | ^^^^^^^^^^^^^^^ help: change this to: `"".to_string()` error: this expression borrows a value the compiler would automatically borrow - --> $DIR/needless_borrow.rs:192:13 + --> $DIR/needless_borrow.rs:190:13 | LL | (&self.f)() | ^^^^^^^^^ help: change this to: `(self.f)` error: this expression borrows a value the compiler would automatically borrow - --> $DIR/needless_borrow.rs:201:13 + --> $DIR/needless_borrow.rs:199:13 | LL | (&mut self.f)() | ^^^^^^^^^^^^^ help: change this to: `(self.f)` error: the borrowed expression implements the required traits - --> $DIR/needless_borrow.rs:286:20 + --> $DIR/needless_borrow.rs:283:20 | LL | takes_iter(&mut x) | ^^^^^^ help: change this to: `x` error: the borrowed expression implements the required traits - --> $DIR/needless_borrow.rs:304:55 + --> $DIR/needless_borrow.rs:297:55 | LL | let _ = std::process::Command::new("ls").args(&["-a", "-l"]).status().unwrap(); | ^^^^^^^^^^^^^ help: change this to: `["-a", "-l"]` error: the borrowed expression implements the required traits - --> $DIR/needless_borrow.rs:344:37 + --> $DIR/needless_borrow.rs:335:37 | LL | let _ = std::fs::write("x", &arg); | ^^^^ help: change this to: `arg` error: the borrowed expression implements the required traits - --> $DIR/needless_borrow.rs:345:37 + --> $DIR/needless_borrow.rs:336:37 | LL | let _ = std::fs::write("x", &loc); | ^^^^ help: change this to: `loc` error: the borrowed expression implements the required traits - --> $DIR/needless_borrow.rs:364:15 + --> $DIR/needless_borrow.rs:354:15 | LL | debug(&x); | ^^ help: change this to: `x` error: the borrowed expression implements the required traits - --> $DIR/needless_borrow.rs:374:15 + --> $DIR/needless_borrow.rs:363:15 | LL | use_x(&x); | ^^ help: change this to: `x` error: the borrowed expression implements the required traits - --> $DIR/needless_borrow.rs:474:13 + --> $DIR/needless_borrow.rs:457:13 | LL | foo(&a); | ^^ help: change this to: `a` diff --git a/tests/ui/needless_question_mark.fixed b/tests/ui/needless_question_mark.fixed index ba9d15e59d0e4..7eaca571992f4 100644 --- a/tests/ui/needless_question_mark.fixed +++ b/tests/ui/needless_question_mark.fixed @@ -8,7 +8,6 @@ dead_code, unused_must_use )] -#![feature(custom_inner_attributes)] struct TO { magic: Option, diff --git a/tests/ui/needless_question_mark.rs b/tests/ui/needless_question_mark.rs index 3a6523e8fe872..960bc7b78983f 100644 --- a/tests/ui/needless_question_mark.rs +++ b/tests/ui/needless_question_mark.rs @@ -8,7 +8,6 @@ dead_code, unused_must_use )] -#![feature(custom_inner_attributes)] struct TO { magic: Option, diff --git a/tests/ui/needless_question_mark.stderr b/tests/ui/needless_question_mark.stderr index f8308e24e7712..d1f89e326c67c 100644 --- a/tests/ui/needless_question_mark.stderr +++ b/tests/ui/needless_question_mark.stderr @@ -1,5 +1,5 @@ error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:23:12 + --> $DIR/needless_question_mark.rs:22:12 | LL | return Some(to.magic?); | ^^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `to.magic` @@ -7,67 +7,67 @@ LL | return Some(to.magic?); = note: `-D clippy::needless-question-mark` implied by `-D warnings` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:31:12 + --> $DIR/needless_question_mark.rs:30:12 | LL | return Some(to.magic?) | ^^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `to.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:36:5 + --> $DIR/needless_question_mark.rs:35:5 | LL | Some(to.magic?) | ^^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `to.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:41:21 + --> $DIR/needless_question_mark.rs:40:21 | LL | to.and_then(|t| Some(t.magic?)) | ^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `t.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:50:9 + --> $DIR/needless_question_mark.rs:49:9 | LL | Some(t.magic?) | ^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `t.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:55:12 + --> $DIR/needless_question_mark.rs:54:12 | LL | return Ok(tr.magic?); | ^^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `tr.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:62:12 + --> $DIR/needless_question_mark.rs:61:12 | LL | return Ok(tr.magic?) | ^^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `tr.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:66:5 + --> $DIR/needless_question_mark.rs:65:5 | LL | Ok(tr.magic?) | ^^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `tr.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:70:21 + --> $DIR/needless_question_mark.rs:69:21 | LL | tr.and_then(|t| Ok(t.magic?)) | ^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `t.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:78:9 + --> $DIR/needless_question_mark.rs:77:9 | LL | Ok(t.magic?) | ^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `t.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:85:16 + --> $DIR/needless_question_mark.rs:84:16 | LL | return Ok(t.magic?); | ^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `t.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:120:27 + --> $DIR/needless_question_mark.rs:119:27 | LL | || -> Option<_> { Some(Some($expr)?) }() | ^^^^^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `Some($expr)` @@ -78,13 +78,13 @@ LL | let _x = some_and_qmark_in_macro!(x?); = note: this error originates in the macro `some_and_qmark_in_macro` (in Nightly builds, run with -Z macro-backtrace for more info) error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:131:5 + --> $DIR/needless_question_mark.rs:130:5 | LL | Some(to.magic?) | ^^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `to.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:139:5 + --> $DIR/needless_question_mark.rs:138:5 | LL | Ok(s.magic?) | ^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `s.magic` diff --git a/tests/ui/needless_splitn.fixed b/tests/ui/needless_splitn.fixed index 61f5fc4e679ed..5496031fefabf 100644 --- a/tests/ui/needless_splitn.fixed +++ b/tests/ui/needless_splitn.fixed @@ -1,7 +1,6 @@ // run-rustfix // edition:2018 -#![feature(custom_inner_attributes)] #![warn(clippy::needless_splitn)] #![allow(clippy::iter_skip_next, clippy::iter_nth_zero, clippy::manual_split_once)] @@ -40,8 +39,8 @@ fn _question_mark(s: &str) -> Option<()> { Some(()) } +#[clippy::msrv = "1.51"] fn _test_msrv() { - #![clippy::msrv = "1.51"] // `manual_split_once` MSRV shouldn't apply to `needless_splitn` let _ = "key=value".split('=').nth(0).unwrap(); } diff --git a/tests/ui/needless_splitn.rs b/tests/ui/needless_splitn.rs index 71d9a7077faa6..35c2465bae131 100644 --- a/tests/ui/needless_splitn.rs +++ b/tests/ui/needless_splitn.rs @@ -1,7 +1,6 @@ // run-rustfix // edition:2018 -#![feature(custom_inner_attributes)] #![warn(clippy::needless_splitn)] #![allow(clippy::iter_skip_next, clippy::iter_nth_zero, clippy::manual_split_once)] @@ -40,8 +39,8 @@ fn _question_mark(s: &str) -> Option<()> { Some(()) } +#[clippy::msrv = "1.51"] fn _test_msrv() { - #![clippy::msrv = "1.51"] // `manual_split_once` MSRV shouldn't apply to `needless_splitn` let _ = "key=value".splitn(2, '=').nth(0).unwrap(); } diff --git a/tests/ui/needless_splitn.stderr b/tests/ui/needless_splitn.stderr index f112b29e7f206..f607d8e1ab5f4 100644 --- a/tests/ui/needless_splitn.stderr +++ b/tests/ui/needless_splitn.stderr @@ -1,5 +1,5 @@ error: unnecessary use of `splitn` - --> $DIR/needless_splitn.rs:15:13 + --> $DIR/needless_splitn.rs:14:13 | LL | let _ = str.splitn(2, '=').next(); | ^^^^^^^^^^^^^^^^^^ help: try this: `str.split('=')` @@ -7,73 +7,73 @@ LL | let _ = str.splitn(2, '=').next(); = note: `-D clippy::needless-splitn` implied by `-D warnings` error: unnecessary use of `splitn` - --> $DIR/needless_splitn.rs:16:13 + --> $DIR/needless_splitn.rs:15:13 | LL | let _ = str.splitn(2, '=').nth(0); | ^^^^^^^^^^^^^^^^^^ help: try this: `str.split('=')` error: unnecessary use of `splitn` - --> $DIR/needless_splitn.rs:19:18 + --> $DIR/needless_splitn.rs:18:18 | LL | let (_, _) = str.splitn(3, '=').next_tuple().unwrap(); | ^^^^^^^^^^^^^^^^^^ help: try this: `str.split('=')` error: unnecessary use of `rsplitn` - --> $DIR/needless_splitn.rs:22:13 + --> $DIR/needless_splitn.rs:21:13 | LL | let _ = str.rsplitn(2, '=').next(); | ^^^^^^^^^^^^^^^^^^^ help: try this: `str.rsplit('=')` error: unnecessary use of `rsplitn` - --> $DIR/needless_splitn.rs:23:13 + --> $DIR/needless_splitn.rs:22:13 | LL | let _ = str.rsplitn(2, '=').nth(0); | ^^^^^^^^^^^^^^^^^^^ help: try this: `str.rsplit('=')` error: unnecessary use of `rsplitn` - --> $DIR/needless_splitn.rs:26:18 + --> $DIR/needless_splitn.rs:25:18 | LL | let (_, _) = str.rsplitn(3, '=').next_tuple().unwrap(); | ^^^^^^^^^^^^^^^^^^^ help: try this: `str.rsplit('=')` error: unnecessary use of `splitn` - --> $DIR/needless_splitn.rs:28:13 + --> $DIR/needless_splitn.rs:27:13 | LL | let _ = str.splitn(5, '=').next(); | ^^^^^^^^^^^^^^^^^^ help: try this: `str.split('=')` error: unnecessary use of `splitn` - --> $DIR/needless_splitn.rs:29:13 + --> $DIR/needless_splitn.rs:28:13 | LL | let _ = str.splitn(5, '=').nth(3); | ^^^^^^^^^^^^^^^^^^ help: try this: `str.split('=')` error: unnecessary use of `splitn` - --> $DIR/needless_splitn.rs:35:13 + --> $DIR/needless_splitn.rs:34:13 | LL | let _ = s.splitn(2, '=').next()?; | ^^^^^^^^^^^^^^^^ help: try this: `s.split('=')` error: unnecessary use of `splitn` - --> $DIR/needless_splitn.rs:36:13 + --> $DIR/needless_splitn.rs:35:13 | LL | let _ = s.splitn(2, '=').nth(0)?; | ^^^^^^^^^^^^^^^^ help: try this: `s.split('=')` error: unnecessary use of `rsplitn` - --> $DIR/needless_splitn.rs:37:13 + --> $DIR/needless_splitn.rs:36:13 | LL | let _ = s.rsplitn(2, '=').next()?; | ^^^^^^^^^^^^^^^^^ help: try this: `s.rsplit('=')` error: unnecessary use of `rsplitn` - --> $DIR/needless_splitn.rs:38:13 + --> $DIR/needless_splitn.rs:37:13 | LL | let _ = s.rsplitn(2, '=').nth(0)?; | ^^^^^^^^^^^^^^^^^ help: try this: `s.rsplit('=')` error: unnecessary use of `splitn` - --> $DIR/needless_splitn.rs:46:13 + --> $DIR/needless_splitn.rs:45:13 | LL | let _ = "key=value".splitn(2, '=').nth(0).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"key=value".split('=')` diff --git a/tests/ui/option_as_ref_deref.fixed b/tests/ui/option_as_ref_deref.fixed index bc376d0d7fb39..d124d133faa21 100644 --- a/tests/ui/option_as_ref_deref.fixed +++ b/tests/ui/option_as_ref_deref.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![allow(unused, clippy::redundant_clone)] #![warn(clippy::option_as_ref_deref)] @@ -44,16 +43,14 @@ fn main() { let _ = opt.as_deref(); } +#[clippy::msrv = "1.39"] fn msrv_1_39() { - #![clippy::msrv = "1.39"] - let opt = Some(String::from("123")); let _ = opt.as_ref().map(String::as_str); } +#[clippy::msrv = "1.40"] fn msrv_1_40() { - #![clippy::msrv = "1.40"] - let opt = Some(String::from("123")); let _ = opt.as_deref(); } diff --git a/tests/ui/option_as_ref_deref.rs b/tests/ui/option_as_ref_deref.rs index ba3a2eedc225c..86e354c6716bb 100644 --- a/tests/ui/option_as_ref_deref.rs +++ b/tests/ui/option_as_ref_deref.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![allow(unused, clippy::redundant_clone)] #![warn(clippy::option_as_ref_deref)] @@ -47,16 +46,14 @@ fn main() { let _ = opt.as_ref().map(std::ops::Deref::deref); } +#[clippy::msrv = "1.39"] fn msrv_1_39() { - #![clippy::msrv = "1.39"] - let opt = Some(String::from("123")); let _ = opt.as_ref().map(String::as_str); } +#[clippy::msrv = "1.40"] fn msrv_1_40() { - #![clippy::msrv = "1.40"] - let opt = Some(String::from("123")); let _ = opt.as_ref().map(String::as_str); } diff --git a/tests/ui/option_as_ref_deref.stderr b/tests/ui/option_as_ref_deref.stderr index 7de8b3b6ba435..e471b56eea817 100644 --- a/tests/ui/option_as_ref_deref.stderr +++ b/tests/ui/option_as_ref_deref.stderr @@ -1,5 +1,5 @@ error: called `.as_ref().map(Deref::deref)` on an Option value. This can be done more directly by calling `opt.clone().as_deref()` instead - --> $DIR/option_as_ref_deref.rs:14:13 + --> $DIR/option_as_ref_deref.rs:13:13 | LL | let _ = opt.clone().as_ref().map(Deref::deref).map(str::len); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.clone().as_deref()` @@ -7,7 +7,7 @@ LL | let _ = opt.clone().as_ref().map(Deref::deref).map(str::len); = note: `-D clippy::option-as-ref-deref` implied by `-D warnings` error: called `.as_ref().map(Deref::deref)` on an Option value. This can be done more directly by calling `opt.clone().as_deref()` instead - --> $DIR/option_as_ref_deref.rs:17:13 + --> $DIR/option_as_ref_deref.rs:16:13 | LL | let _ = opt.clone() | _____________^ @@ -17,97 +17,97 @@ LL | | ) | |_________^ help: try using as_deref instead: `opt.clone().as_deref()` error: called `.as_mut().map(DerefMut::deref_mut)` on an Option value. This can be done more directly by calling `opt.as_deref_mut()` instead - --> $DIR/option_as_ref_deref.rs:23:13 + --> $DIR/option_as_ref_deref.rs:22:13 | LL | let _ = opt.as_mut().map(DerefMut::deref_mut); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `opt.as_deref_mut()` error: called `.as_ref().map(String::as_str)` on an Option value. This can be done more directly by calling `opt.as_deref()` instead - --> $DIR/option_as_ref_deref.rs:25:13 + --> $DIR/option_as_ref_deref.rs:24:13 | LL | let _ = opt.as_ref().map(String::as_str); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()` error: called `.as_ref().map(|x| x.as_str())` on an Option value. This can be done more directly by calling `opt.as_deref()` instead - --> $DIR/option_as_ref_deref.rs:26:13 + --> $DIR/option_as_ref_deref.rs:25:13 | LL | let _ = opt.as_ref().map(|x| x.as_str()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()` error: called `.as_mut().map(String::as_mut_str)` on an Option value. This can be done more directly by calling `opt.as_deref_mut()` instead - --> $DIR/option_as_ref_deref.rs:27:13 + --> $DIR/option_as_ref_deref.rs:26:13 | LL | let _ = opt.as_mut().map(String::as_mut_str); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `opt.as_deref_mut()` error: called `.as_mut().map(|x| x.as_mut_str())` on an Option value. This can be done more directly by calling `opt.as_deref_mut()` instead - --> $DIR/option_as_ref_deref.rs:28:13 + --> $DIR/option_as_ref_deref.rs:27:13 | LL | let _ = opt.as_mut().map(|x| x.as_mut_str()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `opt.as_deref_mut()` error: called `.as_ref().map(CString::as_c_str)` on an Option value. This can be done more directly by calling `Some(CString::new(vec![]).unwrap()).as_deref()` instead - --> $DIR/option_as_ref_deref.rs:29:13 + --> $DIR/option_as_ref_deref.rs:28:13 | LL | let _ = Some(CString::new(vec![]).unwrap()).as_ref().map(CString::as_c_str); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `Some(CString::new(vec![]).unwrap()).as_deref()` error: called `.as_ref().map(OsString::as_os_str)` on an Option value. This can be done more directly by calling `Some(OsString::new()).as_deref()` instead - --> $DIR/option_as_ref_deref.rs:30:13 + --> $DIR/option_as_ref_deref.rs:29:13 | LL | let _ = Some(OsString::new()).as_ref().map(OsString::as_os_str); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `Some(OsString::new()).as_deref()` error: called `.as_ref().map(PathBuf::as_path)` on an Option value. This can be done more directly by calling `Some(PathBuf::new()).as_deref()` instead - --> $DIR/option_as_ref_deref.rs:31:13 + --> $DIR/option_as_ref_deref.rs:30:13 | LL | let _ = Some(PathBuf::new()).as_ref().map(PathBuf::as_path); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `Some(PathBuf::new()).as_deref()` error: called `.as_ref().map(Vec::as_slice)` on an Option value. This can be done more directly by calling `Some(Vec::<()>::new()).as_deref()` instead - --> $DIR/option_as_ref_deref.rs:32:13 + --> $DIR/option_as_ref_deref.rs:31:13 | LL | let _ = Some(Vec::<()>::new()).as_ref().map(Vec::as_slice); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `Some(Vec::<()>::new()).as_deref()` error: called `.as_mut().map(Vec::as_mut_slice)` on an Option value. This can be done more directly by calling `Some(Vec::<()>::new()).as_deref_mut()` instead - --> $DIR/option_as_ref_deref.rs:33:13 + --> $DIR/option_as_ref_deref.rs:32:13 | LL | let _ = Some(Vec::<()>::new()).as_mut().map(Vec::as_mut_slice); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `Some(Vec::<()>::new()).as_deref_mut()` error: called `.as_ref().map(|x| x.deref())` on an Option value. This can be done more directly by calling `opt.as_deref()` instead - --> $DIR/option_as_ref_deref.rs:35:13 + --> $DIR/option_as_ref_deref.rs:34:13 | LL | let _ = opt.as_ref().map(|x| x.deref()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()` error: called `.as_mut().map(|x| x.deref_mut())` on an Option value. This can be done more directly by calling `opt.clone().as_deref_mut()` instead - --> $DIR/option_as_ref_deref.rs:36:13 + --> $DIR/option_as_ref_deref.rs:35:13 | LL | let _ = opt.clone().as_mut().map(|x| x.deref_mut()).map(|x| x.len()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `opt.clone().as_deref_mut()` error: called `.as_ref().map(|x| &**x)` on an Option value. This can be done more directly by calling `opt.as_deref()` instead - --> $DIR/option_as_ref_deref.rs:43:13 + --> $DIR/option_as_ref_deref.rs:42:13 | LL | let _ = opt.as_ref().map(|x| &**x); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()` error: called `.as_mut().map(|x| &mut **x)` on an Option value. This can be done more directly by calling `opt.as_deref_mut()` instead - --> $DIR/option_as_ref_deref.rs:44:13 + --> $DIR/option_as_ref_deref.rs:43:13 | LL | let _ = opt.as_mut().map(|x| &mut **x); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `opt.as_deref_mut()` error: called `.as_ref().map(std::ops::Deref::deref)` on an Option value. This can be done more directly by calling `opt.as_deref()` instead - --> $DIR/option_as_ref_deref.rs:47:13 + --> $DIR/option_as_ref_deref.rs:46:13 | LL | let _ = opt.as_ref().map(std::ops::Deref::deref); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()` error: called `.as_ref().map(String::as_str)` on an Option value. This can be done more directly by calling `opt.as_deref()` instead - --> $DIR/option_as_ref_deref.rs:61:13 + --> $DIR/option_as_ref_deref.rs:58:13 | LL | let _ = opt.as_ref().map(String::as_str); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()` diff --git a/tests/ui/ptr_as_ptr.fixed b/tests/ui/ptr_as_ptr.fixed index bea6be66a8e02..df36a9b842bf7 100644 --- a/tests/ui/ptr_as_ptr.fixed +++ b/tests/ui/ptr_as_ptr.fixed @@ -2,7 +2,6 @@ // aux-build:macro_rules.rs #![warn(clippy::ptr_as_ptr)] -#![feature(custom_inner_attributes)] extern crate macro_rules; @@ -45,8 +44,8 @@ fn main() { let _ = macro_rules::ptr_as_ptr_cast!(ptr); } +#[clippy::msrv = "1.37"] fn _msrv_1_37() { - #![clippy::msrv = "1.37"] let ptr: *const u32 = &42_u32; let mut_ptr: *mut u32 = &mut 42_u32; @@ -55,8 +54,8 @@ fn _msrv_1_37() { let _ = mut_ptr as *mut i32; } +#[clippy::msrv = "1.38"] fn _msrv_1_38() { - #![clippy::msrv = "1.38"] let ptr: *const u32 = &42_u32; let mut_ptr: *mut u32 = &mut 42_u32; diff --git a/tests/ui/ptr_as_ptr.rs b/tests/ui/ptr_as_ptr.rs index ca2616b0069a0..302c66462d9b9 100644 --- a/tests/ui/ptr_as_ptr.rs +++ b/tests/ui/ptr_as_ptr.rs @@ -2,7 +2,6 @@ // aux-build:macro_rules.rs #![warn(clippy::ptr_as_ptr)] -#![feature(custom_inner_attributes)] extern crate macro_rules; @@ -45,8 +44,8 @@ fn main() { let _ = macro_rules::ptr_as_ptr_cast!(ptr); } +#[clippy::msrv = "1.37"] fn _msrv_1_37() { - #![clippy::msrv = "1.37"] let ptr: *const u32 = &42_u32; let mut_ptr: *mut u32 = &mut 42_u32; @@ -55,8 +54,8 @@ fn _msrv_1_37() { let _ = mut_ptr as *mut i32; } +#[clippy::msrv = "1.38"] fn _msrv_1_38() { - #![clippy::msrv = "1.38"] let ptr: *const u32 = &42_u32; let mut_ptr: *mut u32 = &mut 42_u32; diff --git a/tests/ui/ptr_as_ptr.stderr b/tests/ui/ptr_as_ptr.stderr index c58c55cfd83a1..a68e1cab6d357 100644 --- a/tests/ui/ptr_as_ptr.stderr +++ b/tests/ui/ptr_as_ptr.stderr @@ -1,5 +1,5 @@ error: `as` casting between raw pointers without changing its mutability - --> $DIR/ptr_as_ptr.rs:19:13 + --> $DIR/ptr_as_ptr.rs:18:13 | LL | let _ = ptr as *const i32; | ^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `ptr.cast::()` @@ -7,31 +7,31 @@ LL | let _ = ptr as *const i32; = note: `-D clippy::ptr-as-ptr` implied by `-D warnings` error: `as` casting between raw pointers without changing its mutability - --> $DIR/ptr_as_ptr.rs:20:13 + --> $DIR/ptr_as_ptr.rs:19:13 | LL | let _ = mut_ptr as *mut i32; | ^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `mut_ptr.cast::()` error: `as` casting between raw pointers without changing its mutability - --> $DIR/ptr_as_ptr.rs:25:17 + --> $DIR/ptr_as_ptr.rs:24:17 | LL | let _ = *ptr_ptr as *const i32; | ^^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `(*ptr_ptr).cast::()` error: `as` casting between raw pointers without changing its mutability - --> $DIR/ptr_as_ptr.rs:38:25 + --> $DIR/ptr_as_ptr.rs:37:25 | LL | let _: *const i32 = ptr as *const _; | ^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `ptr.cast()` error: `as` casting between raw pointers without changing its mutability - --> $DIR/ptr_as_ptr.rs:39:23 + --> $DIR/ptr_as_ptr.rs:38:23 | LL | let _: *mut i32 = mut_ptr as _; | ^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `mut_ptr.cast()` error: `as` casting between raw pointers without changing its mutability - --> $DIR/ptr_as_ptr.rs:11:9 + --> $DIR/ptr_as_ptr.rs:10:9 | LL | $ptr as *const i32 | ^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `$ptr.cast::()` @@ -42,13 +42,13 @@ LL | let _ = cast_it!(ptr); = note: this error originates in the macro `cast_it` (in Nightly builds, run with -Z macro-backtrace for more info) error: `as` casting between raw pointers without changing its mutability - --> $DIR/ptr_as_ptr.rs:63:13 + --> $DIR/ptr_as_ptr.rs:62:13 | LL | let _ = ptr as *const i32; | ^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `ptr.cast::()` error: `as` casting between raw pointers without changing its mutability - --> $DIR/ptr_as_ptr.rs:64:13 + --> $DIR/ptr_as_ptr.rs:63:13 | LL | let _ = mut_ptr as *mut i32; | ^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `mut_ptr.cast::()` diff --git a/tests/ui/range_contains.fixed b/tests/ui/range_contains.fixed index 824f00cb99e85..4923731fe45e8 100644 --- a/tests/ui/range_contains.fixed +++ b/tests/ui/range_contains.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::manual_range_contains)] #![allow(unused)] #![allow(clippy::no_effect)] @@ -65,16 +64,14 @@ pub const fn in_range(a: i32) -> bool { 3 <= a && a <= 20 } +#[clippy::msrv = "1.34"] fn msrv_1_34() { - #![clippy::msrv = "1.34"] - let x = 5; x >= 8 && x < 34; } +#[clippy::msrv = "1.35"] fn msrv_1_35() { - #![clippy::msrv = "1.35"] - let x = 5; (8..35).contains(&x); } diff --git a/tests/ui/range_contains.rs b/tests/ui/range_contains.rs index df925eeadfe5e..d623ccb5da636 100644 --- a/tests/ui/range_contains.rs +++ b/tests/ui/range_contains.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::manual_range_contains)] #![allow(unused)] #![allow(clippy::no_effect)] @@ -65,16 +64,14 @@ pub const fn in_range(a: i32) -> bool { 3 <= a && a <= 20 } +#[clippy::msrv = "1.34"] fn msrv_1_34() { - #![clippy::msrv = "1.34"] - let x = 5; x >= 8 && x < 34; } +#[clippy::msrv = "1.35"] fn msrv_1_35() { - #![clippy::msrv = "1.35"] - let x = 5; x >= 8 && x < 35; } diff --git a/tests/ui/range_contains.stderr b/tests/ui/range_contains.stderr index 9689e665b05c5..ea34023a46645 100644 --- a/tests/ui/range_contains.stderr +++ b/tests/ui/range_contains.stderr @@ -1,5 +1,5 @@ error: manual `Range::contains` implementation - --> $DIR/range_contains.rs:14:5 + --> $DIR/range_contains.rs:13:5 | LL | x >= 8 && x < 12; | ^^^^^^^^^^^^^^^^ help: use: `(8..12).contains(&x)` @@ -7,121 +7,121 @@ LL | x >= 8 && x < 12; = note: `-D clippy::manual-range-contains` implied by `-D warnings` error: manual `Range::contains` implementation - --> $DIR/range_contains.rs:15:5 + --> $DIR/range_contains.rs:14:5 | LL | x < 42 && x >= 21; | ^^^^^^^^^^^^^^^^^ help: use: `(21..42).contains(&x)` error: manual `Range::contains` implementation - --> $DIR/range_contains.rs:16:5 + --> $DIR/range_contains.rs:15:5 | LL | 100 > x && 1 <= x; | ^^^^^^^^^^^^^^^^^ help: use: `(1..100).contains(&x)` error: manual `RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:19:5 + --> $DIR/range_contains.rs:18:5 | LL | x >= 9 && x <= 99; | ^^^^^^^^^^^^^^^^^ help: use: `(9..=99).contains(&x)` error: manual `RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:20:5 + --> $DIR/range_contains.rs:19:5 | LL | x <= 33 && x >= 1; | ^^^^^^^^^^^^^^^^^ help: use: `(1..=33).contains(&x)` error: manual `RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:21:5 + --> $DIR/range_contains.rs:20:5 | LL | 999 >= x && 1 <= x; | ^^^^^^^^^^^^^^^^^^ help: use: `(1..=999).contains(&x)` error: manual `!Range::contains` implementation - --> $DIR/range_contains.rs:24:5 + --> $DIR/range_contains.rs:23:5 | LL | x < 8 || x >= 12; | ^^^^^^^^^^^^^^^^ help: use: `!(8..12).contains(&x)` error: manual `!Range::contains` implementation - --> $DIR/range_contains.rs:25:5 + --> $DIR/range_contains.rs:24:5 | LL | x >= 42 || x < 21; | ^^^^^^^^^^^^^^^^^ help: use: `!(21..42).contains(&x)` error: manual `!Range::contains` implementation - --> $DIR/range_contains.rs:26:5 + --> $DIR/range_contains.rs:25:5 | LL | 100 <= x || 1 > x; | ^^^^^^^^^^^^^^^^^ help: use: `!(1..100).contains(&x)` error: manual `!RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:29:5 + --> $DIR/range_contains.rs:28:5 | LL | x < 9 || x > 99; | ^^^^^^^^^^^^^^^ help: use: `!(9..=99).contains(&x)` error: manual `!RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:30:5 + --> $DIR/range_contains.rs:29:5 | LL | x > 33 || x < 1; | ^^^^^^^^^^^^^^^ help: use: `!(1..=33).contains(&x)` error: manual `!RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:31:5 + --> $DIR/range_contains.rs:30:5 | LL | 999 < x || 1 > x; | ^^^^^^^^^^^^^^^^ help: use: `!(1..=999).contains(&x)` error: manual `Range::contains` implementation - --> $DIR/range_contains.rs:46:5 + --> $DIR/range_contains.rs:45:5 | LL | y >= 0. && y < 1.; | ^^^^^^^^^^^^^^^^^ help: use: `(0. ..1.).contains(&y)` error: manual `!RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:47:5 + --> $DIR/range_contains.rs:46:5 | LL | y < 0. || y > 1.; | ^^^^^^^^^^^^^^^^ help: use: `!(0. ..=1.).contains(&y)` error: manual `RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:50:5 + --> $DIR/range_contains.rs:49:5 | LL | x >= -10 && x <= 10; | ^^^^^^^^^^^^^^^^^^^ help: use: `(-10..=10).contains(&x)` error: manual `RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:52:5 + --> $DIR/range_contains.rs:51:5 | LL | y >= -3. && y <= 3.; | ^^^^^^^^^^^^^^^^^^^ help: use: `(-3. ..=3.).contains(&y)` error: manual `RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:57:30 + --> $DIR/range_contains.rs:56:30 | LL | (x >= 0) && (x <= 10) && (z >= 0) && (z <= 10); | ^^^^^^^^^^^^^^^^^^^^^ help: use: `(0..=10).contains(&z)` error: manual `RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:57:5 + --> $DIR/range_contains.rs:56:5 | LL | (x >= 0) && (x <= 10) && (z >= 0) && (z <= 10); | ^^^^^^^^^^^^^^^^^^^^^ help: use: `(0..=10).contains(&x)` error: manual `!Range::contains` implementation - --> $DIR/range_contains.rs:58:29 + --> $DIR/range_contains.rs:57:29 | LL | (x < 0) || (x >= 10) || (z < 0) || (z >= 10); | ^^^^^^^^^^^^^^^^^^^^ help: use: `!(0..10).contains(&z)` error: manual `!Range::contains` implementation - --> $DIR/range_contains.rs:58:5 + --> $DIR/range_contains.rs:57:5 | LL | (x < 0) || (x >= 10) || (z < 0) || (z >= 10); | ^^^^^^^^^^^^^^^^^^^^ help: use: `!(0..10).contains(&x)` error: manual `Range::contains` implementation - --> $DIR/range_contains.rs:79:5 + --> $DIR/range_contains.rs:76:5 | LL | x >= 8 && x < 35; | ^^^^^^^^^^^^^^^^ help: use: `(8..35).contains(&x)` diff --git a/tests/ui/redundant_field_names.fixed b/tests/ui/redundant_field_names.fixed index 34ab552cb1d8d..ec7f8ae923a79 100644 --- a/tests/ui/redundant_field_names.fixed +++ b/tests/ui/redundant_field_names.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::redundant_field_names)] #![allow(clippy::no_effect, dead_code, unused_variables)] @@ -72,16 +71,14 @@ fn issue_3476() { S { foo: foo:: }; } +#[clippy::msrv = "1.16"] fn msrv_1_16() { - #![clippy::msrv = "1.16"] - let start = 0; let _ = RangeFrom { start: start }; } +#[clippy::msrv = "1.17"] fn msrv_1_17() { - #![clippy::msrv = "1.17"] - let start = 0; let _ = RangeFrom { start }; } diff --git a/tests/ui/redundant_field_names.rs b/tests/ui/redundant_field_names.rs index a051b1f96f0fd..73122016cf69e 100644 --- a/tests/ui/redundant_field_names.rs +++ b/tests/ui/redundant_field_names.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::redundant_field_names)] #![allow(clippy::no_effect, dead_code, unused_variables)] @@ -72,16 +71,14 @@ fn issue_3476() { S { foo: foo:: }; } +#[clippy::msrv = "1.16"] fn msrv_1_16() { - #![clippy::msrv = "1.16"] - let start = 0; let _ = RangeFrom { start: start }; } +#[clippy::msrv = "1.17"] fn msrv_1_17() { - #![clippy::msrv = "1.17"] - let start = 0; let _ = RangeFrom { start: start }; } diff --git a/tests/ui/redundant_field_names.stderr b/tests/ui/redundant_field_names.stderr index 8b82e062b93a6..00a72c50cf7d0 100644 --- a/tests/ui/redundant_field_names.stderr +++ b/tests/ui/redundant_field_names.stderr @@ -1,5 +1,5 @@ error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:36:9 + --> $DIR/redundant_field_names.rs:35:9 | LL | gender: gender, | ^^^^^^^^^^^^^^ help: replace it with: `gender` @@ -7,43 +7,43 @@ LL | gender: gender, = note: `-D clippy::redundant-field-names` implied by `-D warnings` error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:37:9 + --> $DIR/redundant_field_names.rs:36:9 | LL | age: age, | ^^^^^^^^ help: replace it with: `age` error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:58:25 + --> $DIR/redundant_field_names.rs:57:25 | LL | let _ = RangeFrom { start: start }; | ^^^^^^^^^^^^ help: replace it with: `start` error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:59:23 + --> $DIR/redundant_field_names.rs:58:23 | LL | let _ = RangeTo { end: end }; | ^^^^^^^^ help: replace it with: `end` error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:60:21 + --> $DIR/redundant_field_names.rs:59:21 | LL | let _ = Range { start: start, end: end }; | ^^^^^^^^^^^^ help: replace it with: `start` error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:60:35 + --> $DIR/redundant_field_names.rs:59:35 | LL | let _ = Range { start: start, end: end }; | ^^^^^^^^ help: replace it with: `end` error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:62:32 + --> $DIR/redundant_field_names.rs:61:32 | LL | let _ = RangeToInclusive { end: end }; | ^^^^^^^^ help: replace it with: `end` error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:86:25 + --> $DIR/redundant_field_names.rs:83:25 | LL | let _ = RangeFrom { start: start }; | ^^^^^^^^^^^^ help: replace it with: `start` diff --git a/tests/ui/redundant_static_lifetimes.fixed b/tests/ui/redundant_static_lifetimes.fixed index 42110dbe81e84..4c5846fe837ea 100644 --- a/tests/ui/redundant_static_lifetimes.fixed +++ b/tests/ui/redundant_static_lifetimes.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![allow(unused)] #[derive(Debug)] @@ -56,14 +55,12 @@ impl Bar for Foo { const TRAIT_VAR: &'static str = "foo"; } +#[clippy::msrv = "1.16"] fn msrv_1_16() { - #![clippy::msrv = "1.16"] - static V: &'static u8 = &16; } +#[clippy::msrv = "1.17"] fn msrv_1_17() { - #![clippy::msrv = "1.17"] - static V: &u8 = &17; } diff --git a/tests/ui/redundant_static_lifetimes.rs b/tests/ui/redundant_static_lifetimes.rs index bc5200bc8625b..64a66be1a83c7 100644 --- a/tests/ui/redundant_static_lifetimes.rs +++ b/tests/ui/redundant_static_lifetimes.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![allow(unused)] #[derive(Debug)] @@ -56,14 +55,12 @@ impl Bar for Foo { const TRAIT_VAR: &'static str = "foo"; } +#[clippy::msrv = "1.16"] fn msrv_1_16() { - #![clippy::msrv = "1.16"] - static V: &'static u8 = &16; } +#[clippy::msrv = "1.17"] fn msrv_1_17() { - #![clippy::msrv = "1.17"] - static V: &'static u8 = &17; } diff --git a/tests/ui/redundant_static_lifetimes.stderr b/tests/ui/redundant_static_lifetimes.stderr index 735113460d28c..0938ebf783ff1 100644 --- a/tests/ui/redundant_static_lifetimes.stderr +++ b/tests/ui/redundant_static_lifetimes.stderr @@ -1,5 +1,5 @@ error: constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:9:17 + --> $DIR/redundant_static_lifetimes.rs:8:17 | LL | const VAR_ONE: &'static str = "Test constant #1"; // ERROR Consider removing 'static. | -^^^^^^^---- help: consider removing `'static`: `&str` @@ -7,97 +7,97 @@ LL | const VAR_ONE: &'static str = "Test constant #1"; // ERROR Consider removin = note: `-D clippy::redundant-static-lifetimes` implied by `-D warnings` error: constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:13:21 + --> $DIR/redundant_static_lifetimes.rs:12:21 | LL | const VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:15:32 + --> $DIR/redundant_static_lifetimes.rs:14:32 | LL | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:15:47 + --> $DIR/redundant_static_lifetimes.rs:14:47 | LL | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:17:17 + --> $DIR/redundant_static_lifetimes.rs:16:17 | LL | const VAR_SIX: &'static u8 = &5; | -^^^^^^^--- help: consider removing `'static`: `&u8` error: constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:19:20 + --> $DIR/redundant_static_lifetimes.rs:18:20 | LL | const VAR_HEIGHT: &'static Foo = &Foo {}; | -^^^^^^^---- help: consider removing `'static`: `&Foo` error: constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:21:19 + --> $DIR/redundant_static_lifetimes.rs:20:19 | LL | const VAR_SLICE: &'static [u8] = b"Test constant #1"; // ERROR Consider removing 'static. | -^^^^^^^----- help: consider removing `'static`: `&[u8]` error: constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:23:19 + --> $DIR/redundant_static_lifetimes.rs:22:19 | LL | const VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static. | -^^^^^^^--------- help: consider removing `'static`: `&(u8, u8)` error: constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:25:19 + --> $DIR/redundant_static_lifetimes.rs:24:19 | LL | const VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static. | -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]` error: statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:27:25 + --> $DIR/redundant_static_lifetimes.rs:26:25 | LL | static STATIC_VAR_ONE: &'static str = "Test static #1"; // ERROR Consider removing 'static. | -^^^^^^^---- help: consider removing `'static`: `&str` error: statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:31:29 + --> $DIR/redundant_static_lifetimes.rs:30:29 | LL | static STATIC_VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:33:25 + --> $DIR/redundant_static_lifetimes.rs:32:25 | LL | static STATIC_VAR_SIX: &'static u8 = &5; | -^^^^^^^--- help: consider removing `'static`: `&u8` error: statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:35:28 + --> $DIR/redundant_static_lifetimes.rs:34:28 | LL | static STATIC_VAR_HEIGHT: &'static Foo = &Foo {}; | -^^^^^^^---- help: consider removing `'static`: `&Foo` error: statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:37:27 + --> $DIR/redundant_static_lifetimes.rs:36:27 | LL | static STATIC_VAR_SLICE: &'static [u8] = b"Test static #3"; // ERROR Consider removing 'static. | -^^^^^^^----- help: consider removing `'static`: `&[u8]` error: statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:39:27 + --> $DIR/redundant_static_lifetimes.rs:38:27 | LL | static STATIC_VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static. | -^^^^^^^--------- help: consider removing `'static`: `&(u8, u8)` error: statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:41:27 + --> $DIR/redundant_static_lifetimes.rs:40:27 | LL | static STATIC_VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static. | -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]` error: statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:68:16 + --> $DIR/redundant_static_lifetimes.rs:65:16 | LL | static V: &'static u8 = &17; | -^^^^^^^--- help: consider removing `'static`: `&u8` diff --git a/tests/ui/seek_from_current.fixed b/tests/ui/seek_from_current.fixed index 4b5303324bc6f..1309c91b81c98 100644 --- a/tests/ui/seek_from_current.fixed +++ b/tests/ui/seek_from_current.fixed @@ -1,12 +1,11 @@ // run-rustfix #![warn(clippy::seek_from_current)] -#![feature(custom_inner_attributes)] use std::fs::File; use std::io::{self, Seek, SeekFrom, Write}; +#[clippy::msrv = "1.50"] fn _msrv_1_50() -> io::Result<()> { - #![clippy::msrv = "1.50"] let mut f = File::create("foo.txt")?; f.write_all(b"Hi!")?; f.seek(SeekFrom::Current(0))?; @@ -14,8 +13,8 @@ fn _msrv_1_50() -> io::Result<()> { Ok(()) } +#[clippy::msrv = "1.51"] fn _msrv_1_51() -> io::Result<()> { - #![clippy::msrv = "1.51"] let mut f = File::create("foo.txt")?; f.write_all(b"Hi!")?; f.stream_position()?; diff --git a/tests/ui/seek_from_current.rs b/tests/ui/seek_from_current.rs index f93639261a180..5d9b1424cf686 100644 --- a/tests/ui/seek_from_current.rs +++ b/tests/ui/seek_from_current.rs @@ -1,12 +1,11 @@ // run-rustfix #![warn(clippy::seek_from_current)] -#![feature(custom_inner_attributes)] use std::fs::File; use std::io::{self, Seek, SeekFrom, Write}; +#[clippy::msrv = "1.50"] fn _msrv_1_50() -> io::Result<()> { - #![clippy::msrv = "1.50"] let mut f = File::create("foo.txt")?; f.write_all(b"Hi!")?; f.seek(SeekFrom::Current(0))?; @@ -14,8 +13,8 @@ fn _msrv_1_50() -> io::Result<()> { Ok(()) } +#[clippy::msrv = "1.51"] fn _msrv_1_51() -> io::Result<()> { - #![clippy::msrv = "1.51"] let mut f = File::create("foo.txt")?; f.write_all(b"Hi!")?; f.seek(SeekFrom::Current(0))?; diff --git a/tests/ui/seek_from_current.stderr b/tests/ui/seek_from_current.stderr index db1125b53cdf5..c079f36119292 100644 --- a/tests/ui/seek_from_current.stderr +++ b/tests/ui/seek_from_current.stderr @@ -1,5 +1,5 @@ error: using `SeekFrom::Current` to start from current position - --> $DIR/seek_from_current.rs:21:5 + --> $DIR/seek_from_current.rs:20:5 | LL | f.seek(SeekFrom::Current(0))?; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `f.stream_position()` diff --git a/tests/ui/seek_to_start_instead_of_rewind.fixed b/tests/ui/seek_to_start_instead_of_rewind.fixed index 464b6cdef6393..9d0d1124c460e 100644 --- a/tests/ui/seek_to_start_instead_of_rewind.fixed +++ b/tests/ui/seek_to_start_instead_of_rewind.fixed @@ -1,6 +1,5 @@ // run-rustfix #![allow(unused)] -#![feature(custom_inner_attributes)] #![warn(clippy::seek_to_start_instead_of_rewind)] use std::fs::OpenOptions; @@ -94,9 +93,8 @@ fn main() { assert_eq!(&buf, hello); } +#[clippy::msrv = "1.54"] fn msrv_1_54() { - #![clippy::msrv = "1.54"] - let mut f = OpenOptions::new() .write(true) .read(true) @@ -115,9 +113,8 @@ fn msrv_1_54() { assert_eq!(&buf, hello); } +#[clippy::msrv = "1.55"] fn msrv_1_55() { - #![clippy::msrv = "1.55"] - let mut f = OpenOptions::new() .write(true) .read(true) diff --git a/tests/ui/seek_to_start_instead_of_rewind.rs b/tests/ui/seek_to_start_instead_of_rewind.rs index 68e09bd7c1f0f..c5bc57cc3a74c 100644 --- a/tests/ui/seek_to_start_instead_of_rewind.rs +++ b/tests/ui/seek_to_start_instead_of_rewind.rs @@ -1,6 +1,5 @@ // run-rustfix #![allow(unused)] -#![feature(custom_inner_attributes)] #![warn(clippy::seek_to_start_instead_of_rewind)] use std::fs::OpenOptions; @@ -94,9 +93,8 @@ fn main() { assert_eq!(&buf, hello); } +#[clippy::msrv = "1.54"] fn msrv_1_54() { - #![clippy::msrv = "1.54"] - let mut f = OpenOptions::new() .write(true) .read(true) @@ -115,9 +113,8 @@ fn msrv_1_54() { assert_eq!(&buf, hello); } +#[clippy::msrv = "1.55"] fn msrv_1_55() { - #![clippy::msrv = "1.55"] - let mut f = OpenOptions::new() .write(true) .read(true) diff --git a/tests/ui/seek_to_start_instead_of_rewind.stderr b/tests/ui/seek_to_start_instead_of_rewind.stderr index de0eec5d909cd..6cce025359fe2 100644 --- a/tests/ui/seek_to_start_instead_of_rewind.stderr +++ b/tests/ui/seek_to_start_instead_of_rewind.stderr @@ -1,5 +1,5 @@ error: used `seek` to go to the start of the stream - --> $DIR/seek_to_start_instead_of_rewind.rs:54:7 + --> $DIR/seek_to_start_instead_of_rewind.rs:53:7 | LL | t.seek(SeekFrom::Start(0)); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `rewind()` @@ -7,13 +7,13 @@ LL | t.seek(SeekFrom::Start(0)); = note: `-D clippy::seek-to-start-instead-of-rewind` implied by `-D warnings` error: used `seek` to go to the start of the stream - --> $DIR/seek_to_start_instead_of_rewind.rs:59:7 + --> $DIR/seek_to_start_instead_of_rewind.rs:58:7 | LL | t.seek(SeekFrom::Start(0)); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `rewind()` error: used `seek` to go to the start of the stream - --> $DIR/seek_to_start_instead_of_rewind.rs:131:7 + --> $DIR/seek_to_start_instead_of_rewind.rs:128:7 | LL | f.seek(SeekFrom::Start(0)); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `rewind()` diff --git a/tests/ui/transmute_ptr_to_ref.fixed b/tests/ui/transmute_ptr_to_ref.fixed index e5fe9133f975e..074dae5fb2868 100644 --- a/tests/ui/transmute_ptr_to_ref.fixed +++ b/tests/ui/transmute_ptr_to_ref.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::transmute_ptr_to_ref)] #![allow(clippy::match_single_binding)] @@ -51,8 +50,8 @@ unsafe fn _issue8924<'a, 'b, 'c>(x: *const &'a u32, y: *const &'b u32) -> &'c &' } } +#[clippy::msrv = "1.38"] unsafe fn _meets_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 { - #![clippy::msrv = "1.38"] let a = 0u32; let a = &a as *const u32; let _: &u32 = &*a; @@ -63,8 +62,8 @@ unsafe fn _meets_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 { } } +#[clippy::msrv = "1.37"] unsafe fn _under_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 { - #![clippy::msrv = "1.37"] let a = 0u32; let a = &a as *const u32; let _: &u32 = &*a; diff --git a/tests/ui/transmute_ptr_to_ref.rs b/tests/ui/transmute_ptr_to_ref.rs index fe49cdc324fdd..2edc122cf4712 100644 --- a/tests/ui/transmute_ptr_to_ref.rs +++ b/tests/ui/transmute_ptr_to_ref.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::transmute_ptr_to_ref)] #![allow(clippy::match_single_binding)] @@ -51,8 +50,8 @@ unsafe fn _issue8924<'a, 'b, 'c>(x: *const &'a u32, y: *const &'b u32) -> &'c &' } } +#[clippy::msrv = "1.38"] unsafe fn _meets_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 { - #![clippy::msrv = "1.38"] let a = 0u32; let a = &a as *const u32; let _: &u32 = std::mem::transmute(a); @@ -63,8 +62,8 @@ unsafe fn _meets_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 { } } +#[clippy::msrv = "1.37"] unsafe fn _under_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 { - #![clippy::msrv = "1.37"] let a = 0u32; let a = &a as *const u32; let _: &u32 = std::mem::transmute(a); diff --git a/tests/ui/transmute_ptr_to_ref.stderr b/tests/ui/transmute_ptr_to_ref.stderr index 10117ee9182ab..b3e6c09d2d7a1 100644 --- a/tests/ui/transmute_ptr_to_ref.stderr +++ b/tests/ui/transmute_ptr_to_ref.stderr @@ -1,5 +1,5 @@ error: transmute from a pointer type (`*const T`) to a reference type (`&T`) - --> $DIR/transmute_ptr_to_ref.rs:8:17 + --> $DIR/transmute_ptr_to_ref.rs:7:17 | LL | let _: &T = std::mem::transmute(p); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*p` @@ -7,127 +7,127 @@ LL | let _: &T = std::mem::transmute(p); = note: `-D clippy::transmute-ptr-to-ref` implied by `-D warnings` error: transmute from a pointer type (`*mut T`) to a reference type (`&mut T`) - --> $DIR/transmute_ptr_to_ref.rs:11:21 + --> $DIR/transmute_ptr_to_ref.rs:10:21 | LL | let _: &mut T = std::mem::transmute(m); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut *m` error: transmute from a pointer type (`*mut T`) to a reference type (`&T`) - --> $DIR/transmute_ptr_to_ref.rs:14:17 + --> $DIR/transmute_ptr_to_ref.rs:13:17 | LL | let _: &T = std::mem::transmute(m); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*m` error: transmute from a pointer type (`*mut T`) to a reference type (`&mut T`) - --> $DIR/transmute_ptr_to_ref.rs:17:21 + --> $DIR/transmute_ptr_to_ref.rs:16:21 | LL | let _: &mut T = std::mem::transmute(p as *mut T); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut *(p as *mut T)` error: transmute from a pointer type (`*const U`) to a reference type (`&T`) - --> $DIR/transmute_ptr_to_ref.rs:20:17 + --> $DIR/transmute_ptr_to_ref.rs:19:17 | LL | let _: &T = std::mem::transmute(o); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(o as *const T)` error: transmute from a pointer type (`*mut U`) to a reference type (`&mut T`) - --> $DIR/transmute_ptr_to_ref.rs:23:21 + --> $DIR/transmute_ptr_to_ref.rs:22:21 | LL | let _: &mut T = std::mem::transmute(om); | ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut *(om as *mut T)` error: transmute from a pointer type (`*mut U`) to a reference type (`&T`) - --> $DIR/transmute_ptr_to_ref.rs:26:17 + --> $DIR/transmute_ptr_to_ref.rs:25:17 | LL | let _: &T = std::mem::transmute(om); | ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(om as *const T)` error: transmute from a pointer type (`*const i32`) to a reference type (`&_issue1231::Foo<'_, u8>`) - --> $DIR/transmute_ptr_to_ref.rs:36:32 + --> $DIR/transmute_ptr_to_ref.rs:35:32 | LL | let _: &Foo = unsafe { std::mem::transmute::<_, &Foo<_>>(raw) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*raw.cast::>()` error: transmute from a pointer type (`*const i32`) to a reference type (`&_issue1231::Foo<'_, &u8>`) - --> $DIR/transmute_ptr_to_ref.rs:38:33 + --> $DIR/transmute_ptr_to_ref.rs:37:33 | LL | let _: &Foo<&u8> = unsafe { std::mem::transmute::<_, &Foo<&_>>(raw) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*raw.cast::>()` error: transmute from a pointer type (`*const i32`) to a reference type (`&u8`) - --> $DIR/transmute_ptr_to_ref.rs:42:14 + --> $DIR/transmute_ptr_to_ref.rs:41:14 | LL | unsafe { std::mem::transmute::<_, Bar>(raw) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(raw as *const u8)` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:47:14 + --> $DIR/transmute_ptr_to_ref.rs:46:14 | LL | 0 => std::mem::transmute(x), | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*x.cast::<&u32>()` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:48:14 + --> $DIR/transmute_ptr_to_ref.rs:47:14 | LL | 1 => std::mem::transmute(y), | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*y.cast::<&u32>()` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:49:14 + --> $DIR/transmute_ptr_to_ref.rs:48:14 | LL | 2 => std::mem::transmute::<_, &&'b u32>(x), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*x.cast::<&'b u32>()` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:50:14 + --> $DIR/transmute_ptr_to_ref.rs:49:14 | LL | _ => std::mem::transmute::<_, &&'b u32>(y), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*y.cast::<&'b u32>()` error: transmute from a pointer type (`*const u32`) to a reference type (`&u32`) - --> $DIR/transmute_ptr_to_ref.rs:58:19 + --> $DIR/transmute_ptr_to_ref.rs:57:19 | LL | let _: &u32 = std::mem::transmute(a); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*a` error: transmute from a pointer type (`*const u32`) to a reference type (`&u32`) - --> $DIR/transmute_ptr_to_ref.rs:59:19 + --> $DIR/transmute_ptr_to_ref.rs:58:19 | LL | let _: &u32 = std::mem::transmute::<_, &u32>(a); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*a.cast::()` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:61:14 + --> $DIR/transmute_ptr_to_ref.rs:60:14 | LL | 0 => std::mem::transmute(x), | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*x.cast::<&u32>()` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:62:14 + --> $DIR/transmute_ptr_to_ref.rs:61:14 | LL | _ => std::mem::transmute::<_, &&'b u32>(x), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*x.cast::<&'b u32>()` error: transmute from a pointer type (`*const u32`) to a reference type (`&u32`) - --> $DIR/transmute_ptr_to_ref.rs:70:19 + --> $DIR/transmute_ptr_to_ref.rs:69:19 | LL | let _: &u32 = std::mem::transmute(a); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*a` error: transmute from a pointer type (`*const u32`) to a reference type (`&u32`) - --> $DIR/transmute_ptr_to_ref.rs:71:19 + --> $DIR/transmute_ptr_to_ref.rs:70:19 | LL | let _: &u32 = std::mem::transmute::<_, &u32>(a); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(a as *const u32)` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:73:14 + --> $DIR/transmute_ptr_to_ref.rs:72:14 | LL | 0 => std::mem::transmute(x), | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(x as *const () as *const &u32)` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:74:14 + --> $DIR/transmute_ptr_to_ref.rs:73:14 | LL | _ => std::mem::transmute::<_, &&'b u32>(x), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(x as *const () as *const &'b u32)` diff --git a/tests/ui/uninlined_format_args.fixed b/tests/ui/uninlined_format_args.fixed index ca56c95c23f40..46ec7771114f1 100644 --- a/tests/ui/uninlined_format_args.fixed +++ b/tests/ui/uninlined_format_args.fixed @@ -1,6 +1,5 @@ // aux-build:proc_macro_with_span.rs // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::uninlined_format_args)] #![allow(named_arguments_used_positionally, unused_imports, unused_macros, unused_variables)] #![allow(clippy::eq_op, clippy::format_in_format_args, clippy::print_literal)] @@ -164,14 +163,14 @@ fn main() { tester(42); } +#[clippy::msrv = "1.57"] fn _under_msrv() { - #![clippy::msrv = "1.57"] let local_i32 = 1; println!("don't expand='{}'", local_i32); } +#[clippy::msrv = "1.58"] fn _meets_msrv() { - #![clippy::msrv = "1.58"] let local_i32 = 1; println!("expand='{local_i32}'"); } diff --git a/tests/ui/uninlined_format_args.rs b/tests/ui/uninlined_format_args.rs index 8e495ebd083a5..35b3677a8968f 100644 --- a/tests/ui/uninlined_format_args.rs +++ b/tests/ui/uninlined_format_args.rs @@ -1,6 +1,5 @@ // aux-build:proc_macro_with_span.rs // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::uninlined_format_args)] #![allow(named_arguments_used_positionally, unused_imports, unused_macros, unused_variables)] #![allow(clippy::eq_op, clippy::format_in_format_args, clippy::print_literal)] @@ -169,14 +168,14 @@ fn main() { tester(42); } +#[clippy::msrv = "1.57"] fn _under_msrv() { - #![clippy::msrv = "1.57"] let local_i32 = 1; println!("don't expand='{}'", local_i32); } +#[clippy::msrv = "1.58"] fn _meets_msrv() { - #![clippy::msrv = "1.58"] let local_i32 = 1; println!("expand='{}'", local_i32); } diff --git a/tests/ui/uninlined_format_args.stderr b/tests/ui/uninlined_format_args.stderr index 1182d57ce9b7e..05ed5b6616c09 100644 --- a/tests/ui/uninlined_format_args.stderr +++ b/tests/ui/uninlined_format_args.stderr @@ -1,5 +1,5 @@ error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:41:5 + --> $DIR/uninlined_format_args.rs:40:5 | LL | println!("val='{}'", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -12,7 +12,7 @@ LL + println!("val='{local_i32}'"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:42:5 + --> $DIR/uninlined_format_args.rs:41:5 | LL | println!("val='{ }'", local_i32); // 3 spaces | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -24,7 +24,7 @@ LL + println!("val='{local_i32}'"); // 3 spaces | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:43:5 + --> $DIR/uninlined_format_args.rs:42:5 | LL | println!("val='{ }'", local_i32); // tab | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -36,7 +36,7 @@ LL + println!("val='{local_i32}'"); // tab | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:44:5 + --> $DIR/uninlined_format_args.rs:43:5 | LL | println!("val='{ }'", local_i32); // space+tab | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -48,7 +48,7 @@ LL + println!("val='{local_i32}'"); // space+tab | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:45:5 + --> $DIR/uninlined_format_args.rs:44:5 | LL | println!("val='{ }'", local_i32); // tab+space | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -60,7 +60,7 @@ LL + println!("val='{local_i32}'"); // tab+space | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:46:5 + --> $DIR/uninlined_format_args.rs:45:5 | LL | / println!( LL | | "val='{ @@ -70,7 +70,7 @@ LL | | ); | |_____^ error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:51:5 + --> $DIR/uninlined_format_args.rs:50:5 | LL | println!("{}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -82,7 +82,7 @@ LL + println!("{local_i32}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:52:5 + --> $DIR/uninlined_format_args.rs:51:5 | LL | println!("{}", fn_arg); | ^^^^^^^^^^^^^^^^^^^^^^ @@ -94,7 +94,7 @@ LL + println!("{fn_arg}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:53:5 + --> $DIR/uninlined_format_args.rs:52:5 | LL | println!("{:?}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -106,7 +106,7 @@ LL + println!("{local_i32:?}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:54:5 + --> $DIR/uninlined_format_args.rs:53:5 | LL | println!("{:#?}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -118,7 +118,7 @@ LL + println!("{local_i32:#?}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:55:5 + --> $DIR/uninlined_format_args.rs:54:5 | LL | println!("{:4}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -130,7 +130,7 @@ LL + println!("{local_i32:4}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:56:5 + --> $DIR/uninlined_format_args.rs:55:5 | LL | println!("{:04}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -142,7 +142,7 @@ LL + println!("{local_i32:04}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:57:5 + --> $DIR/uninlined_format_args.rs:56:5 | LL | println!("{:<3}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -154,7 +154,7 @@ LL + println!("{local_i32:<3}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:58:5 + --> $DIR/uninlined_format_args.rs:57:5 | LL | println!("{:#010x}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -166,7 +166,7 @@ LL + println!("{local_i32:#010x}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:59:5 + --> $DIR/uninlined_format_args.rs:58:5 | LL | println!("{:.1}", local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -178,7 +178,7 @@ LL + println!("{local_f64:.1}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:60:5 + --> $DIR/uninlined_format_args.rs:59:5 | LL | println!("Hello {} is {:.*}", "x", local_i32, local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -190,7 +190,7 @@ LL + println!("Hello {} is {local_f64:.local_i32$}", "x"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:61:5 + --> $DIR/uninlined_format_args.rs:60:5 | LL | println!("Hello {} is {:.*}", local_i32, 5, local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -202,7 +202,7 @@ LL + println!("Hello {local_i32} is {local_f64:.*}", 5); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:62:5 + --> $DIR/uninlined_format_args.rs:61:5 | LL | println!("Hello {} is {2:.*}", local_i32, 5, local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -214,7 +214,7 @@ LL + println!("Hello {local_i32} is {local_f64:.*}", 5); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:63:5 + --> $DIR/uninlined_format_args.rs:62:5 | LL | println!("{} {}", local_i32, local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -226,7 +226,7 @@ LL + println!("{local_i32} {local_f64}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:64:5 + --> $DIR/uninlined_format_args.rs:63:5 | LL | println!("{}, {}", local_i32, local_opt.unwrap()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -238,7 +238,7 @@ LL + println!("{local_i32}, {}", local_opt.unwrap()); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:65:5 + --> $DIR/uninlined_format_args.rs:64:5 | LL | println!("{}", val); | ^^^^^^^^^^^^^^^^^^^ @@ -250,7 +250,7 @@ LL + println!("{val}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:66:5 + --> $DIR/uninlined_format_args.rs:65:5 | LL | println!("{}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^ @@ -262,7 +262,7 @@ LL + println!("{val}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:68:5 + --> $DIR/uninlined_format_args.rs:67:5 | LL | println!("val='{/t }'", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -274,7 +274,7 @@ LL + println!("val='{local_i32}'"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:69:5 + --> $DIR/uninlined_format_args.rs:68:5 | LL | println!("val='{/n }'", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -286,7 +286,7 @@ LL + println!("val='{local_i32}'"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:70:5 + --> $DIR/uninlined_format_args.rs:69:5 | LL | println!("val='{local_i32}'", local_i32 = local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -298,7 +298,7 @@ LL + println!("val='{local_i32}'"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:71:5 + --> $DIR/uninlined_format_args.rs:70:5 | LL | println!("val='{local_i32}'", local_i32 = fn_arg); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -310,7 +310,7 @@ LL + println!("val='{fn_arg}'"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:72:5 + --> $DIR/uninlined_format_args.rs:71:5 | LL | println!("{0}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -322,7 +322,7 @@ LL + println!("{local_i32}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:73:5 + --> $DIR/uninlined_format_args.rs:72:5 | LL | println!("{0:?}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -334,7 +334,7 @@ LL + println!("{local_i32:?}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:74:5 + --> $DIR/uninlined_format_args.rs:73:5 | LL | println!("{0:#?}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -346,7 +346,7 @@ LL + println!("{local_i32:#?}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:75:5 + --> $DIR/uninlined_format_args.rs:74:5 | LL | println!("{0:04}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -358,7 +358,7 @@ LL + println!("{local_i32:04}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:76:5 + --> $DIR/uninlined_format_args.rs:75:5 | LL | println!("{0:<3}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -370,7 +370,7 @@ LL + println!("{local_i32:<3}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:77:5 + --> $DIR/uninlined_format_args.rs:76:5 | LL | println!("{0:#010x}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -382,7 +382,7 @@ LL + println!("{local_i32:#010x}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:78:5 + --> $DIR/uninlined_format_args.rs:77:5 | LL | println!("{0:.1}", local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -394,7 +394,7 @@ LL + println!("{local_f64:.1}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:79:5 + --> $DIR/uninlined_format_args.rs:78:5 | LL | println!("{0} {0}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -406,7 +406,7 @@ LL + println!("{local_i32} {local_i32}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:80:5 + --> $DIR/uninlined_format_args.rs:79:5 | LL | println!("{1} {} {0} {}", local_i32, local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -418,7 +418,7 @@ LL + println!("{local_f64} {local_i32} {local_i32} {local_f64}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:81:5 + --> $DIR/uninlined_format_args.rs:80:5 | LL | println!("{0} {1}", local_i32, local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -430,7 +430,7 @@ LL + println!("{local_i32} {local_f64}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:82:5 + --> $DIR/uninlined_format_args.rs:81:5 | LL | println!("{1} {0}", local_i32, local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -442,7 +442,7 @@ LL + println!("{local_f64} {local_i32}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:83:5 + --> $DIR/uninlined_format_args.rs:82:5 | LL | println!("{1} {0} {1} {0}", local_i32, local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -454,7 +454,7 @@ LL + println!("{local_f64} {local_i32} {local_f64} {local_i32}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:85:5 + --> $DIR/uninlined_format_args.rs:84:5 | LL | println!("{v}", v = local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -466,7 +466,7 @@ LL + println!("{local_i32}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:86:5 + --> $DIR/uninlined_format_args.rs:85:5 | LL | println!("{local_i32:0$}", width); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -478,7 +478,7 @@ LL + println!("{local_i32:width$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:87:5 + --> $DIR/uninlined_format_args.rs:86:5 | LL | println!("{local_i32:w$}", w = width); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -490,7 +490,7 @@ LL + println!("{local_i32:width$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:88:5 + --> $DIR/uninlined_format_args.rs:87:5 | LL | println!("{local_i32:.0$}", prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -502,7 +502,7 @@ LL + println!("{local_i32:.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:89:5 + --> $DIR/uninlined_format_args.rs:88:5 | LL | println!("{local_i32:.p$}", p = prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -514,7 +514,7 @@ LL + println!("{local_i32:.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:90:5 + --> $DIR/uninlined_format_args.rs:89:5 | LL | println!("{:0$}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -526,7 +526,7 @@ LL + println!("{val:val$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:91:5 + --> $DIR/uninlined_format_args.rs:90:5 | LL | println!("{0:0$}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -538,7 +538,7 @@ LL + println!("{val:val$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:92:5 + --> $DIR/uninlined_format_args.rs:91:5 | LL | println!("{:0$.0$}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -550,7 +550,7 @@ LL + println!("{val:val$.val$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:93:5 + --> $DIR/uninlined_format_args.rs:92:5 | LL | println!("{0:0$.0$}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -562,7 +562,7 @@ LL + println!("{val:val$.val$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:94:5 + --> $DIR/uninlined_format_args.rs:93:5 | LL | println!("{0:0$.v$}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -574,7 +574,7 @@ LL + println!("{val:val$.val$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:95:5 + --> $DIR/uninlined_format_args.rs:94:5 | LL | println!("{0:v$.0$}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -586,7 +586,7 @@ LL + println!("{val:val$.val$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:96:5 + --> $DIR/uninlined_format_args.rs:95:5 | LL | println!("{v:0$.0$}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -598,7 +598,7 @@ LL + println!("{val:val$.val$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:97:5 + --> $DIR/uninlined_format_args.rs:96:5 | LL | println!("{v:v$.0$}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -610,7 +610,7 @@ LL + println!("{val:val$.val$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:98:5 + --> $DIR/uninlined_format_args.rs:97:5 | LL | println!("{v:0$.v$}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -622,7 +622,7 @@ LL + println!("{val:val$.val$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:99:5 + --> $DIR/uninlined_format_args.rs:98:5 | LL | println!("{v:v$.v$}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -634,7 +634,7 @@ LL + println!("{val:val$.val$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:100:5 + --> $DIR/uninlined_format_args.rs:99:5 | LL | println!("{:0$}", width); | ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -646,7 +646,7 @@ LL + println!("{width:width$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:101:5 + --> $DIR/uninlined_format_args.rs:100:5 | LL | println!("{:1$}", local_i32, width); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -658,7 +658,7 @@ LL + println!("{local_i32:width$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:102:5 + --> $DIR/uninlined_format_args.rs:101:5 | LL | println!("{:w$}", w = width); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -670,7 +670,7 @@ LL + println!("{width:width$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:103:5 + --> $DIR/uninlined_format_args.rs:102:5 | LL | println!("{:w$}", local_i32, w = width); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -682,7 +682,7 @@ LL + println!("{local_i32:width$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:104:5 + --> $DIR/uninlined_format_args.rs:103:5 | LL | println!("{:.0$}", prec); | ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -694,7 +694,7 @@ LL + println!("{prec:.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:105:5 + --> $DIR/uninlined_format_args.rs:104:5 | LL | println!("{:.1$}", local_i32, prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -706,7 +706,7 @@ LL + println!("{local_i32:.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:106:5 + --> $DIR/uninlined_format_args.rs:105:5 | LL | println!("{:.p$}", p = prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -718,7 +718,7 @@ LL + println!("{prec:.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:107:5 + --> $DIR/uninlined_format_args.rs:106:5 | LL | println!("{:.p$}", local_i32, p = prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -730,7 +730,7 @@ LL + println!("{local_i32:.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:108:5 + --> $DIR/uninlined_format_args.rs:107:5 | LL | println!("{:0$.1$}", width, prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -742,7 +742,7 @@ LL + println!("{width:width$.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:109:5 + --> $DIR/uninlined_format_args.rs:108:5 | LL | println!("{:0$.w$}", width, w = prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -754,7 +754,7 @@ LL + println!("{width:width$.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:110:5 + --> $DIR/uninlined_format_args.rs:109:5 | LL | println!("{:1$.2$}", local_f64, width, prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -766,7 +766,7 @@ LL + println!("{local_f64:width$.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:111:5 + --> $DIR/uninlined_format_args.rs:110:5 | LL | println!("{:1$.2$} {0} {1} {2}", local_f64, width, prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -778,7 +778,7 @@ LL + println!("{local_f64:width$.prec$} {local_f64} {width} {prec}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:112:5 + --> $DIR/uninlined_format_args.rs:111:5 | LL | / println!( LL | | "{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$}", @@ -787,7 +787,7 @@ LL | | ); | |_____^ error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:123:5 + --> $DIR/uninlined_format_args.rs:122:5 | LL | println!("Width = {}, value with width = {:0$}", local_i32, local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -799,7 +799,7 @@ LL + println!("Width = {local_i32}, value with width = {local_f64:local_i32$ | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:124:5 + --> $DIR/uninlined_format_args.rs:123:5 | LL | println!("{:w$.p$}", local_i32, w = width, p = prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -811,7 +811,7 @@ LL + println!("{local_i32:width$.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:125:5 + --> $DIR/uninlined_format_args.rs:124:5 | LL | println!("{:w$.p$}", w = width, p = prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -823,7 +823,7 @@ LL + println!("{width:width$.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:126:20 + --> $DIR/uninlined_format_args.rs:125:20 | LL | println!("{}", format!("{}", local_i32)); | ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -835,7 +835,7 @@ LL + println!("{}", format!("{local_i32}")); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:144:5 + --> $DIR/uninlined_format_args.rs:143:5 | LL | / println!( LL | | "{}", @@ -845,7 +845,7 @@ LL | | ); | |_____^ error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:149:5 + --> $DIR/uninlined_format_args.rs:148:5 | LL | println!("{}", /* comment with a comma , in it */ val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -857,7 +857,7 @@ LL + println!("{val}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:155:9 + --> $DIR/uninlined_format_args.rs:154:9 | LL | panic!("p1 {}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -869,7 +869,7 @@ LL + panic!("p1 {local_i32}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:158:9 + --> $DIR/uninlined_format_args.rs:157:9 | LL | panic!("p2 {0}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -881,7 +881,7 @@ LL + panic!("p2 {local_i32}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:161:9 + --> $DIR/uninlined_format_args.rs:160:9 | LL | panic!("p3 {local_i32}", local_i32 = local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -893,7 +893,7 @@ LL + panic!("p3 {local_i32}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:181:5 + --> $DIR/uninlined_format_args.rs:180:5 | LL | println!("expand='{}'", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/unnecessary_to_owned.fixed b/tests/ui/unnecessary_to_owned.fixed index fadcf7f9c9e86..ddeda795f8179 100644 --- a/tests/ui/unnecessary_to_owned.fixed +++ b/tests/ui/unnecessary_to_owned.fixed @@ -2,7 +2,6 @@ #![allow(clippy::needless_borrow, clippy::ptr_arg)] #![warn(clippy::unnecessary_to_owned)] -#![feature(custom_inner_attributes)] use std::borrow::Cow; use std::ffi::{CStr, CString, OsStr, OsString}; @@ -215,14 +214,14 @@ fn get_file_path(_file_type: &FileType) -> Result Result $DIR/unnecessary_to_owned.rs:151:64 + --> $DIR/unnecessary_to_owned.rs:150:64 | LL | require_c_str(&CString::from_vec_with_nul(vec![0]).unwrap().to_owned()); | ^^^^^^^^^^^ help: remove this | note: this value is dropped without further use - --> $DIR/unnecessary_to_owned.rs:151:20 + --> $DIR/unnecessary_to_owned.rs:150:20 | LL | require_c_str(&CString::from_vec_with_nul(vec![0]).unwrap().to_owned()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: `-D clippy::redundant-clone` implied by `-D warnings` error: redundant clone - --> $DIR/unnecessary_to_owned.rs:152:40 + --> $DIR/unnecessary_to_owned.rs:151:40 | LL | require_os_str(&OsString::from("x").to_os_string()); | ^^^^^^^^^^^^^^^ help: remove this | note: this value is dropped without further use - --> $DIR/unnecessary_to_owned.rs:152:21 + --> $DIR/unnecessary_to_owned.rs:151:21 | LL | require_os_str(&OsString::from("x").to_os_string()); | ^^^^^^^^^^^^^^^^^^^ error: redundant clone - --> $DIR/unnecessary_to_owned.rs:153:48 + --> $DIR/unnecessary_to_owned.rs:152:48 | LL | require_path(&std::path::PathBuf::from("x").to_path_buf()); | ^^^^^^^^^^^^^^ help: remove this | note: this value is dropped without further use - --> $DIR/unnecessary_to_owned.rs:153:19 + --> $DIR/unnecessary_to_owned.rs:152:19 | LL | require_path(&std::path::PathBuf::from("x").to_path_buf()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: redundant clone - --> $DIR/unnecessary_to_owned.rs:154:35 + --> $DIR/unnecessary_to_owned.rs:153:35 | LL | require_str(&String::from("x").to_string()); | ^^^^^^^^^^^^ help: remove this | note: this value is dropped without further use - --> $DIR/unnecessary_to_owned.rs:154:18 + --> $DIR/unnecessary_to_owned.rs:153:18 | LL | require_str(&String::from("x").to_string()); | ^^^^^^^^^^^^^^^^^ error: redundant clone - --> $DIR/unnecessary_to_owned.rs:155:39 + --> $DIR/unnecessary_to_owned.rs:154:39 | LL | require_slice(&[String::from("x")].to_owned()); | ^^^^^^^^^^^ help: remove this | note: this value is dropped without further use - --> $DIR/unnecessary_to_owned.rs:155:20 + --> $DIR/unnecessary_to_owned.rs:154:20 | LL | require_slice(&[String::from("x")].to_owned()); | ^^^^^^^^^^^^^^^^^^^ error: unnecessary use of `into_owned` - --> $DIR/unnecessary_to_owned.rs:60:36 + --> $DIR/unnecessary_to_owned.rs:59:36 | LL | require_c_str(&Cow::from(c_str).into_owned()); | ^^^^^^^^^^^^^ help: remove this @@ -68,415 +68,415 @@ LL | require_c_str(&Cow::from(c_str).into_owned()); = note: `-D clippy::unnecessary-to-owned` implied by `-D warnings` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:61:19 + --> $DIR/unnecessary_to_owned.rs:60:19 | LL | require_c_str(&c_str.to_owned()); | ^^^^^^^^^^^^^^^^^ help: use: `c_str` error: unnecessary use of `to_os_string` - --> $DIR/unnecessary_to_owned.rs:63:20 + --> $DIR/unnecessary_to_owned.rs:62:20 | LL | require_os_str(&os_str.to_os_string()); | ^^^^^^^^^^^^^^^^^^^^^^ help: use: `os_str` error: unnecessary use of `into_owned` - --> $DIR/unnecessary_to_owned.rs:64:38 + --> $DIR/unnecessary_to_owned.rs:63:38 | LL | require_os_str(&Cow::from(os_str).into_owned()); | ^^^^^^^^^^^^^ help: remove this error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:65:20 + --> $DIR/unnecessary_to_owned.rs:64:20 | LL | require_os_str(&os_str.to_owned()); | ^^^^^^^^^^^^^^^^^^ help: use: `os_str` error: unnecessary use of `to_path_buf` - --> $DIR/unnecessary_to_owned.rs:67:18 + --> $DIR/unnecessary_to_owned.rs:66:18 | LL | require_path(&path.to_path_buf()); | ^^^^^^^^^^^^^^^^^^^ help: use: `path` error: unnecessary use of `into_owned` - --> $DIR/unnecessary_to_owned.rs:68:34 + --> $DIR/unnecessary_to_owned.rs:67:34 | LL | require_path(&Cow::from(path).into_owned()); | ^^^^^^^^^^^^^ help: remove this error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:69:18 + --> $DIR/unnecessary_to_owned.rs:68:18 | LL | require_path(&path.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `path` error: unnecessary use of `to_string` - --> $DIR/unnecessary_to_owned.rs:71:17 + --> $DIR/unnecessary_to_owned.rs:70:17 | LL | require_str(&s.to_string()); | ^^^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `into_owned` - --> $DIR/unnecessary_to_owned.rs:72:30 + --> $DIR/unnecessary_to_owned.rs:71:30 | LL | require_str(&Cow::from(s).into_owned()); | ^^^^^^^^^^^^^ help: remove this error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:73:17 + --> $DIR/unnecessary_to_owned.rs:72:17 | LL | require_str(&s.to_owned()); | ^^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_string` - --> $DIR/unnecessary_to_owned.rs:74:17 + --> $DIR/unnecessary_to_owned.rs:73:17 | LL | require_str(&x_ref.to_string()); | ^^^^^^^^^^^^^^^^^^ help: use: `x_ref.as_ref()` error: unnecessary use of `to_vec` - --> $DIR/unnecessary_to_owned.rs:76:19 + --> $DIR/unnecessary_to_owned.rs:75:19 | LL | require_slice(&slice.to_vec()); | ^^^^^^^^^^^^^^^ help: use: `slice` error: unnecessary use of `into_owned` - --> $DIR/unnecessary_to_owned.rs:77:36 + --> $DIR/unnecessary_to_owned.rs:76:36 | LL | require_slice(&Cow::from(slice).into_owned()); | ^^^^^^^^^^^^^ help: remove this error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:78:19 + --> $DIR/unnecessary_to_owned.rs:77:19 | LL | require_slice(&array.to_owned()); | ^^^^^^^^^^^^^^^^^ help: use: `array.as_ref()` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:79:19 + --> $DIR/unnecessary_to_owned.rs:78:19 | LL | require_slice(&array_ref.to_owned()); | ^^^^^^^^^^^^^^^^^^^^^ help: use: `array_ref.as_ref()` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:80:19 + --> $DIR/unnecessary_to_owned.rs:79:19 | LL | require_slice(&slice.to_owned()); | ^^^^^^^^^^^^^^^^^ help: use: `slice` error: unnecessary use of `into_owned` - --> $DIR/unnecessary_to_owned.rs:83:42 + --> $DIR/unnecessary_to_owned.rs:82:42 | LL | require_x(&Cow::::Owned(x.clone()).into_owned()); | ^^^^^^^^^^^^^ help: remove this error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:86:25 + --> $DIR/unnecessary_to_owned.rs:85:25 | LL | require_deref_c_str(c_str.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `c_str` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:87:26 + --> $DIR/unnecessary_to_owned.rs:86:26 | LL | require_deref_os_str(os_str.to_owned()); | ^^^^^^^^^^^^^^^^^ help: use: `os_str` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:88:24 + --> $DIR/unnecessary_to_owned.rs:87:24 | LL | require_deref_path(path.to_owned()); | ^^^^^^^^^^^^^^^ help: use: `path` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:89:23 + --> $DIR/unnecessary_to_owned.rs:88:23 | LL | require_deref_str(s.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:90:25 + --> $DIR/unnecessary_to_owned.rs:89:25 | LL | require_deref_slice(slice.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `slice` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:92:30 + --> $DIR/unnecessary_to_owned.rs:91:30 | LL | require_impl_deref_c_str(c_str.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `c_str` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:93:31 + --> $DIR/unnecessary_to_owned.rs:92:31 | LL | require_impl_deref_os_str(os_str.to_owned()); | ^^^^^^^^^^^^^^^^^ help: use: `os_str` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:94:29 + --> $DIR/unnecessary_to_owned.rs:93:29 | LL | require_impl_deref_path(path.to_owned()); | ^^^^^^^^^^^^^^^ help: use: `path` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:95:28 + --> $DIR/unnecessary_to_owned.rs:94:28 | LL | require_impl_deref_str(s.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:96:30 + --> $DIR/unnecessary_to_owned.rs:95:30 | LL | require_impl_deref_slice(slice.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `slice` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:98:29 + --> $DIR/unnecessary_to_owned.rs:97:29 | LL | require_deref_str_slice(s.to_owned(), slice.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:98:43 + --> $DIR/unnecessary_to_owned.rs:97:43 | LL | require_deref_str_slice(s.to_owned(), slice.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `slice` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:99:29 + --> $DIR/unnecessary_to_owned.rs:98:29 | LL | require_deref_slice_str(slice.to_owned(), s.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `slice` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:99:47 + --> $DIR/unnecessary_to_owned.rs:98:47 | LL | require_deref_slice_str(slice.to_owned(), s.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:101:26 + --> $DIR/unnecessary_to_owned.rs:100:26 | LL | require_as_ref_c_str(c_str.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `c_str` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:102:27 + --> $DIR/unnecessary_to_owned.rs:101:27 | LL | require_as_ref_os_str(os_str.to_owned()); | ^^^^^^^^^^^^^^^^^ help: use: `os_str` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:103:25 + --> $DIR/unnecessary_to_owned.rs:102:25 | LL | require_as_ref_path(path.to_owned()); | ^^^^^^^^^^^^^^^ help: use: `path` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:104:24 + --> $DIR/unnecessary_to_owned.rs:103:24 | LL | require_as_ref_str(s.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:105:24 + --> $DIR/unnecessary_to_owned.rs:104:24 | LL | require_as_ref_str(x.to_owned()); | ^^^^^^^^^^^^ help: use: `&x` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:106:26 + --> $DIR/unnecessary_to_owned.rs:105:26 | LL | require_as_ref_slice(array.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `array` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:107:26 + --> $DIR/unnecessary_to_owned.rs:106:26 | LL | require_as_ref_slice(array_ref.to_owned()); | ^^^^^^^^^^^^^^^^^^^^ help: use: `array_ref` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:108:26 + --> $DIR/unnecessary_to_owned.rs:107:26 | LL | require_as_ref_slice(slice.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `slice` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:110:31 + --> $DIR/unnecessary_to_owned.rs:109:31 | LL | require_impl_as_ref_c_str(c_str.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `c_str` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:111:32 + --> $DIR/unnecessary_to_owned.rs:110:32 | LL | require_impl_as_ref_os_str(os_str.to_owned()); | ^^^^^^^^^^^^^^^^^ help: use: `os_str` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:112:30 + --> $DIR/unnecessary_to_owned.rs:111:30 | LL | require_impl_as_ref_path(path.to_owned()); | ^^^^^^^^^^^^^^^ help: use: `path` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:113:29 + --> $DIR/unnecessary_to_owned.rs:112:29 | LL | require_impl_as_ref_str(s.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:114:29 + --> $DIR/unnecessary_to_owned.rs:113:29 | LL | require_impl_as_ref_str(x.to_owned()); | ^^^^^^^^^^^^ help: use: `&x` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:115:31 + --> $DIR/unnecessary_to_owned.rs:114:31 | LL | require_impl_as_ref_slice(array.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `array` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:116:31 + --> $DIR/unnecessary_to_owned.rs:115:31 | LL | require_impl_as_ref_slice(array_ref.to_owned()); | ^^^^^^^^^^^^^^^^^^^^ help: use: `array_ref` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:117:31 + --> $DIR/unnecessary_to_owned.rs:116:31 | LL | require_impl_as_ref_slice(slice.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `slice` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:119:30 + --> $DIR/unnecessary_to_owned.rs:118:30 | LL | require_as_ref_str_slice(s.to_owned(), array.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:119:44 + --> $DIR/unnecessary_to_owned.rs:118:44 | LL | require_as_ref_str_slice(s.to_owned(), array.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `array` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:120:30 + --> $DIR/unnecessary_to_owned.rs:119:30 | LL | require_as_ref_str_slice(s.to_owned(), array_ref.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:120:44 + --> $DIR/unnecessary_to_owned.rs:119:44 | LL | require_as_ref_str_slice(s.to_owned(), array_ref.to_owned()); | ^^^^^^^^^^^^^^^^^^^^ help: use: `array_ref` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:121:30 + --> $DIR/unnecessary_to_owned.rs:120:30 | LL | require_as_ref_str_slice(s.to_owned(), slice.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:121:44 + --> $DIR/unnecessary_to_owned.rs:120:44 | LL | require_as_ref_str_slice(s.to_owned(), slice.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `slice` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:122:30 + --> $DIR/unnecessary_to_owned.rs:121:30 | LL | require_as_ref_slice_str(array.to_owned(), s.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `array` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:122:48 + --> $DIR/unnecessary_to_owned.rs:121:48 | LL | require_as_ref_slice_str(array.to_owned(), s.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:123:30 + --> $DIR/unnecessary_to_owned.rs:122:30 | LL | require_as_ref_slice_str(array_ref.to_owned(), s.to_owned()); | ^^^^^^^^^^^^^^^^^^^^ help: use: `array_ref` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:123:52 + --> $DIR/unnecessary_to_owned.rs:122:52 | LL | require_as_ref_slice_str(array_ref.to_owned(), s.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:124:30 + --> $DIR/unnecessary_to_owned.rs:123:30 | LL | require_as_ref_slice_str(slice.to_owned(), s.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `slice` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:124:48 + --> $DIR/unnecessary_to_owned.rs:123:48 | LL | require_as_ref_slice_str(slice.to_owned(), s.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_string` - --> $DIR/unnecessary_to_owned.rs:126:20 + --> $DIR/unnecessary_to_owned.rs:125:20 | LL | let _ = x.join(&x_ref.to_string()); | ^^^^^^^^^^^^^^^^^^ help: use: `x_ref` error: unnecessary use of `to_vec` - --> $DIR/unnecessary_to_owned.rs:128:13 + --> $DIR/unnecessary_to_owned.rs:127:13 | LL | let _ = slice.to_vec().into_iter(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `slice.iter().copied()` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:129:13 + --> $DIR/unnecessary_to_owned.rs:128:13 | LL | let _ = slice.to_owned().into_iter(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `slice.iter().copied()` error: unnecessary use of `to_vec` - --> $DIR/unnecessary_to_owned.rs:130:13 + --> $DIR/unnecessary_to_owned.rs:129:13 | LL | let _ = [std::path::PathBuf::new()][..].to_vec().into_iter(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `[std::path::PathBuf::new()][..].iter().cloned()` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:131:13 + --> $DIR/unnecessary_to_owned.rs:130:13 | LL | let _ = [std::path::PathBuf::new()][..].to_owned().into_iter(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `[std::path::PathBuf::new()][..].iter().cloned()` error: unnecessary use of `to_vec` - --> $DIR/unnecessary_to_owned.rs:133:13 + --> $DIR/unnecessary_to_owned.rs:132:13 | LL | let _ = IntoIterator::into_iter(slice.to_vec()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `slice.iter().copied()` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:134:13 + --> $DIR/unnecessary_to_owned.rs:133:13 | LL | let _ = IntoIterator::into_iter(slice.to_owned()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `slice.iter().copied()` error: unnecessary use of `to_vec` - --> $DIR/unnecessary_to_owned.rs:135:13 + --> $DIR/unnecessary_to_owned.rs:134:13 | LL | let _ = IntoIterator::into_iter([std::path::PathBuf::new()][..].to_vec()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `[std::path::PathBuf::new()][..].iter().cloned()` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:136:13 + --> $DIR/unnecessary_to_owned.rs:135:13 | LL | let _ = IntoIterator::into_iter([std::path::PathBuf::new()][..].to_owned()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `[std::path::PathBuf::new()][..].iter().cloned()` error: unnecessary use of `to_vec` - --> $DIR/unnecessary_to_owned.rs:198:14 + --> $DIR/unnecessary_to_owned.rs:197:14 | LL | for t in file_types.to_vec() { | ^^^^^^^^^^^^^^^^^^^ @@ -492,25 +492,25 @@ LL + let path = match get_file_path(t) { | error: unnecessary use of `to_vec` - --> $DIR/unnecessary_to_owned.rs:221:14 + --> $DIR/unnecessary_to_owned.rs:220:14 | LL | let _ = &["x"][..].to_vec().into_iter(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `["x"][..].iter().cloned()` error: unnecessary use of `to_vec` - --> $DIR/unnecessary_to_owned.rs:226:14 + --> $DIR/unnecessary_to_owned.rs:225:14 | LL | let _ = &["x"][..].to_vec().into_iter(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `["x"][..].iter().copied()` error: unnecessary use of `to_string` - --> $DIR/unnecessary_to_owned.rs:273:24 + --> $DIR/unnecessary_to_owned.rs:272:24 | LL | Box::new(build(y.to_string())) | ^^^^^^^^^^^^^ help: use: `y` error: unnecessary use of `to_string` - --> $DIR/unnecessary_to_owned.rs:381:12 + --> $DIR/unnecessary_to_owned.rs:380:12 | LL | id("abc".to_string()) | ^^^^^^^^^^^^^^^^^ help: use: `"abc"` diff --git a/tests/ui/unnested_or_patterns.fixed b/tests/ui/unnested_or_patterns.fixed index 9786c7b12128b..0a8e7b34dfa4f 100644 --- a/tests/ui/unnested_or_patterns.fixed +++ b/tests/ui/unnested_or_patterns.fixed @@ -1,6 +1,6 @@ // run-rustfix -#![feature(box_patterns, custom_inner_attributes)] +#![feature(box_patterns)] #![warn(clippy::unnested_or_patterns)] #![allow(clippy::cognitive_complexity, clippy::match_ref_pats, clippy::upper_case_acronyms)] #![allow(unreachable_patterns, irrefutable_let_patterns, unused)] @@ -34,14 +34,12 @@ fn main() { if let S { x: 0, y, .. } | S { y, x: 1 } = (S { x: 0, y: 1 }) {} } +#[clippy::msrv = "1.52"] fn msrv_1_52() { - #![clippy::msrv = "1.52"] - if let [1] | [52] = [0] {} } +#[clippy::msrv = "1.53"] fn msrv_1_53() { - #![clippy::msrv = "1.53"] - if let [1 | 53] = [0] {} } diff --git a/tests/ui/unnested_or_patterns.rs b/tests/ui/unnested_or_patterns.rs index f57322396d4ac..2c454adfe89db 100644 --- a/tests/ui/unnested_or_patterns.rs +++ b/tests/ui/unnested_or_patterns.rs @@ -1,6 +1,6 @@ // run-rustfix -#![feature(box_patterns, custom_inner_attributes)] +#![feature(box_patterns)] #![warn(clippy::unnested_or_patterns)] #![allow(clippy::cognitive_complexity, clippy::match_ref_pats, clippy::upper_case_acronyms)] #![allow(unreachable_patterns, irrefutable_let_patterns, unused)] @@ -34,14 +34,12 @@ fn main() { if let S { x: 0, y, .. } | S { y, x: 1 } = (S { x: 0, y: 1 }) {} } +#[clippy::msrv = "1.52"] fn msrv_1_52() { - #![clippy::msrv = "1.52"] - if let [1] | [52] = [0] {} } +#[clippy::msrv = "1.53"] fn msrv_1_53() { - #![clippy::msrv = "1.53"] - if let [1] | [53] = [0] {} } diff --git a/tests/ui/unnested_or_patterns.stderr b/tests/ui/unnested_or_patterns.stderr index fbc12fff0b0e7..a1f193db555ad 100644 --- a/tests/ui/unnested_or_patterns.stderr +++ b/tests/ui/unnested_or_patterns.stderr @@ -176,7 +176,7 @@ LL | if let S { x: 0 | 1, y } = (S { x: 0, y: 1 }) {} | ~~~~~~~~~~~~~~~~~ error: unnested or-patterns - --> $DIR/unnested_or_patterns.rs:46:12 + --> $DIR/unnested_or_patterns.rs:44:12 | LL | if let [1] | [53] = [0] {} | ^^^^^^^^^^ diff --git a/tests/ui/use_self.fixed b/tests/ui/use_self.fixed index 3b54fe9d5ff3d..0a6166571ebe3 100644 --- a/tests/ui/use_self.fixed +++ b/tests/ui/use_self.fixed @@ -1,7 +1,6 @@ // run-rustfix // aux-build:proc_macro_derive.rs -#![feature(custom_inner_attributes)] #![warn(clippy::use_self)] #![allow(dead_code, unreachable_code)] #![allow( @@ -619,9 +618,8 @@ mod issue6902 { } } +#[clippy::msrv = "1.36"] fn msrv_1_36() { - #![clippy::msrv = "1.36"] - enum E { A, } @@ -635,9 +633,8 @@ fn msrv_1_36() { } } +#[clippy::msrv = "1.37"] fn msrv_1_37() { - #![clippy::msrv = "1.37"] - enum E { A, } diff --git a/tests/ui/use_self.rs b/tests/ui/use_self.rs index bf87633cd2d8f..39c2b431f7fb9 100644 --- a/tests/ui/use_self.rs +++ b/tests/ui/use_self.rs @@ -1,7 +1,6 @@ // run-rustfix // aux-build:proc_macro_derive.rs -#![feature(custom_inner_attributes)] #![warn(clippy::use_self)] #![allow(dead_code, unreachable_code)] #![allow( @@ -619,9 +618,8 @@ mod issue6902 { } } +#[clippy::msrv = "1.36"] fn msrv_1_36() { - #![clippy::msrv = "1.36"] - enum E { A, } @@ -635,9 +633,8 @@ fn msrv_1_36() { } } +#[clippy::msrv = "1.37"] fn msrv_1_37() { - #![clippy::msrv = "1.37"] - enum E { A, } diff --git a/tests/ui/use_self.stderr b/tests/ui/use_self.stderr index 16fb0609242cb..48364c40c3b26 100644 --- a/tests/ui/use_self.stderr +++ b/tests/ui/use_self.stderr @@ -1,5 +1,5 @@ error: unnecessary structure name repetition - --> $DIR/use_self.rs:23:21 + --> $DIR/use_self.rs:22:21 | LL | fn new() -> Foo { | ^^^ help: use the applicable keyword: `Self` @@ -7,247 +7,247 @@ LL | fn new() -> Foo { = note: `-D clippy::use-self` implied by `-D warnings` error: unnecessary structure name repetition - --> $DIR/use_self.rs:24:13 + --> $DIR/use_self.rs:23:13 | LL | Foo {} | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:26:22 + --> $DIR/use_self.rs:25:22 | LL | fn test() -> Foo { | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:27:13 + --> $DIR/use_self.rs:26:13 | LL | Foo::new() | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:32:25 + --> $DIR/use_self.rs:31:25 | LL | fn default() -> Foo { | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:33:13 + --> $DIR/use_self.rs:32:13 | LL | Foo::new() | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:98:24 + --> $DIR/use_self.rs:97:24 | LL | fn bad(foos: &[Foo]) -> impl Iterator { | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:98:55 + --> $DIR/use_self.rs:97:55 | LL | fn bad(foos: &[Foo]) -> impl Iterator { | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:113:13 + --> $DIR/use_self.rs:112:13 | LL | TS(0) | ^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:148:29 + --> $DIR/use_self.rs:147:29 | LL | fn bar() -> Bar { | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:149:21 + --> $DIR/use_self.rs:148:21 | LL | Bar { foo: Foo {} } | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:160:21 + --> $DIR/use_self.rs:159:21 | LL | fn baz() -> Foo { | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:161:13 + --> $DIR/use_self.rs:160:13 | LL | Foo {} | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:178:21 + --> $DIR/use_self.rs:177:21 | LL | let _ = Enum::B(42); | ^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:179:21 + --> $DIR/use_self.rs:178:21 | LL | let _ = Enum::C { field: true }; | ^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:180:21 + --> $DIR/use_self.rs:179:21 | LL | let _ = Enum::A; | ^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:222:13 + --> $DIR/use_self.rs:221:13 | LL | nested::A::fun_1(); | ^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:223:13 + --> $DIR/use_self.rs:222:13 | LL | nested::A::A; | ^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:225:13 + --> $DIR/use_self.rs:224:13 | LL | nested::A {}; | ^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:244:13 + --> $DIR/use_self.rs:243:13 | LL | TestStruct::from_something() | ^^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:258:25 + --> $DIR/use_self.rs:257:25 | LL | async fn g() -> S { | ^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:259:13 + --> $DIR/use_self.rs:258:13 | LL | S {} | ^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:263:16 + --> $DIR/use_self.rs:262:16 | LL | &p[S::A..S::B] | ^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:263:22 + --> $DIR/use_self.rs:262:22 | LL | &p[S::A..S::B] | ^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:286:29 + --> $DIR/use_self.rs:285:29 | LL | fn foo(value: T) -> Foo { | ^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:287:13 + --> $DIR/use_self.rs:286:13 | LL | Foo:: { value } | ^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:459:13 + --> $DIR/use_self.rs:458:13 | LL | A::new::(submod::B {}) | ^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:496:13 + --> $DIR/use_self.rs:495:13 | LL | S2::new() | ^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:533:17 + --> $DIR/use_self.rs:532:17 | LL | Foo::Bar => unimplemented!(), | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:534:17 + --> $DIR/use_self.rs:533:17 | LL | Foo::Baz => unimplemented!(), | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:540:20 + --> $DIR/use_self.rs:539:20 | LL | if let Foo::Bar = self { | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:564:17 + --> $DIR/use_self.rs:563:17 | LL | Something::Num(n) => *n, | ^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:565:17 + --> $DIR/use_self.rs:564:17 | LL | Something::TupleNums(n, _m) => *n, | ^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:566:17 + --> $DIR/use_self.rs:565:17 | LL | Something::StructNums { one, two: _ } => *one, | ^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:572:17 + --> $DIR/use_self.rs:571:17 | LL | crate::issue8845::Something::Num(n) => *n, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:573:17 + --> $DIR/use_self.rs:572:17 | LL | crate::issue8845::Something::TupleNums(n, _m) => *n, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:574:17 + --> $DIR/use_self.rs:573:17 | LL | crate::issue8845::Something::StructNums { one, two: _ } => *one, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:590:17 + --> $DIR/use_self.rs:589:17 | LL | let Foo(x) = self; | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:595:17 + --> $DIR/use_self.rs:594:17 | LL | let crate::issue8845::Foo(x) = self; | ^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:602:17 + --> $DIR/use_self.rs:601:17 | LL | let Bar { x, .. } = self; | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:607:17 + --> $DIR/use_self.rs:606:17 | LL | let crate::issue8845::Bar { x, .. } = self; | ^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:648:17 + --> $DIR/use_self.rs:645:17 | LL | E::A => {}, | ^ help: use the applicable keyword: `Self` From 8c50dfb54649cbd67e37e7a456e736241740666d Mon Sep 17 00:00:00 2001 From: koka Date: Sun, 27 Nov 2022 22:01:21 +0900 Subject: [PATCH 116/244] Remove blank lines when needless_return returns no value fix https://github.com/rust-lang/rust-clippy/issues/9416 --- clippy_lints/src/returns.rs | 30 +++++++++--- tests/ui/needless_return.fixed | 19 ++++---- tests/ui/needless_return.rs | 13 +++++ tests/ui/needless_return.stderr | 85 +++++++++++++++++++++++---------- 4 files changed, 107 insertions(+), 40 deletions(-) diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs index 2b2a41d160117..933258f9c3ede 100644 --- a/clippy_lints/src/returns.rs +++ b/clippy_lints/src/returns.rs @@ -12,6 +12,7 @@ use rustc_middle::lint::in_external_macro; use rustc_middle::ty::subst::GenericArgKind; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::Span; +use rustc_span::BytePos; declare_clippy_lint! { /// ### What it does @@ -209,13 +210,14 @@ fn check_final_expr<'tcx>( if cx.tcx.hir().attrs(expr.hir_id).is_empty() { let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner)); if !borrows { - emit_return_lint( - cx, - peeled_drop_expr.span, - semi_spans, - inner.as_ref().map(|i| i.span), - replacement, - ); + // check if expr return nothing + let ret_span = if inner.is_none() && replacement == RetReplacement::Empty { + extend_span_to_previous_non_ws(cx, peeled_drop_expr.span) + } else { + peeled_drop_expr.span + }; + + emit_return_lint(cx, ret_span, semi_spans, inner.as_ref().map(|i| i.span), replacement); } } }, @@ -289,3 +291,17 @@ fn last_statement_borrows<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) }) .is_some() } + +// Go backwards while encountering whitespace and extend the given Span to that point. +#[expect(clippy::cast_possible_truncation)] +fn extend_span_to_previous_non_ws(cx: &LateContext<'_>, sp: Span) -> Span { + if let Ok(prev_source) = cx.sess().source_map().span_to_prev_source(sp) { + let ws = [' ', '\t', '\n']; + if let Some(non_ws_pos) = prev_source.rfind(|c| !ws.contains(&c)) { + let len = prev_source.len() - non_ws_pos - 1; + return sp.with_lo(BytePos(sp.lo().0 - len as u32)); + } + } + + sp +} diff --git a/tests/ui/needless_return.fixed b/tests/ui/needless_return.fixed index d2163b14fcadc..4386aaec49e24 100644 --- a/tests/ui/needless_return.fixed +++ b/tests/ui/needless_return.fixed @@ -59,14 +59,11 @@ fn test_macro_call() -> i32 { } fn test_void_fun() { - } fn test_void_if_fun(b: bool) { if b { - } else { - } } @@ -82,7 +79,6 @@ fn test_nested_match(x: u32) { 0 => (), 1 => { let _ = 42; - }, _ => (), } @@ -126,7 +122,6 @@ mod issue6501 { fn test_closure() { let _ = || { - }; let _ = || {}; } @@ -179,14 +174,11 @@ async fn async_test_macro_call() -> i32 { } async fn async_test_void_fun() { - } async fn async_test_void_if_fun(b: bool) { if b { - } else { - } } @@ -269,4 +261,15 @@ fn issue9503(x: usize) -> isize { } } +mod issue9416 { + pub fn with_newline() { + let _ = 42; + } + + #[rustfmt::skip] + pub fn oneline() { + let _ = 42; + } +} + fn main() {} diff --git a/tests/ui/needless_return.rs b/tests/ui/needless_return.rs index 114414b5fac76..666dc54b76b41 100644 --- a/tests/ui/needless_return.rs +++ b/tests/ui/needless_return.rs @@ -269,4 +269,17 @@ fn issue9503(x: usize) -> isize { }; } +mod issue9416 { + pub fn with_newline() { + let _ = 42; + + return; + } + + #[rustfmt::skip] + pub fn oneline() { + let _ = 42; return; + } +} + fn main() {} diff --git a/tests/ui/needless_return.stderr b/tests/ui/needless_return.stderr index 047fb6c2311a2..a8b5d86cd5581 100644 --- a/tests/ui/needless_return.stderr +++ b/tests/ui/needless_return.stderr @@ -72,26 +72,32 @@ LL | return the_answer!(); = help: remove `return` error: unneeded `return` statement - --> $DIR/needless_return.rs:62:5 + --> $DIR/needless_return.rs:61:21 | -LL | return; - | ^^^^^^ +LL | fn test_void_fun() { + | _____________________^ +LL | | return; + | |__________^ | = help: remove `return` error: unneeded `return` statement - --> $DIR/needless_return.rs:67:9 + --> $DIR/needless_return.rs:66:11 | -LL | return; - | ^^^^^^ +LL | if b { + | ___________^ +LL | | return; + | |______________^ | = help: remove `return` error: unneeded `return` statement - --> $DIR/needless_return.rs:69:9 + --> $DIR/needless_return.rs:68:13 | -LL | return; - | ^^^^^^ +LL | } else { + | _____________^ +LL | | return; + | |______________^ | = help: remove `return` @@ -104,10 +110,12 @@ LL | _ => return, = help: replace `return` with a unit value error: unneeded `return` statement - --> $DIR/needless_return.rs:85:13 + --> $DIR/needless_return.rs:84:24 | -LL | return; - | ^^^^^^ +LL | let _ = 42; + | ________________________^ +LL | | return; + | |__________________^ | = help: remove `return` @@ -144,10 +152,12 @@ LL | bar.unwrap_or_else(|_| return) = help: replace `return` with an empty block error: unneeded `return` statement - --> $DIR/needless_return.rs:129:13 + --> $DIR/needless_return.rs:128:21 | -LL | return; - | ^^^^^^ +LL | let _ = || { + | _____________________^ +LL | | return; + | |__________________^ | = help: remove `return` @@ -240,26 +250,32 @@ LL | return the_answer!(); = help: remove `return` error: unneeded `return` statement - --> $DIR/needless_return.rs:182:5 + --> $DIR/needless_return.rs:181:33 | -LL | return; - | ^^^^^^ +LL | async fn async_test_void_fun() { + | _________________________________^ +LL | | return; + | |__________^ | = help: remove `return` error: unneeded `return` statement - --> $DIR/needless_return.rs:187:9 + --> $DIR/needless_return.rs:186:11 | -LL | return; - | ^^^^^^ +LL | if b { + | ___________^ +LL | | return; + | |______________^ | = help: remove `return` error: unneeded `return` statement - --> $DIR/needless_return.rs:189:9 + --> $DIR/needless_return.rs:188:13 | -LL | return; - | ^^^^^^ +LL | } else { + | _____________^ +LL | | return; + | |______________^ | = help: remove `return` @@ -351,5 +367,24 @@ LL | return !*(x as *const isize); | = help: remove `return` -error: aborting due to 44 previous errors +error: unneeded `return` statement + --> $DIR/needless_return.rs:274:20 + | +LL | let _ = 42; + | ____________________^ +LL | | +LL | | return; + | |______________^ + | + = help: remove `return` + +error: unneeded `return` statement + --> $DIR/needless_return.rs:281:20 + | +LL | let _ = 42; return; + | ^^^^^^^ + | + = help: remove `return` + +error: aborting due to 46 previous errors From 39d0477080de2c458cdbeb4a0a44d267d58af521 Mon Sep 17 00:00:00 2001 From: koka Date: Sun, 27 Nov 2022 22:41:06 +0900 Subject: [PATCH 117/244] Refactor BytePos handling --- clippy_lints/src/returns.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs index 933258f9c3ede..81143d7799ea8 100644 --- a/clippy_lints/src/returns.rs +++ b/clippy_lints/src/returns.rs @@ -12,7 +12,7 @@ use rustc_middle::lint::in_external_macro; use rustc_middle::ty::subst::GenericArgKind; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::Span; -use rustc_span::BytePos; +use rustc_span::{BytePos, Pos}; declare_clippy_lint! { /// ### What it does @@ -293,13 +293,12 @@ fn last_statement_borrows<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) } // Go backwards while encountering whitespace and extend the given Span to that point. -#[expect(clippy::cast_possible_truncation)] fn extend_span_to_previous_non_ws(cx: &LateContext<'_>, sp: Span) -> Span { if let Ok(prev_source) = cx.sess().source_map().span_to_prev_source(sp) { let ws = [' ', '\t', '\n']; if let Some(non_ws_pos) = prev_source.rfind(|c| !ws.contains(&c)) { let len = prev_source.len() - non_ws_pos - 1; - return sp.with_lo(BytePos(sp.lo().0 - len as u32)); + return sp.with_lo(sp.lo() - BytePos::from_usize(len)); } } From 60d5d65a4d7931ffa022453a433f433d395afdb9 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 27 Nov 2022 15:20:26 +0100 Subject: [PATCH 118/244] interpret: get rid of run() function --- compiler/rustc_const_eval/src/const_eval/eval_queries.rs | 2 +- compiler/rustc_const_eval/src/interpret/step.rs | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs index c777a840f3fb6..c27790d8887a3 100644 --- a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs +++ b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs @@ -66,7 +66,7 @@ fn eval_body_using_ecx<'mir, 'tcx>( )?; // The main interpreter loop. - ecx.run()?; + while ecx.step()? {} // Intern the result let intern_kind = if cid.promoted.is_some() { diff --git a/compiler/rustc_const_eval/src/interpret/step.rs b/compiler/rustc_const_eval/src/interpret/step.rs index 73f8bf4362e60..60578246eedcc 100644 --- a/compiler/rustc_const_eval/src/interpret/step.rs +++ b/compiler/rustc_const_eval/src/interpret/step.rs @@ -32,11 +32,6 @@ fn binop_right_homogeneous(op: mir::BinOp) -> bool { } impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { - pub fn run(&mut self) -> InterpResult<'tcx> { - while self.step()? {} - Ok(()) - } - /// Returns `true` as long as there are more things to do. /// /// This is used by [priroda](https://github.com/oli-obk/priroda) From 7a2d92e1f29f774acb640b8e4262aed1b8f7f9a4 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Sun, 27 Nov 2022 10:12:51 -0500 Subject: [PATCH 119/244] Add allow-mixed-uninlined-format-args config Implement `allow-mixed-uninlined-format-args` config param to change the behavior of the `uninlined_format_args` lint. Now it is a part of `style`, and won't propose inlining in case of a mixed usage, e.g. `print!("{} {}", var, 1+2)`. If the user sets allow-mixed-uninlined-format-args config param to `false`, then it would behave like before, proposing to inline args even in the mixed case. --- clippy_lints/src/format_args.rs | 62 +- clippy_lints/src/lib.rs | 3 +- clippy_lints/src/utils/conf.rs | 4 + .../auxiliary/proc_macro_with_span.rs | 32 + .../clippy.toml | 1 + .../uninlined_format_args.fixed | 177 ++++ .../uninlined_format_args.rs | 182 ++++ .../uninlined_format_args.stderr | 908 ++++++++++++++++++ .../toml_unknown_key/conf_unknown_key.stderr | 1 + tests/ui/uninlined_format_args.fixed | 8 +- ...lined_format_args_panic.edition2018.stderr | 1 + ...lined_format_args_panic.edition2021.stderr | 1 + 12 files changed, 1359 insertions(+), 21 deletions(-) create mode 100644 tests/ui-toml/allow_mixed_uninlined_format_args/auxiliary/proc_macro_with_span.rs create mode 100644 tests/ui-toml/allow_mixed_uninlined_format_args/clippy.toml create mode 100644 tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.fixed create mode 100644 tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.rs create mode 100644 tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.stderr diff --git a/clippy_lints/src/format_args.rs b/clippy_lints/src/format_args.rs index fd3ce2f3d6cd6..40a37d39ecf84 100644 --- a/clippy_lints/src/format_args.rs +++ b/clippy_lints/src/format_args.rs @@ -1,6 +1,6 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then}; use clippy_utils::is_diag_trait_item; -use clippy_utils::macros::FormatParamKind::{Implicit, Named, Numbered, Starred}; +use clippy_utils::macros::FormatParamKind::{Implicit, Named, NamedInline, Numbered, Starred}; use clippy_utils::macros::{ is_format_macro, is_panic, root_macro_call, Count, FormatArg, FormatArgsExpn, FormatParam, FormatParamUsage, }; @@ -106,19 +106,25 @@ declare_clippy_lint! { /// format!("{var:.prec$}"); /// ``` /// - /// ### Known Problems - /// - /// There may be a false positive if the format string is expanded from certain proc macros: - /// - /// ```ignore - /// println!(indoc!("{}"), var); + /// If allow-mixed-uninlined-format-args is set to false in clippy.toml, + /// the following code will also trigger the lint: + /// ```rust + /// # let var = 42; + /// format!("{} {}", var, 1+2); + /// ``` + /// Use instead: + /// ```rust + /// # let var = 42; + /// format!("{var} {}", 1+2); /// ``` /// + /// ### Known Problems + /// /// If a format string contains a numbered argument that cannot be inlined /// nothing will be suggested, e.g. `println!("{0}={1}", var, 1+2)`. #[clippy::version = "1.65.0"] pub UNINLINED_FORMAT_ARGS, - pedantic, + style, "using non-inlined variables in `format!` calls" } @@ -162,12 +168,16 @@ impl_lint_pass!(FormatArgs => [ pub struct FormatArgs { msrv: Msrv, + ignore_mixed: bool, } impl FormatArgs { #[must_use] - pub fn new(msrv: Msrv) -> Self { - Self { msrv } + pub fn new(msrv: Msrv, allow_mixed_uninlined_format_args: bool) -> Self { + Self { + msrv, + ignore_mixed: allow_mixed_uninlined_format_args, + } } } @@ -192,7 +202,7 @@ impl<'tcx> LateLintPass<'tcx> for FormatArgs { check_to_string_in_format_args(cx, name, arg.param.value); } if self.msrv.meets(msrvs::FORMAT_ARGS_CAPTURE) { - check_uninlined_args(cx, &format_args, outermost_expn_data.call_site, macro_def_id); + check_uninlined_args(cx, &format_args, outermost_expn_data.call_site, macro_def_id, self.ignore_mixed); } } } @@ -270,7 +280,13 @@ fn check_unused_format_specifier(cx: &LateContext<'_>, arg: &FormatArg<'_>) { } } -fn check_uninlined_args(cx: &LateContext<'_>, args: &FormatArgsExpn<'_>, call_site: Span, def_id: DefId) { +fn check_uninlined_args( + cx: &LateContext<'_>, + args: &FormatArgsExpn<'_>, + call_site: Span, + def_id: DefId, + ignore_mixed: bool, +) { if args.format_string.span.from_expansion() { return; } @@ -285,7 +301,7 @@ fn check_uninlined_args(cx: &LateContext<'_>, args: &FormatArgsExpn<'_>, call_si // we cannot remove any other arguments in the format string, // because the index numbers might be wrong after inlining. // Example of an un-inlinable format: print!("{}{1}", foo, 2) - if !args.params().all(|p| check_one_arg(args, &p, &mut fixes)) || fixes.is_empty() { + if !args.params().all(|p| check_one_arg(args, &p, &mut fixes, ignore_mixed)) || fixes.is_empty() { return; } @@ -305,11 +321,23 @@ fn check_uninlined_args(cx: &LateContext<'_>, args: &FormatArgsExpn<'_>, call_si Applicability::MachineApplicable, if multiline_fix { CompletelyHidden } else { ShowCode }, ); + if ignore_mixed { + // Improve lint config discoverability + diag.note_once( + "this lint can also fix mixed format arg inlining if \ + `allow-mixed-uninlined-format-args = false` is set in the `clippy.toml` file", + ); + } }, ); } -fn check_one_arg(args: &FormatArgsExpn<'_>, param: &FormatParam<'_>, fixes: &mut Vec<(Span, String)>) -> bool { +fn check_one_arg( + args: &FormatArgsExpn<'_>, + param: &FormatParam<'_>, + fixes: &mut Vec<(Span, String)>, + ignore_mixed: bool, +) -> bool { if matches!(param.kind, Implicit | Starred | Named(_) | Numbered) && let ExprKind::Path(QPath::Resolved(None, path)) = param.value.kind && let [segment] = path.segments @@ -324,8 +352,10 @@ fn check_one_arg(args: &FormatArgsExpn<'_>, param: &FormatParam<'_>, fixes: &mut fixes.push((arg_span, String::new())); true // successful inlining, continue checking } else { - // if we can't inline a numbered argument, we can't continue - param.kind != Numbered + // Do not continue inlining (return false) in case + // * if we can't inline a numbered argument, e.g. `print!("{0} ...", foo.bar, ...)` + // * if allow_mixed_uninlined_format_args is false and this arg hasn't been inlined already + param.kind != Numbered && (!ignore_mixed || matches!(param.kind, NamedInline(_))) } } diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 17dbd983b6c07..3fe39488ab82c 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -828,7 +828,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: )) }); store.register_late_pass(move |_| Box::new(undocumented_unsafe_blocks::UndocumentedUnsafeBlocks)); - store.register_late_pass(move |_| Box::new(format_args::FormatArgs::new(msrv()))); + let allow_mixed_uninlined = conf.allow_mixed_uninlined_format_args; + store.register_late_pass(move |_| Box::new(format_args::FormatArgs::new(msrv(), allow_mixed_uninlined))); store.register_late_pass(|_| Box::new(trailing_empty_array::TrailingEmptyArray)); store.register_early_pass(|| Box::new(octal_escapes::OctalEscapes)); store.register_late_pass(|_| Box::new(needless_late_init::NeedlessLateInit)); diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index b37d4239477ea..b6dc8cd7ab119 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -402,6 +402,10 @@ define_Conf! { /// A list of paths to types that should be treated like `Arc`, i.e. ignored but /// for the generic parameters for determining interior mutability (ignore_interior_mutability: Vec = Vec::from(["bytes::Bytes".into()])), + /// Lint: UNINLINED_FORMAT_ARGS. + /// + /// Whether to allow mixed uninlined format args, e.g. `format!("{} {}", a, foo.bar)` + (allow_mixed_uninlined_format_args: bool = true), } /// Search for the configuration file. diff --git a/tests/ui-toml/allow_mixed_uninlined_format_args/auxiliary/proc_macro_with_span.rs b/tests/ui-toml/allow_mixed_uninlined_format_args/auxiliary/proc_macro_with_span.rs new file mode 100644 index 0000000000000..8ea631f2bbd42 --- /dev/null +++ b/tests/ui-toml/allow_mixed_uninlined_format_args/auxiliary/proc_macro_with_span.rs @@ -0,0 +1,32 @@ +// compile-flags: --emit=link +// no-prefer-dynamic + +#![crate_type = "proc-macro"] + +extern crate proc_macro; + +use proc_macro::{token_stream::IntoIter, Group, Span, TokenStream, TokenTree}; + +#[proc_macro] +pub fn with_span(input: TokenStream) -> TokenStream { + let mut iter = input.into_iter(); + let span = iter.next().unwrap().span(); + let mut res = TokenStream::new(); + write_with_span(span, iter, &mut res); + res +} + +fn write_with_span(s: Span, input: IntoIter, out: &mut TokenStream) { + for mut tt in input { + if let TokenTree::Group(g) = tt { + let mut stream = TokenStream::new(); + write_with_span(s, g.stream().into_iter(), &mut stream); + let mut group = Group::new(g.delimiter(), stream); + group.set_span(s); + out.extend([TokenTree::Group(group)]); + } else { + tt.set_span(s); + out.extend([tt]); + } + } +} diff --git a/tests/ui-toml/allow_mixed_uninlined_format_args/clippy.toml b/tests/ui-toml/allow_mixed_uninlined_format_args/clippy.toml new file mode 100644 index 0000000000000..b95e806aae24f --- /dev/null +++ b/tests/ui-toml/allow_mixed_uninlined_format_args/clippy.toml @@ -0,0 +1 @@ +allow-mixed-uninlined-format-args = false diff --git a/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.fixed b/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.fixed new file mode 100644 index 0000000000000..ca56c95c23f40 --- /dev/null +++ b/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.fixed @@ -0,0 +1,177 @@ +// aux-build:proc_macro_with_span.rs +// run-rustfix +#![feature(custom_inner_attributes)] +#![warn(clippy::uninlined_format_args)] +#![allow(named_arguments_used_positionally, unused_imports, unused_macros, unused_variables)] +#![allow(clippy::eq_op, clippy::format_in_format_args, clippy::print_literal)] + +extern crate proc_macro_with_span; +use proc_macro_with_span::with_span; + +macro_rules! no_param_str { + () => { + "{}" + }; +} + +macro_rules! my_println { + ($($args:tt),*) => {{ + println!($($args),*) + }}; +} + +macro_rules! my_println_args { + ($($args:tt),*) => {{ + println!("foo: {}", format_args!($($args),*)) + }}; +} + +fn tester(fn_arg: i32) { + let local_i32 = 1; + let local_f64 = 2.0; + let local_opt: Option = Some(3); + let width = 4; + let prec = 5; + let val = 6; + + // make sure this file hasn't been corrupted with tabs converted to spaces + // let _ = ' '; // <- this is a single tab character + let _: &[u8; 3] = b" "; // <- + + println!("val='{local_i32}'"); + println!("val='{local_i32}'"); // 3 spaces + println!("val='{local_i32}'"); // tab + println!("val='{local_i32}'"); // space+tab + println!("val='{local_i32}'"); // tab+space + println!( + "val='{local_i32}'" + ); + println!("{local_i32}"); + println!("{fn_arg}"); + println!("{local_i32:?}"); + println!("{local_i32:#?}"); + println!("{local_i32:4}"); + println!("{local_i32:04}"); + println!("{local_i32:<3}"); + println!("{local_i32:#010x}"); + println!("{local_f64:.1}"); + println!("Hello {} is {local_f64:.local_i32$}", "x"); + println!("Hello {local_i32} is {local_f64:.*}", 5); + println!("Hello {local_i32} is {local_f64:.*}", 5); + println!("{local_i32} {local_f64}"); + println!("{local_i32}, {}", local_opt.unwrap()); + println!("{val}"); + println!("{val}"); + println!("{} {1}", local_i32, 42); + println!("val='{local_i32}'"); + println!("val='{local_i32}'"); + println!("val='{local_i32}'"); + println!("val='{fn_arg}'"); + println!("{local_i32}"); + println!("{local_i32:?}"); + println!("{local_i32:#?}"); + println!("{local_i32:04}"); + println!("{local_i32:<3}"); + println!("{local_i32:#010x}"); + println!("{local_f64:.1}"); + println!("{local_i32} {local_i32}"); + println!("{local_f64} {local_i32} {local_i32} {local_f64}"); + println!("{local_i32} {local_f64}"); + println!("{local_f64} {local_i32}"); + println!("{local_f64} {local_i32} {local_f64} {local_i32}"); + println!("{1} {0}", "str", local_i32); + println!("{local_i32}"); + println!("{local_i32:width$}"); + println!("{local_i32:width$}"); + println!("{local_i32:.prec$}"); + println!("{local_i32:.prec$}"); + println!("{val:val$}"); + println!("{val:val$}"); + println!("{val:val$.val$}"); + println!("{val:val$.val$}"); + println!("{val:val$.val$}"); + println!("{val:val$.val$}"); + println!("{val:val$.val$}"); + println!("{val:val$.val$}"); + println!("{val:val$.val$}"); + println!("{val:val$.val$}"); + println!("{width:width$}"); + println!("{local_i32:width$}"); + println!("{width:width$}"); + println!("{local_i32:width$}"); + println!("{prec:.prec$}"); + println!("{local_i32:.prec$}"); + println!("{prec:.prec$}"); + println!("{local_i32:.prec$}"); + println!("{width:width$.prec$}"); + println!("{width:width$.prec$}"); + println!("{local_f64:width$.prec$}"); + println!("{local_f64:width$.prec$} {local_f64} {width} {prec}"); + println!( + "{local_i32:width$.prec$} {local_i32:prec$.width$} {width:local_i32$.prec$} {width:prec$.local_i32$} {prec:local_i32$.width$} {prec:width$.local_i32$}", + ); + println!( + "{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$} {3}", + local_i32, + width, + prec, + 1 + 2 + ); + println!("Width = {local_i32}, value with width = {local_f64:local_i32$}"); + println!("{local_i32:width$.prec$}"); + println!("{width:width$.prec$}"); + println!("{}", format!("{local_i32}")); + my_println!("{}", local_i32); + my_println_args!("{}", local_i32); + + // these should NOT be modified by the lint + println!(concat!("nope ", "{}"), local_i32); + println!("val='{local_i32}'"); + println!("val='{local_i32 }'"); + println!("val='{local_i32 }'"); // with tab + println!("val='{local_i32\n}'"); + println!("{}", usize::MAX); + println!("{}", local_opt.unwrap()); + println!( + "val='{local_i32 + }'" + ); + println!(no_param_str!(), local_i32); + + println!( + "{val}", + ); + println!("{val}"); + + println!(with_span!("{0} {1}" "{1} {0}"), local_i32, local_f64); + println!("{}", with_span!(span val)); + + if local_i32 > 0 { + panic!("p1 {local_i32}"); + } + if local_i32 > 0 { + panic!("p2 {local_i32}"); + } + if local_i32 > 0 { + panic!("p3 {local_i32}"); + } + if local_i32 > 0 { + panic!("p4 {local_i32}"); + } +} + +fn main() { + tester(42); +} + +fn _under_msrv() { + #![clippy::msrv = "1.57"] + let local_i32 = 1; + println!("don't expand='{}'", local_i32); +} + +fn _meets_msrv() { + #![clippy::msrv = "1.58"] + let local_i32 = 1; + println!("expand='{local_i32}'"); +} diff --git a/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.rs b/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.rs new file mode 100644 index 0000000000000..8e495ebd083a5 --- /dev/null +++ b/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.rs @@ -0,0 +1,182 @@ +// aux-build:proc_macro_with_span.rs +// run-rustfix +#![feature(custom_inner_attributes)] +#![warn(clippy::uninlined_format_args)] +#![allow(named_arguments_used_positionally, unused_imports, unused_macros, unused_variables)] +#![allow(clippy::eq_op, clippy::format_in_format_args, clippy::print_literal)] + +extern crate proc_macro_with_span; +use proc_macro_with_span::with_span; + +macro_rules! no_param_str { + () => { + "{}" + }; +} + +macro_rules! my_println { + ($($args:tt),*) => {{ + println!($($args),*) + }}; +} + +macro_rules! my_println_args { + ($($args:tt),*) => {{ + println!("foo: {}", format_args!($($args),*)) + }}; +} + +fn tester(fn_arg: i32) { + let local_i32 = 1; + let local_f64 = 2.0; + let local_opt: Option = Some(3); + let width = 4; + let prec = 5; + let val = 6; + + // make sure this file hasn't been corrupted with tabs converted to spaces + // let _ = ' '; // <- this is a single tab character + let _: &[u8; 3] = b" "; // <- + + println!("val='{}'", local_i32); + println!("val='{ }'", local_i32); // 3 spaces + println!("val='{ }'", local_i32); // tab + println!("val='{ }'", local_i32); // space+tab + println!("val='{ }'", local_i32); // tab+space + println!( + "val='{ + }'", + local_i32 + ); + println!("{}", local_i32); + println!("{}", fn_arg); + println!("{:?}", local_i32); + println!("{:#?}", local_i32); + println!("{:4}", local_i32); + println!("{:04}", local_i32); + println!("{:<3}", local_i32); + println!("{:#010x}", local_i32); + println!("{:.1}", local_f64); + println!("Hello {} is {:.*}", "x", local_i32, local_f64); + println!("Hello {} is {:.*}", local_i32, 5, local_f64); + println!("Hello {} is {2:.*}", local_i32, 5, local_f64); + println!("{} {}", local_i32, local_f64); + println!("{}, {}", local_i32, local_opt.unwrap()); + println!("{}", val); + println!("{}", v = val); + println!("{} {1}", local_i32, 42); + println!("val='{\t }'", local_i32); + println!("val='{\n }'", local_i32); + println!("val='{local_i32}'", local_i32 = local_i32); + println!("val='{local_i32}'", local_i32 = fn_arg); + println!("{0}", local_i32); + println!("{0:?}", local_i32); + println!("{0:#?}", local_i32); + println!("{0:04}", local_i32); + println!("{0:<3}", local_i32); + println!("{0:#010x}", local_i32); + println!("{0:.1}", local_f64); + println!("{0} {0}", local_i32); + println!("{1} {} {0} {}", local_i32, local_f64); + println!("{0} {1}", local_i32, local_f64); + println!("{1} {0}", local_i32, local_f64); + println!("{1} {0} {1} {0}", local_i32, local_f64); + println!("{1} {0}", "str", local_i32); + println!("{v}", v = local_i32); + println!("{local_i32:0$}", width); + println!("{local_i32:w$}", w = width); + println!("{local_i32:.0$}", prec); + println!("{local_i32:.p$}", p = prec); + println!("{:0$}", v = val); + println!("{0:0$}", v = val); + println!("{:0$.0$}", v = val); + println!("{0:0$.0$}", v = val); + println!("{0:0$.v$}", v = val); + println!("{0:v$.0$}", v = val); + println!("{v:0$.0$}", v = val); + println!("{v:v$.0$}", v = val); + println!("{v:0$.v$}", v = val); + println!("{v:v$.v$}", v = val); + println!("{:0$}", width); + println!("{:1$}", local_i32, width); + println!("{:w$}", w = width); + println!("{:w$}", local_i32, w = width); + println!("{:.0$}", prec); + println!("{:.1$}", local_i32, prec); + println!("{:.p$}", p = prec); + println!("{:.p$}", local_i32, p = prec); + println!("{:0$.1$}", width, prec); + println!("{:0$.w$}", width, w = prec); + println!("{:1$.2$}", local_f64, width, prec); + println!("{:1$.2$} {0} {1} {2}", local_f64, width, prec); + println!( + "{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$}", + local_i32, width, prec, + ); + println!( + "{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$} {3}", + local_i32, + width, + prec, + 1 + 2 + ); + println!("Width = {}, value with width = {:0$}", local_i32, local_f64); + println!("{:w$.p$}", local_i32, w = width, p = prec); + println!("{:w$.p$}", w = width, p = prec); + println!("{}", format!("{}", local_i32)); + my_println!("{}", local_i32); + my_println_args!("{}", local_i32); + + // these should NOT be modified by the lint + println!(concat!("nope ", "{}"), local_i32); + println!("val='{local_i32}'"); + println!("val='{local_i32 }'"); + println!("val='{local_i32 }'"); // with tab + println!("val='{local_i32\n}'"); + println!("{}", usize::MAX); + println!("{}", local_opt.unwrap()); + println!( + "val='{local_i32 + }'" + ); + println!(no_param_str!(), local_i32); + + println!( + "{}", + // comment with a comma , in it + val, + ); + println!("{}", /* comment with a comma , in it */ val); + + println!(with_span!("{0} {1}" "{1} {0}"), local_i32, local_f64); + println!("{}", with_span!(span val)); + + if local_i32 > 0 { + panic!("p1 {}", local_i32); + } + if local_i32 > 0 { + panic!("p2 {0}", local_i32); + } + if local_i32 > 0 { + panic!("p3 {local_i32}", local_i32 = local_i32); + } + if local_i32 > 0 { + panic!("p4 {local_i32}"); + } +} + +fn main() { + tester(42); +} + +fn _under_msrv() { + #![clippy::msrv = "1.57"] + let local_i32 = 1; + println!("don't expand='{}'", local_i32); +} + +fn _meets_msrv() { + #![clippy::msrv = "1.58"] + let local_i32 = 1; + println!("expand='{}'", local_i32); +} diff --git a/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.stderr b/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.stderr new file mode 100644 index 0000000000000..1182d57ce9b7e --- /dev/null +++ b/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.stderr @@ -0,0 +1,908 @@ +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:41:5 + | +LL | println!("val='{}'", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `-D clippy::uninlined-format-args` implied by `-D warnings` +help: change this to + | +LL - println!("val='{}'", local_i32); +LL + println!("val='{local_i32}'"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:42:5 + | +LL | println!("val='{ }'", local_i32); // 3 spaces + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("val='{ }'", local_i32); // 3 spaces +LL + println!("val='{local_i32}'"); // 3 spaces + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:43:5 + | +LL | println!("val='{ }'", local_i32); // tab + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("val='{ }'", local_i32); // tab +LL + println!("val='{local_i32}'"); // tab + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:44:5 + | +LL | println!("val='{ }'", local_i32); // space+tab + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("val='{ }'", local_i32); // space+tab +LL + println!("val='{local_i32}'"); // space+tab + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:45:5 + | +LL | println!("val='{ }'", local_i32); // tab+space + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("val='{ }'", local_i32); // tab+space +LL + println!("val='{local_i32}'"); // tab+space + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:46:5 + | +LL | / println!( +LL | | "val='{ +LL | | }'", +LL | | local_i32 +LL | | ); + | |_____^ + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:51:5 + | +LL | println!("{}", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{}", local_i32); +LL + println!("{local_i32}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:52:5 + | +LL | println!("{}", fn_arg); + | ^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{}", fn_arg); +LL + println!("{fn_arg}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:53:5 + | +LL | println!("{:?}", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:?}", local_i32); +LL + println!("{local_i32:?}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:54:5 + | +LL | println!("{:#?}", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:#?}", local_i32); +LL + println!("{local_i32:#?}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:55:5 + | +LL | println!("{:4}", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:4}", local_i32); +LL + println!("{local_i32:4}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:56:5 + | +LL | println!("{:04}", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:04}", local_i32); +LL + println!("{local_i32:04}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:57:5 + | +LL | println!("{:<3}", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:<3}", local_i32); +LL + println!("{local_i32:<3}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:58:5 + | +LL | println!("{:#010x}", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:#010x}", local_i32); +LL + println!("{local_i32:#010x}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:59:5 + | +LL | println!("{:.1}", local_f64); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:.1}", local_f64); +LL + println!("{local_f64:.1}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:60:5 + | +LL | println!("Hello {} is {:.*}", "x", local_i32, local_f64); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("Hello {} is {:.*}", "x", local_i32, local_f64); +LL + println!("Hello {} is {local_f64:.local_i32$}", "x"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:61:5 + | +LL | println!("Hello {} is {:.*}", local_i32, 5, local_f64); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("Hello {} is {:.*}", local_i32, 5, local_f64); +LL + println!("Hello {local_i32} is {local_f64:.*}", 5); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:62:5 + | +LL | println!("Hello {} is {2:.*}", local_i32, 5, local_f64); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("Hello {} is {2:.*}", local_i32, 5, local_f64); +LL + println!("Hello {local_i32} is {local_f64:.*}", 5); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:63:5 + | +LL | println!("{} {}", local_i32, local_f64); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{} {}", local_i32, local_f64); +LL + println!("{local_i32} {local_f64}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:64:5 + | +LL | println!("{}, {}", local_i32, local_opt.unwrap()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{}, {}", local_i32, local_opt.unwrap()); +LL + println!("{local_i32}, {}", local_opt.unwrap()); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:65:5 + | +LL | println!("{}", val); + | ^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{}", val); +LL + println!("{val}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:66:5 + | +LL | println!("{}", v = val); + | ^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{}", v = val); +LL + println!("{val}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:68:5 + | +LL | println!("val='{/t }'", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("val='{/t }'", local_i32); +LL + println!("val='{local_i32}'"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:69:5 + | +LL | println!("val='{/n }'", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("val='{/n }'", local_i32); +LL + println!("val='{local_i32}'"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:70:5 + | +LL | println!("val='{local_i32}'", local_i32 = local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("val='{local_i32}'", local_i32 = local_i32); +LL + println!("val='{local_i32}'"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:71:5 + | +LL | println!("val='{local_i32}'", local_i32 = fn_arg); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("val='{local_i32}'", local_i32 = fn_arg); +LL + println!("val='{fn_arg}'"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:72:5 + | +LL | println!("{0}", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{0}", local_i32); +LL + println!("{local_i32}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:73:5 + | +LL | println!("{0:?}", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{0:?}", local_i32); +LL + println!("{local_i32:?}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:74:5 + | +LL | println!("{0:#?}", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{0:#?}", local_i32); +LL + println!("{local_i32:#?}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:75:5 + | +LL | println!("{0:04}", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{0:04}", local_i32); +LL + println!("{local_i32:04}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:76:5 + | +LL | println!("{0:<3}", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{0:<3}", local_i32); +LL + println!("{local_i32:<3}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:77:5 + | +LL | println!("{0:#010x}", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{0:#010x}", local_i32); +LL + println!("{local_i32:#010x}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:78:5 + | +LL | println!("{0:.1}", local_f64); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{0:.1}", local_f64); +LL + println!("{local_f64:.1}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:79:5 + | +LL | println!("{0} {0}", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{0} {0}", local_i32); +LL + println!("{local_i32} {local_i32}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:80:5 + | +LL | println!("{1} {} {0} {}", local_i32, local_f64); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{1} {} {0} {}", local_i32, local_f64); +LL + println!("{local_f64} {local_i32} {local_i32} {local_f64}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:81:5 + | +LL | println!("{0} {1}", local_i32, local_f64); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{0} {1}", local_i32, local_f64); +LL + println!("{local_i32} {local_f64}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:82:5 + | +LL | println!("{1} {0}", local_i32, local_f64); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{1} {0}", local_i32, local_f64); +LL + println!("{local_f64} {local_i32}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:83:5 + | +LL | println!("{1} {0} {1} {0}", local_i32, local_f64); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{1} {0} {1} {0}", local_i32, local_f64); +LL + println!("{local_f64} {local_i32} {local_f64} {local_i32}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:85:5 + | +LL | println!("{v}", v = local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{v}", v = local_i32); +LL + println!("{local_i32}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:86:5 + | +LL | println!("{local_i32:0$}", width); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{local_i32:0$}", width); +LL + println!("{local_i32:width$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:87:5 + | +LL | println!("{local_i32:w$}", w = width); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{local_i32:w$}", w = width); +LL + println!("{local_i32:width$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:88:5 + | +LL | println!("{local_i32:.0$}", prec); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{local_i32:.0$}", prec); +LL + println!("{local_i32:.prec$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:89:5 + | +LL | println!("{local_i32:.p$}", p = prec); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{local_i32:.p$}", p = prec); +LL + println!("{local_i32:.prec$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:90:5 + | +LL | println!("{:0$}", v = val); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:0$}", v = val); +LL + println!("{val:val$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:91:5 + | +LL | println!("{0:0$}", v = val); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{0:0$}", v = val); +LL + println!("{val:val$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:92:5 + | +LL | println!("{:0$.0$}", v = val); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:0$.0$}", v = val); +LL + println!("{val:val$.val$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:93:5 + | +LL | println!("{0:0$.0$}", v = val); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{0:0$.0$}", v = val); +LL + println!("{val:val$.val$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:94:5 + | +LL | println!("{0:0$.v$}", v = val); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{0:0$.v$}", v = val); +LL + println!("{val:val$.val$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:95:5 + | +LL | println!("{0:v$.0$}", v = val); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{0:v$.0$}", v = val); +LL + println!("{val:val$.val$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:96:5 + | +LL | println!("{v:0$.0$}", v = val); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{v:0$.0$}", v = val); +LL + println!("{val:val$.val$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:97:5 + | +LL | println!("{v:v$.0$}", v = val); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{v:v$.0$}", v = val); +LL + println!("{val:val$.val$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:98:5 + | +LL | println!("{v:0$.v$}", v = val); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{v:0$.v$}", v = val); +LL + println!("{val:val$.val$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:99:5 + | +LL | println!("{v:v$.v$}", v = val); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{v:v$.v$}", v = val); +LL + println!("{val:val$.val$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:100:5 + | +LL | println!("{:0$}", width); + | ^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:0$}", width); +LL + println!("{width:width$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:101:5 + | +LL | println!("{:1$}", local_i32, width); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:1$}", local_i32, width); +LL + println!("{local_i32:width$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:102:5 + | +LL | println!("{:w$}", w = width); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:w$}", w = width); +LL + println!("{width:width$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:103:5 + | +LL | println!("{:w$}", local_i32, w = width); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:w$}", local_i32, w = width); +LL + println!("{local_i32:width$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:104:5 + | +LL | println!("{:.0$}", prec); + | ^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:.0$}", prec); +LL + println!("{prec:.prec$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:105:5 + | +LL | println!("{:.1$}", local_i32, prec); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:.1$}", local_i32, prec); +LL + println!("{local_i32:.prec$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:106:5 + | +LL | println!("{:.p$}", p = prec); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:.p$}", p = prec); +LL + println!("{prec:.prec$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:107:5 + | +LL | println!("{:.p$}", local_i32, p = prec); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:.p$}", local_i32, p = prec); +LL + println!("{local_i32:.prec$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:108:5 + | +LL | println!("{:0$.1$}", width, prec); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:0$.1$}", width, prec); +LL + println!("{width:width$.prec$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:109:5 + | +LL | println!("{:0$.w$}", width, w = prec); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:0$.w$}", width, w = prec); +LL + println!("{width:width$.prec$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:110:5 + | +LL | println!("{:1$.2$}", local_f64, width, prec); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:1$.2$}", local_f64, width, prec); +LL + println!("{local_f64:width$.prec$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:111:5 + | +LL | println!("{:1$.2$} {0} {1} {2}", local_f64, width, prec); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:1$.2$} {0} {1} {2}", local_f64, width, prec); +LL + println!("{local_f64:width$.prec$} {local_f64} {width} {prec}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:112:5 + | +LL | / println!( +LL | | "{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$}", +LL | | local_i32, width, prec, +LL | | ); + | |_____^ + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:123:5 + | +LL | println!("Width = {}, value with width = {:0$}", local_i32, local_f64); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("Width = {}, value with width = {:0$}", local_i32, local_f64); +LL + println!("Width = {local_i32}, value with width = {local_f64:local_i32$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:124:5 + | +LL | println!("{:w$.p$}", local_i32, w = width, p = prec); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:w$.p$}", local_i32, w = width, p = prec); +LL + println!("{local_i32:width$.prec$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:125:5 + | +LL | println!("{:w$.p$}", w = width, p = prec); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{:w$.p$}", w = width, p = prec); +LL + println!("{width:width$.prec$}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:126:20 + | +LL | println!("{}", format!("{}", local_i32)); + | ^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{}", format!("{}", local_i32)); +LL + println!("{}", format!("{local_i32}")); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:144:5 + | +LL | / println!( +LL | | "{}", +LL | | // comment with a comma , in it +LL | | val, +LL | | ); + | |_____^ + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:149:5 + | +LL | println!("{}", /* comment with a comma , in it */ val); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{}", /* comment with a comma , in it */ val); +LL + println!("{val}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:155:9 + | +LL | panic!("p1 {}", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - panic!("p1 {}", local_i32); +LL + panic!("p1 {local_i32}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:158:9 + | +LL | panic!("p2 {0}", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - panic!("p2 {0}", local_i32); +LL + panic!("p2 {local_i32}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:161:9 + | +LL | panic!("p3 {local_i32}", local_i32 = local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - panic!("p3 {local_i32}", local_i32 = local_i32); +LL + panic!("p3 {local_i32}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:181:5 + | +LL | println!("expand='{}'", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("expand='{}'", local_i32); +LL + println!("expand='{local_i32}'"); + | + +error: aborting due to 76 previous errors + diff --git a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr index f91d285c2e0ee..01a5e962c9491 100644 --- a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr +++ b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr @@ -1,6 +1,7 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of allow-dbg-in-tests allow-expect-in-tests + allow-mixed-uninlined-format-args allow-print-in-tests allow-unwrap-in-tests allowed-scripts diff --git a/tests/ui/uninlined_format_args.fixed b/tests/ui/uninlined_format_args.fixed index 46ec7771114f1..9d08e80cf9a55 100644 --- a/tests/ui/uninlined_format_args.fixed +++ b/tests/ui/uninlined_format_args.fixed @@ -54,11 +54,11 @@ fn tester(fn_arg: i32) { println!("{local_i32:<3}"); println!("{local_i32:#010x}"); println!("{local_f64:.1}"); - println!("Hello {} is {local_f64:.local_i32$}", "x"); - println!("Hello {local_i32} is {local_f64:.*}", 5); - println!("Hello {local_i32} is {local_f64:.*}", 5); + println!("Hello {} is {:.*}", "x", local_i32, local_f64); + println!("Hello {} is {:.*}", local_i32, 5, local_f64); + println!("Hello {} is {2:.*}", local_i32, 5, local_f64); println!("{local_i32} {local_f64}"); - println!("{local_i32}, {}", local_opt.unwrap()); + println!("{}, {}", local_i32, local_opt.unwrap()); println!("{val}"); println!("{val}"); println!("{} {1}", local_i32, 42); diff --git a/tests/ui/uninlined_format_args_panic.edition2018.stderr b/tests/ui/uninlined_format_args_panic.edition2018.stderr index 2c8061259229a..1afdb4d0fbab4 100644 --- a/tests/ui/uninlined_format_args_panic.edition2018.stderr +++ b/tests/ui/uninlined_format_args_panic.edition2018.stderr @@ -4,6 +4,7 @@ error: variables can be used directly in the `format!` string LL | println!("val='{}'", var); | ^^^^^^^^^^^^^^^^^^^^^^^^^ | + = note: this lint can also fix mixed format arg inlining if `allow-mixed-uninlined-format-args = false` is set in the `clippy.toml` file = note: `-D clippy::uninlined-format-args` implied by `-D warnings` help: change this to | diff --git a/tests/ui/uninlined_format_args_panic.edition2021.stderr b/tests/ui/uninlined_format_args_panic.edition2021.stderr index 0f09c45f41324..a38ea4168bc9c 100644 --- a/tests/ui/uninlined_format_args_panic.edition2021.stderr +++ b/tests/ui/uninlined_format_args_panic.edition2021.stderr @@ -4,6 +4,7 @@ error: variables can be used directly in the `format!` string LL | println!("val='{}'", var); | ^^^^^^^^^^^^^^^^^^^^^^^^^ | + = note: this lint can also fix mixed format arg inlining if `allow-mixed-uninlined-format-args = false` is set in the `clippy.toml` file = note: `-D clippy::uninlined-format-args` implied by `-D warnings` help: change this to | From 13dfbb2fa526afc4d425459868d7951e248ba65c Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 24 Oct 2022 16:33:53 -0700 Subject: [PATCH 120/244] Migrate from highfive to triagebot --- triagebot.toml | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/triagebot.toml b/triagebot.toml index 80c30393832c1..c615d18f8687e 100644 --- a/triagebot.toml +++ b/triagebot.toml @@ -4,9 +4,23 @@ allow-unauthenticated = [ "good-first-issue" ] -[assign] - # Allows shortcuts like `@rustbot ready` # # See https://github.com/rust-lang/triagebot/wiki/Shortcuts [shortcut] + +[assign] +contributing_url = "https://github.com/rust-lang/rust-clippy/blob/master/CONTRIBUTING.md" + +[assign.owners] +"/.github" = ["@flip1995"] +"*" = [ + "@flip1995", + "@Manishearth", + "@llogiq", + "@giraffate", + "@xFrednet", + "@Alexendoo", + "@dswij", + "@Jarcho", +] From 5856e408859e37c4a2173ad4f9f146b0792b5859 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 27 Nov 2022 16:31:34 +0100 Subject: [PATCH 121/244] Remove Crate::primitives field --- src/librustdoc/clean/types.rs | 3 +-- src/librustdoc/clean/utils.rs | 2 +- src/librustdoc/passes/collect_trait_impls.rs | 6 ++++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 340d6d7df180f..2894b19877cc7 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -115,7 +115,6 @@ impl From for ItemId { #[derive(Clone, Debug)] pub(crate) struct Crate { pub(crate) module: Item, - pub(crate) primitives: ThinVec<(DefId, PrimitiveType)>, /// Only here so that they can be filtered through the rustdoc passes. pub(crate) external_traits: Rc>>, } @@ -2572,7 +2571,7 @@ mod size_asserts { use super::*; use rustc_data_structures::static_assert_size; // tidy-alphabetical-start - static_assert_size!(Crate, 72); // frequently moved by-value + static_assert_size!(Crate, 64); // frequently moved by-value static_assert_size!(DocFragment, 32); static_assert_size!(GenericArg, 32); static_assert_size!(GenericArgs, 32); diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 35e2720fdff89..3c48dd80b6fc5 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -73,7 +73,7 @@ pub(crate) fn krate(cx: &mut DocContext<'_>) -> Crate { })); } - Crate { module, primitives, external_traits: cx.external_traits.clone() } + Crate { module, external_traits: cx.external_traits.clone() } } pub(crate) fn substs_to_args<'tcx>( diff --git a/src/librustdoc/passes/collect_trait_impls.rs b/src/librustdoc/passes/collect_trait_impls.rs index 6b699c7901434..d57f981d51a84 100644 --- a/src/librustdoc/passes/collect_trait_impls.rs +++ b/src/librustdoc/passes/collect_trait_impls.rs @@ -8,7 +8,7 @@ use crate::formats::cache::Cache; use crate::visit::DocVisitor; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::{DefId, LOCAL_CRATE}; use rustc_middle::ty::{self, DefIdTree}; use rustc_span::symbol::sym; @@ -25,7 +25,9 @@ pub(crate) fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) -> synth.impls }); - let prims: FxHashSet = krate.primitives.iter().map(|p| p.1).collect(); + let local_crate = ExternalCrate { crate_num: LOCAL_CRATE }; + let prims: FxHashSet = + local_crate.primitives(cx.tcx).iter().map(|p| p.1).collect(); let crate_items = { let mut coll = ItemCollector::new(); From 23c3941942ef1ea3cf2980e385235463add95460 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 27 Nov 2022 16:31:52 +0100 Subject: [PATCH 122/244] Add rustdoc test for Deref to primitive types --- src/test/rustdoc/deref-to-primitive.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/test/rustdoc/deref-to-primitive.rs diff --git a/src/test/rustdoc/deref-to-primitive.rs b/src/test/rustdoc/deref-to-primitive.rs new file mode 100644 index 0000000000000..527de780d4892 --- /dev/null +++ b/src/test/rustdoc/deref-to-primitive.rs @@ -0,0 +1,15 @@ +#![crate_name = "foo"] + +// @has 'foo/struct.Foo.html' +// @has - '//*[@id="deref-methods-i32"]' 'Methods from Deref' +// @has - '//*[@id="deref-methods-i32-1"]//*[@id="associatedconstant.BITS"]/h4' \ +// 'pub const BITS: u32 = 32u32' +pub struct Foo(i32); + +impl std::ops::Deref for Foo { + type Target = i32; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} From ab576afc18e0b15167388c1a294587962a94d264 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Sun, 27 Nov 2022 10:34:13 -0500 Subject: [PATCH 123/244] addressed review feedback --- clippy_lints/src/format_args.rs | 7 - .../auxiliary/proc_macro_with_span.rs | 32 - .../uninlined_format_args.fixed | 167 +--- .../uninlined_format_args.rs | 170 +--- .../uninlined_format_args.stderr | 860 +----------------- tests/ui/uninlined_format_args.stderr | 50 +- ...lined_format_args_panic.edition2018.stderr | 1 - ...lined_format_args_panic.edition2021.stderr | 1 - 8 files changed, 18 insertions(+), 1270 deletions(-) delete mode 100644 tests/ui-toml/allow_mixed_uninlined_format_args/auxiliary/proc_macro_with_span.rs diff --git a/clippy_lints/src/format_args.rs b/clippy_lints/src/format_args.rs index 40a37d39ecf84..f0995a81329d9 100644 --- a/clippy_lints/src/format_args.rs +++ b/clippy_lints/src/format_args.rs @@ -321,13 +321,6 @@ fn check_uninlined_args( Applicability::MachineApplicable, if multiline_fix { CompletelyHidden } else { ShowCode }, ); - if ignore_mixed { - // Improve lint config discoverability - diag.note_once( - "this lint can also fix mixed format arg inlining if \ - `allow-mixed-uninlined-format-args = false` is set in the `clippy.toml` file", - ); - } }, ); } diff --git a/tests/ui-toml/allow_mixed_uninlined_format_args/auxiliary/proc_macro_with_span.rs b/tests/ui-toml/allow_mixed_uninlined_format_args/auxiliary/proc_macro_with_span.rs deleted file mode 100644 index 8ea631f2bbd42..0000000000000 --- a/tests/ui-toml/allow_mixed_uninlined_format_args/auxiliary/proc_macro_with_span.rs +++ /dev/null @@ -1,32 +0,0 @@ -// compile-flags: --emit=link -// no-prefer-dynamic - -#![crate_type = "proc-macro"] - -extern crate proc_macro; - -use proc_macro::{token_stream::IntoIter, Group, Span, TokenStream, TokenTree}; - -#[proc_macro] -pub fn with_span(input: TokenStream) -> TokenStream { - let mut iter = input.into_iter(); - let span = iter.next().unwrap().span(); - let mut res = TokenStream::new(); - write_with_span(span, iter, &mut res); - res -} - -fn write_with_span(s: Span, input: IntoIter, out: &mut TokenStream) { - for mut tt in input { - if let TokenTree::Group(g) = tt { - let mut stream = TokenStream::new(); - write_with_span(s, g.stream().into_iter(), &mut stream); - let mut group = Group::new(g.delimiter(), stream); - group.set_span(s); - out.extend([TokenTree::Group(group)]); - } else { - tt.set_span(s); - out.extend([tt]); - } - } -} diff --git a/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.fixed b/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.fixed index ca56c95c23f40..aa8b45b5fe7d4 100644 --- a/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.fixed +++ b/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.fixed @@ -1,177 +1,14 @@ -// aux-build:proc_macro_with_span.rs // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::uninlined_format_args)] -#![allow(named_arguments_used_positionally, unused_imports, unused_macros, unused_variables)] -#![allow(clippy::eq_op, clippy::format_in_format_args, clippy::print_literal)] -extern crate proc_macro_with_span; -use proc_macro_with_span::with_span; - -macro_rules! no_param_str { - () => { - "{}" - }; -} - -macro_rules! my_println { - ($($args:tt),*) => {{ - println!($($args),*) - }}; -} - -macro_rules! my_println_args { - ($($args:tt),*) => {{ - println!("foo: {}", format_args!($($args),*)) - }}; -} - -fn tester(fn_arg: i32) { +fn main() { let local_i32 = 1; let local_f64 = 2.0; let local_opt: Option = Some(3); - let width = 4; - let prec = 5; - let val = 6; - - // make sure this file hasn't been corrupted with tabs converted to spaces - // let _ = ' '; // <- this is a single tab character - let _: &[u8; 3] = b" "; // <- println!("val='{local_i32}'"); - println!("val='{local_i32}'"); // 3 spaces - println!("val='{local_i32}'"); // tab - println!("val='{local_i32}'"); // space+tab - println!("val='{local_i32}'"); // tab+space - println!( - "val='{local_i32}'" - ); - println!("{local_i32}"); - println!("{fn_arg}"); - println!("{local_i32:?}"); - println!("{local_i32:#?}"); - println!("{local_i32:4}"); - println!("{local_i32:04}"); - println!("{local_i32:<3}"); - println!("{local_i32:#010x}"); - println!("{local_f64:.1}"); - println!("Hello {} is {local_f64:.local_i32$}", "x"); + println!("Hello x is {local_f64:.local_i32$}"); println!("Hello {local_i32} is {local_f64:.*}", 5); println!("Hello {local_i32} is {local_f64:.*}", 5); - println!("{local_i32} {local_f64}"); println!("{local_i32}, {}", local_opt.unwrap()); - println!("{val}"); - println!("{val}"); - println!("{} {1}", local_i32, 42); - println!("val='{local_i32}'"); - println!("val='{local_i32}'"); - println!("val='{local_i32}'"); - println!("val='{fn_arg}'"); - println!("{local_i32}"); - println!("{local_i32:?}"); - println!("{local_i32:#?}"); - println!("{local_i32:04}"); - println!("{local_i32:<3}"); - println!("{local_i32:#010x}"); - println!("{local_f64:.1}"); - println!("{local_i32} {local_i32}"); - println!("{local_f64} {local_i32} {local_i32} {local_f64}"); - println!("{local_i32} {local_f64}"); - println!("{local_f64} {local_i32}"); - println!("{local_f64} {local_i32} {local_f64} {local_i32}"); - println!("{1} {0}", "str", local_i32); - println!("{local_i32}"); - println!("{local_i32:width$}"); - println!("{local_i32:width$}"); - println!("{local_i32:.prec$}"); - println!("{local_i32:.prec$}"); - println!("{val:val$}"); - println!("{val:val$}"); - println!("{val:val$.val$}"); - println!("{val:val$.val$}"); - println!("{val:val$.val$}"); - println!("{val:val$.val$}"); - println!("{val:val$.val$}"); - println!("{val:val$.val$}"); - println!("{val:val$.val$}"); - println!("{val:val$.val$}"); - println!("{width:width$}"); - println!("{local_i32:width$}"); - println!("{width:width$}"); - println!("{local_i32:width$}"); - println!("{prec:.prec$}"); - println!("{local_i32:.prec$}"); - println!("{prec:.prec$}"); - println!("{local_i32:.prec$}"); - println!("{width:width$.prec$}"); - println!("{width:width$.prec$}"); - println!("{local_f64:width$.prec$}"); - println!("{local_f64:width$.prec$} {local_f64} {width} {prec}"); - println!( - "{local_i32:width$.prec$} {local_i32:prec$.width$} {width:local_i32$.prec$} {width:prec$.local_i32$} {prec:local_i32$.width$} {prec:width$.local_i32$}", - ); - println!( - "{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$} {3}", - local_i32, - width, - prec, - 1 + 2 - ); - println!("Width = {local_i32}, value with width = {local_f64:local_i32$}"); - println!("{local_i32:width$.prec$}"); - println!("{width:width$.prec$}"); - println!("{}", format!("{local_i32}")); - my_println!("{}", local_i32); - my_println_args!("{}", local_i32); - - // these should NOT be modified by the lint - println!(concat!("nope ", "{}"), local_i32); - println!("val='{local_i32}'"); - println!("val='{local_i32 }'"); - println!("val='{local_i32 }'"); // with tab - println!("val='{local_i32\n}'"); - println!("{}", usize::MAX); - println!("{}", local_opt.unwrap()); - println!( - "val='{local_i32 - }'" - ); - println!(no_param_str!(), local_i32); - - println!( - "{val}", - ); - println!("{val}"); - - println!(with_span!("{0} {1}" "{1} {0}"), local_i32, local_f64); - println!("{}", with_span!(span val)); - - if local_i32 > 0 { - panic!("p1 {local_i32}"); - } - if local_i32 > 0 { - panic!("p2 {local_i32}"); - } - if local_i32 > 0 { - panic!("p3 {local_i32}"); - } - if local_i32 > 0 { - panic!("p4 {local_i32}"); - } -} - -fn main() { - tester(42); -} - -fn _under_msrv() { - #![clippy::msrv = "1.57"] - let local_i32 = 1; - println!("don't expand='{}'", local_i32); -} - -fn _meets_msrv() { - #![clippy::msrv = "1.58"] - let local_i32 = 1; - println!("expand='{local_i32}'"); } diff --git a/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.rs b/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.rs index 8e495ebd083a5..ad2e4863ee8ed 100644 --- a/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.rs +++ b/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.rs @@ -1,182 +1,14 @@ -// aux-build:proc_macro_with_span.rs // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::uninlined_format_args)] -#![allow(named_arguments_used_positionally, unused_imports, unused_macros, unused_variables)] -#![allow(clippy::eq_op, clippy::format_in_format_args, clippy::print_literal)] -extern crate proc_macro_with_span; -use proc_macro_with_span::with_span; - -macro_rules! no_param_str { - () => { - "{}" - }; -} - -macro_rules! my_println { - ($($args:tt),*) => {{ - println!($($args),*) - }}; -} - -macro_rules! my_println_args { - ($($args:tt),*) => {{ - println!("foo: {}", format_args!($($args),*)) - }}; -} - -fn tester(fn_arg: i32) { +fn main() { let local_i32 = 1; let local_f64 = 2.0; let local_opt: Option = Some(3); - let width = 4; - let prec = 5; - let val = 6; - - // make sure this file hasn't been corrupted with tabs converted to spaces - // let _ = ' '; // <- this is a single tab character - let _: &[u8; 3] = b" "; // <- println!("val='{}'", local_i32); - println!("val='{ }'", local_i32); // 3 spaces - println!("val='{ }'", local_i32); // tab - println!("val='{ }'", local_i32); // space+tab - println!("val='{ }'", local_i32); // tab+space - println!( - "val='{ - }'", - local_i32 - ); - println!("{}", local_i32); - println!("{}", fn_arg); - println!("{:?}", local_i32); - println!("{:#?}", local_i32); - println!("{:4}", local_i32); - println!("{:04}", local_i32); - println!("{:<3}", local_i32); - println!("{:#010x}", local_i32); - println!("{:.1}", local_f64); println!("Hello {} is {:.*}", "x", local_i32, local_f64); println!("Hello {} is {:.*}", local_i32, 5, local_f64); println!("Hello {} is {2:.*}", local_i32, 5, local_f64); - println!("{} {}", local_i32, local_f64); println!("{}, {}", local_i32, local_opt.unwrap()); - println!("{}", val); - println!("{}", v = val); - println!("{} {1}", local_i32, 42); - println!("val='{\t }'", local_i32); - println!("val='{\n }'", local_i32); - println!("val='{local_i32}'", local_i32 = local_i32); - println!("val='{local_i32}'", local_i32 = fn_arg); - println!("{0}", local_i32); - println!("{0:?}", local_i32); - println!("{0:#?}", local_i32); - println!("{0:04}", local_i32); - println!("{0:<3}", local_i32); - println!("{0:#010x}", local_i32); - println!("{0:.1}", local_f64); - println!("{0} {0}", local_i32); - println!("{1} {} {0} {}", local_i32, local_f64); - println!("{0} {1}", local_i32, local_f64); - println!("{1} {0}", local_i32, local_f64); - println!("{1} {0} {1} {0}", local_i32, local_f64); - println!("{1} {0}", "str", local_i32); - println!("{v}", v = local_i32); - println!("{local_i32:0$}", width); - println!("{local_i32:w$}", w = width); - println!("{local_i32:.0$}", prec); - println!("{local_i32:.p$}", p = prec); - println!("{:0$}", v = val); - println!("{0:0$}", v = val); - println!("{:0$.0$}", v = val); - println!("{0:0$.0$}", v = val); - println!("{0:0$.v$}", v = val); - println!("{0:v$.0$}", v = val); - println!("{v:0$.0$}", v = val); - println!("{v:v$.0$}", v = val); - println!("{v:0$.v$}", v = val); - println!("{v:v$.v$}", v = val); - println!("{:0$}", width); - println!("{:1$}", local_i32, width); - println!("{:w$}", w = width); - println!("{:w$}", local_i32, w = width); - println!("{:.0$}", prec); - println!("{:.1$}", local_i32, prec); - println!("{:.p$}", p = prec); - println!("{:.p$}", local_i32, p = prec); - println!("{:0$.1$}", width, prec); - println!("{:0$.w$}", width, w = prec); - println!("{:1$.2$}", local_f64, width, prec); - println!("{:1$.2$} {0} {1} {2}", local_f64, width, prec); - println!( - "{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$}", - local_i32, width, prec, - ); - println!( - "{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$} {3}", - local_i32, - width, - prec, - 1 + 2 - ); - println!("Width = {}, value with width = {:0$}", local_i32, local_f64); - println!("{:w$.p$}", local_i32, w = width, p = prec); - println!("{:w$.p$}", w = width, p = prec); - println!("{}", format!("{}", local_i32)); - my_println!("{}", local_i32); - my_println_args!("{}", local_i32); - - // these should NOT be modified by the lint - println!(concat!("nope ", "{}"), local_i32); - println!("val='{local_i32}'"); - println!("val='{local_i32 }'"); - println!("val='{local_i32 }'"); // with tab - println!("val='{local_i32\n}'"); - println!("{}", usize::MAX); - println!("{}", local_opt.unwrap()); - println!( - "val='{local_i32 - }'" - ); - println!(no_param_str!(), local_i32); - - println!( - "{}", - // comment with a comma , in it - val, - ); - println!("{}", /* comment with a comma , in it */ val); - - println!(with_span!("{0} {1}" "{1} {0}"), local_i32, local_f64); - println!("{}", with_span!(span val)); - - if local_i32 > 0 { - panic!("p1 {}", local_i32); - } - if local_i32 > 0 { - panic!("p2 {0}", local_i32); - } - if local_i32 > 0 { - panic!("p3 {local_i32}", local_i32 = local_i32); - } - if local_i32 > 0 { - panic!("p4 {local_i32}"); - } -} - -fn main() { - tester(42); -} - -fn _under_msrv() { - #![clippy::msrv = "1.57"] - let local_i32 = 1; - println!("don't expand='{}'", local_i32); -} - -fn _meets_msrv() { - #![clippy::msrv = "1.58"] - let local_i32 = 1; - println!("expand='{}'", local_i32); } diff --git a/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.stderr b/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.stderr index 1182d57ce9b7e..ee94176219615 100644 --- a/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.stderr +++ b/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.stderr @@ -1,5 +1,5 @@ error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:41:5 + --> $DIR/uninlined_format_args.rs:9:5 | LL | println!("val='{}'", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -11,174 +11,21 @@ LL - println!("val='{}'", local_i32); LL + println!("val='{local_i32}'"); | -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:42:5 - | -LL | println!("val='{ }'", local_i32); // 3 spaces - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("val='{ }'", local_i32); // 3 spaces -LL + println!("val='{local_i32}'"); // 3 spaces - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:43:5 - | -LL | println!("val='{ }'", local_i32); // tab - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("val='{ }'", local_i32); // tab -LL + println!("val='{local_i32}'"); // tab - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:44:5 - | -LL | println!("val='{ }'", local_i32); // space+tab - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("val='{ }'", local_i32); // space+tab -LL + println!("val='{local_i32}'"); // space+tab - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:45:5 - | -LL | println!("val='{ }'", local_i32); // tab+space - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("val='{ }'", local_i32); // tab+space -LL + println!("val='{local_i32}'"); // tab+space - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:46:5 - | -LL | / println!( -LL | | "val='{ -LL | | }'", -LL | | local_i32 -LL | | ); - | |_____^ - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:51:5 - | -LL | println!("{}", local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{}", local_i32); -LL + println!("{local_i32}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:52:5 - | -LL | println!("{}", fn_arg); - | ^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{}", fn_arg); -LL + println!("{fn_arg}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:53:5 - | -LL | println!("{:?}", local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ +error: literal with an empty format string + --> $DIR/uninlined_format_args.rs:10:35 | -help: change this to - | -LL - println!("{:?}", local_i32); -LL + println!("{local_i32:?}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:54:5 - | -LL | println!("{:#?}", local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:#?}", local_i32); -LL + println!("{local_i32:#?}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:55:5 - | -LL | println!("{:4}", local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:4}", local_i32); -LL + println!("{local_i32:4}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:56:5 - | -LL | println!("{:04}", local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:04}", local_i32); -LL + println!("{local_i32:04}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:57:5 - | -LL | println!("{:<3}", local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:<3}", local_i32); -LL + println!("{local_i32:<3}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:58:5 - | -LL | println!("{:#010x}", local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:#010x}", local_i32); -LL + println!("{local_i32:#010x}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:59:5 +LL | println!("Hello {} is {:.*}", "x", local_i32, local_f64); + | ^^^ | -LL | println!("{:.1}", local_f64); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: `-D clippy::print-literal` implied by `-D warnings` +help: try this | -help: change this to - | -LL - println!("{:.1}", local_f64); -LL + println!("{local_f64:.1}"); +LL - println!("Hello {} is {:.*}", "x", local_i32, local_f64); +LL + println!("Hello x is {:.*}", local_i32, local_f64); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:60:5 + --> $DIR/uninlined_format_args.rs:10:5 | LL | println!("Hello {} is {:.*}", "x", local_i32, local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -190,7 +37,7 @@ LL + println!("Hello {} is {local_f64:.local_i32$}", "x"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:61:5 + --> $DIR/uninlined_format_args.rs:11:5 | LL | println!("Hello {} is {:.*}", local_i32, 5, local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -202,7 +49,7 @@ LL + println!("Hello {local_i32} is {local_f64:.*}", 5); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:62:5 + --> $DIR/uninlined_format_args.rs:12:5 | LL | println!("Hello {} is {2:.*}", local_i32, 5, local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -214,19 +61,7 @@ LL + println!("Hello {local_i32} is {local_f64:.*}", 5); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:63:5 - | -LL | println!("{} {}", local_i32, local_f64); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{} {}", local_i32, local_f64); -LL + println!("{local_i32} {local_f64}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:64:5 + --> $DIR/uninlined_format_args.rs:13:5 | LL | println!("{}, {}", local_i32, local_opt.unwrap()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -237,672 +72,5 @@ LL - println!("{}, {}", local_i32, local_opt.unwrap()); LL + println!("{local_i32}, {}", local_opt.unwrap()); | -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:65:5 - | -LL | println!("{}", val); - | ^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{}", val); -LL + println!("{val}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:66:5 - | -LL | println!("{}", v = val); - | ^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{}", v = val); -LL + println!("{val}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:68:5 - | -LL | println!("val='{/t }'", local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("val='{/t }'", local_i32); -LL + println!("val='{local_i32}'"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:69:5 - | -LL | println!("val='{/n }'", local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("val='{/n }'", local_i32); -LL + println!("val='{local_i32}'"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:70:5 - | -LL | println!("val='{local_i32}'", local_i32 = local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("val='{local_i32}'", local_i32 = local_i32); -LL + println!("val='{local_i32}'"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:71:5 - | -LL | println!("val='{local_i32}'", local_i32 = fn_arg); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("val='{local_i32}'", local_i32 = fn_arg); -LL + println!("val='{fn_arg}'"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:72:5 - | -LL | println!("{0}", local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{0}", local_i32); -LL + println!("{local_i32}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:73:5 - | -LL | println!("{0:?}", local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{0:?}", local_i32); -LL + println!("{local_i32:?}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:74:5 - | -LL | println!("{0:#?}", local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{0:#?}", local_i32); -LL + println!("{local_i32:#?}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:75:5 - | -LL | println!("{0:04}", local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{0:04}", local_i32); -LL + println!("{local_i32:04}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:76:5 - | -LL | println!("{0:<3}", local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{0:<3}", local_i32); -LL + println!("{local_i32:<3}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:77:5 - | -LL | println!("{0:#010x}", local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{0:#010x}", local_i32); -LL + println!("{local_i32:#010x}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:78:5 - | -LL | println!("{0:.1}", local_f64); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{0:.1}", local_f64); -LL + println!("{local_f64:.1}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:79:5 - | -LL | println!("{0} {0}", local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{0} {0}", local_i32); -LL + println!("{local_i32} {local_i32}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:80:5 - | -LL | println!("{1} {} {0} {}", local_i32, local_f64); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{1} {} {0} {}", local_i32, local_f64); -LL + println!("{local_f64} {local_i32} {local_i32} {local_f64}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:81:5 - | -LL | println!("{0} {1}", local_i32, local_f64); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{0} {1}", local_i32, local_f64); -LL + println!("{local_i32} {local_f64}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:82:5 - | -LL | println!("{1} {0}", local_i32, local_f64); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{1} {0}", local_i32, local_f64); -LL + println!("{local_f64} {local_i32}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:83:5 - | -LL | println!("{1} {0} {1} {0}", local_i32, local_f64); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{1} {0} {1} {0}", local_i32, local_f64); -LL + println!("{local_f64} {local_i32} {local_f64} {local_i32}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:85:5 - | -LL | println!("{v}", v = local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{v}", v = local_i32); -LL + println!("{local_i32}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:86:5 - | -LL | println!("{local_i32:0$}", width); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{local_i32:0$}", width); -LL + println!("{local_i32:width$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:87:5 - | -LL | println!("{local_i32:w$}", w = width); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{local_i32:w$}", w = width); -LL + println!("{local_i32:width$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:88:5 - | -LL | println!("{local_i32:.0$}", prec); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{local_i32:.0$}", prec); -LL + println!("{local_i32:.prec$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:89:5 - | -LL | println!("{local_i32:.p$}", p = prec); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{local_i32:.p$}", p = prec); -LL + println!("{local_i32:.prec$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:90:5 - | -LL | println!("{:0$}", v = val); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:0$}", v = val); -LL + println!("{val:val$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:91:5 - | -LL | println!("{0:0$}", v = val); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{0:0$}", v = val); -LL + println!("{val:val$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:92:5 - | -LL | println!("{:0$.0$}", v = val); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:0$.0$}", v = val); -LL + println!("{val:val$.val$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:93:5 - | -LL | println!("{0:0$.0$}", v = val); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{0:0$.0$}", v = val); -LL + println!("{val:val$.val$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:94:5 - | -LL | println!("{0:0$.v$}", v = val); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{0:0$.v$}", v = val); -LL + println!("{val:val$.val$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:95:5 - | -LL | println!("{0:v$.0$}", v = val); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{0:v$.0$}", v = val); -LL + println!("{val:val$.val$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:96:5 - | -LL | println!("{v:0$.0$}", v = val); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{v:0$.0$}", v = val); -LL + println!("{val:val$.val$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:97:5 - | -LL | println!("{v:v$.0$}", v = val); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{v:v$.0$}", v = val); -LL + println!("{val:val$.val$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:98:5 - | -LL | println!("{v:0$.v$}", v = val); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{v:0$.v$}", v = val); -LL + println!("{val:val$.val$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:99:5 - | -LL | println!("{v:v$.v$}", v = val); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{v:v$.v$}", v = val); -LL + println!("{val:val$.val$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:100:5 - | -LL | println!("{:0$}", width); - | ^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:0$}", width); -LL + println!("{width:width$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:101:5 - | -LL | println!("{:1$}", local_i32, width); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:1$}", local_i32, width); -LL + println!("{local_i32:width$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:102:5 - | -LL | println!("{:w$}", w = width); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:w$}", w = width); -LL + println!("{width:width$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:103:5 - | -LL | println!("{:w$}", local_i32, w = width); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:w$}", local_i32, w = width); -LL + println!("{local_i32:width$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:104:5 - | -LL | println!("{:.0$}", prec); - | ^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:.0$}", prec); -LL + println!("{prec:.prec$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:105:5 - | -LL | println!("{:.1$}", local_i32, prec); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:.1$}", local_i32, prec); -LL + println!("{local_i32:.prec$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:106:5 - | -LL | println!("{:.p$}", p = prec); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:.p$}", p = prec); -LL + println!("{prec:.prec$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:107:5 - | -LL | println!("{:.p$}", local_i32, p = prec); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:.p$}", local_i32, p = prec); -LL + println!("{local_i32:.prec$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:108:5 - | -LL | println!("{:0$.1$}", width, prec); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:0$.1$}", width, prec); -LL + println!("{width:width$.prec$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:109:5 - | -LL | println!("{:0$.w$}", width, w = prec); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:0$.w$}", width, w = prec); -LL + println!("{width:width$.prec$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:110:5 - | -LL | println!("{:1$.2$}", local_f64, width, prec); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:1$.2$}", local_f64, width, prec); -LL + println!("{local_f64:width$.prec$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:111:5 - | -LL | println!("{:1$.2$} {0} {1} {2}", local_f64, width, prec); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:1$.2$} {0} {1} {2}", local_f64, width, prec); -LL + println!("{local_f64:width$.prec$} {local_f64} {width} {prec}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:112:5 - | -LL | / println!( -LL | | "{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$}", -LL | | local_i32, width, prec, -LL | | ); - | |_____^ - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:123:5 - | -LL | println!("Width = {}, value with width = {:0$}", local_i32, local_f64); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("Width = {}, value with width = {:0$}", local_i32, local_f64); -LL + println!("Width = {local_i32}, value with width = {local_f64:local_i32$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:124:5 - | -LL | println!("{:w$.p$}", local_i32, w = width, p = prec); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:w$.p$}", local_i32, w = width, p = prec); -LL + println!("{local_i32:width$.prec$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:125:5 - | -LL | println!("{:w$.p$}", w = width, p = prec); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{:w$.p$}", w = width, p = prec); -LL + println!("{width:width$.prec$}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:126:20 - | -LL | println!("{}", format!("{}", local_i32)); - | ^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{}", format!("{}", local_i32)); -LL + println!("{}", format!("{local_i32}")); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:144:5 - | -LL | / println!( -LL | | "{}", -LL | | // comment with a comma , in it -LL | | val, -LL | | ); - | |_____^ - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:149:5 - | -LL | println!("{}", /* comment with a comma , in it */ val); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{}", /* comment with a comma , in it */ val); -LL + println!("{val}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:155:9 - | -LL | panic!("p1 {}", local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - panic!("p1 {}", local_i32); -LL + panic!("p1 {local_i32}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:158:9 - | -LL | panic!("p2 {0}", local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - panic!("p2 {0}", local_i32); -LL + panic!("p2 {local_i32}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:161:9 - | -LL | panic!("p3 {local_i32}", local_i32 = local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - panic!("p3 {local_i32}", local_i32 = local_i32); -LL + panic!("p3 {local_i32}"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:181:5 - | -LL | println!("expand='{}'", local_i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("expand='{}'", local_i32); -LL + println!("expand='{local_i32}'"); - | - -error: aborting due to 76 previous errors +error: aborting due to 6 previous errors diff --git a/tests/ui/uninlined_format_args.stderr b/tests/ui/uninlined_format_args.stderr index 05ed5b6616c09..a12abf8bef8af 100644 --- a/tests/ui/uninlined_format_args.stderr +++ b/tests/ui/uninlined_format_args.stderr @@ -177,42 +177,6 @@ LL - println!("{:.1}", local_f64); LL + println!("{local_f64:.1}"); | -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:59:5 - | -LL | println!("Hello {} is {:.*}", "x", local_i32, local_f64); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("Hello {} is {:.*}", "x", local_i32, local_f64); -LL + println!("Hello {} is {local_f64:.local_i32$}", "x"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:60:5 - | -LL | println!("Hello {} is {:.*}", local_i32, 5, local_f64); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("Hello {} is {:.*}", local_i32, 5, local_f64); -LL + println!("Hello {local_i32} is {local_f64:.*}", 5); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:61:5 - | -LL | println!("Hello {} is {2:.*}", local_i32, 5, local_f64); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("Hello {} is {2:.*}", local_i32, 5, local_f64); -LL + println!("Hello {local_i32} is {local_f64:.*}", 5); - | - error: variables can be used directly in the `format!` string --> $DIR/uninlined_format_args.rs:62:5 | @@ -225,18 +189,6 @@ LL - println!("{} {}", local_i32, local_f64); LL + println!("{local_i32} {local_f64}"); | -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:63:5 - | -LL | println!("{}, {}", local_i32, local_opt.unwrap()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{}, {}", local_i32, local_opt.unwrap()); -LL + println!("{local_i32}, {}", local_opt.unwrap()); - | - error: variables can be used directly in the `format!` string --> $DIR/uninlined_format_args.rs:64:5 | @@ -904,5 +856,5 @@ LL - println!("expand='{}'", local_i32); LL + println!("expand='{local_i32}'"); | -error: aborting due to 76 previous errors +error: aborting due to 72 previous errors diff --git a/tests/ui/uninlined_format_args_panic.edition2018.stderr b/tests/ui/uninlined_format_args_panic.edition2018.stderr index 1afdb4d0fbab4..2c8061259229a 100644 --- a/tests/ui/uninlined_format_args_panic.edition2018.stderr +++ b/tests/ui/uninlined_format_args_panic.edition2018.stderr @@ -4,7 +4,6 @@ error: variables can be used directly in the `format!` string LL | println!("val='{}'", var); | ^^^^^^^^^^^^^^^^^^^^^^^^^ | - = note: this lint can also fix mixed format arg inlining if `allow-mixed-uninlined-format-args = false` is set in the `clippy.toml` file = note: `-D clippy::uninlined-format-args` implied by `-D warnings` help: change this to | diff --git a/tests/ui/uninlined_format_args_panic.edition2021.stderr b/tests/ui/uninlined_format_args_panic.edition2021.stderr index a38ea4168bc9c..0f09c45f41324 100644 --- a/tests/ui/uninlined_format_args_panic.edition2021.stderr +++ b/tests/ui/uninlined_format_args_panic.edition2021.stderr @@ -4,7 +4,6 @@ error: variables can be used directly in the `format!` string LL | println!("val='{}'", var); | ^^^^^^^^^^^^^^^^^^^^^^^^^ | - = note: this lint can also fix mixed format arg inlining if `allow-mixed-uninlined-format-args = false` is set in the `clippy.toml` file = note: `-D clippy::uninlined-format-args` implied by `-D warnings` help: change this to | From d74240791768624eeb4efe728a2d4ec84c2a7e65 Mon Sep 17 00:00:00 2001 From: Pratush Rai Date: Sun, 27 Nov 2022 22:23:16 +0530 Subject: [PATCH 124/244] suggested changes --- src/bootstrap/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index 5411647c065b7..a3d1893afbb65 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -647,7 +647,6 @@ impl Build { if !update(true).status().map_or(false, |status| status.success()) { self.run(&mut update(false)); } - self.run(Command::new("git").args(&["stash", "push"]).current_dir(&absolute_path)); self.run(Command::new("git").args(&["reset", "-q", "--hard"]).current_dir(&absolute_path)); self.run(Command::new("git").args(&["clean", "-qdfx"]).current_dir(&absolute_path)); From cd68bd9baba18496ae8ecb9ac652d884ebc0b79d Mon Sep 17 00:00:00 2001 From: Markus Everling Date: Sun, 27 Nov 2022 15:55:13 +0100 Subject: [PATCH 125/244] Fix natvis `VecDeque` formatter --- src/etc/natvis/liballoc.natvis | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/etc/natvis/liballoc.natvis b/src/etc/natvis/liballoc.natvis index 41f4a3767f599..c4ad98ec1d3a9 100644 --- a/src/etc/natvis/liballoc.natvis +++ b/src/etc/natvis/liballoc.natvis @@ -12,20 +12,19 @@ - {{ len={tail <= head ? head - tail : buf.cap - tail + head} }} + {{ len={len} }} - tail <= head ? head - tail : buf.cap - tail + head + len buf.cap - - - tail <= head ? head - tail : buf.cap - tail + head + + len - + - buf.ptr.pointer.pointer[i] - i = (i + 1 == buf.cap ? 0 : i + 1) + buf.ptr.pointer.pointer[(i + head) % buf.cap] + i = i + 1 From 9cd12cdf788e3bc46ff72ec7f77448316cfa64c3 Mon Sep 17 00:00:00 2001 From: Ding Xiang Fei Date: Mon, 28 Nov 2022 01:48:35 +0800 Subject: [PATCH 126/244] inference test for #104649 --- src/test/ui/inference/issue-104649.rs | 32 +++++++++++++++++++++++ src/test/ui/inference/issue-104649.stderr | 14 ++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/test/ui/inference/issue-104649.rs create mode 100644 src/test/ui/inference/issue-104649.stderr diff --git a/src/test/ui/inference/issue-104649.rs b/src/test/ui/inference/issue-104649.rs new file mode 100644 index 0000000000000..4637b884d445d --- /dev/null +++ b/src/test/ui/inference/issue-104649.rs @@ -0,0 +1,32 @@ +type Result = ::std::result::Result; +struct Error; + +trait ForEach { + type Input; + fn for_each(self, f: F) + where + F: FnOnce(Self::Input) -> U; +} + +impl ForEach for A { + type Input = T; + fn for_each(self, f: F) + where + F: FnOnce(Self::Input) -> U, + { + todo!() + } +} + +struct A(T); + +fn main() { + let a = A(Result::Ok(Result::Ok(()))); //~ ERROR type annotations needed + a.for_each(|a: Result<_>| { + let f = || match a { + Ok(Ok(a)) => {} + Ok(Err(a)) => {} + Err(a) => {} + }; + }); +} diff --git a/src/test/ui/inference/issue-104649.stderr b/src/test/ui/inference/issue-104649.stderr new file mode 100644 index 0000000000000..4962b21f9fdb4 --- /dev/null +++ b/src/test/ui/inference/issue-104649.stderr @@ -0,0 +1,14 @@ +error[E0282]: type annotations needed for `A, Error>>` + --> $DIR/issue-104649.rs:24:9 + | +LL | let a = A(Result::Ok(Result::Ok(()))); + | ^ + | +help: consider giving `a` an explicit type, where the type for type parameter `E` is specified + | +LL | let a: A, Error>> = A(Result::Ok(Result::Ok(()))); + | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0282`. From 5b0e80ecf32e2c4f01d6f45fbcfd9b1709381289 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 13 Nov 2022 13:03:01 +0300 Subject: [PATCH 127/244] Stabilize native library modifier `verbatim` --- compiler/rustc_codegen_ssa/src/back/link.rs | 16 +++---- compiler/rustc_codegen_ssa/src/back/linker.rs | 2 +- compiler/rustc_codegen_ssa/src/lib.rs | 4 +- compiler/rustc_feature/src/accepted.rs | 2 + compiler/rustc_feature/src/active.rs | 2 - compiler/rustc_metadata/src/native_libs.rs | 13 ++--- compiler/rustc_session/src/config.rs | 5 +- src/doc/rustc/src/command-line-arguments.md | 27 +++++++++++ .../native-link-modifiers-verbatim.md | 20 -------- .../Makefile | 4 +- .../Makefile | 4 +- src/test/run-make/raw-dylib-c/lib.rs | 2 +- .../rust_dep.rs | 1 - ...ure-gate-native_link_modifiers_verbatim.rs | 5 -- ...gate-native_link_modifiers_verbatim.stderr | 12 ----- .../linkage-attr/link-attr-validation-late.rs | 1 - .../link-attr-validation-late.stderr | 48 +++++++++---------- 17 files changed, 73 insertions(+), 95 deletions(-) delete mode 100644 src/doc/unstable-book/src/language-features/native-link-modifiers-verbatim.md delete mode 100644 src/test/ui/feature-gates/feature-gate-native_link_modifiers_verbatim.rs delete mode 100644 src/test/ui/feature-gates/feature-gate-native_link_modifiers_verbatim.stderr diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 762430c618721..b103c7de74c14 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -377,12 +377,8 @@ fn link_rlib<'a>( find_native_static_library(name.as_str(), lib.verbatim, &lib_search_paths, sess); if sess.opts.unstable_opts.packed_bundled_libs && flavor == RlibFlavor::Normal { let filename = lib.filename.unwrap(); - let lib_path = find_native_static_library( - filename.as_str(), - Some(true), - &lib_search_paths, - sess, - ); + let lib_path = + find_native_static_library(filename.as_str(), true, &lib_search_paths, sess); let src = read(lib_path) .map_err(|e| sess.emit_fatal(errors::ReadFileError { message: e }))?; let (data, _) = create_wrapper_file(sess, b".bundled_lib".to_vec(), &src); @@ -465,7 +461,7 @@ fn collate_raw_dylibs<'a, 'b>( for lib in used_libraries { if lib.kind == NativeLibKind::RawDylib { - let ext = if matches!(lib.verbatim, Some(true)) { "" } else { ".dll" }; + let ext = if lib.verbatim { "" } else { ".dll" }; let name = format!("{}{}", lib.name.expect("unnamed raw-dylib library"), ext); let imports = dylib_table.entry(name.clone()).or_default(); for import in &lib.dll_imports { @@ -1335,7 +1331,7 @@ fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLib]) { NativeLibKind::Static { bundle: Some(false), .. } | NativeLibKind::Dylib { .. } | NativeLibKind::Unspecified => { - let verbatim = lib.verbatim.unwrap_or(false); + let verbatim = lib.verbatim; if sess.target.is_like_msvc { Some(format!("{}{}", name, if verbatim { "" } else { ".lib" })) } else if sess.target.linker_flavor.is_gnu() { @@ -2306,7 +2302,7 @@ fn add_native_libs_from_crate( _ => &codegen_results.crate_info.native_libraries[&cnum], }; - let mut last = (None, NativeLibKind::Unspecified, None); + let mut last = (None, NativeLibKind::Unspecified, false); for lib in native_libs { let Some(name) = lib.name else { continue; @@ -2323,7 +2319,7 @@ fn add_native_libs_from_crate( }; let name = name.as_str(); - let verbatim = lib.verbatim.unwrap_or(false); + let verbatim = lib.verbatim; match lib.kind { NativeLibKind::Static { bundle, whole_archive } => { if link_static { diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index 7f0c2861f7e29..5fb11e6b914b8 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -515,7 +515,7 @@ impl<'a> Linker for GccLinker<'a> { // -force_load is the macOS equivalent of --whole-archive, but it // involves passing the full path to the library to link. self.linker_arg("-force_load"); - let lib = find_native_static_library(lib, Some(verbatim), search_path, &self.sess); + let lib = find_native_static_library(lib, verbatim, search_path, &self.sess); self.linker_arg(&lib); } } diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index ade33b6c77728..def6390f6a36b 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -116,7 +116,7 @@ pub struct NativeLib { pub name: Option, pub filename: Option, pub cfg: Option, - pub verbatim: Option, + pub verbatim: bool, pub dll_imports: Vec, } @@ -127,7 +127,7 @@ impl From<&cstore::NativeLib> for NativeLib { filename: lib.filename, name: lib.name, cfg: lib.cfg.clone(), - verbatim: lib.verbatim, + verbatim: lib.verbatim.unwrap_or(false), dll_imports: lib.dll_imports.clone(), } } diff --git a/compiler/rustc_feature/src/accepted.rs b/compiler/rustc_feature/src/accepted.rs index 1646727a1c85f..7678ce323dfbc 100644 --- a/compiler/rustc_feature/src/accepted.rs +++ b/compiler/rustc_feature/src/accepted.rs @@ -237,6 +237,8 @@ declare_features! ( (accepted, native_link_modifiers, "1.61.0", Some(81490), None), /// Allows specifying the bundle link modifier (accepted, native_link_modifiers_bundle, "1.63.0", Some(81490), None), + /// Allows specifying the verbatim link modifier + (accepted, native_link_modifiers_verbatim, "CURRENT_RUSTC_VERSION", Some(81490), None), /// Allows specifying the whole-archive link modifier (accepted, native_link_modifiers_whole_archive, "1.61.0", Some(81490), None), /// Allows using non lexical lifetimes (RFC 2094). diff --git a/compiler/rustc_feature/src/active.rs b/compiler/rustc_feature/src/active.rs index 9ca63c393c623..69c5297bf6b82 100644 --- a/compiler/rustc_feature/src/active.rs +++ b/compiler/rustc_feature/src/active.rs @@ -455,8 +455,6 @@ declare_features! ( (active, naked_functions, "1.9.0", Some(32408), None), /// Allows specifying the as-needed link modifier (active, native_link_modifiers_as_needed, "1.53.0", Some(81490), None), - /// Allows specifying the verbatim link modifier - (active, native_link_modifiers_verbatim, "1.53.0", Some(81490), None), /// Allow negative trait implementations. (active, negative_impls, "1.44.0", Some(68318), None), /// Allows the `!` type. Does not imply 'exhaustive_patterns' (below) any more. diff --git a/compiler/rustc_metadata/src/native_libs.rs b/compiler/rustc_metadata/src/native_libs.rs index 20a2e78299aed..1fd35adf1bd2c 100644 --- a/compiler/rustc_metadata/src/native_libs.rs +++ b/compiler/rustc_metadata/src/native_libs.rs @@ -29,11 +29,11 @@ use std::path::PathBuf; pub fn find_native_static_library( name: &str, - verbatim: Option, + verbatim: bool, search_paths: &[PathBuf], sess: &Session, ) -> PathBuf { - let formats = if verbatim.unwrap_or(false) { + let formats = if verbatim { vec![("".into(), "".into())] } else { let os = (sess.target.staticlib_prefix.clone(), sess.target.staticlib_suffix.clone()); @@ -52,7 +52,7 @@ pub fn find_native_static_library( } } - sess.emit_fatal(MissingNativeLibrary::new(name, verbatim.unwrap_or(false))); + sess.emit_fatal(MissingNativeLibrary::new(name, verbatim)); } fn find_bundled_library( @@ -66,7 +66,7 @@ fn find_bundled_library( let NativeLibKind::Static { bundle: Some(true) | None, .. } = kind { find_native_static_library( name.unwrap().as_str(), - verbatim, + verbatim.unwrap_or(false), &sess.target_filesearch(PathKind::Native).search_path_dirs(), sess, ).file_name().and_then(|s| s.to_str()).map(Symbol::intern) @@ -311,10 +311,7 @@ impl<'tcx> Collector<'tcx> { sess.emit_err(BundleNeedsStatic { span }); } - ("verbatim", _) => { - report_unstable_modifier!(native_link_modifiers_verbatim); - assign_modifier(&mut verbatim) - } + ("verbatim", _) => assign_modifier(&mut verbatim), ("whole-archive", Some(NativeLibKind::Static { whole_archive, .. })) => { assign_modifier(whole_archive) diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 3b1b33aa095a1..927810351e958 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -2029,10 +2029,7 @@ fn parse_native_lib_modifiers( "linking modifier `bundle` is only compatible with `static` linking kind", ), - ("verbatim", _) => { - report_unstable_modifier(); - assign_modifier(&mut verbatim) - } + ("verbatim", _) => assign_modifier(&mut verbatim), ("whole-archive", NativeLibKind::Static { whole_archive, .. }) => { assign_modifier(whole_archive) diff --git a/src/doc/rustc/src/command-line-arguments.md b/src/doc/rustc/src/command-line-arguments.md index 2dc182b3d83ed..ef6eee75f1ce6 100644 --- a/src/doc/rustc/src/command-line-arguments.md +++ b/src/doc/rustc/src/command-line-arguments.md @@ -104,6 +104,33 @@ This modifier has no effect when building other targets like executables or dyna The default for this modifier is `+bundle`. +### Linking modifiers: `verbatim` + +This modifier is compatible with all linking kinds. + +`+verbatim` means that rustc itself won't add any target-specified library prefixes or suffixes +(like `lib` or `.a`) to the library name, and will try its best to ask for the same thing from the +linker. + +For `ld`-like linkers supporting GNU extensions rustc will use the `-l:filename` syntax (note the +colon) when passing the library, so the linker won't add any prefixes or suffixes to it. +See [`-l namespec`](https://sourceware.org/binutils/docs/ld/Options.html) in ld documentation for +more details. \ +For linkers not supporting any verbatim modifiers (e.g. `link.exe` or `ld64`) the library name will +be passed as is. So the most reliable cross-platform use scenarios for this option are when no +linker is involved, for example bundling native libraries into rlibs. + +`-verbatim` means that rustc will either add a target-specific prefix and suffix to the library +name before passing it to linker, or won't prevent linker from implicitly adding it. \ +In case of `raw-dylib` kind in particular `.dll` will be added to the library name on Windows. + +The default for this modifier is `-verbatim`. + +NOTE: Even with `+verbatim` and `-l:filename` syntax `ld`-like linkers do not typically support +passing absolute paths to libraries. Usually such paths need to be passed as input files without +using any options like `-l`, e.g. `ld /my/absolute/path`. \ +`-Clink-arg=/my/absolute/path` can be used for doing this from stable `rustc`. + ## `--crate-type`: a list of types of crates for the compiler to emit diff --git a/src/doc/unstable-book/src/language-features/native-link-modifiers-verbatim.md b/src/doc/unstable-book/src/language-features/native-link-modifiers-verbatim.md deleted file mode 100644 index 02bd87e50956d..0000000000000 --- a/src/doc/unstable-book/src/language-features/native-link-modifiers-verbatim.md +++ /dev/null @@ -1,20 +0,0 @@ -# `native_link_modifiers_verbatim` - -The tracking issue for this feature is: [#81490] - -[#81490]: https://github.com/rust-lang/rust/issues/81490 - ------------------------- - -The `native_link_modifiers_verbatim` feature allows you to use the `verbatim` modifier. - -`+verbatim` means that rustc itself won't add any target-specified library prefixes or suffixes (like `lib` or `.a`) to the library name, and will try its best to ask for the same thing from the linker. - -For `ld`-like linkers rustc will use the `-l:filename` syntax (note the colon) when passing the library, so the linker won't add any prefixes or suffixes as well. -See [`-l namespec`](https://sourceware.org/binutils/docs/ld/Options.html) in ld documentation for more details. -For linkers not supporting any verbatim modifiers (e.g. `link.exe` or `ld64`) the library name will be passed as is. - -The default for this modifier is `-verbatim`. - -This RFC changes the behavior of `raw-dylib` linking kind specified by [RFC 2627](https://github.com/rust-lang/rfcs/pull/2627). The `.dll` suffix (or other target-specified suffixes for other targets) is now added automatically. -If your DLL doesn't have the `.dll` suffix, it can be specified with `+verbatim`. diff --git a/src/test/run-make/native-link-modifier-verbatim-linker/Makefile b/src/test/run-make/native-link-modifier-verbatim-linker/Makefile index e56e1e94ec5b1..666e4084ce295 100644 --- a/src/test/run-make/native-link-modifier-verbatim-linker/Makefile +++ b/src/test/run-make/native-link-modifier-verbatim-linker/Makefile @@ -6,10 +6,10 @@ include ../../run-make-fulldeps/tools.mk all: # Verbatim allows specify precise name. $(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/local_some_strange_name.ext - $(RUSTC) main.rs -Zunstable-options -l static:+verbatim=local_some_strange_name.ext + $(RUSTC) main.rs -l static:+verbatim=local_some_strange_name.ext # With verbatim any other name cannot be used (local). $(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/liblocal_native_dep.a $(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/local_native_dep.a $(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/local_native_dep.lib - $(RUSTC) main.rs -Zunstable-options -l static:+verbatim=local_native_dep 2>&1 | $(CGREP) "local_native_dep" + $(RUSTC) main.rs -l static:+verbatim=local_native_dep 2>&1 | $(CGREP) "local_native_dep" diff --git a/src/test/run-make/native-link-modifier-verbatim-rustc/Makefile b/src/test/run-make/native-link-modifier-verbatim-rustc/Makefile index 1093b1cd3694d..6f01f37804a2a 100644 --- a/src/test/run-make/native-link-modifier-verbatim-rustc/Makefile +++ b/src/test/run-make/native-link-modifier-verbatim-rustc/Makefile @@ -3,10 +3,10 @@ include ../../run-make-fulldeps/tools.mk all: # Verbatim allows specify precise name. $(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/upstream_some_strange_name.ext - $(RUSTC) rust_dep.rs -Zunstable-options -l static:+verbatim=upstream_some_strange_name.ext --crate-type rlib + $(RUSTC) rust_dep.rs -l static:+verbatim=upstream_some_strange_name.ext --crate-type rlib # With verbatim any other name cannot be used (upstream). $(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/libupstream_native_dep.a $(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/upstream_native_dep.a $(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/upstream_native_dep.lib - $(RUSTC) rust_dep.rs -Zunstable-options -l static:+verbatim=upstream_native_dep --crate-type rlib 2>&1 | $(CGREP) "upstream_native_dep" + $(RUSTC) rust_dep.rs -l static:+verbatim=upstream_native_dep --crate-type rlib 2>&1 | $(CGREP) "upstream_native_dep" diff --git a/src/test/run-make/raw-dylib-c/lib.rs b/src/test/run-make/raw-dylib-c/lib.rs index 005ffcdda5c2e..5fb1204037c93 100644 --- a/src/test/run-make/raw-dylib-c/lib.rs +++ b/src/test/run-make/raw-dylib-c/lib.rs @@ -1,4 +1,4 @@ -#![feature(raw_dylib, native_link_modifiers_verbatim)] +#![feature(raw_dylib)] #[link(name = "extern_1.dll", kind = "raw-dylib", modifiers = "+verbatim")] extern { diff --git a/src/test/run-make/rlib-format-packed-bundled-libs-2/rust_dep.rs b/src/test/run-make/rlib-format-packed-bundled-libs-2/rust_dep.rs index d99dda05cf215..77e41e237d42a 100644 --- a/src/test/run-make/rlib-format-packed-bundled-libs-2/rust_dep.rs +++ b/src/test/run-make/rlib-format-packed-bundled-libs-2/rust_dep.rs @@ -1,4 +1,3 @@ -#![feature(native_link_modifiers_verbatim)] #[link(name = "native_dep.ext", kind = "static", modifiers = "+verbatim")] extern "C" { fn native_f1() -> i32; diff --git a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_verbatim.rs b/src/test/ui/feature-gates/feature-gate-native_link_modifiers_verbatim.rs deleted file mode 100644 index 7b09195dc3fb0..0000000000000 --- a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_verbatim.rs +++ /dev/null @@ -1,5 +0,0 @@ -#[link(name = "foo", modifiers = "+verbatim")] -//~^ ERROR: linking modifier `verbatim` is unstable -extern "C" {} - -fn main() {} diff --git a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_verbatim.stderr b/src/test/ui/feature-gates/feature-gate-native_link_modifiers_verbatim.stderr deleted file mode 100644 index 3bfbeb8db35fe..0000000000000 --- a/src/test/ui/feature-gates/feature-gate-native_link_modifiers_verbatim.stderr +++ /dev/null @@ -1,12 +0,0 @@ -error[E0658]: linking modifier `verbatim` is unstable - --> $DIR/feature-gate-native_link_modifiers_verbatim.rs:1:34 - | -LL | #[link(name = "foo", modifiers = "+verbatim")] - | ^^^^^^^^^^^ - | - = note: see issue #81490 for more information - = help: add `#![feature(native_link_modifiers_verbatim)]` to the crate attributes to enable - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/linkage-attr/link-attr-validation-late.rs b/src/test/ui/linkage-attr/link-attr-validation-late.rs index b454fbd0ed117..34f720dd2d3c5 100644 --- a/src/test/ui/linkage-attr/link-attr-validation-late.rs +++ b/src/test/ui/linkage-attr/link-attr-validation-late.rs @@ -1,4 +1,3 @@ -#![feature(native_link_modifiers_verbatim)] #![feature(link_cfg)] // Top-level ill-formed diff --git a/src/test/ui/linkage-attr/link-attr-validation-late.stderr b/src/test/ui/linkage-attr/link-attr-validation-late.stderr index dd0f1dba2ecf7..1ad5fbaf7de80 100644 --- a/src/test/ui/linkage-attr/link-attr-validation-late.stderr +++ b/src/test/ui/linkage-attr/link-attr-validation-late.stderr @@ -1,143 +1,143 @@ error: unexpected `#[link]` argument, expected one of: name, kind, modifiers, cfg, wasm_import_module, import_name_type - --> $DIR/link-attr-validation-late.rs:5:22 + --> $DIR/link-attr-validation-late.rs:4:22 | LL | #[link(name = "...", "literal")] | ^^^^^^^^^ error: unexpected `#[link]` argument, expected one of: name, kind, modifiers, cfg, wasm_import_module, import_name_type - --> $DIR/link-attr-validation-late.rs:6:22 + --> $DIR/link-attr-validation-late.rs:5:22 | LL | #[link(name = "...", unknown)] | ^^^^^^^ error: multiple `name` arguments in a single `#[link]` attribute - --> $DIR/link-attr-validation-late.rs:10:22 + --> $DIR/link-attr-validation-late.rs:9:22 | LL | #[link(name = "foo", name = "bar")] | ^^^^^^^^^^^^ error: multiple `kind` arguments in a single `#[link]` attribute - --> $DIR/link-attr-validation-late.rs:11:38 + --> $DIR/link-attr-validation-late.rs:10:38 | LL | #[link(name = "...", kind = "dylib", kind = "bar")] | ^^^^^^^^^^^^ error: multiple `modifiers` arguments in a single `#[link]` attribute - --> $DIR/link-attr-validation-late.rs:12:47 + --> $DIR/link-attr-validation-late.rs:11:47 | LL | #[link(name = "...", modifiers = "+verbatim", modifiers = "bar")] | ^^^^^^^^^^^^^^^^^ error: multiple `cfg` arguments in a single `#[link]` attribute - --> $DIR/link-attr-validation-late.rs:13:34 + --> $DIR/link-attr-validation-late.rs:12:34 | LL | #[link(name = "...", cfg(FALSE), cfg(FALSE))] | ^^^^^^^^^^ error: multiple `wasm_import_module` arguments in a single `#[link]` attribute - --> $DIR/link-attr-validation-late.rs:14:36 + --> $DIR/link-attr-validation-late.rs:13:36 | LL | #[link(wasm_import_module = "foo", wasm_import_module = "bar")] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: link name must be of the form `name = "string"` - --> $DIR/link-attr-validation-late.rs:18:8 + --> $DIR/link-attr-validation-late.rs:17:8 | LL | #[link(name)] | ^^^^ error[E0459]: `#[link]` attribute requires a `name = "string"` argument - --> $DIR/link-attr-validation-late.rs:18:1 + --> $DIR/link-attr-validation-late.rs:17:1 | LL | #[link(name)] | ^^^^^^^^^^^^^ missing `name` argument error: link name must be of the form `name = "string"` - --> $DIR/link-attr-validation-late.rs:20:8 + --> $DIR/link-attr-validation-late.rs:19:8 | LL | #[link(name())] | ^^^^^^ error[E0459]: `#[link]` attribute requires a `name = "string"` argument - --> $DIR/link-attr-validation-late.rs:20:1 + --> $DIR/link-attr-validation-late.rs:19:1 | LL | #[link(name())] | ^^^^^^^^^^^^^^^ missing `name` argument error: link kind must be of the form `kind = "string"` - --> $DIR/link-attr-validation-late.rs:22:22 + --> $DIR/link-attr-validation-late.rs:21:22 | LL | #[link(name = "...", kind)] | ^^^^ error: link kind must be of the form `kind = "string"` - --> $DIR/link-attr-validation-late.rs:23:22 + --> $DIR/link-attr-validation-late.rs:22:22 | LL | #[link(name = "...", kind())] | ^^^^^^ error: link modifiers must be of the form `modifiers = "string"` - --> $DIR/link-attr-validation-late.rs:24:22 + --> $DIR/link-attr-validation-late.rs:23:22 | LL | #[link(name = "...", modifiers)] | ^^^^^^^^^ error: link modifiers must be of the form `modifiers = "string"` - --> $DIR/link-attr-validation-late.rs:25:22 + --> $DIR/link-attr-validation-late.rs:24:22 | LL | #[link(name = "...", modifiers())] | ^^^^^^^^^^^ error: link cfg must be of the form `cfg(/* predicate */)` - --> $DIR/link-attr-validation-late.rs:26:22 + --> $DIR/link-attr-validation-late.rs:25:22 | LL | #[link(name = "...", cfg)] | ^^^ error: link cfg must be of the form `cfg(/* predicate */)` - --> $DIR/link-attr-validation-late.rs:27:22 + --> $DIR/link-attr-validation-late.rs:26:22 | LL | #[link(name = "...", cfg = "literal")] | ^^^^^^^^^^^^^^^ error: link cfg must have a single predicate argument - --> $DIR/link-attr-validation-late.rs:28:22 + --> $DIR/link-attr-validation-late.rs:27:22 | LL | #[link(name = "...", cfg("literal"))] | ^^^^^^^^^^^^^^ error: wasm import module must be of the form `wasm_import_module = "string"` - --> $DIR/link-attr-validation-late.rs:29:22 + --> $DIR/link-attr-validation-late.rs:28:22 | LL | #[link(name = "...", wasm_import_module)] | ^^^^^^^^^^^^^^^^^^ error: wasm import module must be of the form `wasm_import_module = "string"` - --> $DIR/link-attr-validation-late.rs:30:22 + --> $DIR/link-attr-validation-late.rs:29:22 | LL | #[link(name = "...", wasm_import_module())] | ^^^^^^^^^^^^^^^^^^^^ error: invalid linking modifier syntax, expected '+' or '-' prefix before one of: bundle, verbatim, whole-archive, as-needed - --> $DIR/link-attr-validation-late.rs:34:34 + --> $DIR/link-attr-validation-late.rs:33:34 | LL | #[link(name = "...", modifiers = "")] | ^^ error: invalid linking modifier syntax, expected '+' or '-' prefix before one of: bundle, verbatim, whole-archive, as-needed - --> $DIR/link-attr-validation-late.rs:35:34 + --> $DIR/link-attr-validation-late.rs:34:34 | LL | #[link(name = "...", modifiers = "no-plus-minus")] | ^^^^^^^^^^^^^^^ error: unknown linking modifier `unknown`, expected one of: bundle, verbatim, whole-archive, as-needed - --> $DIR/link-attr-validation-late.rs:36:34 + --> $DIR/link-attr-validation-late.rs:35:34 | LL | #[link(name = "...", modifiers = "+unknown")] | ^^^^^^^^^^ error: multiple `verbatim` modifiers in a single `modifiers` argument - --> $DIR/link-attr-validation-late.rs:37:34 + --> $DIR/link-attr-validation-late.rs:36:34 | LL | #[link(name = "...", modifiers = "+verbatim,+verbatim")] | ^^^^^^^^^^^^^^^^^^^^^ From 99c3dda06649ae9878138366bf2423ce45ce198b Mon Sep 17 00:00:00 2001 From: lcnr Date: Sun, 27 Nov 2022 20:48:31 +0100 Subject: [PATCH 128/244] fix type Co-authored-by: fee1-dead --- compiler/rustc_hir_typeck/src/callee.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_hir_typeck/src/callee.rs b/compiler/rustc_hir_typeck/src/callee.rs index cd1322d1a3bcc..b390d34b37bda 100644 --- a/compiler/rustc_hir_typeck/src/callee.rs +++ b/compiler/rustc_hir_typeck/src/callee.rs @@ -181,7 +181,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // where F:Fn and so forth. In the particular case of types // like `f: &mut FnMut()`, if there is a call `f()`, we would // normally translate to `FnMut::call_mut(&mut f, ())`, but - // that winds up requiring the user to potentially mark their + // that winds up potentially requiring the user to mark their // variable as `mut` which feels unnecessary and unexpected. // // fn foo(f: &mut impl FnMut()) { f() } From b1c3c6380f5293b57cd2073908c84f2e270238ca Mon Sep 17 00:00:00 2001 From: Markus Everling Date: Sun, 27 Nov 2022 23:08:14 +0100 Subject: [PATCH 129/244] Fix `pretty-std` test --- src/test/debuginfo/pretty-std.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/debuginfo/pretty-std.rs b/src/test/debuginfo/pretty-std.rs index d8c6344e0b6ac..7bb2810c2b213 100644 --- a/src/test/debuginfo/pretty-std.rs +++ b/src/test/debuginfo/pretty-std.rs @@ -138,7 +138,7 @@ // cdb-command: dx vecdeque // cdb-check:vecdeque : { len=0x2 } [Type: alloc::collections::vec_deque::VecDeque] // cdb-check: [] [Type: alloc::collections::vec_deque::VecDeque] -// cdb-check: [len] : 0x2 +// cdb-check: [len] : 0x2 [Type: unsigned [...]] // cdb-check: [capacity] : 0x8 [Type: unsigned [...]] // cdb-check: [0x0] : 90 [Type: int] // cdb-check: [0x1] : 20 [Type: int] @@ -175,7 +175,7 @@ fn main() { linkedlist.push_front(128); // VecDeque - let mut vecdeque = VecDeque::new(); + let mut vecdeque = VecDeque::with_capacity(8); vecdeque.push_back(20); vecdeque.push_front(90); From bb273a17f81853a7d6ecc7649f5f15f5bacbe463 Mon Sep 17 00:00:00 2001 From: Jakob Degen Date: Sun, 27 Nov 2022 17:18:38 -0800 Subject: [PATCH 130/244] Update my mailmap --- .mailmap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.mailmap b/.mailmap index f887d29096e03..4ef723e51468a 100644 --- a/.mailmap +++ b/.mailmap @@ -229,7 +229,7 @@ Jacob Jacob Greenfield Jacob Pratt Jake Vossen -Jakob Degen +Jakob Degen Jakob Lautrup Nysom Jakub Adam Wieczorek Jakub Adam Wieczorek From 7a378dd0fb2b383be75722666d8a3c761882af15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20A=2E=20Muci=C3=B1o?= Date: Sun, 27 Nov 2022 20:50:13 -0600 Subject: [PATCH 131/244] Avoid ICE if the Clone trait is not found while building error suggestions --- .../src/diagnostics/conflict_errors.rs | 16 +++++++------- .../missing-clone-for-suggestion.rs | 20 ++++++++++++++++++ .../missing-clone-for-suggestion.stderr | 21 +++++++++++++++++++ 3 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 src/test/ui/lang-items/missing-clone-for-suggestion.rs create mode 100644 src/test/ui/lang-items/missing-clone-for-suggestion.stderr diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index 5ec9c5f5c1b55..d221da5c17ed0 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -732,13 +732,15 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { let tcx = self.infcx.tcx; // Try to find predicates on *generic params* that would allow copying `ty` let infcx = tcx.infer_ctxt().build(); - if infcx - .type_implements_trait( - tcx.lang_items().clone_trait().unwrap(), - [tcx.erase_regions(ty)], - self.param_env, - ) - .must_apply_modulo_regions() + + if let Some(clone_trait_def) = tcx.lang_items().clone_trait() + && infcx + .type_implements_trait( + clone_trait_def, + [tcx.erase_regions(ty)], + self.param_env, + ) + .must_apply_modulo_regions() { err.span_suggestion_verbose( span.shrink_to_hi(), diff --git a/src/test/ui/lang-items/missing-clone-for-suggestion.rs b/src/test/ui/lang-items/missing-clone-for-suggestion.rs new file mode 100644 index 0000000000000..e8290c0098afd --- /dev/null +++ b/src/test/ui/lang-items/missing-clone-for-suggestion.rs @@ -0,0 +1,20 @@ +// Avoid panicking if the Clone trait is not found while building error suggestions +// See #104870 + +#![feature(no_core, lang_items)] +#![no_core] + +#[lang = "sized"] +trait Sized {} + +#[lang = "copy"] +trait Copy {} + +fn g(x: T) {} + +fn f(x: *mut u8) { + g(x); + g(x); //~ ERROR use of moved value: `x` +} + +fn main() {} diff --git a/src/test/ui/lang-items/missing-clone-for-suggestion.stderr b/src/test/ui/lang-items/missing-clone-for-suggestion.stderr new file mode 100644 index 0000000000000..35783a1be78ad --- /dev/null +++ b/src/test/ui/lang-items/missing-clone-for-suggestion.stderr @@ -0,0 +1,21 @@ +error[E0382]: use of moved value: `x` + --> $DIR/missing-clone-for-suggestion.rs:17:7 + | +LL | fn f(x: *mut u8) { + | - move occurs because `x` has type `*mut u8`, which does not implement the `Copy` trait +LL | g(x); + | - value moved here +LL | g(x); + | ^ value used here after move + | +note: consider changing this parameter type in function `g` to borrow instead if owning the value isn't necessary + --> $DIR/missing-clone-for-suggestion.rs:13:12 + | +LL | fn g(x: T) {} + | - ^ this parameter takes ownership of the value + | | + | in this function + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0382`. From c3b280fb67f2efac9124f4cbd8731e9ae5632043 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Sun, 20 Nov 2022 01:28:19 +0000 Subject: [PATCH 132/244] test-various: Add tests for {i686,aarch64}-unknown-uefi This extends the existing test for x86_64-unknown-uefi to test the other two UEFI targets as well. --- .../host-x86_64/test-various/Dockerfile | 13 +++- .../test-various/uefi_qemu_test/run.py | 78 +++++++++++++++---- 2 files changed, 74 insertions(+), 17 deletions(-) diff --git a/src/ci/docker/host-x86_64/test-various/Dockerfile b/src/ci/docker/host-x86_64/test-various/Dockerfile index 0bddffa3436f4..7c09e3a582fd6 100644 --- a/src/ci/docker/host-x86_64/test-various/Dockerfile +++ b/src/ci/docker/host-x86_64/test-various/Dockerfile @@ -19,11 +19,18 @@ RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-ins wget \ patch \ ovmf \ + qemu-efi-aarch64 \ + qemu-system-arm \ qemu-system-x86 RUN curl -sL https://nodejs.org/dist/v15.14.0/node-v15.14.0-linux-x64.tar.xz | \ tar -xJ +# Install 32-bit OVMF files for the i686-unknown-uefi test. This package +# is not available in ubuntu 20.04, so download a 22.04 package. +RUN curl -sL --output ovmf-ia32.deb http://mirrors.kernel.org/ubuntu/pool/universe/e/edk2/ovmf-ia32_2022.02-3_all.deb +RUN dpkg -i ovmf-ia32.deb && rm ovmf-ia32.deb + WORKDIR /build/ COPY scripts/musl-patch-configure.diff /build/ COPY scripts/musl-toolchain.sh /build/ @@ -68,7 +75,11 @@ ENV MUSL_TARGETS=x86_64-unknown-linux-musl \ ENV MUSL_SCRIPT python3 /checkout/x.py --stage 2 test --host='' --target $MUSL_TARGETS COPY host-x86_64/test-various/uefi_qemu_test /uefi_qemu_test -ENV UEFI_TARGETS=x86_64-unknown-uefi \ +ENV UEFI_TARGETS=aarch64-unknown-uefi,i686-unknown-uefi,x86_64-unknown-uefi \ + CC_aarch64_unknown_uefi=clang-11 \ + CXX_aarch64_unknown_uefi=clang++-11 \ + CC_i686_unknown_uefi=clang-11 \ + CXX_i686_unknown_uefi=clang++-11 \ CC_x86_64_unknown_uefi=clang-11 \ CXX_x86_64_unknown_uefi=clang++-11 ENV UEFI_SCRIPT python3 /checkout/x.py --stage 2 build --host='' --target $UEFI_TARGETS && \ diff --git a/src/ci/docker/host-x86_64/test-various/uefi_qemu_test/run.py b/src/ci/docker/host-x86_64/test-various/uefi_qemu_test/run.py index 46793ce3afa15..ffae7b0d4ac27 100644 --- a/src/ci/docker/host-x86_64/test-various/uefi_qemu_test/run.py +++ b/src/ci/docker/host-x86_64/test-various/uefi_qemu_test/run.py @@ -8,8 +8,11 @@ from pathlib import Path +TARGET_AARCH64 = 'aarch64-unknown-uefi' +TARGET_I686 = 'i686-unknown-uefi' +TARGET_X86_64 = 'x86_64-unknown-uefi' -def run(*cmd, capture=False, check=True, env=None): +def run(*cmd, capture=False, check=True, env=None, timeout=None): """Print and run a command, optionally capturing the output.""" cmd = [str(p) for p in cmd] print(' '.join(cmd)) @@ -17,10 +20,39 @@ def run(*cmd, capture=False, check=True, env=None): capture_output=capture, check=check, env=env, - text=True) - + text=True, + timeout=timeout) + +def build_and_run(tmp_dir, target): + if target == TARGET_AARCH64: + boot_file_name = 'bootaa64.efi' + ovmf_dir = Path('/usr/share/AAVMF') + ovmf_code = 'AAVMF_CODE.fd' + ovmf_vars = 'AAVMF_VARS.fd' + qemu = 'qemu-system-aarch64' + machine = 'virt' + cpu = 'cortex-a72' + elif target == TARGET_I686: + boot_file_name = 'bootia32.efi' + ovmf_dir = Path('/usr/share/OVMF') + ovmf_code = 'OVMF32_CODE_4M.secboot.fd' + ovmf_vars = 'OVMF32_VARS_4M.fd' + # The i686 target intentionally uses 64-bit qemu; the important + # difference is that the OVMF code provides a 32-bit environment. + qemu = 'qemu-system-x86_64' + machine = 'q35' + cpu = 'qemu64' + elif target == TARGET_X86_64: + boot_file_name = 'bootx64.efi' + ovmf_dir = Path('/usr/share/OVMF') + ovmf_code = 'OVMF_CODE.fd' + ovmf_vars = 'OVMF_VARS.fd' + qemu = 'qemu-system-x86_64' + machine = 'q35' + cpu = 'qemu64' + else: + raise KeyError('invalid target') -def build_and_run(tmp_dir): host_artifacts = Path('/checkout/obj/build/x86_64-unknown-linux-gnu') stage0 = host_artifacts / 'stage0/bin' stage2 = host_artifacts / 'stage2/bin' @@ -33,7 +65,6 @@ def build_and_run(tmp_dir): shutil.copytree('/uefi_qemu_test', test_crate) # Build the UEFI executable. - target = 'x86_64-unknown-uefi' run('cargo', 'build', '--manifest-path', @@ -49,14 +80,24 @@ def build_and_run(tmp_dir): # Copy the executable into the ESP. src_exe_path = test_crate / 'target' / target / 'debug/uefi_qemu_test.efi' - shutil.copy(src_exe_path, boot / 'bootx64.efi') + shutil.copy(src_exe_path, boot / boot_file_name) + print(src_exe_path, boot / boot_file_name) + + # Select the appropriate EDK2 build. + ovmf_code = ovmf_dir / ovmf_code + ovmf_vars = ovmf_dir / ovmf_vars + + # Make a writable copy of the vars file. aarch64 doesn't boot + # correctly with read-only vars. + ovmf_rw_vars = Path(tmp_dir) / 'vars.fd' + shutil.copy(ovmf_vars, ovmf_rw_vars) # Run the executable in QEMU and capture the output. - qemu = 'qemu-system-x86_64' - ovmf_dir = Path('/usr/share/OVMF') - ovmf_code = ovmf_dir / 'OVMF_CODE.fd' - ovmf_vars = ovmf_dir / 'OVMF_VARS.fd' output = run(qemu, + '-machine', + machine, + '-cpu', + cpu, '-display', 'none', '-serial', @@ -64,7 +105,7 @@ def build_and_run(tmp_dir): '-drive', f'if=pflash,format=raw,readonly=on,file={ovmf_code}', '-drive', - f'if=pflash,format=raw,readonly=on,file={ovmf_vars}', + f'if=pflash,format=raw,readonly=off,file={ovmf_rw_vars}', '-drive', f'format=raw,file=fat:rw:{esp}', capture=True, @@ -73,7 +114,9 @@ def build_and_run(tmp_dir): # shutdown under some circumstances. That has been # fixed in newer versions of QEMU, but for now just # don't check the exit status. - check=False).stdout + check=False, + # Set a timeout to kill the VM in case something goes wrong. + timeout=60).stdout if 'Hello World!' in output: print('VM produced expected output') @@ -86,10 +129,13 @@ def build_and_run(tmp_dir): def main(): - # Create a temporary directory so that we have a writeable - # workspace. - with tempfile.TemporaryDirectory() as tmp_dir: - build_and_run(tmp_dir) + targets = [TARGET_AARCH64, TARGET_I686, TARGET_X86_64] + + for target in targets: + # Create a temporary directory so that we have a writeable + # workspace. + with tempfile.TemporaryDirectory() as tmp_dir: + build_and_run(tmp_dir, target) if __name__ == "__main__": From 8cfc8153da71c599ba6e118279d366b74638057e Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 23 Nov 2022 17:00:37 +1100 Subject: [PATCH 133/244] Remove `Lit::from_included_bytes`. `Lit::from_included_bytes` calls `Lit::from_lit_kind`, but the two call sites only need the resulting `token::Lit`, not the full `ast::Lit`. This commit changes those call sites to use `LitKind::to_token_lit`, which means `from_included_bytes` can be removed. --- compiler/rustc_ast/src/util/literal.rs | 8 -------- compiler/rustc_ast_pretty/src/pprust/state/expr.rs | 4 ++-- compiler/rustc_expand/src/proc_macro_server.rs | 4 ++-- 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_ast/src/util/literal.rs b/compiler/rustc_ast/src/util/literal.rs index db2ac9626afd5..8eec869fbe5f0 100644 --- a/compiler/rustc_ast/src/util/literal.rs +++ b/compiler/rustc_ast/src/util/literal.rs @@ -2,7 +2,6 @@ use crate::ast::{self, Lit, LitKind}; use crate::token::{self, Token}; -use rustc_data_structures::sync::Lrc; use rustc_lexer::unescape::{byte_from_char, unescape_byte, unescape_char, unescape_literal, Mode}; use rustc_span::symbol::{kw, sym, Symbol}; use rustc_span::Span; @@ -215,13 +214,6 @@ impl Lit { Lit { token_lit: kind.to_token_lit(), kind, span } } - /// Recovers an AST literal from a string of bytes produced by `include_bytes!`. - /// This requires ASCII-escaping the string, which can result in poor performance - /// for very large strings of bytes. - pub fn from_included_bytes(bytes: &Lrc<[u8]>, span: Span) -> Lit { - Self::from_lit_kind(LitKind::ByteStr(bytes.clone()), span) - } - /// Losslessly convert an AST literal into a token. pub fn to_token(&self) -> Token { let kind = match self.token_lit.kind { diff --git a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs index 949d98f96ab6a..f4d77549eff4c 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs @@ -328,8 +328,8 @@ impl<'a> State<'a> { self.print_token_literal(token_lit, expr.span); } ast::ExprKind::IncludedBytes(ref bytes) => { - let lit = ast::Lit::from_included_bytes(bytes, expr.span); - self.print_literal(&lit) + let lit = ast::LitKind::ByteStr(bytes.clone()).to_token_lit(); + self.print_token_literal(lit, expr.span) } ast::ExprKind::Cast(ref expr, ref ty) => { let prec = AssocOp::As.precedence() as i8; diff --git a/compiler/rustc_expand/src/proc_macro_server.rs b/compiler/rustc_expand/src/proc_macro_server.rs index 2e832deeecda0..b69556c0e7cc2 100644 --- a/compiler/rustc_expand/src/proc_macro_server.rs +++ b/compiler/rustc_expand/src/proc_macro_server.rs @@ -526,9 +526,9 @@ impl server::TokenStream for Rustc<'_, '_> { Ok(tokenstream::TokenStream::token_alone(token::Literal(*token_lit), expr.span)) } ast::ExprKind::IncludedBytes(bytes) => { - let lit = ast::Lit::from_included_bytes(bytes, expr.span); + let lit = ast::LitKind::ByteStr(bytes.clone()).to_token_lit(); Ok(tokenstream::TokenStream::token_alone( - token::TokenKind::Literal(lit.token_lit), + token::TokenKind::Literal(lit), expr.span, )) } From aa10aad1ac5058e8278d8871c1cb4473134d3d54 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 24 Nov 2022 13:47:57 +1100 Subject: [PATCH 134/244] Factor out a repeated expression in `lower_attr_args`. --- compiler/rustc_ast_lowering/src/lib.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index a123a58a8fbfa..530cf7d978796 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -948,15 +948,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { AttrArgs::Eq(eq_span, AttrArgsEq::Ast(expr)) => { // In valid code the value always ends up as a single literal. Otherwise, a dummy // literal suffices because the error is handled elsewhere. - let lit = if let ExprKind::Lit(token_lit) = expr.kind { - match Lit::from_token_lit(token_lit, expr.span) { - Ok(lit) => lit, - Err(_err) => Lit { - token_lit: token::Lit::new(token::LitKind::Err, kw::Empty, None), - kind: LitKind::Err, - span: DUMMY_SP, - }, - } + let lit = if let ExprKind::Lit(token_lit) = expr.kind + && let Ok(lit) = Lit::from_token_lit(token_lit, expr.span) + { + lit } else { Lit { token_lit: token::Lit::new(token::LitKind::Err, kw::Empty, None), From e4a9150872a08db286208d07f5a6a90e466ca39c Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 23 Nov 2022 15:39:42 +1100 Subject: [PATCH 135/244] Rename `ast::Lit` as `ast::MetaItemLit`. --- compiler/rustc_ast/src/ast.rs | 21 ++++++++------- compiler/rustc_ast/src/attr/mod.rs | 18 ++++++------- compiler/rustc_ast/src/util/literal.rs | 24 ++++++++--------- compiler/rustc_ast_lowering/src/lib.rs | 6 ++--- compiler/rustc_ast_pretty/src/pprust/state.rs | 12 ++++----- compiler/rustc_attr/src/builtin.rs | 12 +++++---- compiler/rustc_builtin_macros/src/derive.rs | 4 +-- .../rustc_expand/src/proc_macro_server.rs | 5 +--- compiler/rustc_hir_analysis/src/collect.rs | 6 +++-- compiler/rustc_middle/src/ty/context.rs | 5 ++-- compiler/rustc_parse/src/parser/attr.rs | 9 ++++--- compiler/rustc_parse/src/parser/expr.rs | 27 ++++++++++--------- compiler/rustc_parse/src/parser/stmt.rs | 2 +- compiler/rustc_parse/src/validate_attr.rs | 2 +- compiler/rustc_passes/src/check_attr.rs | 7 +++-- src/tools/clippy/clippy_lints/src/attrs.rs | 4 +-- src/tools/rustfmt/src/attr.rs | 11 +++++--- src/tools/rustfmt/src/modules/visitor.rs | 12 ++++++--- 18 files changed, 103 insertions(+), 84 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 8bb4442d1bb27..5d470f1c453fe 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -13,7 +13,7 @@ //! - [`FnDecl`], [`FnHeader`] and [`Param`]: Metadata associated with a function declaration. //! - [`Generics`], [`GenericParam`], [`WhereClause`]: Metadata associated with generic parameters. //! - [`EnumDef`] and [`Variant`]: Enum declaration. -//! - [`Lit`] and [`LitKind`]: Literal expressions. +//! - [`MetaItemLit`] and [`LitKind`]: Literal expressions. //! - [`MacroDef`], [`MacStmtStyle`], [`MacCall`], [`MacDelimiter`]: Macro definition and invocation. //! - [`Attribute`]: Metadata associated with item. //! - [`UnOp`], [`BinOp`], and [`BinOpKind`]: Unary and binary operators. @@ -489,7 +489,7 @@ pub enum NestedMetaItem { /// A literal. /// /// E.g., `"foo"`, `64`, `true`. - Literal(Lit), + Literal(MetaItemLit), } /// A spanned compile-time attribute item. @@ -518,7 +518,7 @@ pub enum MetaItemKind { /// Name value meta item. /// /// E.g., `feature = "foo"` as in `#[feature = "foo"]`. - NameValue(Lit), + NameValue(MetaItemLit), } /// A block (`{ .. }`). @@ -1599,12 +1599,12 @@ pub enum AttrArgs { } // The RHS of an `AttrArgs::Eq` starts out as an expression. Once macro -// expansion is completed, all cases end up either as a literal, which is the -// form used after lowering to HIR, or as an error. +// expansion is completed, all cases end up either as a meta item literal, +// which is the form used after lowering to HIR, or as an error. #[derive(Clone, Encodable, Decodable, Debug)] pub enum AttrArgsEq { Ast(P), - Hir(Lit), + Hir(MetaItemLit), } impl AttrArgs { @@ -1726,14 +1726,13 @@ pub enum StrStyle { Raw(u8), } -/// An AST literal. +/// A literal in a meta item. #[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)] -pub struct Lit { +pub struct MetaItemLit { /// The original literal token as written in source code. pub token_lit: token::Lit, /// The "semantic" representation of the literal lowered from the original tokens. /// Strings are unescaped, hexadecimal forms are eliminated, etc. - /// FIXME: Remove this and only create the semantic representation during lowering to HIR. pub kind: LitKind, pub span: Span, } @@ -1783,6 +1782,8 @@ pub enum LitFloatType { Unsuffixed, } +/// This type is used within both `ast::MetaItemLit` and `hir::Lit`. +/// /// Note that the entire literal (including the suffix) is considered when /// deciding the `LitKind`. This means that float literals like `1f32` are /// classified by this type as `Float`. This is different to `token::LitKind` @@ -3096,9 +3097,9 @@ mod size_asserts { static_assert_size!(Impl, 184); static_assert_size!(Item, 184); static_assert_size!(ItemKind, 112); - static_assert_size!(Lit, 48); static_assert_size!(LitKind, 24); static_assert_size!(Local, 72); + static_assert_size!(MetaItemLit, 48); static_assert_size!(Param, 40); static_assert_size!(Pat, 88); static_assert_size!(Path, 24); diff --git a/compiler/rustc_ast/src/attr/mod.rs b/compiler/rustc_ast/src/attr/mod.rs index c948faeb35835..a31fe36dac705 100644 --- a/compiler/rustc_ast/src/attr/mod.rs +++ b/compiler/rustc_ast/src/attr/mod.rs @@ -2,7 +2,7 @@ use crate::ast; use crate::ast::{AttrArgs, AttrArgsEq, AttrId, AttrItem, AttrKind, AttrStyle, Attribute}; -use crate::ast::{DelimArgs, Lit, LitKind}; +use crate::ast::{DelimArgs, LitKind, MetaItemLit}; use crate::ast::{MacDelimiter, MetaItem, MetaItemKind, NestedMetaItem}; use crate::ast::{Path, PathSegment}; use crate::ptr::P; @@ -50,8 +50,8 @@ impl NestedMetaItem { } } - /// Returns the `Lit` if `self` is a `NestedMetaItem::Literal`s. - pub fn literal(&self) -> Option<&Lit> { + /// Returns the `MetaItemLit` if `self` is a `NestedMetaItem::Literal`s. + pub fn literal(&self) -> Option<&MetaItemLit> { match self { NestedMetaItem::Literal(lit) => Some(lit), _ => None, @@ -78,7 +78,7 @@ impl NestedMetaItem { } /// Returns a name and single literal value tuple of the `MetaItem`. - pub fn name_value_literal(&self) -> Option<(Symbol, &Lit)> { + pub fn name_value_literal(&self) -> Option<(Symbol, &MetaItemLit)> { self.meta_item().and_then(|meta_item| { meta_item.meta_item_list().and_then(|meta_item_list| { if meta_item_list.len() == 1 @@ -179,7 +179,7 @@ impl MetaItem { /// #[attribute(name = "value")] /// ^^^^^^^^^^^^^^ /// ``` - pub fn name_value_literal(&self) -> Option<&Lit> { + pub fn name_value_literal(&self) -> Option<&MetaItemLit> { match &self.kind { MetaItemKind::NameValue(v) => Some(v), _ => None, @@ -334,7 +334,7 @@ pub fn mk_name_value_item_str(ident: Ident, str: Symbol, str_span: Span) -> Meta } pub fn mk_name_value_item(ident: Ident, lit_kind: LitKind, lit_span: Span) -> MetaItem { - let lit = Lit::from_lit_kind(lit_kind, lit_span); + let lit = MetaItemLit::from_lit_kind(lit_kind, lit_span); let span = ident.span.to(lit_span); MetaItem { path: Path::from_ident(ident), span, kind: MetaItemKind::NameValue(lit) } } @@ -604,7 +604,7 @@ impl MetaItemKind { MetaItemKind::name_value_from_tokens(&mut inner_tokens.into_trees()) } Some(TokenTree::Token(token, _)) => { - Lit::from_token(&token).map(MetaItemKind::NameValue) + MetaItemLit::from_token(&token).map(MetaItemKind::NameValue) } _ => None, } @@ -622,7 +622,7 @@ impl MetaItemKind { AttrArgs::Eq(_, AttrArgsEq::Ast(expr)) => match expr.kind { ast::ExprKind::Lit(token_lit) => { // Turn failures to `None`, we'll get parse errors elsewhere. - Lit::from_token_lit(token_lit, expr.span) + MetaItemLit::from_token_lit(token_lit, expr.span) .ok() .map(|lit| MetaItemKind::NameValue(lit)) } @@ -674,7 +674,7 @@ impl NestedMetaItem { { match tokens.peek() { Some(TokenTree::Token(token, _)) - if let Some(lit) = Lit::from_token(token) => + if let Some(lit) = MetaItemLit::from_token(token) => { tokens.next(); return Some(NestedMetaItem::Literal(lit)); diff --git a/compiler/rustc_ast/src/util/literal.rs b/compiler/rustc_ast/src/util/literal.rs index 8eec869fbe5f0..42cba07fcef8d 100644 --- a/compiler/rustc_ast/src/util/literal.rs +++ b/compiler/rustc_ast/src/util/literal.rs @@ -1,6 +1,6 @@ //! Code related to parsing literals. -use crate::ast::{self, Lit, LitKind}; +use crate::ast::{self, LitKind, MetaItemLit}; use crate::token::{self, Token}; use rustc_lexer::unescape::{byte_from_char, unescape_byte, unescape_char, unescape_literal, Mode}; use rustc_span::symbol::{kw, sym, Symbol}; @@ -195,26 +195,26 @@ impl LitKind { } } -impl Lit { - /// Converts literal token into an AST literal. - pub fn from_token_lit(token_lit: token::Lit, span: Span) -> Result { - Ok(Lit { token_lit, kind: LitKind::from_token_lit(token_lit)?, span }) +impl MetaItemLit { + /// Converts token literal into a meta item literal. + pub fn from_token_lit(token_lit: token::Lit, span: Span) -> Result { + Ok(MetaItemLit { token_lit, kind: LitKind::from_token_lit(token_lit)?, span }) } - /// Converts an arbitrary token into an AST literal. - pub fn from_token(token: &Token) -> Option { + /// Converts an arbitrary token into meta item literal. + pub fn from_token(token: &Token) -> Option { token::Lit::from_token(token) - .and_then(|token_lit| Lit::from_token_lit(token_lit, token.span).ok()) + .and_then(|token_lit| MetaItemLit::from_token_lit(token_lit, token.span).ok()) } - /// Attempts to recover an AST literal from semantic literal. + /// Attempts to create a meta item literal from a `LitKind`. /// This function is used when the original token doesn't exist (e.g. the literal is created /// by an AST-based macro) or unavailable (e.g. from HIR pretty-printing). - pub fn from_lit_kind(kind: LitKind, span: Span) -> Lit { - Lit { token_lit: kind.to_token_lit(), kind, span } + pub fn from_lit_kind(kind: LitKind, span: Span) -> MetaItemLit { + MetaItemLit { token_lit: kind.to_token_lit(), kind, span } } - /// Losslessly convert an AST literal into a token. + /// Losslessly convert a meta item literal into a token. pub fn to_token(&self) -> Token { let kind = match self.token_lit.kind { token::Bool => token::Ident(self.token_lit.symbol, false), diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 530cf7d978796..ad6e72d015695 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -948,12 +948,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { AttrArgs::Eq(eq_span, AttrArgsEq::Ast(expr)) => { // In valid code the value always ends up as a single literal. Otherwise, a dummy // literal suffices because the error is handled elsewhere. - let lit = if let ExprKind::Lit(token_lit) = expr.kind - && let Ok(lit) = Lit::from_token_lit(token_lit, expr.span) + let lit = if let ExprKind::Lit(token_lit) = expr.kind + && let Ok(lit) = MetaItemLit::from_token_lit(token_lit, expr.span) { lit } else { - Lit { + MetaItemLit { token_lit: token::Lit::new(token::LitKind::Err, kw::Empty, None), kind: LitKind::Err, span: DUMMY_SP, diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index 991f6e0ba2243..27faef25e6745 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -371,7 +371,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere } } - fn print_literal(&mut self, lit: &ast::Lit) { + fn print_meta_item_lit(&mut self, lit: &ast::MetaItemLit) { self.print_token_literal(lit.token_lit, lit.span) } @@ -488,7 +488,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere self.print_path(&item.path, false, 0); self.space(); self.word_space("="); - let token_str = self.literal_to_string(lit); + let token_str = self.meta_item_lit_to_string(lit); self.word(token_str); } } @@ -498,7 +498,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere fn print_meta_list_item(&mut self, item: &ast::NestedMetaItem) { match item { ast::NestedMetaItem::MetaItem(ref mi) => self.print_meta_item(mi), - ast::NestedMetaItem::Literal(ref lit) => self.print_literal(lit), + ast::NestedMetaItem::Literal(ref lit) => self.print_meta_item_lit(lit), } } @@ -510,7 +510,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere self.print_path(&item.path, false, 0); self.space(); self.word_space("="); - self.print_literal(value); + self.print_meta_item_lit(value); } ast::MetaItemKind::List(ref items) => { self.print_path(&item.path, false, 0); @@ -825,8 +825,8 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere Self::to_string(|s| s.print_expr(e)) } - fn literal_to_string(&self, lit: &ast::Lit) -> String { - Self::to_string(|s| s.print_literal(lit)) + fn meta_item_lit_to_string(&self, lit: &ast::MetaItemLit) -> String { + Self::to_string(|s| s.print_meta_item_lit(lit)) } fn tt_to_string(&self, tt: &TokenTree) -> String { diff --git a/compiler/rustc_attr/src/builtin.rs b/compiler/rustc_attr/src/builtin.rs index 753f62dd589d0..ee2498a5724a1 100644 --- a/compiler/rustc_attr/src/builtin.rs +++ b/compiler/rustc_attr/src/builtin.rs @@ -1,7 +1,7 @@ //! Parsing and validation of builtin attributes use rustc_ast as ast; -use rustc_ast::{Attribute, Lit, LitKind, MetaItem, MetaItemKind, NestedMetaItem, NodeId}; +use rustc_ast::{Attribute, LitKind, MetaItem, MetaItemKind, MetaItemLit, NestedMetaItem, NodeId}; use rustc_ast_pretty::pprust; use rustc_feature::{find_gated_cfg, is_builtin_attr_name, Features, GatedCfg}; use rustc_macros::HashStable_Generic; @@ -658,11 +658,13 @@ pub fn eval_condition( ast::MetaItemKind::List(ref mis) if cfg.name_or_empty() == sym::version => { try_gate_cfg(sym::version, cfg.span, sess, features); let (min_version, span) = match &mis[..] { - [NestedMetaItem::Literal(Lit { kind: LitKind::Str(sym, ..), span, .. })] => { - (sym, span) - } [ - NestedMetaItem::Literal(Lit { span, .. }) + NestedMetaItem::Literal(MetaItemLit { + kind: LitKind::Str(sym, ..), span, .. + }), + ] => (sym, span), + [ + NestedMetaItem::Literal(MetaItemLit { span, .. }) | NestedMetaItem::MetaItem(MetaItem { span, .. }), ] => { sess.emit_err(session_diagnostics::ExpectedVersionLiteral { span: *span }); diff --git a/compiler/rustc_builtin_macros/src/derive.rs b/compiler/rustc_builtin_macros/src/derive.rs index 01f237e6ab5fa..b9b3163acca6a 100644 --- a/compiler/rustc_builtin_macros/src/derive.rs +++ b/compiler/rustc_builtin_macros/src/derive.rs @@ -50,7 +50,7 @@ impl MultiItemModifier for Expander { NestedMetaItem::MetaItem(meta) => Some(meta), NestedMetaItem::Literal(lit) => { // Reject `#[derive("Debug")]`. - report_unexpected_literal(sess, &lit); + report_unexpected_meta_item_lit(sess, &lit); None } }) @@ -127,7 +127,7 @@ fn report_bad_target(sess: &Session, item: &Annotatable, span: Span) -> bool { bad_target } -fn report_unexpected_literal(sess: &Session, lit: &ast::Lit) { +fn report_unexpected_meta_item_lit(sess: &Session, lit: &ast::MetaItemLit) { let help_msg = match lit.token_lit.kind { token::Str if rustc_lexer::is_ident(lit.token_lit.symbol.as_str()) => { format!("try using `#[derive({})]`", lit.token_lit.symbol) diff --git a/compiler/rustc_expand/src/proc_macro_server.rs b/compiler/rustc_expand/src/proc_macro_server.rs index b69556c0e7cc2..7616579611711 100644 --- a/compiler/rustc_expand/src/proc_macro_server.rs +++ b/compiler/rustc_expand/src/proc_macro_server.rs @@ -527,10 +527,7 @@ impl server::TokenStream for Rustc<'_, '_> { } ast::ExprKind::IncludedBytes(bytes) => { let lit = ast::LitKind::ByteStr(bytes.clone()).to_token_lit(); - Ok(tokenstream::TokenStream::token_alone( - token::TokenKind::Literal(lit), - expr.span, - )) + Ok(tokenstream::TokenStream::token_alone(token::TokenKind::Literal(lit), expr.span)) } ast::ExprKind::Unary(ast::UnOp::Neg, e) => match &e.kind { ast::ExprKind::Lit(token_lit) => match token_lit { diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 4c1d95a452d5e..9e92ca8243cc3 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -2145,7 +2145,7 @@ fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool { } fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &ast::Attribute) -> Option { - use rustc_ast::{Lit, LitIntType, LitKind}; + use rustc_ast::{LitIntType, LitKind, MetaItemLit}; if !tcx.features().raw_dylib && tcx.sess.target.arch == "x86" { feature_err( &tcx.sess.parse_sess, @@ -2168,7 +2168,9 @@ fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &ast::Attribute) -> Option { } _ => None, }; - if let Some(Lit { kind: LitKind::Int(ordinal, LitIntType::Unsuffixed), .. }) = sole_meta_list { + if let Some(MetaItemLit { kind: LitKind::Int(ordinal, LitIntType::Unsuffixed), .. }) = + sole_meta_list + { // According to the table at https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#import-header, // the ordinal must fit into 16 bits. Similarly, the Ordinal field in COFFShortExport (defined // in llvm/include/llvm/Object/COFFImportFile.h), which we use to communicate import information diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index bf30a403d9b94..74c58e4fc4821 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -1191,8 +1191,9 @@ impl<'tcx> TyCtxt<'tcx> { debug!("layout_scalar_valid_range: attr={:?}", attr); if let Some( &[ - ast::NestedMetaItem::Literal(ast::Lit { - kind: ast::LitKind::Int(a, _), .. + ast::NestedMetaItem::Literal(ast::MetaItemLit { + kind: ast::LitKind::Int(a, _), + .. }), ], ) = attr.meta_item_list().as_deref() diff --git a/compiler/rustc_parse/src/parser/attr.rs b/compiler/rustc_parse/src/parser/attr.rs index 0ed24fe849c07..c825554e2ffd2 100644 --- a/compiler/rustc_parse/src/parser/attr.rs +++ b/compiler/rustc_parse/src/parser/attr.rs @@ -315,8 +315,9 @@ impl<'a> Parser<'a> { Ok(attrs) } - pub(crate) fn parse_unsuffixed_lit(&mut self) -> PResult<'a, ast::Lit> { - let lit = self.parse_ast_lit()?; + // Note: must be unsuffixed. + pub(crate) fn parse_unsuffixed_meta_item_lit(&mut self) -> PResult<'a, ast::MetaItemLit> { + let lit = self.parse_meta_item_lit()?; debug!("checking if {:?} is unsuffixed", lit); if !lit.kind.is_unsuffixed() { @@ -391,7 +392,7 @@ impl<'a> Parser<'a> { pub(crate) fn parse_meta_item_kind(&mut self) -> PResult<'a, ast::MetaItemKind> { Ok(if self.eat(&token::Eq) { - ast::MetaItemKind::NameValue(self.parse_unsuffixed_lit()?) + ast::MetaItemKind::NameValue(self.parse_unsuffixed_meta_item_lit()?) } else if self.check(&token::OpenDelim(Delimiter::Parenthesis)) { // Matches `meta_seq = ( COMMASEP(meta_item_inner) )`. let (list, _) = self.parse_paren_comma_seq(|p| p.parse_meta_item_inner())?; @@ -403,7 +404,7 @@ impl<'a> Parser<'a> { /// Matches `meta_item_inner : (meta_item | UNSUFFIXED_LIT) ;`. fn parse_meta_item_inner(&mut self) -> PResult<'a, ast::NestedMetaItem> { - match self.parse_unsuffixed_lit() { + match self.parse_unsuffixed_meta_item_lit() { Ok(lit) => return Ok(ast::NestedMetaItem::Literal(lit)), Err(err) => err.cancel(), } diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 9f2267efb8287..e0443a697b504 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -33,10 +33,10 @@ use rustc_ast::util::case::Case; use rustc_ast::util::classify; use rustc_ast::util::parser::{prec_let_scrutinee_needs_par, AssocOp, Fixity}; use rustc_ast::visit::Visitor; -use rustc_ast::{self as ast, AttrStyle, AttrVec, CaptureBy, ExprField, Lit, UnOp, DUMMY_NODE_ID}; +use rustc_ast::{self as ast, AttrStyle, AttrVec, CaptureBy, ExprField, UnOp, DUMMY_NODE_ID}; use rustc_ast::{AnonConst, BinOp, BinOpKind, FnDecl, FnRetTy, MacCall, Param, Ty, TyKind}; use rustc_ast::{Arm, Async, BlockCheckMode, Expr, ExprKind, Label, Movability, RangeLimits}; -use rustc_ast::{ClosureBinder, StmtKind}; +use rustc_ast::{ClosureBinder, MetaItemLit, StmtKind}; use rustc_ast_pretty::pprust; use rustc_errors::{ Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, IntoDiagnostic, PResult, @@ -1631,7 +1631,7 @@ impl<'a> Parser<'a> { &self, lifetime: Ident, err: impl FnOnce(&Self) -> DiagnosticBuilder<'a, ErrorGuaranteed>, - ) -> ast::Lit { + ) -> ast::MetaItemLit { if let Some(mut diag) = self.sess.span_diagnostic.steal_diagnostic(lifetime.span, StashKey::LifetimeIsChar) { @@ -1653,7 +1653,7 @@ impl<'a> Parser<'a> { .emit(); } let name = lifetime.without_first_quote().name; - ast::Lit { + ast::MetaItemLit { token_lit: token::Lit::new(token::LitKind::Char, name, None), kind: ast::LitKind::Char(name.as_str().chars().next().unwrap_or('_')), span: lifetime.span, @@ -1768,8 +1768,8 @@ impl<'a> Parser<'a> { /// Returns a string literal if the next token is a string literal. /// In case of error returns `Some(lit)` if the next token is a literal with a wrong kind, /// and returns `None` if the next token is not literal at all. - pub fn parse_str_lit(&mut self) -> Result> { - match self.parse_opt_ast_lit() { + pub fn parse_str_lit(&mut self) -> Result> { + match self.parse_opt_meta_item_lit() { Some(lit) => match lit.kind { ast::LitKind::Str(symbol_unescaped, style) => Ok(ast::StrLit { style, @@ -1784,7 +1784,7 @@ impl<'a> Parser<'a> { } } - fn handle_missing_lit(&mut self) -> PResult<'a, Lit> { + fn handle_missing_lit(&mut self) -> PResult<'a, MetaItemLit> { if let token::Interpolated(inner) = &self.token.kind { let expr = match inner.as_ref() { token::NtExpr(expr) => Some(expr), @@ -1820,8 +1820,8 @@ impl<'a> Parser<'a> { .or_else(|()| self.handle_missing_lit().map(|lit| (lit.token_lit, lit.span))) } - pub(super) fn parse_ast_lit(&mut self) -> PResult<'a, Lit> { - self.parse_opt_ast_lit().ok_or(()).or_else(|()| self.handle_missing_lit()) + pub(super) fn parse_meta_item_lit(&mut self) -> PResult<'a, MetaItemLit> { + self.parse_opt_meta_item_lit().ok_or(()).or_else(|()| self.handle_missing_lit()) } fn recover_after_dot(&mut self) -> Option { @@ -1867,12 +1867,12 @@ impl<'a> Parser<'a> { /// Matches `lit = true | false | token_lit`. /// Returns `None` if the next token is not a literal. - pub(super) fn parse_opt_ast_lit(&mut self) -> Option { + pub(super) fn parse_opt_meta_item_lit(&mut self) -> Option { let recovered = self.recover_after_dot(); let token = recovered.as_ref().unwrap_or(&self.token); match token::Lit::from_token(token) { Some(token_lit) => { - match Lit::from_token_lit(token_lit, token.span) { + match MetaItemLit::from_token_lit(token_lit, token.span) { Ok(lit) => { self.bump(); Some(lit) @@ -1889,7 +1889,10 @@ impl<'a> Parser<'a> { let suffixless_lit = token::Lit::new(lit.kind, lit.symbol, None); let symbol = Symbol::intern(&suffixless_lit.to_string()); let lit = token::Lit::new(token::Err, symbol, lit.suffix); - Some(Lit::from_token_lit(lit, span).unwrap_or_else(|_| unreachable!())) + Some( + MetaItemLit::from_token_lit(lit, span) + .unwrap_or_else(|_| unreachable!()), + ) } } } diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index 1b56cd72db079..ff1ddfd97dfc6 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -358,7 +358,7 @@ impl<'a> Parser<'a> { /// report error for `let 1x = 123` pub fn report_invalid_identifier_error(&mut self) -> PResult<'a, ()> { if let token::Literal(lit) = self.token.uninterpolate().kind && - rustc_ast::Lit::from_token(&self.token).is_none() && + rustc_ast::MetaItemLit::from_token(&self.token).is_none() && (lit.kind == token::LitKind::Integer || lit.kind == token::LitKind::Float) && self.look_ahead(1, |t| matches!(t.kind, token::Eq) || matches!(t.kind, token::Colon ) ) { return Err(self.sess.create_err(InvalidIdentiferStartsWithNumber { span: self.token.span })); diff --git a/compiler/rustc_parse/src/validate_attr.rs b/compiler/rustc_parse/src/validate_attr.rs index e2f95d74a3d22..59e564114e5cf 100644 --- a/compiler/rustc_parse/src/validate_attr.rs +++ b/compiler/rustc_parse/src/validate_attr.rs @@ -51,7 +51,7 @@ pub fn parse_meta<'a>(sess: &'a ParseSess, attr: &Attribute) -> PResult<'a, Meta } AttrArgs::Eq(_, AttrArgsEq::Ast(expr)) => { if let ast::ExprKind::Lit(token_lit) = expr.kind - && let Ok(lit) = ast::Lit::from_token_lit(token_lit, expr.span) + && let Ok(lit) = ast::MetaItemLit::from_token_lit(token_lit, expr.span) { if token_lit.suffix.is_some() { let mut err = sess.span_diagnostic.struct_span_err( diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index acb9bd8e78a4a..c3794660d5c94 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -8,7 +8,7 @@ use crate::errors::{ self, AttrApplication, DebugVisualizerUnreadable, InvalidAttrAtCrateLevel, ObjectLifetimeErr, OnlyHasEffectOn, TransparentIncompatible, UnrecognizedReprHint, }; -use rustc_ast::{ast, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem}; +use rustc_ast::{ast, AttrStyle, Attribute, LitKind, MetaItemKind, MetaItemLit, NestedMetaItem}; use rustc_data_structures::fx::FxHashMap; use rustc_errors::{fluent, Applicability, MultiSpan}; use rustc_expand::base::resolve_path; @@ -1355,7 +1355,10 @@ impl CheckAttrVisitor<'_> { return false; }; - if matches!(&list[..], &[NestedMetaItem::Literal(Lit { kind: LitKind::Int(..), .. })]) { + if matches!( + &list[..], + &[NestedMetaItem::Literal(MetaItemLit { kind: LitKind::Int(..), .. })] + ) { true } else { self.tcx.sess.emit_err(errors::RustcLayoutScalarValidRangeArg { attr_span: attr.span }); diff --git a/src/tools/clippy/clippy_lints/src/attrs.rs b/src/tools/clippy/clippy_lints/src/attrs.rs index ecf8e83375dbf..018f10f258867 100644 --- a/src/tools/clippy/clippy_lints/src/attrs.rs +++ b/src/tools/clippy/clippy_lints/src/attrs.rs @@ -6,7 +6,7 @@ use clippy_utils::msrvs; use clippy_utils::source::{first_line_of_span, is_present_in_source, snippet_opt, without_block_comments}; use clippy_utils::{extract_msrv_attr, meets_msrv}; use if_chain::if_chain; -use rustc_ast::{AttrKind, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem}; +use rustc_ast::{AttrKind, AttrStyle, Attribute, LitKind, MetaItemKind, MetaItemLit, NestedMetaItem}; use rustc_errors::Applicability; use rustc_hir::{ Block, Expr, ExprKind, ImplItem, ImplItemKind, Item, ItemKind, StmtKind, TraitFn, TraitItem, TraitItemKind, @@ -576,7 +576,7 @@ fn check_attrs(cx: &LateContext<'_>, span: Span, name: Symbol, attrs: &[Attribut } } -fn check_semver(cx: &LateContext<'_>, span: Span, lit: &Lit) { +fn check_semver(cx: &LateContext<'_>, span: Span, lit: &MetaItemLit) { if let LitKind::Str(is, _) = lit.kind { if Version::parse(is.as_str()).is_ok() { return; diff --git a/src/tools/rustfmt/src/attr.rs b/src/tools/rustfmt/src/attr.rs index 23f55db773e6c..8cba2a850e5bc 100644 --- a/src/tools/rustfmt/src/attr.rs +++ b/src/tools/rustfmt/src/attr.rs @@ -527,14 +527,19 @@ pub(crate) trait MetaVisitor<'ast> { fn visit_meta_word(&mut self, _meta_item: &'ast ast::MetaItem) {} - fn visit_meta_name_value(&mut self, _meta_item: &'ast ast::MetaItem, _lit: &'ast ast::Lit) {} + fn visit_meta_name_value( + &mut self, + _meta_item: &'ast ast::MetaItem, + _lit: &'ast ast::MetaItemLit, + ) { + } fn visit_nested_meta_item(&mut self, nm: &'ast ast::NestedMetaItem) { match nm { ast::NestedMetaItem::MetaItem(ref meta_item) => self.visit_meta_item(meta_item), - ast::NestedMetaItem::Literal(ref lit) => self.visit_literal(lit), + ast::NestedMetaItem::Literal(ref lit) => self.visit_meta_item_lit(lit), } } - fn visit_literal(&mut self, _lit: &'ast ast::Lit) {} + fn visit_meta_item_lit(&mut self, _lit: &'ast ast::MetaItemLit) {} } diff --git a/src/tools/rustfmt/src/modules/visitor.rs b/src/tools/rustfmt/src/modules/visitor.rs index ea67977c17a2b..48431693332a6 100644 --- a/src/tools/rustfmt/src/modules/visitor.rs +++ b/src/tools/rustfmt/src/modules/visitor.rs @@ -84,15 +84,19 @@ impl PathVisitor { } impl<'ast> MetaVisitor<'ast> for PathVisitor { - fn visit_meta_name_value(&mut self, meta_item: &'ast ast::MetaItem, lit: &'ast ast::Lit) { + fn visit_meta_name_value( + &mut self, + meta_item: &'ast ast::MetaItem, + lit: &'ast ast::MetaItemLit, + ) { if meta_item.has_name(Symbol::intern("path")) && lit.kind.is_str() { - self.paths.push(lit_to_str(lit)); + self.paths.push(meta_item_lit_to_str(lit)); } } } #[cfg(not(windows))] -fn lit_to_str(lit: &ast::Lit) -> String { +fn meta_item_lit_to_str(lit: &ast::MetaItemLit) -> String { match lit.kind { ast::LitKind::Str(symbol, ..) => symbol.to_string(), _ => unreachable!(), @@ -100,7 +104,7 @@ fn lit_to_str(lit: &ast::Lit) -> String { } #[cfg(windows)] -fn lit_to_str(lit: &ast::Lit) -> String { +fn meta_item_lit_to_str(lit: &ast::MetaItemLit) -> String { match lit.kind { ast::LitKind::Str(symbol, ..) => symbol.as_str().replace("/", "\\"), _ => unreachable!(), From 5011c675adf627e9c2a6e226fa9b504ad740a554 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 23 Nov 2022 15:39:42 +1100 Subject: [PATCH 136/244] Rename `ast::Lit` as `ast::MetaItemLit`. --- clippy_lints/src/attrs.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/attrs.rs b/clippy_lints/src/attrs.rs index ecf8e83375dbf..018f10f258867 100644 --- a/clippy_lints/src/attrs.rs +++ b/clippy_lints/src/attrs.rs @@ -6,7 +6,7 @@ use clippy_utils::msrvs; use clippy_utils::source::{first_line_of_span, is_present_in_source, snippet_opt, without_block_comments}; use clippy_utils::{extract_msrv_attr, meets_msrv}; use if_chain::if_chain; -use rustc_ast::{AttrKind, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem}; +use rustc_ast::{AttrKind, AttrStyle, Attribute, LitKind, MetaItemKind, MetaItemLit, NestedMetaItem}; use rustc_errors::Applicability; use rustc_hir::{ Block, Expr, ExprKind, ImplItem, ImplItemKind, Item, ItemKind, StmtKind, TraitFn, TraitItem, TraitItemKind, @@ -576,7 +576,7 @@ fn check_attrs(cx: &LateContext<'_>, span: Span, name: Symbol, attrs: &[Attribut } } -fn check_semver(cx: &LateContext<'_>, span: Span, lit: &Lit) { +fn check_semver(cx: &LateContext<'_>, span: Span, lit: &MetaItemLit) { if let LitKind::Str(is, _) = lit.kind { if Version::parse(is.as_str()).is_ok() { return; From 1c65264f3cbfb9b6e4b06ff0a89fc706f2d20a85 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 25 Nov 2022 08:18:57 +1100 Subject: [PATCH 137/244] Adjust comments on `StrLit`. --- compiler/rustc_ast/src/ast.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 5d470f1c453fe..3b6716de710b0 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -1737,7 +1737,7 @@ pub struct MetaItemLit { pub span: Span, } -/// Same as `Lit`, but restricted to string literals. +/// Similar to `MetaItemLit`, but restricted to string literals. #[derive(Clone, Copy, Encodable, Decodable, Debug)] pub struct StrLit { /// The original literal token as written in source code. @@ -1746,7 +1746,6 @@ pub struct StrLit { pub suffix: Option, pub span: Span, /// The unescaped "semantic" representation of the literal lowered from the original token. - /// FIXME: Remove this and only create the semantic representation during lowering to HIR. pub symbol_unescaped: Symbol, } From a60e337c884f3201e693e6a5111b663bbc54de27 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 24 Nov 2022 15:00:09 +1100 Subject: [PATCH 138/244] Rename `NestedMetaItem::[Ll]iteral` as `NestedMetaItem::[Ll]it`. We already use a mix of `Literal` and `Lit`. The latter is better because it is shorter without causing any ambiguity. --- compiler/rustc_ast/src/ast.rs | 2 +- compiler/rustc_ast/src/attr/mod.rs | 12 ++++++------ compiler/rustc_ast/src/mut_visit.rs | 2 +- compiler/rustc_ast_pretty/src/pprust/state.rs | 2 +- compiler/rustc_attr/src/builtin.rs | 14 ++++++-------- compiler/rustc_builtin_macros/src/derive.rs | 2 +- compiler/rustc_hir_analysis/src/collect.rs | 2 +- compiler/rustc_interface/src/interface.rs | 2 +- compiler/rustc_lint/src/internal.rs | 4 ++-- compiler/rustc_middle/src/ty/context.rs | 2 +- compiler/rustc_parse/src/parser/attr.rs | 2 +- compiler/rustc_passes/src/check_attr.rs | 9 +++------ compiler/rustc_resolve/src/lib.rs | 2 +- src/librustdoc/clean/cfg.rs | 2 +- src/librustdoc/clean/mod.rs | 2 +- src/librustdoc/clean/types.rs | 2 +- src/tools/rustfmt/src/attr.rs | 6 ++---- src/tools/rustfmt/src/overflow.rs | 4 ++-- src/tools/rustfmt/src/utils.rs | 2 +- 19 files changed, 34 insertions(+), 41 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 3b6716de710b0..d0bb05c36549b 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -489,7 +489,7 @@ pub enum NestedMetaItem { /// A literal. /// /// E.g., `"foo"`, `64`, `true`. - Literal(MetaItemLit), + Lit(MetaItemLit), } /// A spanned compile-time attribute item. diff --git a/compiler/rustc_ast/src/attr/mod.rs b/compiler/rustc_ast/src/attr/mod.rs index a31fe36dac705..7a86b471ba298 100644 --- a/compiler/rustc_ast/src/attr/mod.rs +++ b/compiler/rustc_ast/src/attr/mod.rs @@ -51,9 +51,9 @@ impl NestedMetaItem { } /// Returns the `MetaItemLit` if `self` is a `NestedMetaItem::Literal`s. - pub fn literal(&self) -> Option<&MetaItemLit> { + pub fn lit(&self) -> Option<&MetaItemLit> { match self { - NestedMetaItem::Literal(lit) => Some(lit), + NestedMetaItem::Lit(lit) => Some(lit), _ => None, } } @@ -83,7 +83,7 @@ impl NestedMetaItem { meta_item.meta_item_list().and_then(|meta_item_list| { if meta_item_list.len() == 1 && let Some(ident) = meta_item.ident() - && let Some(lit) = meta_item_list[0].literal() + && let Some(lit) = meta_item_list[0].lit() { return Some((ident.name, lit)); } @@ -655,14 +655,14 @@ impl NestedMetaItem { pub fn span(&self) -> Span { match self { NestedMetaItem::MetaItem(item) => item.span, - NestedMetaItem::Literal(lit) => lit.span, + NestedMetaItem::Lit(lit) => lit.span, } } fn token_trees(&self) -> Vec { match self { NestedMetaItem::MetaItem(item) => item.token_trees(), - NestedMetaItem::Literal(lit) => { + NestedMetaItem::Lit(lit) => { vec![TokenTree::Token(lit.to_token(), Spacing::Alone)] } } @@ -677,7 +677,7 @@ impl NestedMetaItem { if let Some(lit) = MetaItemLit::from_token(token) => { tokens.next(); - return Some(NestedMetaItem::Literal(lit)); + return Some(NestedMetaItem::Lit(lit)); } Some(TokenTree::Delimited(_, Delimiter::Invisible, inner_tokens)) => { let inner_tokens = inner_tokens.clone(); diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 11def67c46365..cb3c54fa03ce5 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -628,7 +628,7 @@ pub fn noop_visit_macro_def(macro_def: &mut MacroDef, vis: &mut T pub fn noop_visit_meta_list_item(li: &mut NestedMetaItem, vis: &mut T) { match li { NestedMetaItem::MetaItem(mi) => vis.visit_meta_item(mi), - NestedMetaItem::Literal(_lit) => {} + NestedMetaItem::Lit(_lit) => {} } } diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index 27faef25e6745..7a9243c511b92 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -498,7 +498,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere fn print_meta_list_item(&mut self, item: &ast::NestedMetaItem) { match item { ast::NestedMetaItem::MetaItem(ref mi) => self.print_meta_item(mi), - ast::NestedMetaItem::Literal(ref lit) => self.print_meta_item_lit(lit), + ast::NestedMetaItem::Lit(ref lit) => self.print_meta_item_lit(lit), } } diff --git a/compiler/rustc_attr/src/builtin.rs b/compiler/rustc_attr/src/builtin.rs index ee2498a5724a1..13b48d8f89ab4 100644 --- a/compiler/rustc_attr/src/builtin.rs +++ b/compiler/rustc_attr/src/builtin.rs @@ -486,7 +486,7 @@ where continue 'outer; } }, - NestedMetaItem::Literal(lit) => { + NestedMetaItem::Lit(lit) => { handle_errors( &sess.parse_sess, lit.span, @@ -658,13 +658,11 @@ pub fn eval_condition( ast::MetaItemKind::List(ref mis) if cfg.name_or_empty() == sym::version => { try_gate_cfg(sym::version, cfg.span, sess, features); let (min_version, span) = match &mis[..] { + [NestedMetaItem::Lit(MetaItemLit { kind: LitKind::Str(sym, ..), span, .. })] => { + (sym, span) + } [ - NestedMetaItem::Literal(MetaItemLit { - kind: LitKind::Str(sym, ..), span, .. - }), - ] => (sym, span), - [ - NestedMetaItem::Literal(MetaItemLit { span, .. }) + NestedMetaItem::Lit(MetaItemLit { span, .. }) | NestedMetaItem::MetaItem(MetaItem { span, .. }), ] => { sess.emit_err(session_diagnostics::ExpectedVersionLiteral { span: *span }); @@ -901,7 +899,7 @@ where continue 'outer; } }, - NestedMetaItem::Literal(lit) => { + NestedMetaItem::Lit(lit) => { handle_errors( &sess.parse_sess, lit.span, diff --git a/compiler/rustc_builtin_macros/src/derive.rs b/compiler/rustc_builtin_macros/src/derive.rs index b9b3163acca6a..c8a2fca00e800 100644 --- a/compiler/rustc_builtin_macros/src/derive.rs +++ b/compiler/rustc_builtin_macros/src/derive.rs @@ -48,7 +48,7 @@ impl MultiItemModifier for Expander { .into_iter() .filter_map(|nested_meta| match nested_meta { NestedMetaItem::MetaItem(meta) => Some(meta), - NestedMetaItem::Literal(lit) => { + NestedMetaItem::Lit(lit) => { // Reject `#[derive("Debug")]`. report_unexpected_meta_item_lit(sess, &lit); None diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 9e92ca8243cc3..638dd6d756b58 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -2158,7 +2158,7 @@ fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &ast::Attribute) -> Option { let meta_item_list = attr.meta_item_list(); let meta_item_list = meta_item_list.as_deref(); let sole_meta_list = match meta_item_list { - Some([item]) => item.literal(), + Some([item]) => item.lit(), Some(_) => { tcx.sess .struct_span_err(attr.span, "incorrect number of arguments to `#[link_ordinal]`") diff --git a/compiler/rustc_interface/src/interface.rs b/compiler/rustc_interface/src/interface.rs index 99c934862c480..4c22ab68a5681 100644 --- a/compiler/rustc_interface/src/interface.rs +++ b/compiler/rustc_interface/src/interface.rs @@ -194,7 +194,7 @@ pub fn parse_check_cfg(specs: Vec) -> CheckCfg { for val in values { if let Some(LitKind::Str(s, _)) = - val.literal().map(|lit| &lit.kind) + val.lit().map(|lit| &lit.kind) { ident_values.insert(s.to_string()); } else { diff --git a/compiler/rustc_lint/src/internal.rs b/compiler/rustc_lint/src/internal.rs index 293f1c5c471a2..a6c7e819482c2 100644 --- a/compiler/rustc_lint/src/internal.rs +++ b/compiler/rustc_lint/src/internal.rs @@ -462,8 +462,8 @@ impl LateLintPass<'_> for BadOptAccess { let Some(attr) = cx.tcx.get_attr(field.did, sym::rustc_lint_opt_deny_field_access) && let Some(items) = attr.meta_item_list() && let Some(item) = items.first() && - let Some(literal) = item.literal() && - let ast::LitKind::Str(val, _) = literal.kind + let Some(lit) = item.lit() && + let ast::LitKind::Str(val, _) = lit.kind { cx.struct_span_lint(BAD_OPT_ACCESS, expr.span, val.as_str(), |lint| lint diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 74c58e4fc4821..297433d37c4f3 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -1191,7 +1191,7 @@ impl<'tcx> TyCtxt<'tcx> { debug!("layout_scalar_valid_range: attr={:?}", attr); if let Some( &[ - ast::NestedMetaItem::Literal(ast::MetaItemLit { + ast::NestedMetaItem::Lit(ast::MetaItemLit { kind: ast::LitKind::Int(a, _), .. }), diff --git a/compiler/rustc_parse/src/parser/attr.rs b/compiler/rustc_parse/src/parser/attr.rs index c825554e2ffd2..c7d239b647f35 100644 --- a/compiler/rustc_parse/src/parser/attr.rs +++ b/compiler/rustc_parse/src/parser/attr.rs @@ -405,7 +405,7 @@ impl<'a> Parser<'a> { /// Matches `meta_item_inner : (meta_item | UNSUFFIXED_LIT) ;`. fn parse_meta_item_inner(&mut self) -> PResult<'a, ast::NestedMetaItem> { match self.parse_unsuffixed_meta_item_lit() { - Ok(lit) => return Ok(ast::NestedMetaItem::Literal(lit)), + Ok(lit) => return Ok(ast::NestedMetaItem::Lit(lit)), Err(err) => err.cancel(), } diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index c3794660d5c94..2e2874dbccb9e 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -715,7 +715,7 @@ impl CheckAttrVisitor<'_> { if let Some(values) = meta.meta_item_list() { let mut errors = 0; for v in values { - match v.literal() { + match v.lit() { Some(l) => match l.kind { LitKind::Str(s, _) => { if !self.check_doc_alias_value(v, s, hir_id, target, true, aliases) { @@ -1355,10 +1355,7 @@ impl CheckAttrVisitor<'_> { return false; }; - if matches!( - &list[..], - &[NestedMetaItem::Literal(MetaItemLit { kind: LitKind::Int(..), .. })] - ) { + if matches!(&list[..], &[NestedMetaItem::Lit(MetaItemLit { kind: LitKind::Int(..), .. })]) { true } else { self.tcx.sess.emit_err(errors::RustcLayoutScalarValidRangeArg { attr_span: attr.span }); @@ -1421,7 +1418,7 @@ impl CheckAttrVisitor<'_> { let arg_count = decl.inputs.len() as u128 + generics.params.len() as u128; let mut invalid_args = vec![]; for meta in list { - if let Some(LitKind::Int(val, _)) = meta.literal().map(|lit| &lit.kind) { + if let Some(LitKind::Int(val, _)) = meta.lit().map(|lit| &lit.kind) { if *val >= arg_count { let span = meta.span(); self.tcx.sess.emit_err(errors::RustcLegacyConstGenericsIndexExceed { diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 82214d4c3c438..4ef89cfb2554f 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1989,7 +1989,7 @@ impl<'a> Resolver<'a> { .find(|a| a.has_name(sym::rustc_legacy_const_generics))?; let mut ret = Vec::new(); for meta in attr.meta_item_list()? { - match meta.literal()?.kind { + match meta.lit()?.kind { LitKind::Int(a, _) => ret.push(a as usize), _ => panic!("invalid arg index"), } diff --git a/src/librustdoc/clean/cfg.rs b/src/librustdoc/clean/cfg.rs index f33f5d27d1a9f..1843a21205cfa 100644 --- a/src/librustdoc/clean/cfg.rs +++ b/src/librustdoc/clean/cfg.rs @@ -50,7 +50,7 @@ impl Cfg { ) -> Result, InvalidCfgError> { match nested_cfg { NestedMetaItem::MetaItem(ref cfg) => Cfg::parse_without(cfg, exclude), - NestedMetaItem::Literal(ref lit) => { + NestedMetaItem::Lit(ref lit) => { Err(InvalidCfgError { msg: "unexpected literal", span: lit.span }) } } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 582586d33febe..42328222fd382 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -893,7 +893,7 @@ fn clean_fn_decl_legacy_const_generics(func: &mut Function, attrs: &[ast::Attrib .filter(|a| a.has_name(sym::rustc_legacy_const_generics)) .filter_map(|a| a.meta_item_list()) { - for (pos, literal) in meta_item_list.iter().filter_map(|meta| meta.literal()).enumerate() { + for (pos, literal) in meta_item_list.iter().filter_map(|meta| meta.lit()).enumerate() { match literal.kind { ast::LitKind::Int(a, _) => { let gen = func.generics.params.remove(0); diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 2894b19877cc7..ed4e9508f4309 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1305,7 +1305,7 @@ impl Attributes { for attr in self.other_attrs.lists(sym::doc).filter(|a| a.has_name(sym::alias)) { if let Some(values) = attr.meta_item_list() { for l in values { - match l.literal().unwrap().kind { + match l.lit().unwrap().kind { ast::LitKind::Str(s, _) => { aliases.insert(s); } diff --git a/src/tools/rustfmt/src/attr.rs b/src/tools/rustfmt/src/attr.rs index 8cba2a850e5bc..2ac703b957b86 100644 --- a/src/tools/rustfmt/src/attr.rs +++ b/src/tools/rustfmt/src/attr.rs @@ -260,9 +260,7 @@ impl Rewrite for ast::NestedMetaItem { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { match self { ast::NestedMetaItem::MetaItem(ref meta_item) => meta_item.rewrite(context, shape), - ast::NestedMetaItem::Literal(ref l) => { - rewrite_literal(context, l.token_lit, l.span, shape) - } + ast::NestedMetaItem::Lit(ref l) => rewrite_literal(context, l.token_lit, l.span, shape), } } } @@ -537,7 +535,7 @@ pub(crate) trait MetaVisitor<'ast> { fn visit_nested_meta_item(&mut self, nm: &'ast ast::NestedMetaItem) { match nm { ast::NestedMetaItem::MetaItem(ref meta_item) => self.visit_meta_item(meta_item), - ast::NestedMetaItem::Literal(ref lit) => self.visit_meta_item_lit(lit), + ast::NestedMetaItem::Lit(ref lit) => self.visit_meta_item_lit(lit), } } diff --git a/src/tools/rustfmt/src/overflow.rs b/src/tools/rustfmt/src/overflow.rs index 6bf8cd0c70be0..af0b95430a197 100644 --- a/src/tools/rustfmt/src/overflow.rs +++ b/src/tools/rustfmt/src/overflow.rs @@ -125,7 +125,7 @@ impl<'a> OverflowableItem<'a> { OverflowableItem::MacroArg(MacroArg::Keyword(..)) => true, OverflowableItem::MacroArg(MacroArg::Expr(expr)) => is_simple_expr(expr), OverflowableItem::NestedMetaItem(nested_meta_item) => match nested_meta_item { - ast::NestedMetaItem::Literal(..) => true, + ast::NestedMetaItem::Lit(..) => true, ast::NestedMetaItem::MetaItem(ref meta_item) => { matches!(meta_item.kind, ast::MetaItemKind::Word) } @@ -169,7 +169,7 @@ impl<'a> OverflowableItem<'a> { }, OverflowableItem::NestedMetaItem(nested_meta_item) if len == 1 => { match nested_meta_item { - ast::NestedMetaItem::Literal(..) => false, + ast::NestedMetaItem::Lit(..) => false, ast::NestedMetaItem::MetaItem(..) => true, } } diff --git a/src/tools/rustfmt/src/utils.rs b/src/tools/rustfmt/src/utils.rs index 136a2c7fce24a..3e884419f1a32 100644 --- a/src/tools/rustfmt/src/utils.rs +++ b/src/tools/rustfmt/src/utils.rs @@ -263,7 +263,7 @@ fn is_skip(meta_item: &MetaItem) -> bool { fn is_skip_nested(meta_item: &NestedMetaItem) -> bool { match meta_item { NestedMetaItem::MetaItem(ref mi) => is_skip(mi), - NestedMetaItem::Literal(_) => false, + NestedMetaItem::Lit(_) => false, } } From 3e72a015662edc61bc33c1461f4392b469086433 Mon Sep 17 00:00:00 2001 From: Florian Bartels Date: Mon, 28 Nov 2022 08:11:19 +0100 Subject: [PATCH 139/244] Run Windows-only tests only on Windows This removes the need to maintain a list of all other OSs which ignore the tests. --- src/test/codegen/dllimports/main.rs | 14 +------------- src/test/codegen/panic-abort-windows.rs | 13 +------------ 2 files changed, 2 insertions(+), 25 deletions(-) diff --git a/src/test/codegen/dllimports/main.rs b/src/test/codegen/dllimports/main.rs index bb3134e81c930..f7978749a9173 100644 --- a/src/test/codegen/dllimports/main.rs +++ b/src/test/codegen/dllimports/main.rs @@ -1,17 +1,5 @@ // This test is for *-windows-msvc only. -// ignore-android -// ignore-dragonfly -// ignore-emscripten -// ignore-freebsd -// ignore-gnu -// ignore-haiku -// ignore-ios -// ignore-linux -// ignore-macos -// ignore-netbsd -// ignore-openbsd -// ignore-solaris -// ignore-sgx no dynamic linking +// only-windows // aux-build:dummy.rs // aux-build:wrapper.rs diff --git a/src/test/codegen/panic-abort-windows.rs b/src/test/codegen/panic-abort-windows.rs index 9ee4bfc471168..d22df6200afae 100644 --- a/src/test/codegen/panic-abort-windows.rs +++ b/src/test/codegen/panic-abort-windows.rs @@ -1,16 +1,5 @@ // This test is for *-windows-msvc only. -// ignore-android -// ignore-dragonfly -// ignore-emscripten -// ignore-freebsd -// ignore-haiku -// ignore-ios -// ignore-linux -// ignore-macos -// ignore-netbsd -// ignore-openbsd -// ignore-solaris -// ignore-sgx +// only-windows // compile-flags: -C no-prepopulate-passes -C panic=abort -O From ab04080b56448c4bcd7e5b79215a1097e1a9b217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 23 Nov 2022 14:28:45 -0800 Subject: [PATCH 140/244] Change multiline span ASCII art visual order --- compiler/rustc_errors/src/emitter.rs | 8 +- compiler/rustc_expand/src/tests.rs | 108 +++++++++--------- ...alloc-error-handler-bad-signature-1.stderr | 24 ++-- ...alloc-error-handler-bad-signature-2.stderr | 24 ++-- src/test/ui/inference/deref-suggestion.stderr | 12 +- src/test/ui/issues/issue-13497-2.stderr | 6 +- src/test/ui/lint/suggestions.stderr | 6 +- .../issue-86188-return-not-in-fn-body.stderr | 12 +- src/test/ui/suggestions/issue-99240-2.stderr | 6 +- .../suggestions/suggest-remove-refs-3.stderr | 10 +- 10 files changed, 111 insertions(+), 105 deletions(-) diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index bf20c7431e4eb..d22f3ca42144a 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -2300,8 +2300,14 @@ impl FileWithAnnotatedLines { } let mut max_depth = 0; // max overlapping multiline spans - for (file, ann) in multiline_annotations { + for (_, ann) in &multiline_annotations { max_depth = max(max_depth, ann.depth); + } + // Change order of multispan depth to minimize the number of overlaps in the ASCII art. + for (_, a) in multiline_annotations.iter_mut() { + a.depth = max_depth - a.depth + 1; + } + for (file, ann) in multiline_annotations { let mut end_ann = ann.as_end(); if !ann.overlaps_exactly { // avoid output like diff --git a/compiler/rustc_expand/src/tests.rs b/compiler/rustc_expand/src/tests.rs index d82a7a54030c6..539b04535a0d0 100644 --- a/compiler/rustc_expand/src/tests.rs +++ b/compiler/rustc_expand/src/tests.rs @@ -272,13 +272,13 @@ error: foo --> test.rs:3:3 | 3 | X0 Y0 - | ____^__- - | | ___| + | ___^__- + | |___| | || 4 | || X1 Y1 5 | || X2 Y2 | ||____^__- `Y` is a good letter too - | |____| + | |_____| | `X` is a good letter "#, @@ -311,12 +311,12 @@ error: foo --> test.rs:3:3 | 3 | X0 Y0 - | ____^__- - | | ___| + | ___^__- + | |___| | || 4 | || Y1 X1 | ||____-__^ `X` is a good letter - | |_____| + | |____| | `Y` is a good letter too "#, @@ -351,13 +351,13 @@ error: foo --> test.rs:3:6 | 3 | X0 Y0 Z0 - | ______^ -4 | | X1 Y1 Z1 - | |_________- + | _______^ +4 | | X1 Y1 Z1 + | | _________- 5 | || X2 Y2 Z2 | ||____^ `X` is a good letter -6 | | X3 Y3 Z3 - | |_____- `Y` is a good letter too +6 | | X3 Y3 Z3 + | |____- `Y` is a good letter too "#, ); @@ -395,15 +395,15 @@ error: foo --> test.rs:3:3 | 3 | X0 Y0 Z0 - | _____^__-__- - | | ____|__| - | || ___| + | ___^__-__- + | |___|__| + | ||___| | ||| 4 | ||| X1 Y1 Z1 5 | ||| X2 Y2 Z2 | |||____^__-__- `Z` label - | ||____|__| - | |____| `Y` is a good letter too + | ||_____|__| + | |______| `Y` is a good letter too | `X` is a good letter "#, @@ -487,17 +487,17 @@ error: foo --> test.rs:3:6 | 3 | X0 Y0 Z0 - | ______^ -4 | | X1 Y1 Z1 - | |____^_- + | _______^ +4 | | X1 Y1 Z1 + | | ____^_- | ||____| - | | `X` is a good letter -5 | | X2 Y2 Z2 - | |____-______- `Y` is a good letter too - | ____| - | | -6 | | X3 Y3 Z3 - | |________- `Z` + | | `X` is a good letter +5 | | X2 Y2 Z2 + | |___-______- `Y` is a good letter too + | ___| + | | +6 | | X3 Y3 Z3 + | |_______- `Z` "#, ); @@ -570,14 +570,14 @@ error: foo --> test.rs:3:6 | 3 | X0 Y0 Z0 - | ______^ -4 | | X1 Y1 Z1 - | |____^____- + | _______^ +4 | | X1 Y1 Z1 + | | ____^____- | ||____| - | | `X` is a good letter -5 | | X2 Y2 Z2 -6 | | X3 Y3 Z3 - | |___________- `Y` is a good letter too + | | `X` is a good letter +5 | | X2 Y2 Z2 +6 | | X3 Y3 Z3 + | |__________- `Y` is a good letter too "#, ); @@ -941,18 +941,18 @@ error: foo --> test.rs:3:6 | 3 | X0 Y0 Z0 - | ______^ -4 | | X1 Y1 Z1 - | |____^____- + | _______^ +4 | | X1 Y1 Z1 + | | ____^____- | ||____| - | | `X` is a good letter -5 | | 1 -6 | | 2 -7 | | 3 -... | -15 | | X2 Y2 Z2 -16 | | X3 Y3 Z3 - | |___________- `Y` is a good letter too + | | `X` is a good letter +5 | | 1 +6 | | 2 +7 | | 3 +... | +15 | | X2 Y2 Z2 +16 | | X3 Y3 Z3 + | |__________- `Y` is a good letter too "#, ); @@ -996,21 +996,21 @@ error: foo --> test.rs:3:6 | 3 | X0 Y0 Z0 - | ______^ -4 | | 1 -5 | | 2 -6 | | 3 -7 | | X1 Y1 Z1 - | |_________- + | _______^ +4 | | 1 +5 | | 2 +6 | | 3 +7 | | X1 Y1 Z1 + | | _________- 8 | || 4 9 | || 5 10 | || 6 11 | || X2 Y2 Z2 | ||__________- `Z` is a good letter too -... | -15 | | 10 -16 | | X3 Y3 Z3 - | |_______^ `Y` is a good letter +... | +15 | | 10 +16 | | X3 Y3 Z3 + | |________^ `Y` is a good letter "#, ); diff --git a/src/test/ui/alloc-error/alloc-error-handler-bad-signature-1.stderr b/src/test/ui/alloc-error/alloc-error-handler-bad-signature-1.stderr index d0911fa3985d0..3e840a28a92ad 100644 --- a/src/test/ui/alloc-error/alloc-error-handler-bad-signature-1.stderr +++ b/src/test/ui/alloc-error/alloc-error-handler-bad-signature-1.stderr @@ -4,16 +4,16 @@ error[E0308]: mismatched types LL | #[alloc_error_handler] | ---------------------- in this procedural macro expansion LL | fn oom( - | _^ - | |_| + | __^ + | | _| | || LL | || info: &Layout, LL | || ) -> () | ||_______- arguments to this function are incorrect -LL | | { -LL | | loop {} -LL | | } - | |__^ expected `&Layout`, found struct `Layout` +LL | | { +LL | | loop {} +LL | | } + | |_^ expected `&Layout`, found struct `Layout` | note: function defined here --> $DIR/alloc-error-handler-bad-signature-1.rs:10:4 @@ -30,16 +30,16 @@ error[E0308]: mismatched types LL | #[alloc_error_handler] | ---------------------- in this procedural macro expansion LL | fn oom( - | _^ - | |_| + | __^ + | | _| | || LL | || info: &Layout, LL | || ) -> () | ||_______^ expected `!`, found `()` -LL | | { -LL | | loop {} -LL | | } - | |__- expected `!` because of return type +LL | | { +LL | | loop {} +LL | | } + | |_- expected `!` because of return type | = note: expected type `!` found unit type `()` diff --git a/src/test/ui/alloc-error/alloc-error-handler-bad-signature-2.stderr b/src/test/ui/alloc-error/alloc-error-handler-bad-signature-2.stderr index 5777279855d97..7cbfd55770515 100644 --- a/src/test/ui/alloc-error/alloc-error-handler-bad-signature-2.stderr +++ b/src/test/ui/alloc-error/alloc-error-handler-bad-signature-2.stderr @@ -4,17 +4,17 @@ error[E0308]: mismatched types LL | #[alloc_error_handler] | ---------------------- in this procedural macro expansion LL | fn oom( - | _^ - | |_| + | __^ + | | _| | || LL | || info: Layout, LL | || ) { | || - | ||_| - | | arguments to this function are incorrect -LL | | loop {} -LL | | } - | |__^ expected struct `Layout`, found struct `core::alloc::Layout` + | | arguments to this function are incorrect +LL | | loop {} +LL | | } + | |_^ expected struct `Layout`, found struct `core::alloc::Layout` | = note: struct `core::alloc::Layout` and struct `Layout` have similar names, but are actually distinct types note: struct `core::alloc::Layout` is defined in crate `core` @@ -42,17 +42,17 @@ error[E0308]: mismatched types LL | #[alloc_error_handler] | ---------------------- in this procedural macro expansion LL | fn oom( - | _^ - | |_| + | __^ + | | _| | || LL | || info: Layout, LL | || ) { | || ^ | ||_| - | | expected `!`, found `()` -LL | | loop {} -LL | | } - | |__- expected `!` because of return type + | | expected `!`, found `()` +LL | | loop {} +LL | | } + | |_- expected `!` because of return type | = note: expected type `!` found unit type `()` diff --git a/src/test/ui/inference/deref-suggestion.stderr b/src/test/ui/inference/deref-suggestion.stderr index d729f2d682a61..034005697b434 100644 --- a/src/test/ui/inference/deref-suggestion.stderr +++ b/src/test/ui/inference/deref-suggestion.stderr @@ -157,11 +157,11 @@ error[E0308]: `if` and `else` have incompatible types --> $DIR/deref-suggestion.rs:69:12 | LL | let val = if true { - | _______________- -LL | | *a - | | -- expected because of this -LL | | } else if true { - | |____________^ + | ________________- +LL | | *a + | | -- expected because of this +LL | | } else if true { + | | ____________^ LL | || LL | || b LL | || } else { @@ -169,7 +169,7 @@ LL | || &0 LL | || }; | || ^ | ||_____| - | |______`if` and `else` have incompatible types + | |_____`if` and `else` have incompatible types | expected `i32`, found `&{integer}` error: aborting due to 13 previous errors diff --git a/src/test/ui/issues/issue-13497-2.stderr b/src/test/ui/issues/issue-13497-2.stderr index 6f72b79f2a5e0..3abeadf9e4bbe 100644 --- a/src/test/ui/issues/issue-13497-2.stderr +++ b/src/test/ui/issues/issue-13497-2.stderr @@ -2,12 +2,12 @@ error[E0515]: cannot return value referencing local variable `rawLines` --> $DIR/issue-13497-2.rs:3:5 | LL | rawLines - | _____^ - | |_____| + | ______^ + | | _____| | || LL | || .iter().map(|l| l.trim()).collect() | ||_______________-___________________________^ returns a value referencing data owned by the current function - | |________________| + | |_______________| | `rawLines` is borrowed here error: aborting due to previous error diff --git a/src/test/ui/lint/suggestions.stderr b/src/test/ui/lint/suggestions.stderr index f4c0e2141b232..4caee777a131b 100644 --- a/src/test/ui/lint/suggestions.stderr +++ b/src/test/ui/lint/suggestions.stderr @@ -41,12 +41,12 @@ warning: variable does not need to be mutable --> $DIR/suggestions.rs:54:13 | LL | let mut - | _____________^ - | |_____________| + | ______________^ + | | _____________| | || LL | || b = 1; | ||____________-^ - | |____________| + | |_____________| | help: remove this `mut` error: const items should never be `#[no_mangle]` diff --git a/src/test/ui/return/issue-86188-return-not-in-fn-body.stderr b/src/test/ui/return/issue-86188-return-not-in-fn-body.stderr index d7eeb3a729099..4f938670e5e2c 100644 --- a/src/test/ui/return/issue-86188-return-not-in-fn-body.stderr +++ b/src/test/ui/return/issue-86188-return-not-in-fn-body.stderr @@ -35,17 +35,17 @@ LL | | } error[E0572]: return statement outside of function body --> $DIR/issue-86188-return-not-in-fn-body.rs:36:10 | -LL | / fn main() { -LL | | -LL | | [(); return || { - | |__________^ +LL | / fn main() { +LL | | +LL | | [(); return || { + | | __________^ LL | || LL | || LL | || let tx; LL | || }]; | ||_____^ the return is part of this body... -LL | | } - | |_- ...not the enclosing function body +LL | | } + | |__- ...not the enclosing function body error: aborting due to 4 previous errors diff --git a/src/test/ui/suggestions/issue-99240-2.stderr b/src/test/ui/suggestions/issue-99240-2.stderr index 2af60f5975992..260df85653b15 100644 --- a/src/test/ui/suggestions/issue-99240-2.stderr +++ b/src/test/ui/suggestions/issue-99240-2.stderr @@ -5,12 +5,12 @@ LL | Unit, | ---- enum variant `Alias::Unit` defined here ... LL | Alias:: - | _____^ - | |_____| + | ______^ + | | _____| | || LL | || Unit(); | ||________^_- call expression requires function - | |_________| + | |________| | | help: `Alias::Unit` is a unit enum variant, and does not take parentheses to be constructed diff --git a/src/test/ui/suggestions/suggest-remove-refs-3.stderr b/src/test/ui/suggestions/suggest-remove-refs-3.stderr index 4d07324273c30..b1afa2b0a6b11 100644 --- a/src/test/ui/suggestions/suggest-remove-refs-3.stderr +++ b/src/test/ui/suggestions/suggest-remove-refs-3.stderr @@ -2,14 +2,14 @@ error[E0277]: `&&&&&Enumerate>` is not an iterat --> $DIR/suggest-remove-refs-3.rs:6:19 | LL | for (i, _) in & & & - | ___________________^ - | |___________________| + | ____________________^ + | | ___________________| | || LL | || & &v | ||___________- help: consider removing 5 leading `&`-references -LL | | .iter() -LL | | .enumerate() { - | |_____________________^ `&&&&&Enumerate>` is not an iterator +LL | | .iter() +LL | | .enumerate() { + | |____________________^ `&&&&&Enumerate>` is not an iterator | = help: the trait `Iterator` is not implemented for `&&&&&Enumerate>` = note: required for `&&&&&Enumerate>` to implement `IntoIterator` From 6f64432a3933475321d0083b1876d07b2f06cd7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 23 Nov 2022 14:51:24 -0800 Subject: [PATCH 141/244] Tweak output --- compiler/rustc_errors/src/emitter.rs | 2 +- ...alloc-error-handler-bad-signature-1.stderr | 16 +++++++-------- ...alloc-error-handler-bad-signature-2.stderr | 20 ++++++++----------- .../suggestions/suggest-remove-refs-3.stderr | 6 +++--- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index d22f3ca42144a..1fabe15ff83ba 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -2282,7 +2282,7 @@ impl FileWithAnnotatedLines { } // Find overlapping multiline annotations, put them at different depths - multiline_annotations.sort_by_key(|&(_, ref ml)| (ml.line_start, ml.line_end)); + multiline_annotations.sort_by_key(|&(_, ref ml)| (ml.line_start, usize::MAX - ml.line_end)); for (_, ann) in multiline_annotations.clone() { for (_, a) in multiline_annotations.iter_mut() { // Move all other multiline annotations overlapping with this one diff --git a/src/test/ui/alloc-error/alloc-error-handler-bad-signature-1.stderr b/src/test/ui/alloc-error/alloc-error-handler-bad-signature-1.stderr index 3e840a28a92ad..dd3665f22ac78 100644 --- a/src/test/ui/alloc-error/alloc-error-handler-bad-signature-1.stderr +++ b/src/test/ui/alloc-error/alloc-error-handler-bad-signature-1.stderr @@ -10,10 +10,10 @@ LL | fn oom( LL | || info: &Layout, LL | || ) -> () | ||_______- arguments to this function are incorrect -LL | | { -LL | | loop {} -LL | | } - | |_^ expected `&Layout`, found struct `Layout` +LL | | { +LL | | loop {} +LL | | } + | |__^ expected `&Layout`, found struct `Layout` | note: function defined here --> $DIR/alloc-error-handler-bad-signature-1.rs:10:4 @@ -36,10 +36,10 @@ LL | fn oom( LL | || info: &Layout, LL | || ) -> () | ||_______^ expected `!`, found `()` -LL | | { -LL | | loop {} -LL | | } - | |_- expected `!` because of return type +LL | | { +LL | | loop {} +LL | | } + | |__- expected `!` because of return type | = note: expected type `!` found unit type `()` diff --git a/src/test/ui/alloc-error/alloc-error-handler-bad-signature-2.stderr b/src/test/ui/alloc-error/alloc-error-handler-bad-signature-2.stderr index 7cbfd55770515..adb652fe61659 100644 --- a/src/test/ui/alloc-error/alloc-error-handler-bad-signature-2.stderr +++ b/src/test/ui/alloc-error/alloc-error-handler-bad-signature-2.stderr @@ -9,12 +9,10 @@ LL | fn oom( | || LL | || info: Layout, LL | || ) { - | || - - | ||_| - | | arguments to this function are incorrect -LL | | loop {} -LL | | } - | |_^ expected struct `Layout`, found struct `core::alloc::Layout` + | ||_- arguments to this function are incorrect +LL | | loop {} +LL | | } + | |__^ expected struct `Layout`, found struct `core::alloc::Layout` | = note: struct `core::alloc::Layout` and struct `Layout` have similar names, but are actually distinct types note: struct `core::alloc::Layout` is defined in crate `core` @@ -47,12 +45,10 @@ LL | fn oom( | || LL | || info: Layout, LL | || ) { - | || ^ - | ||_| - | | expected `!`, found `()` -LL | | loop {} -LL | | } - | |_- expected `!` because of return type + | ||_^ expected `!`, found `()` +LL | | loop {} +LL | | } + | |__- expected `!` because of return type | = note: expected type `!` found unit type `()` diff --git a/src/test/ui/suggestions/suggest-remove-refs-3.stderr b/src/test/ui/suggestions/suggest-remove-refs-3.stderr index b1afa2b0a6b11..31cca323d0e42 100644 --- a/src/test/ui/suggestions/suggest-remove-refs-3.stderr +++ b/src/test/ui/suggestions/suggest-remove-refs-3.stderr @@ -7,9 +7,9 @@ LL | for (i, _) in & & & | || LL | || & &v | ||___________- help: consider removing 5 leading `&`-references -LL | | .iter() -LL | | .enumerate() { - | |____________________^ `&&&&&Enumerate>` is not an iterator +LL | | .iter() +LL | | .enumerate() { + | |_____________________^ `&&&&&Enumerate>` is not an iterator | = help: the trait `Iterator` is not implemented for `&&&&&Enumerate>` = note: required for `&&&&&Enumerate>` to implement `IntoIterator` From 136ffa2ab1e4c1b8c1f30a775040b63f65b871da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 28 Nov 2022 00:41:31 -0800 Subject: [PATCH 142/244] fix clippy tests --- .../clippy/tests/ui/async_yields_async.stderr | 20 +++++++++---------- .../ui/result_map_unit_fn_unfixable.stderr | 6 +++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/tools/clippy/tests/ui/async_yields_async.stderr b/src/tools/clippy/tests/ui/async_yields_async.stderr index b0c4215e7ddf1..92ba359296780 100644 --- a/src/tools/clippy/tests/ui/async_yields_async.stderr +++ b/src/tools/clippy/tests/ui/async_yields_async.stderr @@ -2,14 +2,14 @@ error: an async construct yields a type which is itself awaitable --> $DIR/async_yields_async.rs:39:9 | LL | let _h = async { - | ____________________- -LL | | async { - | |_________^ + | _____________________- +LL | | async { + | | _________^ LL | || 3 LL | || } | ||_________^ awaitable value not awaited -LL | | }; - | |_____- outer async construct +LL | | }; + | |______- outer async construct | = note: `-D clippy::async-yields-async` implied by `-D warnings` help: consider awaiting this value @@ -36,14 +36,14 @@ error: an async construct yields a type which is itself awaitable --> $DIR/async_yields_async.rs:50:9 | LL | let _j = async || { - | _______________________- -LL | | async { - | |_________^ + | ________________________- +LL | | async { + | | _________^ LL | || 3 LL | || } | ||_________^ awaitable value not awaited -LL | | }; - | |_____- outer async construct +LL | | }; + | |______- outer async construct | help: consider awaiting this value | diff --git a/src/tools/clippy/tests/ui/result_map_unit_fn_unfixable.stderr b/src/tools/clippy/tests/ui/result_map_unit_fn_unfixable.stderr index 88e4efdb0f054..2e1eb8eb18066 100644 --- a/src/tools/clippy/tests/ui/result_map_unit_fn_unfixable.stderr +++ b/src/tools/clippy/tests/ui/result_map_unit_fn_unfixable.stderr @@ -20,14 +20,14 @@ error: called `map(f)` on an `Result` value where `f` is a closure that returns --> $DIR/result_map_unit_fn_unfixable.rs:29:5 | LL | x.field.map(|value| { - | _____^ - | |_____| + | ______^ + | | _____| | || LL | || do_nothing(value); LL | || do_nothing(value) LL | || }); | ||______^- help: try this: `if let Ok(value) = x.field { ... }` - | |_______| + | |______| | error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()` From fc108d4b61cb56943c5e44d6d89d7418bfa6c1c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 28 Nov 2022 00:41:31 -0800 Subject: [PATCH 143/244] fix clippy tests --- tests/ui/async_yields_async.stderr | 20 ++++++++++---------- tests/ui/result_map_unit_fn_unfixable.stderr | 6 +++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/ui/async_yields_async.stderr b/tests/ui/async_yields_async.stderr index b0c4215e7ddf1..92ba359296780 100644 --- a/tests/ui/async_yields_async.stderr +++ b/tests/ui/async_yields_async.stderr @@ -2,14 +2,14 @@ error: an async construct yields a type which is itself awaitable --> $DIR/async_yields_async.rs:39:9 | LL | let _h = async { - | ____________________- -LL | | async { - | |_________^ + | _____________________- +LL | | async { + | | _________^ LL | || 3 LL | || } | ||_________^ awaitable value not awaited -LL | | }; - | |_____- outer async construct +LL | | }; + | |______- outer async construct | = note: `-D clippy::async-yields-async` implied by `-D warnings` help: consider awaiting this value @@ -36,14 +36,14 @@ error: an async construct yields a type which is itself awaitable --> $DIR/async_yields_async.rs:50:9 | LL | let _j = async || { - | _______________________- -LL | | async { - | |_________^ + | ________________________- +LL | | async { + | | _________^ LL | || 3 LL | || } | ||_________^ awaitable value not awaited -LL | | }; - | |_____- outer async construct +LL | | }; + | |______- outer async construct | help: consider awaiting this value | diff --git a/tests/ui/result_map_unit_fn_unfixable.stderr b/tests/ui/result_map_unit_fn_unfixable.stderr index 88e4efdb0f054..2e1eb8eb18066 100644 --- a/tests/ui/result_map_unit_fn_unfixable.stderr +++ b/tests/ui/result_map_unit_fn_unfixable.stderr @@ -20,14 +20,14 @@ error: called `map(f)` on an `Result` value where `f` is a closure that returns --> $DIR/result_map_unit_fn_unfixable.rs:29:5 | LL | x.field.map(|value| { - | _____^ - | |_____| + | ______^ + | | _____| | || LL | || do_nothing(value); LL | || do_nothing(value) LL | || }); | ||______^- help: try this: `if let Ok(value) = x.field { ... }` - | |_______| + | |______| | error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()` From 58dd62a756b634ee705d4133684b4cf48d48df3d Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 28 Nov 2022 10:45:12 +0100 Subject: [PATCH 144/244] sanity_check_layout: less rightwards drift --- .../rustc_ty_utils/src/layout_sanity_check.rs | 527 +++++++++--------- 1 file changed, 265 insertions(+), 262 deletions(-) diff --git a/compiler/rustc_ty_utils/src/layout_sanity_check.rs b/compiler/rustc_ty_utils/src/layout_sanity_check.rs index 9eb8f684bdb59..3339e910d7c77 100644 --- a/compiler/rustc_ty_utils/src/layout_sanity_check.rs +++ b/compiler/rustc_ty_utils/src/layout_sanity_check.rs @@ -20,283 +20,286 @@ pub(super) fn sanity_check_layout<'tcx>( bug!("size is not a multiple of align, in the following layout:\n{layout:#?}"); } - if cfg!(debug_assertions) { - /// Yields non-ZST fields of the type - fn non_zst_fields<'tcx, 'a>( - cx: &'a LayoutCx<'tcx, TyCtxt<'tcx>>, - layout: &'a TyAndLayout<'tcx>, - ) -> impl Iterator)> + 'a { - (0..layout.layout.fields().count()).filter_map(|i| { - let field = layout.field(cx, i); - // Also checking `align == 1` here leads to test failures in - // `layout/zero-sized-array-union.rs`, where a type has a zero-size field with - // alignment 4 that still gets ignored during layout computation (which is okay - // since other fields already force alignment 4). - let zst = field.is_zst(); - (!zst).then(|| (layout.fields.offset(i), field)) - }) - } + if !cfg!(debug_assertions) { + // Stop here, the rest is kind of expensive. + return; + } - fn skip_newtypes<'tcx>( - cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, - layout: &TyAndLayout<'tcx>, - ) -> TyAndLayout<'tcx> { - if matches!(layout.layout.variants(), Variants::Multiple { .. }) { - // Definitely not a newtype of anything. - return *layout; - } - let mut fields = non_zst_fields(cx, layout); - let Some(first) = fields.next() else { - // No fields here, so this could be a primitive or enum -- either way it's not a newtype around a thing - return *layout - }; - if fields.next().is_none() { - let (offset, first) = first; - if offset == Size::ZERO && first.layout.size() == layout.size { - // This is a newtype, so keep recursing. - // FIXME(RalfJung): I don't think it would be correct to do any checks for - // alignment here, so we don't. Is that correct? - return skip_newtypes(cx, &first); - } + /// Yields non-ZST fields of the type + fn non_zst_fields<'tcx, 'a>( + cx: &'a LayoutCx<'tcx, TyCtxt<'tcx>>, + layout: &'a TyAndLayout<'tcx>, + ) -> impl Iterator)> + 'a { + (0..layout.layout.fields().count()).filter_map(|i| { + let field = layout.field(cx, i); + // Also checking `align == 1` here leads to test failures in + // `layout/zero-sized-array-union.rs`, where a type has a zero-size field with + // alignment 4 that still gets ignored during layout computation (which is okay + // since other fields already force alignment 4). + let zst = field.is_zst(); + (!zst).then(|| (layout.fields.offset(i), field)) + }) + } + + fn skip_newtypes<'tcx>( + cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, + layout: &TyAndLayout<'tcx>, + ) -> TyAndLayout<'tcx> { + if matches!(layout.layout.variants(), Variants::Multiple { .. }) { + // Definitely not a newtype of anything. + return *layout; + } + let mut fields = non_zst_fields(cx, layout); + let Some(first) = fields.next() else { + // No fields here, so this could be a primitive or enum -- either way it's not a newtype around a thing + return *layout + }; + if fields.next().is_none() { + let (offset, first) = first; + if offset == Size::ZERO && first.layout.size() == layout.size { + // This is a newtype, so keep recursing. + // FIXME(RalfJung): I don't think it would be correct to do any checks for + // alignment here, so we don't. Is that correct? + return skip_newtypes(cx, &first); } - // No more newtypes here. - *layout } + // No more newtypes here. + *layout + } - fn check_layout_abi<'tcx>(cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, layout: &TyAndLayout<'tcx>) { - match layout.layout.abi() { - Abi::Scalar(scalar) => { - // No padding in scalars. - let size = scalar.size(cx); - let align = scalar.align(cx).abi; - assert_eq!( - layout.layout.size(), - size, - "size mismatch between ABI and layout in {layout:#?}" - ); - assert_eq!( - layout.layout.align().abi, - align, - "alignment mismatch between ABI and layout in {layout:#?}" - ); - // Check that this matches the underlying field. - let inner = skip_newtypes(cx, layout); - assert!( - matches!(inner.layout.abi(), Abi::Scalar(_)), - "`Scalar` type {} is newtype around non-`Scalar` type {}", - layout.ty, - inner.ty - ); - match inner.layout.fields() { - FieldsShape::Primitive => { - // Fine. - } - FieldsShape::Union(..) => { - // FIXME: I guess we could also check something here? Like, look at all fields? - return; - } - FieldsShape::Arbitrary { .. } => { - // Should be an enum, the only field is the discriminant. - assert!( - inner.ty.is_enum(), - "`Scalar` layout for non-primitive non-enum type {}", - inner.ty - ); - assert_eq!( - inner.layout.fields().count(), - 1, - "`Scalar` layout for multiple-field type in {inner:#?}", - ); - let offset = inner.layout.fields().offset(0); - let field = inner.field(cx, 0); - // The field should be at the right offset, and match the `scalar` layout. - assert_eq!( - offset, - Size::ZERO, - "`Scalar` field at non-0 offset in {inner:#?}", - ); - assert_eq!( - field.size, size, - "`Scalar` field with bad size in {inner:#?}", - ); - assert_eq!( - field.align.abi, align, - "`Scalar` field with bad align in {inner:#?}", - ); - assert!( - matches!(field.abi, Abi::Scalar(_)), - "`Scalar` field with bad ABI in {inner:#?}", - ); - } - _ => { - panic!("`Scalar` layout for non-primitive non-enum type {}", inner.ty); - } + fn check_layout_abi<'tcx>(cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, layout: &TyAndLayout<'tcx>) { + match layout.layout.abi() { + Abi::Scalar(scalar) => { + // No padding in scalars. + let size = scalar.size(cx); + let align = scalar.align(cx).abi; + assert_eq!( + layout.layout.size(), + size, + "size mismatch between ABI and layout in {layout:#?}" + ); + assert_eq!( + layout.layout.align().abi, + align, + "alignment mismatch between ABI and layout in {layout:#?}" + ); + // Check that this matches the underlying field. + let inner = skip_newtypes(cx, layout); + assert!( + matches!(inner.layout.abi(), Abi::Scalar(_)), + "`Scalar` type {} is newtype around non-`Scalar` type {}", + layout.ty, + inner.ty + ); + match inner.layout.fields() { + FieldsShape::Primitive => { + // Fine. } - } - Abi::ScalarPair(scalar1, scalar2) => { - // Sanity-check scalar pairs. These are a bit more flexible and support - // padding, but we can at least ensure both fields actually fit into the layout - // and the alignment requirement has not been weakened. - let size1 = scalar1.size(cx); - let align1 = scalar1.align(cx).abi; - let size2 = scalar2.size(cx); - let align2 = scalar2.align(cx).abi; - assert!( - layout.layout.align().abi >= cmp::max(align1, align2), - "alignment mismatch between ABI and layout in {layout:#?}", - ); - let field2_offset = size1.align_to(align2); - assert!( - layout.layout.size() >= field2_offset + size2, - "size mismatch between ABI and layout in {layout:#?}" - ); - // Check that the underlying pair of fields matches. - let inner = skip_newtypes(cx, layout); - assert!( - matches!(inner.layout.abi(), Abi::ScalarPair(..)), - "`ScalarPair` type {} is newtype around non-`ScalarPair` type {}", - layout.ty, - inner.ty - ); - if matches!(inner.layout.variants(), Variants::Multiple { .. }) { - // FIXME: ScalarPair for enums is enormously complicated and it is very hard - // to check anything about them. + FieldsShape::Union(..) => { + // FIXME: I guess we could also check something here? Like, look at all fields? return; } - match inner.layout.fields() { - FieldsShape::Arbitrary { .. } => { - // Checked below. - } - FieldsShape::Union(..) => { - // FIXME: I guess we could also check something here? Like, look at all fields? - return; - } - _ => { - panic!("`ScalarPair` layout with unexpected field shape in {inner:#?}"); - } + FieldsShape::Arbitrary { .. } => { + // Should be an enum, the only field is the discriminant. + assert!( + inner.ty.is_enum(), + "`Scalar` layout for non-primitive non-enum type {}", + inner.ty + ); + assert_eq!( + inner.layout.fields().count(), + 1, + "`Scalar` layout for multiple-field type in {inner:#?}", + ); + let offset = inner.layout.fields().offset(0); + let field = inner.field(cx, 0); + // The field should be at the right offset, and match the `scalar` layout. + assert_eq!( + offset, + Size::ZERO, + "`Scalar` field at non-0 offset in {inner:#?}", + ); + assert_eq!(field.size, size, "`Scalar` field with bad size in {inner:#?}",); + assert_eq!( + field.align.abi, align, + "`Scalar` field with bad align in {inner:#?}", + ); + assert!( + matches!(field.abi, Abi::Scalar(_)), + "`Scalar` field with bad ABI in {inner:#?}", + ); + } + _ => { + panic!("`Scalar` layout for non-primitive non-enum type {}", inner.ty); } - let mut fields = non_zst_fields(cx, &inner); - let (offset1, field1) = fields.next().unwrap_or_else(|| { - panic!("`ScalarPair` layout for type with not even one non-ZST field: {inner:#?}") - }); - let (offset2, field2) = fields.next().unwrap_or_else(|| { - panic!("`ScalarPair` layout for type with less than two non-ZST fields: {inner:#?}") - }); - assert!( - fields.next().is_none(), - "`ScalarPair` layout for type with at least three non-ZST fields: {inner:#?}" - ); - // The fields might be in opposite order. - let (offset1, field1, offset2, field2) = if offset1 <= offset2 { - (offset1, field1, offset2, field2) - } else { - (offset2, field2, offset1, field1) - }; - // The fields should be at the right offset, and match the `scalar` layout. - assert_eq!( - offset1, - Size::ZERO, - "`ScalarPair` first field at non-0 offset in {inner:#?}", - ); - assert_eq!( - field1.size, size1, - "`ScalarPair` first field with bad size in {inner:#?}", - ); - assert_eq!( - field1.align.abi, align1, - "`ScalarPair` first field with bad align in {inner:#?}", - ); - assert!( - matches!(field1.abi, Abi::Scalar(_)), - "`ScalarPair` first field with bad ABI in {inner:#?}", - ); - assert_eq!( - offset2, field2_offset, - "`ScalarPair` second field at bad offset in {inner:#?}", - ); - assert_eq!( - field2.size, size2, - "`ScalarPair` second field with bad size in {inner:#?}", - ); - assert_eq!( - field2.align.abi, align2, - "`ScalarPair` second field with bad align in {inner:#?}", - ); - assert!( - matches!(field2.abi, Abi::Scalar(_)), - "`ScalarPair` second field with bad ABI in {inner:#?}", - ); } - Abi::Vector { count, element } => { - // No padding in vectors. Alignment can be strengthened, though. - assert!( - layout.layout.align().abi >= element.align(cx).abi, - "alignment mismatch between ABI and layout in {layout:#?}" - ); - let size = element.size(cx) * count; - assert_eq!( - layout.layout.size(), - size.align_to(cx.data_layout().vector_align(size).abi), - "size mismatch between ABI and layout in {layout:#?}" - ); + } + Abi::ScalarPair(scalar1, scalar2) => { + // Sanity-check scalar pairs. These are a bit more flexible and support + // padding, but we can at least ensure both fields actually fit into the layout + // and the alignment requirement has not been weakened. + let size1 = scalar1.size(cx); + let align1 = scalar1.align(cx).abi; + let size2 = scalar2.size(cx); + let align2 = scalar2.align(cx).abi; + assert!( + layout.layout.align().abi >= cmp::max(align1, align2), + "alignment mismatch between ABI and layout in {layout:#?}", + ); + let field2_offset = size1.align_to(align2); + assert!( + layout.layout.size() >= field2_offset + size2, + "size mismatch between ABI and layout in {layout:#?}" + ); + // Check that the underlying pair of fields matches. + let inner = skip_newtypes(cx, layout); + assert!( + matches!(inner.layout.abi(), Abi::ScalarPair(..)), + "`ScalarPair` type {} is newtype around non-`ScalarPair` type {}", + layout.ty, + inner.ty + ); + if matches!(inner.layout.variants(), Variants::Multiple { .. }) { + // FIXME: ScalarPair for enums is enormously complicated and it is very hard + // to check anything about them. + return; + } + match inner.layout.fields() { + FieldsShape::Arbitrary { .. } => { + // Checked below. + } + FieldsShape::Union(..) => { + // FIXME: I guess we could also check something here? Like, look at all fields? + return; + } + _ => { + panic!("`ScalarPair` layout with unexpected field shape in {inner:#?}"); + } } - Abi::Uninhabited | Abi::Aggregate { .. } => {} // Nothing to check. + let mut fields = non_zst_fields(cx, &inner); + let (offset1, field1) = fields.next().unwrap_or_else(|| { + panic!( + "`ScalarPair` layout for type with not even one non-ZST field: {inner:#?}" + ) + }); + let (offset2, field2) = fields.next().unwrap_or_else(|| { + panic!( + "`ScalarPair` layout for type with less than two non-ZST fields: {inner:#?}" + ) + }); + assert!( + fields.next().is_none(), + "`ScalarPair` layout for type with at least three non-ZST fields: {inner:#?}" + ); + // The fields might be in opposite order. + let (offset1, field1, offset2, field2) = if offset1 <= offset2 { + (offset1, field1, offset2, field2) + } else { + (offset2, field2, offset1, field1) + }; + // The fields should be at the right offset, and match the `scalar` layout. + assert_eq!( + offset1, + Size::ZERO, + "`ScalarPair` first field at non-0 offset in {inner:#?}", + ); + assert_eq!( + field1.size, size1, + "`ScalarPair` first field with bad size in {inner:#?}", + ); + assert_eq!( + field1.align.abi, align1, + "`ScalarPair` first field with bad align in {inner:#?}", + ); + assert!( + matches!(field1.abi, Abi::Scalar(_)), + "`ScalarPair` first field with bad ABI in {inner:#?}", + ); + assert_eq!( + offset2, field2_offset, + "`ScalarPair` second field at bad offset in {inner:#?}", + ); + assert_eq!( + field2.size, size2, + "`ScalarPair` second field with bad size in {inner:#?}", + ); + assert_eq!( + field2.align.abi, align2, + "`ScalarPair` second field with bad align in {inner:#?}", + ); + assert!( + matches!(field2.abi, Abi::Scalar(_)), + "`ScalarPair` second field with bad ABI in {inner:#?}", + ); } + Abi::Vector { count, element } => { + // No padding in vectors. Alignment can be strengthened, though. + assert!( + layout.layout.align().abi >= element.align(cx).abi, + "alignment mismatch between ABI and layout in {layout:#?}" + ); + let size = element.size(cx) * count; + assert_eq!( + layout.layout.size(), + size.align_to(cx.data_layout().vector_align(size).abi), + "size mismatch between ABI and layout in {layout:#?}" + ); + } + Abi::Uninhabited | Abi::Aggregate { .. } => {} // Nothing to check. } + } - check_layout_abi(cx, layout); + check_layout_abi(cx, layout); - if let Variants::Multiple { variants, .. } = &layout.variants { - for variant in variants.iter() { - // No nested "multiple". - assert!(matches!(variant.variants, Variants::Single { .. })); - // Variants should have the same or a smaller size as the full thing, - // and same for alignment. - if variant.size > layout.size { - bug!( - "Type with size {} bytes has variant with size {} bytes: {layout:#?}", - layout.size.bytes(), - variant.size.bytes(), - ) - } - if variant.align.abi > layout.align.abi { - bug!( - "Type with alignment {} bytes has variant with alignment {} bytes: {layout:#?}", - layout.align.abi.bytes(), - variant.align.abi.bytes(), - ) - } - // Skip empty variants. - if variant.size == Size::ZERO - || variant.fields.count() == 0 - || variant.abi.is_uninhabited() - { - // These are never actually accessed anyway, so we can skip the coherence check - // for them. They also fail that check, since they have - // `Aggregate`/`Uninhbaited` ABI even when the main type is - // `Scalar`/`ScalarPair`. (Note that sometimes, variants with fields have size - // 0, and sometimes, variants without fields have non-0 size.) - continue; - } - // The top-level ABI and the ABI of the variants should be coherent. - let scalar_coherent = |s1: Scalar, s2: Scalar| { - s1.size(cx) == s2.size(cx) && s1.align(cx) == s2.align(cx) - }; - let abi_coherent = match (layout.abi, variant.abi) { - (Abi::Scalar(s1), Abi::Scalar(s2)) => scalar_coherent(s1, s2), - (Abi::ScalarPair(a1, b1), Abi::ScalarPair(a2, b2)) => { - scalar_coherent(a1, a2) && scalar_coherent(b1, b2) - } - (Abi::Uninhabited, _) => true, - (Abi::Aggregate { .. }, _) => true, - _ => false, - }; - if !abi_coherent { - bug!( - "Variant ABI is incompatible with top-level ABI:\nvariant={:#?}\nTop-level: {layout:#?}", - variant - ); + if let Variants::Multiple { variants, .. } = &layout.variants { + for variant in variants.iter() { + // No nested "multiple". + assert!(matches!(variant.variants, Variants::Single { .. })); + // Variants should have the same or a smaller size as the full thing, + // and same for alignment. + if variant.size > layout.size { + bug!( + "Type with size {} bytes has variant with size {} bytes: {layout:#?}", + layout.size.bytes(), + variant.size.bytes(), + ) + } + if variant.align.abi > layout.align.abi { + bug!( + "Type with alignment {} bytes has variant with alignment {} bytes: {layout:#?}", + layout.align.abi.bytes(), + variant.align.abi.bytes(), + ) + } + // Skip empty variants. + if variant.size == Size::ZERO + || variant.fields.count() == 0 + || variant.abi.is_uninhabited() + { + // These are never actually accessed anyway, so we can skip the coherence check + // for them. They also fail that check, since they have + // `Aggregate`/`Uninhbaited` ABI even when the main type is + // `Scalar`/`ScalarPair`. (Note that sometimes, variants with fields have size + // 0, and sometimes, variants without fields have non-0 size.) + continue; + } + // The top-level ABI and the ABI of the variants should be coherent. + let scalar_coherent = + |s1: Scalar, s2: Scalar| s1.size(cx) == s2.size(cx) && s1.align(cx) == s2.align(cx); + let abi_coherent = match (layout.abi, variant.abi) { + (Abi::Scalar(s1), Abi::Scalar(s2)) => scalar_coherent(s1, s2), + (Abi::ScalarPair(a1, b1), Abi::ScalarPair(a2, b2)) => { + scalar_coherent(a1, a2) && scalar_coherent(b1, b2) } + (Abi::Uninhabited, _) => true, + (Abi::Aggregate { .. }, _) => true, + _ => false, + }; + if !abi_coherent { + bug!( + "Variant ABI is incompatible with top-level ABI:\nvariant={:#?}\nTop-level: {layout:#?}", + variant + ); } } } From 11525e506e2e469d5b13981420cd9702eddeb2e9 Mon Sep 17 00:00:00 2001 From: Neutron3529 Date: Mon, 28 Nov 2022 18:31:55 +0800 Subject: [PATCH 145/244] fix document https://users.rust-lang.org/t/is-the-document-in-sortedmap-in-rustc-data-structures-sorted-map-correct/84939 SortedMap have `O(n)` insertions and removal rather than `O(log(n))` --- compiler/rustc_data_structures/src/sorted_map.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_data_structures/src/sorted_map.rs b/compiler/rustc_data_structures/src/sorted_map.rs index fe257e10205fa..d607a5c8314e1 100644 --- a/compiler/rustc_data_structures/src/sorted_map.rs +++ b/compiler/rustc_data_structures/src/sorted_map.rs @@ -10,8 +10,8 @@ mod index_map; pub use index_map::SortedIndexMultiMap; /// `SortedMap` is a data structure with similar characteristics as BTreeMap but -/// slightly different trade-offs: lookup, insertion, and removal are *O*(log(*n*)) -/// and elements can be iterated in order cheaply. +/// slightly different trade-offs: lookup is *O*(log(*n*)), insertion and removal +/// are *O*(*n*) but elements can be iterated in order cheaply. /// /// `SortedMap` can be faster than a `BTreeMap` for small sizes (<50) since it /// stores data in a more compact way. It also supports accessing contiguous From 891a4da4f3cf99d5ee6413d9a6948031306179af Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 28 Nov 2022 10:45:22 +0100 Subject: [PATCH 146/244] stricter alignment enforcement for ScalarPair and Vector --- .../rustc_ty_utils/src/layout_sanity_check.rs | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_ty_utils/src/layout_sanity_check.rs b/compiler/rustc_ty_utils/src/layout_sanity_check.rs index 3339e910d7c77..a5311dbd1b770 100644 --- a/compiler/rustc_ty_utils/src/layout_sanity_check.rs +++ b/compiler/rustc_ty_utils/src/layout_sanity_check.rs @@ -135,22 +135,24 @@ pub(super) fn sanity_check_layout<'tcx>( } } Abi::ScalarPair(scalar1, scalar2) => { - // Sanity-check scalar pairs. These are a bit more flexible and support - // padding, but we can at least ensure both fields actually fit into the layout - // and the alignment requirement has not been weakened. + // Sanity-check scalar pairs. Computing the expected size and alignment is a bit of work. let size1 = scalar1.size(cx); let align1 = scalar1.align(cx).abi; let size2 = scalar2.size(cx); let align2 = scalar2.align(cx).abi; - assert!( - layout.layout.align().abi >= cmp::max(align1, align2), - "alignment mismatch between ABI and layout in {layout:#?}", - ); + let align = cmp::max(align1, align2); let field2_offset = size1.align_to(align2); - assert!( - layout.layout.size() >= field2_offset + size2, + let size = (field2_offset + size2).align_to(align); + assert_eq!( + layout.layout.size(), + size, "size mismatch between ABI and layout in {layout:#?}" ); + assert_eq!( + layout.layout.align().abi, + align, + "alignment mismatch between ABI and layout in {layout:#?}", + ); // Check that the underlying pair of fields matches. let inner = skip_newtypes(cx, layout); assert!( @@ -233,17 +235,22 @@ pub(super) fn sanity_check_layout<'tcx>( ); } Abi::Vector { count, element } => { - // No padding in vectors. Alignment can be strengthened, though. - assert!( - layout.layout.align().abi >= element.align(cx).abi, - "alignment mismatch between ABI and layout in {layout:#?}" - ); + // No padding in vectors, except possibly for trailing padding to make the size a multiple of align. let size = element.size(cx) * count; + let align = cx.data_layout().vector_align(size).abi; + let size = size.align_to(align); // needed e.g. for vectors of size 3 + assert!(align >= element.align(cx).abi); // just sanity-checking `vector_align`. assert_eq!( layout.layout.size(), - size.align_to(cx.data_layout().vector_align(size).abi), + size, "size mismatch between ABI and layout in {layout:#?}" ); + assert_eq!( + layout.layout.align().abi, + align, + "alignment mismatch between ABI and layout in {layout:#?}" + ); + // FIXME: Do some kind of check of the inner type, like for Scalar and ScalarPair. } Abi::Uninhabited | Abi::Aggregate { .. } => {} // Nothing to check. } From 348a0585054d633b50f515d95e3c42ea8721e106 Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Tue, 8 Nov 2022 23:23:08 +0530 Subject: [PATCH 147/244] Extract WStrUnits to sys_common::wstr This commit extracts WStrUnits from sys::windows::args to sys_common::wstr. This allows using the same structure for other targets which use wtf8 (example UEFI). This was originally a part of https://github.com/rust-lang/rust/pull/100316 Signed-off-by: Ayush Singh --- library/std/src/sys/windows/args.rs | 54 +------------------------- library/std/src/sys_common/mod.rs | 1 + library/std/src/sys_common/wstr.rs | 59 +++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 52 deletions(-) create mode 100644 library/std/src/sys_common/wstr.rs diff --git a/library/std/src/sys/windows/args.rs b/library/std/src/sys/windows/args.rs index 01f26298290f0..6741ae46d32dd 100644 --- a/library/std/src/sys/windows/args.rs +++ b/library/std/src/sys/windows/args.rs @@ -9,17 +9,16 @@ mod tests; use crate::ffi::OsString; use crate::fmt; use crate::io; -use crate::marker::PhantomData; use crate::num::NonZeroU16; use crate::os::windows::prelude::*; use crate::path::PathBuf; -use crate::ptr::NonNull; use crate::sys::c; use crate::sys::process::ensure_no_nuls; use crate::sys::windows::os::current_exe; +use crate::sys_common::wstr::WStrUnits; use crate::vec; -use core::iter; +use crate::iter; /// This is the const equivalent to `NonZeroU16::new(n).unwrap()` /// @@ -199,55 +198,6 @@ impl ExactSizeIterator for Args { } } -/// A safe iterator over a LPWSTR -/// (aka a pointer to a series of UTF-16 code units terminated by a NULL). -struct WStrUnits<'a> { - // The pointer must never be null... - lpwstr: NonNull, - // ...and the memory it points to must be valid for this lifetime. - lifetime: PhantomData<&'a [u16]>, -} -impl WStrUnits<'_> { - /// Create the iterator. Returns `None` if `lpwstr` is null. - /// - /// SAFETY: `lpwstr` must point to a null-terminated wide string that lives - /// at least as long as the lifetime of this struct. - unsafe fn new(lpwstr: *const u16) -> Option { - Some(Self { lpwstr: NonNull::new(lpwstr as _)?, lifetime: PhantomData }) - } - fn peek(&self) -> Option { - // SAFETY: It's always safe to read the current item because we don't - // ever move out of the array's bounds. - unsafe { NonZeroU16::new(*self.lpwstr.as_ptr()) } - } - /// Advance the iterator while `predicate` returns true. - /// Returns the number of items it advanced by. - fn advance_while bool>(&mut self, mut predicate: P) -> usize { - let mut counter = 0; - while let Some(w) = self.peek() { - if !predicate(w) { - break; - } - counter += 1; - self.next(); - } - counter - } -} -impl Iterator for WStrUnits<'_> { - // This can never return zero as that marks the end of the string. - type Item = NonZeroU16; - fn next(&mut self) -> Option { - // SAFETY: If NULL is reached we immediately return. - // Therefore it's safe to advance the pointer after that. - unsafe { - let next = self.peek()?; - self.lpwstr = NonNull::new_unchecked(self.lpwstr.as_ptr().add(1)); - Some(next) - } - } -} - #[derive(Debug)] pub(crate) enum Arg { /// Add quotes (if needed) diff --git a/library/std/src/sys_common/mod.rs b/library/std/src/sys_common/mod.rs index 8c19f9332dc56..53259e8dbbd46 100644 --- a/library/std/src/sys_common/mod.rs +++ b/library/std/src/sys_common/mod.rs @@ -35,6 +35,7 @@ pub mod thread; pub mod thread_info; pub mod thread_local_dtor; pub mod thread_parker; +pub mod wstr; pub mod wtf8; cfg_if::cfg_if! { diff --git a/library/std/src/sys_common/wstr.rs b/library/std/src/sys_common/wstr.rs new file mode 100644 index 0000000000000..b230fd1a829f7 --- /dev/null +++ b/library/std/src/sys_common/wstr.rs @@ -0,0 +1,59 @@ +//! This module contains constructs to work with 16-bit characters (UCS-2 or UTF-16) +#![allow(dead_code)] + +use crate::marker::PhantomData; +use crate::num::NonZeroU16; +use crate::ptr::NonNull; + +/// A safe iterator over a LPWSTR +/// (aka a pointer to a series of UTF-16 code units terminated by a NULL). +pub struct WStrUnits<'a> { + // The pointer must never be null... + lpwstr: NonNull, + // ...and the memory it points to must be valid for this lifetime. + lifetime: PhantomData<&'a [u16]>, +} + +impl WStrUnits<'_> { + /// Create the iterator. Returns `None` if `lpwstr` is null. + /// + /// SAFETY: `lpwstr` must point to a null-terminated wide string that lives + /// at least as long as the lifetime of this struct. + pub unsafe fn new(lpwstr: *const u16) -> Option { + Some(Self { lpwstr: NonNull::new(lpwstr as _)?, lifetime: PhantomData }) + } + + pub fn peek(&self) -> Option { + // SAFETY: It's always safe to read the current item because we don't + // ever move out of the array's bounds. + unsafe { NonZeroU16::new(*self.lpwstr.as_ptr()) } + } + + /// Advance the iterator while `predicate` returns true. + /// Returns the number of items it advanced by. + pub fn advance_while bool>(&mut self, mut predicate: P) -> usize { + let mut counter = 0; + while let Some(w) = self.peek() { + if !predicate(w) { + break; + } + counter += 1; + self.next(); + } + counter + } +} + +impl Iterator for WStrUnits<'_> { + // This can never return zero as that marks the end of the string. + type Item = NonZeroU16; + fn next(&mut self) -> Option { + // SAFETY: If NULL is reached we immediately return. + // Therefore it's safe to advance the pointer after that. + unsafe { + let next = self.peek()?; + self.lpwstr = NonNull::new_unchecked(self.lpwstr.as_ptr().add(1)); + Some(next) + } + } +} From 20f389db8905f5609f55fcffe686bf95880f8e7a Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 28 Nov 2022 17:23:50 +0100 Subject: [PATCH 148/244] Add sentence when rustdoc search is running --- src/librustdoc/html/static/js/main.js | 10 ++++++---- src/librustdoc/html/static/js/search.js | 10 ++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index 7230df36c075b..623f46b109666 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -302,13 +302,15 @@ function loadCss(cssUrl) { const params = searchState.getQueryStringParams(); if (params.search !== undefined) { - const search = searchState.outputElement(); - search.innerHTML = "

" + - searchState.loadingText + "

"; - searchState.showResults(search); + searchState.setLoadingSearch(); loadSearch(); } }, + setLoadingSearch: () => { + const search = searchState.outputElement(); + search.innerHTML = "

" + searchState.loadingText + "

"; + searchState.showResults(search); + }, }; function getPageId() { diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 4999bb3599487..23ae4e97082e3 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -1766,13 +1766,13 @@ function initSearch(rawSearchIndex) { * @param {boolean} [forced] */ function search(e, forced) { - const params = searchState.getQueryStringParams(); - const query = parseQuery(searchState.input.value.trim()); - if (e) { e.preventDefault(); } + const query = parseQuery(searchState.input.value.trim()); + let filterCrates = getFilterCrates(); + if (!forced && query.userQuery === currentResults) { if (query.userQuery.length > 0) { putBackSearch(); @@ -1780,7 +1780,9 @@ function initSearch(rawSearchIndex) { return; } - let filterCrates = getFilterCrates(); + searchState.setLoadingSearch(); + + const params = searchState.getQueryStringParams(); // In case we have no information about the saved crate and there is a URL query parameter, // we override it with the URL query parameter. From 7181b8f3bc990710f77c54048e78db4fd040ff43 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Mon, 28 Nov 2022 09:55:36 -0700 Subject: [PATCH 149/244] rustdoc: merge background-image rules in rustdoc-toggle CSS --- src/librustdoc/html/static/css/rustdoc.css | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 98a5b761dedf5..c64c104dcae64 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -1512,6 +1512,7 @@ details.rustdoc-toggle > summary.hideme > span { } details.rustdoc-toggle > summary::before { + background-image: url("toggle-plus-1092eb4930d581b0.svg"); content: ""; cursor: pointer; width: 16px; @@ -1599,14 +1600,6 @@ details.rustdoc-toggle[open] > summary.hideme > span { details.rustdoc-toggle[open] > summary::before, details.rustdoc-toggle[open] > summary.hideme::before { background-image: url("toggle-minus-31bbd6e4c77f5c96.svg"); -} - -details.rustdoc-toggle > summary::before { - background-image: url("toggle-plus-1092eb4930d581b0.svg"); -} - -details.rustdoc-toggle[open] > summary::before, -details.rustdoc-toggle[open] > summary.hideme::before { width: 16px; height: 16px; background-repeat: no-repeat; From 9d66ab0f9dea6d8203ceeb29e7da0fd48945ddb7 Mon Sep 17 00:00:00 2001 From: Andres Suarez Date: Mon, 28 Nov 2022 02:40:38 +0000 Subject: [PATCH 150/244] Add `as_mut_os_string` to `&mut PathBuf` and `as_mut_os_str` to `&mut Path` Implements rust-lang/libs-team#140 --- library/std/src/path.rs | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/library/std/src/path.rs b/library/std/src/path.rs index af88b9070c189..6c957c2fa90eb 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -1463,6 +1463,30 @@ impl PathBuf { true } + /// Yields a mutable reference to the underlying [`OsString`] instance. + /// + /// # Examples + /// + /// ``` + /// #![feature(path_as_mut_os_str)] + /// use std::path::{Path, PathBuf}; + /// + /// let mut path = PathBuf::from("/foo"); + /// + /// path.push("bar"); + /// assert_eq!(path, Path::new("/foo/bar")); + /// + /// // OsString's `push` does not add a separator. + /// path.as_mut_os_string().push("baz"); + /// assert_eq!(path, Path::new("/foo/barbaz")); + /// ``` + #[unstable(feature = "path_as_mut_os_str", issue = "105021")] + #[must_use] + #[inline] + pub fn as_mut_os_string(&mut self) -> &mut OsString { + &mut self.inner + } + /// Consumes the `PathBuf`, yielding its internal [`OsString`] storage. /// /// # Examples @@ -1993,6 +2017,28 @@ impl Path { &self.inner } + /// Yields a mutable reference to the underlying [`OsStr`] slice. + /// + /// # Examples + /// + /// ``` + /// #![feature(path_as_mut_os_str)] + /// use std::path::{Path, PathBuf}; + /// + /// let mut path = PathBuf::from("/Foo.TXT").into_boxed_path(); + /// + /// assert_ne!(&*path, Path::new("/foo.txt")); + /// + /// path.as_mut_os_str().make_ascii_lowercase(); + /// assert_eq!(&*path, Path::new("/foo.txt")); + /// ``` + #[unstable(feature = "path_as_mut_os_str", issue = "105021")] + #[must_use] + #[inline] + pub fn as_mut_os_str(&mut self) -> &mut OsStr { + &mut self.inner + } + /// Yields a [`&str`] slice if the `Path` is valid unicode. /// /// This conversion may entail doing a check for UTF-8 validity. From e20e506f7df271e933827a2290c0e0a704146443 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 28 Nov 2022 11:11:45 +0000 Subject: [PATCH 151/244] Make `tcx.mk_const` more permissive wrt `kind` argument - Accept `impl Into` - Implement `From<>` for `ConstKind` Note: this adds a dependency on `derive_more` (MIT license). It allows to derive a lot of traits (like `From` here) that would be otherwise tedious to implement. --- Cargo.lock | 20 ++++++++++++++++++++ compiler/rustc_middle/Cargo.toml | 1 + compiler/rustc_middle/src/ty/consts/kind.rs | 8 ++++++++ compiler/rustc_middle/src/ty/context.rs | 4 ++-- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dbf1e06ee6e92..7d43dbc9e0640 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -870,6 +870,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "convert_case" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" + [[package]] name = "core" version = "0.0.0" @@ -1060,6 +1066,19 @@ dependencies = [ "syn", ] +[[package]] +name = "derive_more" +version = "0.99.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" +dependencies = [ + "convert_case", + "proc-macro2", + "quote", + "rustc_version", + "syn", +] + [[package]] name = "diff" version = "0.1.13" @@ -3979,6 +3998,7 @@ version = "0.0.0" dependencies = [ "bitflags", "chalk-ir", + "derive_more", "either", "gsgdt", "polonius-engine", diff --git a/compiler/rustc_middle/Cargo.toml b/compiler/rustc_middle/Cargo.toml index fc1167c105ae8..cf1ab47de861a 100644 --- a/compiler/rustc_middle/Cargo.toml +++ b/compiler/rustc_middle/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] bitflags = "1.2.1" chalk-ir = "0.87.0" +derive_more = "0.99.17" either = "1.5.0" gsgdt = "0.1.2" polonius-engine = "0.13.0" diff --git a/compiler/rustc_middle/src/ty/consts/kind.rs b/compiler/rustc_middle/src/ty/consts/kind.rs index de63dae8a3df6..becc2b805dd1e 100644 --- a/compiler/rustc_middle/src/ty/consts/kind.rs +++ b/compiler/rustc_middle/src/ty/consts/kind.rs @@ -49,6 +49,7 @@ impl<'tcx> UnevaluatedConst<'tcx> { /// Represents a constant in Rust. #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)] #[derive(Hash, HashStable, TypeFoldable, TypeVisitable)] +#[derive(derive_more::From)] pub enum ConstKind<'tcx> { /// A const generic parameter. Param(ty::ParamConst), @@ -71,12 +72,19 @@ pub enum ConstKind<'tcx> { /// A placeholder for a const which could not be computed; this is /// propagated to avoid useless error messages. + #[from(ignore)] Error(ErrorGuaranteed), /// Expr which contains an expression which has partially evaluated items. Expr(Expr<'tcx>), } +impl<'tcx> From> for ConstKind<'tcx> { + fn from(const_vid: ty::ConstVid<'tcx>) -> Self { + InferConst::Var(const_vid).into() + } +} + #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)] #[derive(HashStable, TyEncodable, TyDecodable, TypeVisitable, TypeFoldable)] pub enum Expr<'tcx> { diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index bf30a403d9b94..f422fdcc9aef6 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -2598,8 +2598,8 @@ impl<'tcx> TyCtxt<'tcx> { } #[inline] - pub fn mk_const(self, kind: ty::ConstKind<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> { - self.mk_const_internal(ty::ConstS { kind, ty }) + pub fn mk_const(self, kind: impl Into>, ty: Ty<'tcx>) -> Const<'tcx> { + self.mk_const_internal(ty::ConstS { kind: kind.into(), ty }) } #[inline] From 7087d9b2a08385eaf7eb035e5ed420a84ef0902c Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 28 Nov 2022 11:27:18 +0000 Subject: [PATCH 152/244] Remove `tcx.mk_const_var` ... `tcx.mk_const` can now be used instead --- compiler/rustc_infer/src/infer/combine.rs | 4 +-- compiler/rustc_infer/src/infer/freshen.rs | 2 +- compiler/rustc_infer/src/infer/mod.rs | 6 ++-- .../rustc_infer/src/infer/nll_relate/mod.rs | 2 +- compiler/rustc_middle/src/ty/context.rs | 28 ++++++------------- 5 files changed, 15 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_infer/src/infer/combine.rs b/compiler/rustc_infer/src/infer/combine.rs index eec938cefbb70..ec2017b3d8959 100644 --- a/compiler/rustc_infer/src/infer/combine.rs +++ b/compiler/rustc_infer/src/infer/combine.rs @@ -753,7 +753,7 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> { origin: var_value.origin, val: ConstVariableValue::Unknown { universe: self.for_universe }, }); - Ok(self.tcx().mk_const_var(new_var_id, c.ty())) + Ok(self.tcx().mk_const(new_var_id, c.ty())) } } } @@ -975,7 +975,7 @@ impl<'tcx> TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> { }, }, ); - Ok(self.tcx().mk_const_var(new_var_id, c.ty())) + Ok(self.tcx().mk_const(new_var_id, c.ty())) } } } diff --git a/compiler/rustc_infer/src/infer/freshen.rs b/compiler/rustc_infer/src/infer/freshen.rs index 27a94ec5e30e1..f6946929bd23f 100644 --- a/compiler/rustc_infer/src/infer/freshen.rs +++ b/compiler/rustc_infer/src/infer/freshen.rs @@ -102,7 +102,7 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> { Entry::Vacant(entry) => { let index = self.const_freshen_count; self.const_freshen_count += 1; - let ct = self.infcx.tcx.mk_const_infer(freshener(index), ty); + let ct = self.infcx.tcx.mk_const(freshener(index), ty); entry.insert(ct); ct } diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index cda9299dcb6e7..df00ed0cb9cc2 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -1065,7 +1065,7 @@ impl<'tcx> InferCtxt<'tcx> { } pub fn next_const_var(&self, ty: Ty<'tcx>, origin: ConstVariableOrigin) -> ty::Const<'tcx> { - self.tcx.mk_const_var(self.next_const_var_id(origin), ty) + self.tcx.mk_const(self.next_const_var_id(origin), ty) } pub fn next_const_var_in_universe( @@ -1079,7 +1079,7 @@ impl<'tcx> InferCtxt<'tcx> { .borrow_mut() .const_unification_table() .new_key(ConstVarValue { origin, val: ConstVariableValue::Unknown { universe } }); - self.tcx.mk_const_var(vid, ty) + self.tcx.mk_const(vid, ty) } pub fn next_const_var_id(&self, origin: ConstVariableOrigin) -> ConstVid<'tcx> { @@ -1195,7 +1195,7 @@ impl<'tcx> InferCtxt<'tcx> { origin, val: ConstVariableValue::Unknown { universe: self.universe() }, }); - self.tcx.mk_const_var(const_var_id, self.tcx.type_of(param.def_id)).into() + self.tcx.mk_const(const_var_id, self.tcx.type_of(param.def_id)).into() } } } diff --git a/compiler/rustc_infer/src/infer/nll_relate/mod.rs b/compiler/rustc_infer/src/infer/nll_relate/mod.rs index 4f8460955c3de..f6bc4db0d59df 100644 --- a/compiler/rustc_infer/src/infer/nll_relate/mod.rs +++ b/compiler/rustc_infer/src/infer/nll_relate/mod.rs @@ -1087,7 +1087,7 @@ where origin: var_value.origin, val: ConstVariableValue::Unknown { universe: self.universe }, }); - Ok(self.tcx().mk_const_var(new_var_id, a.ty())) + Ok(self.tcx().mk_const(new_var_id, a.ty())) } } } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index f422fdcc9aef6..83b6bdedcde20 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -17,8 +17,8 @@ use crate::traits; use crate::ty::query::{self, TyCtxtAt}; use crate::ty::{ self, AdtDef, AdtDefData, AdtKind, Binder, BindingMode, BoundVar, CanonicalPolyFnSig, - ClosureSizeProfileData, Const, ConstS, ConstVid, DefIdTree, FloatTy, FloatVar, FloatVid, - GenericParamDefKind, InferConst, InferTy, IntTy, IntVar, IntVid, List, ParamConst, ParamTy, + ClosureSizeProfileData, Const, ConstS, DefIdTree, FloatTy, FloatVar, FloatVid, + GenericParamDefKind, InferTy, IntTy, IntVar, IntVid, List, ParamConst, ParamTy, PolyExistentialPredicate, PolyFnSig, Predicate, PredicateKind, PredicateS, ProjectionTy, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut, UintTy, Visibility, @@ -2602,11 +2602,6 @@ impl<'tcx> TyCtxt<'tcx> { self.mk_const_internal(ty::ConstS { kind: kind.into(), ty }) } - #[inline] - pub fn mk_const_var(self, v: ConstVid<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> { - self.mk_const(ty::ConstKind::Infer(InferConst::Var(v)), ty) - } - #[inline] pub fn mk_int_var(self, v: IntVid) -> Ty<'tcx> { self.mk_ty_infer(IntVar(v)) @@ -2622,30 +2617,23 @@ impl<'tcx> TyCtxt<'tcx> { self.mk_ty(Infer(it)) } - #[inline] - pub fn mk_const_infer(self, ic: InferConst<'tcx>, ty: Ty<'tcx>) -> ty::Const<'tcx> { - self.mk_const(ty::ConstKind::Infer(ic), ty) - } - #[inline] pub fn mk_ty_param(self, index: u32, name: Symbol) -> Ty<'tcx> { self.mk_ty(Param(ParamTy { index, name })) } - #[inline] - pub fn mk_const_param(self, index: u32, name: Symbol, ty: Ty<'tcx>) -> Const<'tcx> { - self.mk_const(ty::ConstKind::Param(ParamConst { index, name }), ty) - } - pub fn mk_param_from_def(self, param: &ty::GenericParamDef) -> GenericArg<'tcx> { match param.kind { GenericParamDefKind::Lifetime => { self.mk_region(ty::ReEarlyBound(param.to_early_bound_region_data())).into() } GenericParamDefKind::Type { .. } => self.mk_ty_param(param.index, param.name).into(), - GenericParamDefKind::Const { .. } => { - self.mk_const_param(param.index, param.name, self.type_of(param.def_id)).into() - } + GenericParamDefKind::Const { .. } => self + .mk_const( + ParamConst { index: param.index, name: param.name }, + self.type_of(param.def_id), + ) + .into(), } } From 26b87bf8ff8406e80e70559b71cf0095475cc64a Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 28 Nov 2022 12:28:32 +0000 Subject: [PATCH 153/244] Simplify calls to `tcx.mk_const` `mk_const(ty::ConstKind::X(...), ty)` can now be simplified to `mk_cosnt(..., ty)`. I searched with the following regex: \mk_const\([\n\s]*(ty::)?ConstKind\ I've left `ty::ConstKind::{Bound, Error}` as-is, they seem clearer this way. --- .../rustc_infer/src/infer/canonical/mod.rs | 2 +- compiler/rustc_infer/src/infer/combine.rs | 10 ++----- .../src/infer/higher_ranked/mod.rs | 9 ++---- compiler/rustc_infer/src/infer/mod.rs | 4 +-- compiler/rustc_middle/src/mir/mod.rs | 3 +- compiler/rustc_middle/src/ty/consts.rs | 8 +++--- compiler/rustc_middle/src/ty/relate.rs | 5 +--- .../src/build/expr/as_constant.rs | 2 +- .../src/traits/project.rs | 2 +- compiler/rustc_ty_utils/src/consts.rs | 28 ++++++++----------- 10 files changed, 27 insertions(+), 46 deletions(-) diff --git a/compiler/rustc_infer/src/infer/canonical/mod.rs b/compiler/rustc_infer/src/infer/canonical/mod.rs index 0794792d8cb37..ba0ce16bb81ae 100644 --- a/compiler/rustc_infer/src/infer/canonical/mod.rs +++ b/compiler/rustc_infer/src/infer/canonical/mod.rs @@ -147,7 +147,7 @@ impl<'tcx> InferCtxt<'tcx> { CanonicalVarKind::PlaceholderConst(ty::PlaceholderConst { universe, name }, ty) => { let universe_mapped = universe_map(universe); let placeholder_mapped = ty::PlaceholderConst { universe: universe_mapped, name }; - self.tcx.mk_const(ty::ConstKind::Placeholder(placeholder_mapped), ty).into() + self.tcx.mk_const(placeholder_mapped, ty).into() } } } diff --git a/compiler/rustc_infer/src/infer/combine.rs b/compiler/rustc_infer/src/infer/combine.rs index ec2017b3d8959..cf895ed0d3e59 100644 --- a/compiler/rustc_infer/src/infer/combine.rs +++ b/compiler/rustc_infer/src/infer/combine.rs @@ -765,10 +765,7 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> { substs, substs, )?; - Ok(self.tcx().mk_const( - ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }), - c.ty(), - )) + Ok(self.tcx().mk_const(ty::UnevaluatedConst { def, substs }, c.ty())) } _ => relate::super_relate_consts(self, c, c), } @@ -988,10 +985,7 @@ impl<'tcx> TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> { substs, )?; - Ok(self.tcx().mk_const( - ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }), - c.ty(), - )) + Ok(self.tcx().mk_const(ty::UnevaluatedConst { def, substs }, c.ty())) } _ => relate::super_relate_consts(self, c, c), } diff --git a/compiler/rustc_infer/src/infer/higher_ranked/mod.rs b/compiler/rustc_infer/src/infer/higher_ranked/mod.rs index d739323de77c8..817ae10c76087 100644 --- a/compiler/rustc_infer/src/infer/higher_ranked/mod.rs +++ b/compiler/rustc_infer/src/infer/higher_ranked/mod.rs @@ -94,13 +94,8 @@ impl<'tcx> InferCtxt<'tcx> { })) }, consts: &mut |bound_var: ty::BoundVar, ty| { - self.tcx.mk_const( - ty::ConstKind::Placeholder(ty::PlaceholderConst { - universe: next_universe, - name: bound_var, - }), - ty, - ) + self.tcx + .mk_const(ty::PlaceholderConst { universe: next_universe, name: bound_var }, ty) }, }; diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index df00ed0cb9cc2..c5de1d6d78af7 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -2049,10 +2049,10 @@ fn replace_param_and_infer_substs_with_placeholder<'tcx>( bug!("const `{ct}`'s type should not reference params or types"); } tcx.mk_const( - ty::ConstKind::Placeholder(ty::PlaceholderConst { + ty::PlaceholderConst { universe: ty::UniverseIndex::ROOT, name: ty::BoundVar::from_usize(idx), - }), + }, ty, ) .into() diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 364c1b375ae5d..20dde64e51b01 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -2527,8 +2527,7 @@ impl<'tcx> ConstantKind<'tcx> { let generics = tcx.generics_of(item_def_id); let index = generics.param_def_id_to_index[&def_id]; let name = tcx.item_name(def_id); - let ty_const = - tcx.mk_const(ty::ConstKind::Param(ty::ParamConst::new(index, name)), ty); + let ty_const = tcx.mk_const(ty::ParamConst::new(index, name), ty); debug!(?ty_const); return Self::Ty(ty_const); diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index 9a58a196ed721..cd0b280ce4e48 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -76,10 +76,10 @@ impl<'tcx> Const<'tcx> { match Self::try_eval_lit_or_param(tcx, ty, expr) { Some(v) => v, None => tcx.mk_const( - ty::ConstKind::Unevaluated(ty::UnevaluatedConst { + ty::UnevaluatedConst { def: def.to_global(), substs: InternalSubsts::identity_for_item(tcx, def.did.to_def_id()), - }), + }, ty, ), } @@ -134,7 +134,7 @@ impl<'tcx> Const<'tcx> { let generics = tcx.generics_of(item_def_id); let index = generics.param_def_id_to_index[&def_id]; let name = tcx.item_name(def_id); - Some(tcx.mk_const(ty::ConstKind::Param(ty::ParamConst::new(index, name)), ty)) + Some(tcx.mk_const(ty::ParamConst::new(index, name), ty)) } _ => None, } @@ -143,7 +143,7 @@ impl<'tcx> Const<'tcx> { /// Interns the given value as a constant. #[inline] pub fn from_value(tcx: TyCtxt<'tcx>, val: ty::ValTree<'tcx>, ty: Ty<'tcx>) -> Self { - tcx.mk_const(ConstKind::Value(val), ty) + tcx.mk_const(val, ty) } /// Panics if self.kind != ty::ConstKind::Value diff --git a/compiler/rustc_middle/src/ty/relate.rs b/compiler/rustc_middle/src/ty/relate.rs index e6340040e9c19..c759fb6d5e4f4 100644 --- a/compiler/rustc_middle/src/ty/relate.rs +++ b/compiler/rustc_middle/src/ty/relate.rs @@ -663,10 +663,7 @@ pub fn super_relate_consts<'tcx, R: TypeRelation<'tcx>>( au.substs, bu.substs, )?; - return Ok(tcx.mk_const( - ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def: au.def, substs }), - a.ty(), - )); + return Ok(tcx.mk_const(ty::UnevaluatedConst { def: au.def, substs }, a.ty())); } // Before calling relate on exprs, it is necessary to ensure that the nested consts // have identical types. diff --git a/compiler/rustc_mir_build/src/build/expr/as_constant.rs b/compiler/rustc_mir_build/src/build/expr/as_constant.rs index 7d8a940bde5ce..32c0207cb680c 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_constant.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_constant.rs @@ -77,7 +77,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { Constant { user_ty, span, literal } } ExprKind::ConstParam { param, def_id: _ } => { - let const_param = tcx.mk_const(ty::ConstKind::Param(param), expr.ty); + let const_param = tcx.mk_const(param, expr.ty); let literal = ConstantKind::Ty(const_param); Constant { user_ty: None, span, literal } diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 18f4379d8f890..e33e89e9c5c3e 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -818,7 +818,7 @@ impl<'tcx> TypeFolder<'tcx> for BoundVarReplacer<'_, 'tcx> { let universe = self.universe_for(debruijn); let p = ty::PlaceholderConst { universe, name: bound_const }; self.mapped_consts.insert(p, bound_const); - self.infcx.tcx.mk_const(ty::ConstKind::Placeholder(p), ct.ty()) + self.infcx.tcx.mk_const(p, ct.ty()) } _ => ct.super_fold_with(self), } diff --git a/compiler/rustc_ty_utils/src/consts.rs b/compiler/rustc_ty_utils/src/consts.rs index 2b7018bc9c300..77cb224348263 100644 --- a/compiler/rustc_ty_utils/src/consts.rs +++ b/compiler/rustc_ty_utils/src/consts.rs @@ -5,7 +5,7 @@ use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput}; use rustc_middle::thir::visit; use rustc_middle::thir::visit::Visitor; use rustc_middle::ty::abstract_const::CastKind; -use rustc_middle::ty::{self, ConstKind, Expr, TyCtxt, TypeVisitable}; +use rustc_middle::ty::{self, Expr, TyCtxt, TypeVisitable}; use rustc_middle::{mir, thir}; use rustc_span::Span; use rustc_target::abi::VariantIdx; @@ -32,10 +32,8 @@ pub(crate) fn destructure_const<'tcx>( let (fields, variant) = match const_.ty().kind() { ty::Array(inner_ty, _) | ty::Slice(inner_ty) => { // construct the consts for the elements of the array/slice - let field_consts = branches - .iter() - .map(|b| tcx.mk_const(ty::ConstKind::Value(*b), *inner_ty)) - .collect::>(); + let field_consts = + branches.iter().map(|b| tcx.mk_const(*b, *inner_ty)).collect::>(); debug!(?field_consts); (field_consts, None) @@ -53,7 +51,7 @@ pub(crate) fn destructure_const<'tcx>( for (field, field_valtree) in iter::zip(fields, branches) { let field_ty = field.ty(tcx, substs); - let field_const = tcx.mk_const(ty::ConstKind::Value(*field_valtree), field_ty); + let field_const = tcx.mk_const(*field_valtree, field_ty); field_consts.push(field_const); } debug!(?field_consts); @@ -62,9 +60,7 @@ pub(crate) fn destructure_const<'tcx>( } ty::Tuple(elem_tys) => { let fields = iter::zip(*elem_tys, branches) - .map(|(elem_ty, elem_valtree)| { - tcx.mk_const(ty::ConstKind::Value(*elem_valtree), elem_ty) - }) + .map(|(elem_ty, elem_valtree)| tcx.mk_const(*elem_valtree, elem_ty)) .collect::>(); (fields, None) @@ -137,9 +133,9 @@ fn recurse_build<'tcx>( } &ExprKind::NamedConst { def_id, substs, user_ty: _ } => { let uneval = ty::UnevaluatedConst::new(ty::WithOptConstParam::unknown(def_id), substs); - tcx.mk_const(ty::ConstKind::Unevaluated(uneval), node.ty) + tcx.mk_const(uneval, node.ty) } - ExprKind::ConstParam { param, .. } => tcx.mk_const(ty::ConstKind::Param(*param), node.ty), + ExprKind::ConstParam { param, .. } => tcx.mk_const(*param, node.ty), ExprKind::Call { fun, args, .. } => { let fun = recurse_build(tcx, body, *fun, root_span)?; @@ -149,16 +145,16 @@ fn recurse_build<'tcx>( new_args.push(recurse_build(tcx, body, id, root_span)?); } let new_args = tcx.mk_const_list(new_args.iter()); - tcx.mk_const(ConstKind::Expr(Expr::FunctionCall(fun, new_args)), node.ty) + tcx.mk_const(Expr::FunctionCall(fun, new_args), node.ty) } &ExprKind::Binary { op, lhs, rhs } if check_binop(op) => { let lhs = recurse_build(tcx, body, lhs, root_span)?; let rhs = recurse_build(tcx, body, rhs, root_span)?; - tcx.mk_const(ConstKind::Expr(Expr::Binop(op, lhs, rhs)), node.ty) + tcx.mk_const(Expr::Binop(op, lhs, rhs), node.ty) } &ExprKind::Unary { op, arg } if check_unop(op) => { let arg = recurse_build(tcx, body, arg, root_span)?; - tcx.mk_const(ConstKind::Expr(Expr::UnOp(op, arg)), node.ty) + tcx.mk_const(Expr::UnOp(op, arg), node.ty) } // This is necessary so that the following compiles: // @@ -179,11 +175,11 @@ fn recurse_build<'tcx>( // This is important so that `N as usize as usize` doesnt unify with `N as usize`. (untested) &ExprKind::Use { source } => { let arg = recurse_build(tcx, body, source, root_span)?; - tcx.mk_const(ConstKind::Expr(Expr::Cast(CastKind::Use, arg, node.ty)), node.ty) + tcx.mk_const(Expr::Cast(CastKind::Use, arg, node.ty), node.ty) } &ExprKind::Cast { source } => { let arg = recurse_build(tcx, body, source, root_span)?; - tcx.mk_const(ConstKind::Expr(Expr::Cast(CastKind::As, arg, node.ty)), node.ty) + tcx.mk_const(Expr::Cast(CastKind::As, arg, node.ty), node.ty) } ExprKind::Borrow { arg, .. } => { let arg_node = &body.exprs[*arg]; From 63915be212d0c054c764b7dd5c408e2c10af219c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Mon, 28 Nov 2022 00:00:00 +0000 Subject: [PATCH 154/244] Statics used in reachable function's inline asm are reachable --- compiler/rustc_passes/src/reachable.rs | 11 ++++++++++ .../codegen-units/item-collection/asm-sym.rs | 20 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/test/codegen-units/item-collection/asm-sym.rs diff --git a/compiler/rustc_passes/src/reachable.rs b/compiler/rustc_passes/src/reachable.rs index 73ea06a6370d4..e7c3c712852dd 100644 --- a/compiler/rustc_passes/src/reachable.rs +++ b/compiler/rustc_passes/src/reachable.rs @@ -116,6 +116,17 @@ impl<'tcx> Visitor<'tcx> for ReachableContext<'tcx> { intravisit::walk_expr(self, expr) } + + fn visit_inline_asm(&mut self, asm: &'tcx hir::InlineAsm<'tcx>, id: hir::HirId) { + for (op, _) in asm.operands { + if let hir::InlineAsmOperand::SymStatic { def_id, .. } = op { + if let Some(def_id) = def_id.as_local() { + self.reachable_symbols.insert(def_id); + } + } + } + intravisit::walk_inline_asm(self, asm, id); + } } impl<'tcx> ReachableContext<'tcx> { diff --git a/src/test/codegen-units/item-collection/asm-sym.rs b/src/test/codegen-units/item-collection/asm-sym.rs new file mode 100644 index 0000000000000..8bafb95bc16d9 --- /dev/null +++ b/src/test/codegen-units/item-collection/asm-sym.rs @@ -0,0 +1,20 @@ +// needs-asm-support +// compile-flags: -Ccodegen-units=1 -Zprint-mono-items=lazy --crate-type=lib + +#[inline(always)] +pub unsafe fn f() { + //~ MONO_ITEM static f::S @@ asm_sym-cgu.0[External] + static S: usize = 1; + //~ MONO_ITEM fn f::fun @@ asm_sym-cgu.0[External] + fn fun() {} + core::arch::asm!("/* {0} {1} */", sym S, sym fun); +} + +//~ MONO_ITEM fn g @@ asm_sym-cgu.0[External] +pub unsafe fn g() { + //~ MONO_ITEM static g::S @@ asm_sym-cgu.0[Internal] + static S: usize = 2; + //~ MONO_ITEM fn g::fun @@ asm_sym-cgu.0[Internal] + fn fun() {} + core::arch::asm!("/* {0} {1} */", sym S, sym fun); +} From f4d00fe78550eac378594a9c0d0708a6c871f337 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 28 Nov 2022 12:38:15 +0000 Subject: [PATCH 155/244] Remove `Const::from_value` ...it's just `mk_const` but without the sparcles --- compiler/rustc_infer/src/infer/mod.rs | 2 +- compiler/rustc_middle/src/ty/consts.rs | 13 +++---------- compiler/rustc_middle/src/ty/print/pretty.rs | 3 +-- compiler/rustc_mir_build/src/thir/constant.rs | 2 +- .../rustc_trait_selection/src/traits/auto_trait.rs | 4 +--- compiler/rustc_ty_utils/src/consts.rs | 4 ++-- 6 files changed, 9 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index c5de1d6d78af7..2bcb47cc38399 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -1580,7 +1580,7 @@ impl<'tcx> InferCtxt<'tcx> { span: Option, ) -> Result, ErrorHandled> { match self.const_eval_resolve(param_env, unevaluated, span) { - Ok(Some(val)) => Ok(ty::Const::from_value(self.tcx, val, ty)), + Ok(Some(val)) => Ok(self.tcx.mk_const(val, ty)), Ok(None) => { let tcx = self.tcx; let def_id = unevaluated.def.did; diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index cd0b280ce4e48..eaeb08c7aed40 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -140,12 +140,6 @@ impl<'tcx> Const<'tcx> { } } - /// Interns the given value as a constant. - #[inline] - pub fn from_value(tcx: TyCtxt<'tcx>, val: ty::ValTree<'tcx>, ty: Ty<'tcx>) -> Self { - tcx.mk_const(val, ty) - } - /// Panics if self.kind != ty::ConstKind::Value pub fn to_valtree(self) -> ty::ValTree<'tcx> { match self.kind() { @@ -156,7 +150,7 @@ impl<'tcx> Const<'tcx> { pub fn from_scalar_int(tcx: TyCtxt<'tcx>, i: ScalarInt, ty: Ty<'tcx>) -> Self { let valtree = ty::ValTree::from_scalar_int(i); - Self::from_value(tcx, valtree, ty) + tcx.mk_const(valtree, ty) } #[inline] @@ -172,8 +166,7 @@ impl<'tcx> Const<'tcx> { #[inline] /// Creates an interned zst constant. pub fn zero_sized(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Self { - let valtree = ty::ValTree::zst(); - Self::from_value(tcx, valtree, ty) + tcx.mk_const(ty::ValTree::zst(), ty) } #[inline] @@ -220,7 +213,7 @@ impl<'tcx> Const<'tcx> { pub fn eval(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Const<'tcx> { if let Some(val) = self.kind().try_eval_for_typeck(tcx, param_env) { match val { - Ok(val) => Const::from_value(tcx, val, self.ty()), + Ok(val) => tcx.mk_const(val, self.ty()), Err(guar) => tcx.const_error_with_guaranteed(self.ty(), guar), } } else { diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index bd17f7d34ad9c..5303341ba443c 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -1468,8 +1468,7 @@ pub trait PrettyPrinter<'tcx>: } // Aggregates, printed as array/tuple/struct/variant construction syntax. (ty::ValTree::Branch(_), ty::Array(..) | ty::Tuple(..) | ty::Adt(..)) => { - let contents = - self.tcx().destructure_const(ty::Const::from_value(self.tcx(), valtree, ty)); + let contents = self.tcx().destructure_const(self.tcx().mk_const(valtree, ty)); let fields = contents.fields.iter().copied(); match *ty.kind() { ty::Array(..) => { diff --git a/compiler/rustc_mir_build/src/thir/constant.rs b/compiler/rustc_mir_build/src/thir/constant.rs index 85e8801bda3ec..a9ed945d4a15a 100644 --- a/compiler/rustc_mir_build/src/thir/constant.rs +++ b/compiler/rustc_mir_build/src/thir/constant.rs @@ -61,5 +61,5 @@ pub(crate) fn lit_to_const<'tcx>( _ => return Err(LitToConstError::TypeError), }; - Ok(ty::Const::from_value(tcx, valtree, ty)) + Ok(tcx.mk_const(valtree, ty)) } diff --git a/compiler/rustc_trait_selection/src/traits/auto_trait.rs b/compiler/rustc_trait_selection/src/traits/auto_trait.rs index 88a13f75c7eb1..8e04da4f9be24 100644 --- a/compiler/rustc_trait_selection/src/traits/auto_trait.rs +++ b/compiler/rustc_trait_selection/src/traits/auto_trait.rs @@ -799,9 +799,7 @@ impl<'tcx> AutoTraitFinder<'tcx> { unevaluated, Some(obligation.cause.span), ) { - Ok(Some(valtree)) => { - Ok(ty::Const::from_value(selcx.tcx(), valtree, c.ty())) - } + Ok(Some(valtree)) => Ok(selcx.tcx().mk_const(valtree, c.ty())), Ok(None) => { let tcx = self.tcx; let def_id = unevaluated.def.did; diff --git a/compiler/rustc_ty_utils/src/consts.rs b/compiler/rustc_ty_utils/src/consts.rs index 77cb224348263..f8ff31f971be6 100644 --- a/compiler/rustc_ty_utils/src/consts.rs +++ b/compiler/rustc_ty_utils/src/consts.rs @@ -125,11 +125,11 @@ fn recurse_build<'tcx>( } &ExprKind::NonHirLiteral { lit, user_ty: _ } => { let val = ty::ValTree::from_scalar_int(lit); - ty::Const::from_value(tcx, val, node.ty) + tcx.mk_const(val, node.ty) } &ExprKind::ZstLiteral { user_ty: _ } => { let val = ty::ValTree::zst(); - ty::Const::from_value(tcx, val, node.ty) + tcx.mk_const(val, node.ty) } &ExprKind::NamedConst { def_id, substs, user_ty: _ } => { let uneval = ty::UnevaluatedConst::new(ty::WithOptConstParam::unknown(def_id), substs); From b44817f95eeed6c80cc8a4477a456b3d567ce22c Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 28 Nov 2022 13:37:36 +0000 Subject: [PATCH 156/244] Permit deps (scarry) --- src/tools/tidy/src/deps.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/tools/tidy/src/deps.rs b/src/tools/tidy/src/deps.rs index 8155ec9dd27e2..a7f4016728416 100644 --- a/src/tools/tidy/src/deps.rs +++ b/src/tools/tidy/src/deps.rs @@ -98,6 +98,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[ "chalk-ir", "chalk-solve", "chrono", + "convert_case", // dependency of derive_more "compiler_builtins", "cpufeatures", "crc32fast", @@ -108,6 +109,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[ "crypto-common", "cstr", "datafrog", + "derive_more", "difference", "digest", "displaydoc", From f12e772b83c3ff6a099a66920f73fa145e21b0f3 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 25 Nov 2022 16:45:33 +0000 Subject: [PATCH 157/244] Rename At::normalize to At::query_normalize --- .../rustc_trait_selection/src/traits/error_reporting/mod.rs | 2 +- compiler/rustc_trait_selection/src/traits/query/normalize.rs | 4 ++-- compiler/rustc_traits/src/dropck_outlives.rs | 2 +- compiler/rustc_traits/src/normalize_erasing_regions.rs | 2 +- compiler/rustc_traits/src/type_op.rs | 2 +- src/librustdoc/clean/mod.rs | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index e96b9b64e7874..809f107404ddf 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -1933,7 +1933,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> { let infcx = self.tcx.infer_ctxt().build(); infcx .at(&ObligationCause::dummy(), ty::ParamEnv::empty()) - .normalize(candidate) + .query_normalize(candidate) .map_or(candidate, |normalized| normalized.value) }; diff --git a/compiler/rustc_trait_selection/src/traits/query/normalize.rs b/compiler/rustc_trait_selection/src/traits/query/normalize.rs index a875ea1578dcb..1d52999065222 100644 --- a/compiler/rustc_trait_selection/src/traits/query/normalize.rs +++ b/compiler/rustc_trait_selection/src/traits/query/normalize.rs @@ -23,7 +23,7 @@ use super::NoSolution; pub use rustc_middle::traits::query::NormalizationResult; pub trait AtExt<'tcx> { - fn normalize(&self, value: T) -> Result, NoSolution> + fn query_normalize(&self, value: T) -> Result, NoSolution> where T: TypeFoldable<'tcx>; } @@ -42,7 +42,7 @@ impl<'cx, 'tcx> AtExt<'tcx> for At<'cx, 'tcx> { /// normalizing, but for now should be used only when we actually /// know that normalization will succeed, since error reporting /// and other details are still "under development". - fn normalize(&self, value: T) -> Result, NoSolution> + fn query_normalize(&self, value: T) -> Result, NoSolution> where T: TypeFoldable<'tcx>, { diff --git a/compiler/rustc_traits/src/dropck_outlives.rs b/compiler/rustc_traits/src/dropck_outlives.rs index 7b4ad9fea137a..87f9862017fc5 100644 --- a/compiler/rustc_traits/src/dropck_outlives.rs +++ b/compiler/rustc_traits/src/dropck_outlives.rs @@ -100,7 +100,7 @@ fn dropck_outlives<'tcx>( // to push them onto the stack to be expanded. for ty in constraints.dtorck_types.drain(..) { let Normalized { value: ty, obligations } = - ocx.infcx.at(&cause, param_env).normalize(ty)?; + ocx.infcx.at(&cause, param_env).query_normalize(ty)?; ocx.register_obligations(obligations); debug!("dropck_outlives: ty from dtorck_types = {:?}", ty); diff --git a/compiler/rustc_traits/src/normalize_erasing_regions.rs b/compiler/rustc_traits/src/normalize_erasing_regions.rs index bd22d113b4092..0309e2ba92c27 100644 --- a/compiler/rustc_traits/src/normalize_erasing_regions.rs +++ b/compiler/rustc_traits/src/normalize_erasing_regions.rs @@ -29,7 +29,7 @@ fn try_normalize_after_erasing_regions<'tcx, T: TypeFoldable<'tcx> + PartialEq + let ParamEnvAnd { param_env, value } = goal; let infcx = tcx.infer_ctxt().build(); let cause = ObligationCause::dummy(); - match infcx.at(&cause, param_env).normalize(value) { + match infcx.at(&cause, param_env).query_normalize(value) { Ok(Normalized { value: normalized_value, obligations: normalized_obligations }) => { // We don't care about the `obligations`; they are // always only region relations, and we are about to diff --git a/compiler/rustc_traits/src/type_op.rs b/compiler/rustc_traits/src/type_op.rs index c6c072ea3d2bd..8ffa89023c6cb 100644 --- a/compiler/rustc_traits/src/type_op.rs +++ b/compiler/rustc_traits/src/type_op.rs @@ -137,7 +137,7 @@ where { let (param_env, Normalize { value }) = key.into_parts(); let Normalized { value, obligations } = - ocx.infcx.at(&ObligationCause::dummy(), param_env).normalize(value)?; + ocx.infcx.at(&ObligationCause::dummy(), param_env).query_normalize(value)?; ocx.register_obligations(obligations); Ok(value) } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 582586d33febe..9d572e2291e5c 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1588,7 +1588,7 @@ fn normalize<'tcx>(cx: &mut DocContext<'tcx>, ty: Ty<'tcx>) -> Option> let infcx = cx.tcx.infer_ctxt().build(); let normalized = infcx .at(&ObligationCause::dummy(), cx.param_env) - .normalize(ty) + .query_normalize(ty) .map(|resolved| infcx.resolve_vars_if_possible(resolved.value)); match normalized { Ok(normalized_value) => { From fc710832eadb31c747fb2997d882c5a08de2c10e Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 25 Nov 2022 17:11:15 +0000 Subject: [PATCH 158/244] partially_normalize_... -> At::normalize --- .../src/diagnostics/bound_region_errors.rs | 6 ++-- .../rustc_hir_typeck/src/fn_ctxt/_impl.rs | 17 +++++------ compiler/rustc_hir_typeck/src/inherited.rs | 5 ++-- compiler/rustc_hir_typeck/src/method/probe.rs | 19 ++++--------- .../rustc_trait_selection/src/autoderef.rs | 11 ++++---- compiler/rustc_trait_selection/src/infer.rs | 28 ------------------- .../src/traits/engine.rs | 5 ++-- .../src/traits/error_reporting/mod.rs | 10 ++----- .../src/traits/error_reporting/suggestions.rs | 9 ++---- .../rustc_trait_selection/src/traits/mod.rs | 2 +- .../src/traits/project.rs | 14 ++++++++++ .../src/traits/query/normalize.rs | 4 +-- .../rustc_trait_selection/src/traits/util.rs | 12 ++++---- compiler/rustc_traits/src/dropck_outlives.rs | 2 +- .../src/normalize_erasing_regions.rs | 2 +- compiler/rustc_traits/src/type_op.rs | 2 +- src/librustdoc/clean/mod.rs | 2 +- src/tools/clippy/clippy_utils/src/ty.rs | 1 + 18 files changed, 58 insertions(+), 93 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs b/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs index b99bfda1a51fe..009787cc94cb8 100644 --- a/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs @@ -291,9 +291,9 @@ where // FIXME(lqd): Unify and de-duplicate the following with the actual // `rustc_traits::type_op::type_op_normalize` query to allow the span we need in the // `ObligationCause`. The normalization results are currently different between - // `AtExt::normalize` used in the query and `normalize` called below: the former fails - // to normalize the `nll/relate_tys/impl-fn-ignore-binder-via-bottom.rs` test. Check - // after #85499 lands to see if its fixes have erased this difference. + // `QueryNormalizeExt::query_normalize` used in the query and `normalize` called below: + // the former fails to normalize the `nll/relate_tys/impl-fn-ignore-binder-via-bottom.rs` test. + // Check after #85499 lands to see if its fixes have erased this difference. let (param_env, value) = key.into_parts(); let _ = ocx.normalize(cause, param_env, value.value); diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index 994fe48c9fee0..9ec85f0b1426d 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -30,9 +30,10 @@ use rustc_span::def_id::LocalDefId; use rustc_span::hygiene::DesugaringKind; use rustc_span::symbol::{kw, sym, Ident}; use rustc_span::{Span, DUMMY_SP}; -use rustc_trait_selection::infer::InferCtxtExt as _; use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _; -use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode, ObligationCtxt}; +use rustc_trait_selection::traits::{ + self, NormalizeExt, ObligationCause, ObligationCauseCode, ObligationCtxt, +}; use std::collections::hash_map::Entry; use std::slice; @@ -382,11 +383,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { where T: TypeFoldable<'tcx>, { - self.inh.partially_normalize_associated_types_in( - ObligationCause::misc(span, self.body_id), - self.param_env, - value, - ) + self.at(&ObligationCause::misc(span, self.body_id), self.param_env).normalize(value) } pub(in super::super) fn normalize_op_associated_types_in_as_infer_ok( @@ -398,8 +395,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { where T: TypeFoldable<'tcx>, { - self.inh.partially_normalize_associated_types_in( - ObligationCause::new( + self.at( + &ObligationCause::new( span, self.body_id, traits::BinOp { @@ -410,8 +407,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { }, ), self.param_env, - value, ) + .normalize(value) } pub fn require_type_meets( diff --git a/compiler/rustc_hir_typeck/src/inherited.rs b/compiler/rustc_hir_typeck/src/inherited.rs index e5c9f439af3e0..55179ff4011fb 100644 --- a/compiler/rustc_hir_typeck/src/inherited.rs +++ b/compiler/rustc_hir_typeck/src/inherited.rs @@ -12,9 +12,8 @@ use rustc_middle::ty::visit::TypeVisitable; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_span::def_id::LocalDefIdMap; use rustc_span::{self, Span}; -use rustc_trait_selection::infer::InferCtxtExt as _; use rustc_trait_selection::traits::{ - self, ObligationCause, ObligationCtxt, TraitEngine, TraitEngineExt as _, + self, NormalizeExt, ObligationCause, ObligationCtxt, TraitEngine, TraitEngineExt as _, }; use std::cell::RefCell; @@ -206,7 +205,7 @@ impl<'tcx> Inherited<'tcx> { where T: TypeFoldable<'tcx>, { - let ok = self.partially_normalize_associated_types_in(cause, param_env, value); + let ok = self.at(&cause, param_env).normalize(value); debug!(?ok); self.register_infer_ok_obligations(ok) } diff --git a/compiler/rustc_hir_typeck/src/method/probe.rs b/compiler/rustc_hir_typeck/src/method/probe.rs index 4380e66a0d248..c78a32c29dcda 100644 --- a/compiler/rustc_hir_typeck/src/method/probe.rs +++ b/compiler/rustc_hir_typeck/src/method/probe.rs @@ -31,13 +31,13 @@ use rustc_span::lev_distance::{ use rustc_span::symbol::sym; use rustc_span::{symbol::Ident, Span, Symbol, DUMMY_SP}; use rustc_trait_selection::autoderef::{self, Autoderef}; -use rustc_trait_selection::infer::InferCtxtExt as _; use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt; use rustc_trait_selection::traits::query::method_autoderef::MethodAutoderefBadTy; use rustc_trait_selection::traits::query::method_autoderef::{ CandidateStep, MethodAutoderefStepsResult, }; use rustc_trait_selection::traits::query::CanonicalTyGoal; +use rustc_trait_selection::traits::NormalizeExt; use rustc_trait_selection::traits::{self, ObligationCause}; use std::cmp::max; use std::iter; @@ -716,9 +716,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { // maybe shouldn't include `Param`s, but rather fresh variables or be canonicalized, // see issue #89650 let cause = traits::ObligationCause::misc(self.span, self.body_id); - let InferOk { value: xform_self_ty, obligations } = self - .fcx - .partially_normalize_associated_types_in(cause, self.param_env, xform_self_ty); + let InferOk { value: xform_self_ty, obligations } = + self.fcx.at(&cause, self.param_env).normalize(xform_self_ty); debug!( "assemble_inherent_impl_probe after normalization: xform_self_ty = {:?}/{:?}", @@ -1507,11 +1506,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { let InferOk { value: normalized_xform_ret_ty, obligations: normalization_obligations, - } = self.fcx.partially_normalize_associated_types_in( - cause.clone(), - self.param_env, - probe.xform_ret_ty, - ); + } = self.fcx.at(&cause, self.param_env).normalize(probe.xform_ret_ty); xform_ret_ty = normalized_xform_ret_ty; debug!("xform_ret_ty after normalization: {:?}", xform_ret_ty); @@ -1521,11 +1516,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { let impl_bounds = impl_bounds.instantiate(self.tcx, substs); let InferOk { value: impl_bounds, obligations: norm_obligations } = - self.fcx.partially_normalize_associated_types_in( - cause.clone(), - self.param_env, - impl_bounds, - ); + self.fcx.at(&cause, self.param_env).normalize(impl_bounds); // Convert the bounds into obligations. let impl_obligations = traits::predicates_for_generics( diff --git a/compiler/rustc_trait_selection/src/autoderef.rs b/compiler/rustc_trait_selection/src/autoderef.rs index af3a7ae248665..e988c77a064f6 100644 --- a/compiler/rustc_trait_selection/src/autoderef.rs +++ b/compiler/rustc_trait_selection/src/autoderef.rs @@ -1,6 +1,6 @@ use crate::errors::AutoDerefReachedRecursionLimit; -use crate::infer::InferCtxtExt as _; use crate::traits::query::evaluate_obligation::InferCtxtExt; +use crate::traits::NormalizeExt; use crate::traits::{self, TraitEngine, TraitEngineExt}; use rustc_hir as hir; use rustc_infer::infer::InferCtxt; @@ -138,11 +138,10 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> { return None; } - let normalized_ty = self.infcx.partially_normalize_associated_types_in( - cause, - self.param_env, - tcx.mk_projection(tcx.lang_items().deref_target()?, trait_ref.substs), - ); + let normalized_ty = self + .infcx + .at(&cause, self.param_env) + .normalize(tcx.mk_projection(tcx.lang_items().deref_target()?, trait_ref.substs)); let mut fulfillcx = >::new_in_snapshot(tcx); let normalized_ty = normalized_ty.into_value_registering_obligations(self.infcx, &mut *fulfillcx); diff --git a/compiler/rustc_trait_selection/src/infer.rs b/compiler/rustc_trait_selection/src/infer.rs index 25a9c29caa7ab..6c70bbf75163a 100644 --- a/compiler/rustc_trait_selection/src/infer.rs +++ b/compiler/rustc_trait_selection/src/infer.rs @@ -3,7 +3,6 @@ use crate::traits::{self, ObligationCtxt}; use rustc_hir::def_id::DefId; use rustc_hir::lang_items::LangItem; -use rustc_infer::traits::ObligationCause; use rustc_middle::arena::ArenaAllocatable; use rustc_middle::infer::canonical::{Canonical, CanonicalizedQueryResponse, QueryResponse}; use rustc_middle::traits::query::Fallible; @@ -30,15 +29,6 @@ pub trait InferCtxtExt<'tcx> { span: Span, ) -> bool; - fn partially_normalize_associated_types_in( - &self, - cause: ObligationCause<'tcx>, - param_env: ty::ParamEnv<'tcx>, - value: T, - ) -> InferOk<'tcx, T> - where - T: TypeFoldable<'tcx>; - /// Check whether a `ty` implements given trait(trait_def_id). /// The inputs are: /// @@ -88,24 +78,6 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> { traits::type_known_to_meet_bound_modulo_regions(self, param_env, ty, lang_item, span) } - /// Normalizes associated types in `value`, potentially returning - /// new obligations that must further be processed. - #[instrument(level = "debug", skip(self, cause, param_env), ret)] - fn partially_normalize_associated_types_in( - &self, - cause: ObligationCause<'tcx>, - param_env: ty::ParamEnv<'tcx>, - value: T, - ) -> InferOk<'tcx, T> - where - T: TypeFoldable<'tcx>, - { - let mut selcx = traits::SelectionContext::new(self); - let traits::Normalized { value, obligations } = - traits::normalize(&mut selcx, param_env, cause, value); - InferOk { value, obligations } - } - #[instrument(level = "debug", skip(self, params), ret)] fn type_implements_trait( &self, diff --git a/compiler/rustc_trait_selection/src/traits/engine.rs b/compiler/rustc_trait_selection/src/traits/engine.rs index 3a05708aebc6c..86c877347c9e6 100644 --- a/compiler/rustc_trait_selection/src/traits/engine.rs +++ b/compiler/rustc_trait_selection/src/traits/engine.rs @@ -3,7 +3,7 @@ use std::fmt::Debug; use super::TraitEngine; use super::{ChalkFulfillmentContext, FulfillmentContext}; -use crate::infer::InferCtxtExt; +use crate::traits::NormalizeExt; use rustc_data_structures::fx::FxIndexSet; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_infer::infer::at::ToTrace; @@ -104,11 +104,12 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> { pub fn normalize>( &self, + // FIXME(compiler-errors): Make this borrow cause: ObligationCause<'tcx>, param_env: ty::ParamEnv<'tcx>, value: T, ) -> T { - let infer_ok = self.infcx.partially_normalize_associated_types_in(cause, param_env, value); + let infer_ok = self.infcx.at(&cause, param_env).normalize(value); self.register_infer_ok_obligations(infer_ok) } diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index 809f107404ddf..e8468567cba0f 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -9,11 +9,11 @@ use super::{ }; use crate::infer::error_reporting::{TyCategory, TypeAnnotationNeeded as ErrorCode}; use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; -use crate::infer::InferCtxtExt as _; use crate::infer::{self, InferCtxt, TyCtxtInferExt}; use crate::traits::query::evaluate_obligation::InferCtxtExt as _; -use crate::traits::query::normalize::AtExt as _; +use crate::traits::query::normalize::QueryNormalizeExt as _; use crate::traits::specialize::to_pretty_impl_header; +use crate::traits::NormalizeExt; use on_unimplemented::OnUnimplementedNote; use on_unimplemented::TypeErrCtxtExt as _; use rustc_data_structures::fx::{FxHashMap, FxIndexMap}; @@ -2535,11 +2535,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> { pred.fold_with(&mut ParamToVarFolder { infcx: self, var_map: Default::default() }); let InferOk { value: cleaned_pred, .. } = - self.infcx.partially_normalize_associated_types_in( - ObligationCause::dummy(), - param_env, - cleaned_pred, - ); + self.infcx.at(&ObligationCause::dummy(), param_env).normalize(cleaned_pred); let obligation = Obligation::new(self.tcx, ObligationCause::dummy(), param_env, cleaned_pred); diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 992ea1755163f..1cb083ba3404d 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -2,6 +2,7 @@ use super::{DefIdOrName, Obligation, ObligationCause, ObligationCauseCode, Predi use crate::autoderef::Autoderef; use crate::infer::InferCtxt; +use crate::traits::NormalizeExt; use hir::def::CtorOf; use hir::HirId; @@ -2972,12 +2973,8 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { self.tcx.mk_substs_trait(trait_pred.self_ty(), []), ) }); - let InferOk { value: projection_ty, .. } = self - .partially_normalize_associated_types_in( - obligation.cause.clone(), - obligation.param_env, - projection_ty, - ); + let InferOk { value: projection_ty, .. } = + self.at(&obligation.cause, obligation.param_env).normalize(projection_ty); debug!( normalized_projection_type = ?self.resolve_vars_if_possible(projection_ty) diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs index 5285cfa674669..f038b294bd0fe 100644 --- a/compiler/rustc_trait_selection/src/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/mod.rs @@ -56,7 +56,7 @@ pub use self::object_safety::astconv_object_safety_violations; pub use self::object_safety::is_vtable_safe_method; pub use self::object_safety::MethodViolationCode; pub use self::object_safety::ObjectSafetyViolation; -pub use self::project::{normalize, normalize_projection_type, normalize_to}; +pub use self::project::{normalize, normalize_projection_type, normalize_to, NormalizeExt}; pub use self::select::{EvaluationCache, SelectionCache, SelectionContext}; pub use self::select::{EvaluationResult, IntercrateAmbiguityCause, OverflowError}; pub use self::specialize::specialization_graph::FutureCompatOverlapError; diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 18f4379d8f890..55db9277353e0 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -27,6 +27,7 @@ use rustc_errors::ErrorGuaranteed; use rustc_hir::def::DefKind; use rustc_hir::def_id::DefId; use rustc_hir::lang_items::LangItem; +use rustc_infer::infer::at::At; use rustc_infer::infer::resolve::OpportunisticRegionResolver; use rustc_infer::traits::ImplSourceBuiltinData; use rustc_middle::traits::select::OverflowError; @@ -48,6 +49,19 @@ pub type ProjectionTyObligation<'tcx> = Obligation<'tcx, ty::ProjectionTy<'tcx>> pub(super) struct InProgress; +pub trait NormalizeExt<'tcx> { + fn normalize>(&self, t: T) -> InferOk<'tcx, T>; +} + +impl<'tcx> NormalizeExt<'tcx> for At<'_, 'tcx> { + fn normalize>(&self, value: T) -> InferOk<'tcx, T> { + let mut selcx = SelectionContext::new(self.infcx); + let Normalized { value, obligations } = + normalize(&mut selcx, self.param_env, self.cause.clone(), value); + InferOk { value, obligations } + } +} + /// When attempting to resolve `::Name` ... #[derive(Debug)] pub enum ProjectionError<'tcx> { diff --git a/compiler/rustc_trait_selection/src/traits/query/normalize.rs b/compiler/rustc_trait_selection/src/traits/query/normalize.rs index 1d52999065222..1aed66308709e 100644 --- a/compiler/rustc_trait_selection/src/traits/query/normalize.rs +++ b/compiler/rustc_trait_selection/src/traits/query/normalize.rs @@ -22,13 +22,13 @@ use super::NoSolution; pub use rustc_middle::traits::query::NormalizationResult; -pub trait AtExt<'tcx> { +pub trait QueryNormalizeExt<'tcx> { fn query_normalize(&self, value: T) -> Result, NoSolution> where T: TypeFoldable<'tcx>; } -impl<'cx, 'tcx> AtExt<'tcx> for At<'cx, 'tcx> { +impl<'cx, 'tcx> QueryNormalizeExt<'tcx> for At<'cx, 'tcx> { /// Normalize `value` in the context of the inference context, /// yielding a resulting type, or an error if `value` cannot be /// normalized. If you don't care about regions, you should prefer diff --git a/compiler/rustc_trait_selection/src/traits/util.rs b/compiler/rustc_trait_selection/src/traits/util.rs index a06db4c274831..51968c2d7a191 100644 --- a/compiler/rustc_trait_selection/src/traits/util.rs +++ b/compiler/rustc_trait_selection/src/traits/util.rs @@ -8,8 +8,8 @@ use rustc_hir::def_id::DefId; use rustc_middle::ty::{self, ImplSubject, ToPredicate, Ty, TyCtxt, TypeVisitable}; use rustc_middle::ty::{GenericArg, SubstsRef}; +use super::NormalizeExt; use super::{Obligation, ObligationCause, PredicateObligation, SelectionContext}; -use crate::infer::InferCtxtExt; use rustc_infer::infer::InferOk; pub use rustc_infer::traits::{self, util::*}; @@ -202,15 +202,13 @@ pub fn impl_subject_and_oblig<'a, 'tcx>( ) -> (ImplSubject<'tcx>, impl Iterator>) { let subject = selcx.tcx().bound_impl_subject(impl_def_id); let subject = subject.subst(selcx.tcx(), impl_substs); - let InferOk { value: subject, obligations: normalization_obligations1 } = selcx - .infcx - .partially_normalize_associated_types_in(ObligationCause::dummy(), param_env, subject); + let InferOk { value: subject, obligations: normalization_obligations1 } = + selcx.infcx.at(&ObligationCause::dummy(), param_env).normalize(subject); let predicates = selcx.tcx().predicates_of(impl_def_id); let predicates = predicates.instantiate(selcx.tcx(), impl_substs); - let InferOk { value: predicates, obligations: normalization_obligations2 } = selcx - .infcx - .partially_normalize_associated_types_in(ObligationCause::dummy(), param_env, predicates); + let InferOk { value: predicates, obligations: normalization_obligations2 } = + selcx.infcx.at(&ObligationCause::dummy(), param_env).normalize(predicates); let impl_obligations = super::predicates_for_generics(|_, _| ObligationCause::dummy(), param_env, predicates); diff --git a/compiler/rustc_traits/src/dropck_outlives.rs b/compiler/rustc_traits/src/dropck_outlives.rs index 87f9862017fc5..66ab742f15782 100644 --- a/compiler/rustc_traits/src/dropck_outlives.rs +++ b/compiler/rustc_traits/src/dropck_outlives.rs @@ -11,7 +11,7 @@ use rustc_trait_selection::traits::query::dropck_outlives::trivial_dropck_outliv use rustc_trait_selection::traits::query::dropck_outlives::{ DropckConstraint, DropckOutlivesResult, }; -use rustc_trait_selection::traits::query::normalize::AtExt; +use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt; use rustc_trait_selection::traits::query::{CanonicalTyGoal, NoSolution}; use rustc_trait_selection::traits::{Normalized, ObligationCause}; diff --git a/compiler/rustc_traits/src/normalize_erasing_regions.rs b/compiler/rustc_traits/src/normalize_erasing_regions.rs index 0309e2ba92c27..44fd8bfb31f23 100644 --- a/compiler/rustc_traits/src/normalize_erasing_regions.rs +++ b/compiler/rustc_traits/src/normalize_erasing_regions.rs @@ -2,7 +2,7 @@ use rustc_infer::infer::TyCtxtInferExt; use rustc_middle::traits::query::NoSolution; use rustc_middle::ty::query::Providers; use rustc_middle::ty::{self, ParamEnvAnd, TyCtxt, TypeFoldable}; -use rustc_trait_selection::traits::query::normalize::AtExt; +use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt; use rustc_trait_selection::traits::{Normalized, ObligationCause}; use std::sync::atomic::Ordering; diff --git a/compiler/rustc_traits/src/type_op.rs b/compiler/rustc_traits/src/type_op.rs index 8ffa89023c6cb..98141506298d4 100644 --- a/compiler/rustc_traits/src/type_op.rs +++ b/compiler/rustc_traits/src/type_op.rs @@ -8,7 +8,7 @@ use rustc_middle::ty::{ParamEnvAnd, Predicate, ToPredicate}; use rustc_middle::ty::{UserSelfTy, UserSubsts}; use rustc_span::{Span, DUMMY_SP}; use rustc_trait_selection::infer::InferCtxtBuilderExt; -use rustc_trait_selection::traits::query::normalize::AtExt; +use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt; use rustc_trait_selection::traits::query::type_op::ascribe_user_type::AscribeUserType; use rustc_trait_selection::traits::query::type_op::eq::Eq; use rustc_trait_selection::traits::query::type_op::normalize::Normalize; diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 9d572e2291e5c..8bb5af6d96090 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1581,7 +1581,7 @@ fn normalize<'tcx>(cx: &mut DocContext<'tcx>, ty: Ty<'tcx>) -> Option> } use crate::rustc_trait_selection::infer::TyCtxtInferExt; - use crate::rustc_trait_selection::traits::query::normalize::AtExt; + use crate::rustc_trait_selection::traits::query::normalize::QueryNormalizeExt; use rustc_middle::traits::ObligationCause; // Try to normalize `::T` to a type diff --git a/src/tools/clippy/clippy_utils/src/ty.rs b/src/tools/clippy/clippy_utils/src/ty.rs index f4459e3e6633a..82496f120e372 100644 --- a/src/tools/clippy/clippy_utils/src/ty.rs +++ b/src/tools/clippy/clippy_utils/src/ty.rs @@ -22,6 +22,7 @@ use rustc_span::symbol::Ident; use rustc_span::{sym, Span, Symbol, DUMMY_SP}; use rustc_target::abi::{Size, VariantIdx}; use rustc_trait_selection::infer::InferCtxtExt; +use rustc_trait_selection::traits::NormalizeExt; use rustc_trait_selection::traits::query::normalize::AtExt; use std::iter; From 52cd34269644e65b88ac4faf29777b249a2cf188 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 25 Nov 2022 17:28:50 +0000 Subject: [PATCH 159/244] FnCtxt normalization stuff --- .../rustc_hir_analysis/src/astconv/mod.rs | 3 ++ .../rustc_hir_analysis/src/check/wfcheck.rs | 2 ++ compiler/rustc_hir_typeck/src/callee.rs | 2 +- compiler/rustc_hir_typeck/src/cast.rs | 2 +- compiler/rustc_hir_typeck/src/closure.rs | 8 ++--- compiler/rustc_hir_typeck/src/coercion.rs | 3 +- compiler/rustc_hir_typeck/src/expr.rs | 4 +-- .../rustc_hir_typeck/src/fn_ctxt/_impl.rs | 16 +++++---- compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs | 2 +- .../src/fn_ctxt/suggestions.rs | 4 +-- .../src/generator_interior/mod.rs | 2 +- compiler/rustc_hir_typeck/src/inherited.rs | 34 +------------------ compiler/rustc_hir_typeck/src/lib.rs | 17 ++++++---- .../rustc_hir_typeck/src/method/confirm.rs | 4 +-- src/tools/clippy/clippy_utils/src/ty.rs | 5 ++- 15 files changed, 41 insertions(+), 67 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/astconv/mod.rs b/compiler/rustc_hir_analysis/src/astconv/mod.rs index 5f06db4038ff7..22ea83a13f03a 100644 --- a/compiler/rustc_hir_analysis/src/astconv/mod.rs +++ b/compiler/rustc_hir_analysis/src/astconv/mod.rs @@ -109,6 +109,9 @@ pub trait AstConv<'tcx> { ) -> Ty<'tcx>; /// Normalize an associated type coming from the user. + /// + /// This should only be used by astconv. Use `FnCtxt::normalize` + /// or `ObligationCtxt::normalize` in downstream crates. fn normalize_ty(&self, span: Span, ty: Ty<'tcx>) -> Ty<'tcx>; /// Invoked when we encounter an error from some prior pass diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index 8171a2ab270cd..894c0ba34155a 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -53,6 +53,8 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> { self.ocx.infcx.tcx } + // Convenience function to normalize during wfcheck. This performs + // `ObligationCtxt::normalize`, but provides a nice `ObligationCauseCode`. fn normalize(&self, span: Span, loc: Option, value: T) -> T where T: TypeFoldable<'tcx>, diff --git a/compiler/rustc_hir_typeck/src/callee.rs b/compiler/rustc_hir_typeck/src/callee.rs index e50d249849fa8..b09ddf80e2a52 100644 --- a/compiler/rustc_hir_typeck/src/callee.rs +++ b/compiler/rustc_hir_typeck/src/callee.rs @@ -448,7 +448,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // previously appeared within a `Binder<>` and hence would not // have been normalized before. let fn_sig = self.replace_bound_vars_with_fresh_vars(call_expr.span, infer::FnCall, fn_sig); - let fn_sig = self.normalize_associated_types_in(call_expr.span, fn_sig); + let fn_sig = self.normalize(call_expr.span, fn_sig); // Call the generic checker. let expected_arg_tys = self.expected_inputs_for_expected_output( diff --git a/compiler/rustc_hir_typeck/src/cast.rs b/compiler/rustc_hir_typeck/src/cast.rs index 732b4bec58ed5..2a9679eed0461 100644 --- a/compiler/rustc_hir_typeck/src/cast.rs +++ b/compiler/rustc_hir_typeck/src/cast.rs @@ -752,7 +752,7 @@ impl<'a, 'tcx> CastCheck<'tcx> { match *self.expr_ty.kind() { ty::FnDef(..) => { // Attempt a coercion to a fn pointer type. - let f = fcx.normalize_associated_types_in( + let f = fcx.normalize( self.expr_span, self.expr_ty.fn_sig(fcx.tcx), ); diff --git a/compiler/rustc_hir_typeck/src/closure.rs b/compiler/rustc_hir_typeck/src/closure.rs index e584d413c4190..33c87768ec1a3 100644 --- a/compiler/rustc_hir_typeck/src/closure.rs +++ b/compiler/rustc_hir_typeck/src/closure.rs @@ -214,7 +214,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if expected_sig.is_none() && let ty::PredicateKind::Clause(ty::Clause::Projection(proj_predicate)) = bound_predicate.skip_binder() { - expected_sig = self.normalize_associated_types_in( + expected_sig = self.normalize( obligation.cause.span, self.deduce_sig_from_projection( Some(obligation.cause.span), @@ -623,7 +623,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ); // Astconv can't normalize inputs or outputs with escaping bound vars, // so normalize them here, after we've wrapped them in a binder. - let result = self.normalize_associated_types_in(self.tcx.hir().span(hir_id), result); + let result = self.normalize(self.tcx.hir().span(hir_id), result); let c_result = self.inh.infcx.canonicalize_response(result); self.typeck_results.borrow_mut().user_provided_sigs.insert(expr_def_id, c_result); @@ -797,10 +797,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ) -> ClosureSignatures<'tcx> { let liberated_sig = self.tcx().liberate_late_bound_regions(expr_def_id.to_def_id(), bound_sig); - let liberated_sig = self.inh.normalize_associated_types_in( + let liberated_sig = self.normalize( body.value.span, - self.tcx.hir().local_def_id_to_hir_id(expr_def_id), - self.param_env, liberated_sig, ); ClosureSignatures { bound_sig, liberated_sig } diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index b2d9d70fbd2ac..e93d95ee66d9b 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -1141,8 +1141,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return Err(TypeError::IntrinsicCast); } // The signature must match. - let a_sig = self.normalize_associated_types_in(new.span, a_sig); - let b_sig = self.normalize_associated_types_in(new.span, b_sig); + let (a_sig, b_sig) = self.normalize(new.span, (a_sig, b_sig)); let sig = self .at(cause, self.param_env) .trace(prev_ty, new_ty) diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 5166f1fd1c743..7b101fc5c66c8 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -1664,7 +1664,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .fields .iter() .map(|f| { - let fru_ty = self.normalize_associated_types_in( + let fru_ty = self.normalize( expr_span, self.field_ty(base_expr.span, f, fresh_substs), ); @@ -1749,7 +1749,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .fields .iter() .map(|f| { - self.normalize_associated_types_in(expr_span, f.ty(self.tcx, substs)) + self.normalize(expr_span, f.ty(self.tcx, substs)) }) .collect(), _ => { diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index 9ec85f0b1426d..8ce9e40cfdbf8 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -344,7 +344,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { { debug!("instantiate_type_scheme(value={:?}, substs={:?})", value, substs); let value = EarlyBinder(value).subst(self.tcx, substs); - let result = self.normalize_associated_types_in(span, value); + let result = self.normalize(span, value); debug!("instantiate_type_scheme = {:?}", result); result } @@ -360,7 +360,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let bounds = self.tcx.predicates_of(def_id); let spans: Vec = bounds.predicates.iter().map(|(_, span)| *span).collect(); let result = bounds.instantiate(self.tcx, substs); - let result = self.normalize_associated_types_in(span, result); + let result = self.normalize(span, result); debug!( "instantiate_bounds(bounds={:?}, substs={:?}) = {:?}, {:?}", bounds, substs, result, spans, @@ -368,11 +368,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { (result, spans) } - pub(in super::super) fn normalize_associated_types_in(&self, span: Span, value: T) -> T + pub(in super::super) fn normalize(&self, span: Span, value: T) -> T where T: TypeFoldable<'tcx>, { - self.inh.normalize_associated_types_in(span, self.body_id, self.param_env, value) + self.register_infer_ok_obligations( + self.at(&self.misc(span), self.param_env).normalize(value), + ) } pub(in super::super) fn normalize_associated_types_in_as_infer_ok( @@ -487,7 +489,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let span = self.tcx.def_span(anon_const.def_id); let c = ty::Const::from_anon_const(self.tcx, anon_const.def_id); self.register_wf_obligation(c.into(), span, ObligationCauseCode::WellFormed(None)); - self.normalize_associated_types_in(span, c) + self.normalize(span, c) } } } @@ -580,7 +582,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { field: &'tcx ty::FieldDef, substs: SubstsRef<'tcx>, ) -> Ty<'tcx> { - self.normalize_associated_types_in(span, field.ty(self.tcx, substs)) + self.normalize(span, field.ty(self.tcx, substs)) } pub(in super::super) fn resolve_rvalue_scopes(&self, def_id: DefId) { @@ -1107,7 +1109,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if let Res::Local(hid) = res { let ty = self.local_ty(span, hid).decl_ty; - let ty = self.normalize_associated_types_in(span, ty); + let ty = self.normalize(span, ty); self.write_ty(hir_id, ty); return (ty, res); } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs index ed5f7f31764fd..ea141e815bf4a 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs @@ -288,7 +288,7 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> { if ty.has_escaping_bound_vars() { ty // FIXME: normalization and escaping regions } else { - self.normalize_associated_types_in(span, ty) + self.normalize(span, ty) } } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 6c3526a71a328..10cc4e8181944 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -759,7 +759,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { debug!("suggest_missing_return_type: expected type {:?}", ty); let bound_vars = self.tcx.late_bound_vars(fn_id); let ty = Binder::bind_with_vars(ty, bound_vars); - let ty = self.normalize_associated_types_in(span, ty); + let ty = self.normalize(span, ty); let ty = self.tcx.erase_late_bound_regions(ty); if self.can_coerce(expected, ty) { err.subdiagnostic(ExpectedReturnTypeLabel::Other { span, expected }); @@ -920,7 +920,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let ty = >::ast_ty_to_ty(self, ty); let bound_vars = self.tcx.late_bound_vars(fn_id); let ty = self.tcx.erase_late_bound_regions(Binder::bind_with_vars(ty, bound_vars)); - let ty = self.normalize_associated_types_in(expr.span, ty); + let ty = self.normalize(expr.span, ty); let ty = match self.tcx.asyncness(fn_id.owner) { hir::IsAsync::Async => { let infcx = self.tcx.infer_ctxt().build(); diff --git a/compiler/rustc_hir_typeck/src/generator_interior/mod.rs b/compiler/rustc_hir_typeck/src/generator_interior/mod.rs index 4e044d39d50a1..3b1518ff79b4e 100644 --- a/compiler/rustc_hir_typeck/src/generator_interior/mod.rs +++ b/compiler/rustc_hir_typeck/src/generator_interior/mod.rs @@ -235,7 +235,7 @@ pub fn resolve_interior<'a, 'tcx>( counter += 1; ty::BoundRegion { var, kind } }; - let ty = fcx.normalize_associated_types_in(cause.span, cause.ty); + let ty = fcx.normalize(cause.span, cause.ty); let ty = fcx.tcx.fold_regions(ty, |region, current_depth| { let br = match region.kind() { ty::ReVar(vid) => { diff --git a/compiler/rustc_hir_typeck/src/inherited.rs b/compiler/rustc_hir_typeck/src/inherited.rs index 55179ff4011fb..e8a9e4980f613 100644 --- a/compiler/rustc_hir_typeck/src/inherited.rs +++ b/compiler/rustc_hir_typeck/src/inherited.rs @@ -7,13 +7,12 @@ use rustc_hir::def_id::LocalDefId; use rustc_hir::HirIdMap; use rustc_infer::infer; use rustc_infer::infer::{DefiningAnchor, InferCtxt, InferOk, TyCtxtInferExt}; -use rustc_middle::ty::fold::TypeFoldable; use rustc_middle::ty::visit::TypeVisitable; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_span::def_id::LocalDefIdMap; use rustc_span::{self, Span}; use rustc_trait_selection::traits::{ - self, NormalizeExt, ObligationCause, ObligationCtxt, TraitEngine, TraitEngineExt as _, + self, ObligationCause, ObligationCtxt, TraitEngine, TraitEngineExt as _, }; use std::cell::RefCell; @@ -178,35 +177,4 @@ impl<'tcx> Inherited<'tcx> { self.register_predicates(infer_ok.obligations); infer_ok.value } - - pub(super) fn normalize_associated_types_in( - &self, - span: Span, - body_id: hir::HirId, - param_env: ty::ParamEnv<'tcx>, - value: T, - ) -> T - where - T: TypeFoldable<'tcx>, - { - self.normalize_associated_types_in_with_cause( - ObligationCause::misc(span, body_id), - param_env, - value, - ) - } - - pub(super) fn normalize_associated_types_in_with_cause( - &self, - cause: ObligationCause<'tcx>, - param_env: ty::ParamEnv<'tcx>, - value: T, - ) -> T - where - T: TypeFoldable<'tcx>, - { - let ok = self.at(&cause, param_env).normalize(value); - debug!(?ok); - self.register_infer_ok_obligations(ok) - } } diff --git a/compiler/rustc_hir_typeck/src/lib.rs b/compiler/rustc_hir_typeck/src/lib.rs index 5104b44802323..11c56b5fce61e 100644 --- a/compiler/rustc_hir_typeck/src/lib.rs +++ b/compiler/rustc_hir_typeck/src/lib.rs @@ -48,6 +48,8 @@ pub use diverges::Diverges; pub use expectation::Expectation; pub use fn_ctxt::*; pub use inherited::{Inherited, InheritedBuilder}; +use rustc_infer::traits::ObligationCause; +use rustc_trait_selection::traits::NormalizeExt; use crate::check::check_fn; use crate::coercion::DynamicCoerceMany; @@ -245,12 +247,13 @@ fn typeck_with_fallback<'tcx>( // Compute the function signature from point of view of inside the fn. let fn_sig = tcx.liberate_late_bound_regions(def_id.to_def_id(), fn_sig); - let fn_sig = inh.normalize_associated_types_in( - body.value.span, - body_id.hir_id, - param_env, - fn_sig, - ); + // FIXME(compiler-errors): Remove + let fn_sig = inh + .register_infer_ok_obligations( + inh.at(&ObligationCause::misc(body.value.span, body_id.hir_id), + param_env, + ) + .normalize(fn_sig)); check_fn(&inh, param_env, fn_sig, decl, def_id, body, None).0 } else { let fcx = FnCtxt::new(&inh, param_env, body.value.hir_id); @@ -304,7 +307,7 @@ fn typeck_with_fallback<'tcx>( _ => fallback(), }); - let expected_type = fcx.normalize_associated_types_in(body.value.span, expected_type); + let expected_type = fcx.normalize(body.value.span, expected_type); fcx.require_type_is_sized(expected_type, body.value.span, traits::ConstSized); // Gather locals in statics (because of block expressions). diff --git a/compiler/rustc_hir_typeck/src/method/confirm.rs b/compiler/rustc_hir_typeck/src/method/confirm.rs index f50a16dcb236d..03d0e7926de11 100644 --- a/compiler/rustc_hir_typeck/src/method/confirm.rs +++ b/compiler/rustc_hir_typeck/src/method/confirm.rs @@ -106,7 +106,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> { // traits, no trait system method can be called before this point because they // could alter our Self-type, except for normalizing the receiver from the // signature (which is also done during probing). - let method_sig_rcvr = self.normalize_associated_types_in(self.span, method_sig.inputs()[0]); + let method_sig_rcvr = self.normalize(self.span, method_sig.inputs()[0]); debug!( "confirm: self_ty={:?} method_sig_rcvr={:?} method_sig={:?} method_predicates={:?}", self_ty, method_sig_rcvr, method_sig, method_predicates @@ -114,7 +114,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> { self.unify_receivers(self_ty, method_sig_rcvr, &pick, all_substs); let (method_sig, method_predicates) = - self.normalize_associated_types_in(self.span, (method_sig, method_predicates)); + self.normalize(self.span, (method_sig, method_predicates)); let method_sig = ty::Binder::dummy(method_sig); // Make sure nobody calls `drop()` explicitly. diff --git a/src/tools/clippy/clippy_utils/src/ty.rs b/src/tools/clippy/clippy_utils/src/ty.rs index 82496f120e372..2ceda3511fe44 100644 --- a/src/tools/clippy/clippy_utils/src/ty.rs +++ b/src/tools/clippy/clippy_utils/src/ty.rs @@ -22,8 +22,7 @@ use rustc_span::symbol::Ident; use rustc_span::{sym, Span, Symbol, DUMMY_SP}; use rustc_target::abi::{Size, VariantIdx}; use rustc_trait_selection::infer::InferCtxtExt; -use rustc_trait_selection::traits::NormalizeExt; -use rustc_trait_selection::traits::query::normalize::AtExt; +use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt; use std::iter; use crate::{match_def_path, path_res, paths}; @@ -284,7 +283,7 @@ fn is_normalizable_helper<'tcx>( cache.insert(ty, false); let infcx = cx.tcx.infer_ctxt().build(); let cause = rustc_middle::traits::ObligationCause::dummy(); - let result = if infcx.at(&cause, param_env).normalize(ty).is_ok() { + let result = if infcx.at(&cause, param_env).query_normalize(ty).is_ok() { match ty.kind() { ty::Adt(def, substs) => def.variants().iter().all(|variant| { variant From 06786227fdb92f7a905cfd4a0131f2ec9db5072f Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 25 Nov 2022 18:00:27 +0000 Subject: [PATCH 160/244] Simplify more FnCtxt normalization --- .../rustc_hir_analysis/src/astconv/mod.rs | 2 +- compiler/rustc_hir_typeck/src/cast.rs | 5 +-- compiler/rustc_hir_typeck/src/check.rs | 21 ++++------ compiler/rustc_hir_typeck/src/closure.rs | 12 ++---- compiler/rustc_hir_typeck/src/coercion.rs | 8 ++-- compiler/rustc_hir_typeck/src/expr.rs | 4 +- .../rustc_hir_typeck/src/fn_ctxt/_impl.rs | 42 +------------------ .../src/fn_ctxt/suggestions.rs | 3 +- compiler/rustc_hir_typeck/src/lib.rs | 21 +++------- compiler/rustc_hir_typeck/src/method/mod.rs | 39 +++++++---------- 10 files changed, 46 insertions(+), 111 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/astconv/mod.rs b/compiler/rustc_hir_analysis/src/astconv/mod.rs index 22ea83a13f03a..8215031063827 100644 --- a/compiler/rustc_hir_analysis/src/astconv/mod.rs +++ b/compiler/rustc_hir_analysis/src/astconv/mod.rs @@ -109,7 +109,7 @@ pub trait AstConv<'tcx> { ) -> Ty<'tcx>; /// Normalize an associated type coming from the user. - /// + /// /// This should only be used by astconv. Use `FnCtxt::normalize` /// or `ObligationCtxt::normalize` in downstream crates. fn normalize_ty(&self, span: Span, ty: Ty<'tcx>) -> Ty<'tcx>; diff --git a/compiler/rustc_hir_typeck/src/cast.rs b/compiler/rustc_hir_typeck/src/cast.rs index 2a9679eed0461..890a068a7befc 100644 --- a/compiler/rustc_hir_typeck/src/cast.rs +++ b/compiler/rustc_hir_typeck/src/cast.rs @@ -752,10 +752,7 @@ impl<'a, 'tcx> CastCheck<'tcx> { match *self.expr_ty.kind() { ty::FnDef(..) => { // Attempt a coercion to a fn pointer type. - let f = fcx.normalize( - self.expr_span, - self.expr_ty.fn_sig(fcx.tcx), - ); + let f = fcx.normalize(self.expr_span, self.expr_ty.fn_sig(fcx.tcx)); let res = fcx.try_coerce( self.expr, self.expr_ty, diff --git a/compiler/rustc_hir_typeck/src/check.rs b/compiler/rustc_hir_typeck/src/check.rs index 1ceb07def72e7..0c9a350c295f4 100644 --- a/compiler/rustc_hir_typeck/src/check.rs +++ b/compiler/rustc_hir_typeck/src/check.rs @@ -1,6 +1,6 @@ use crate::coercion::CoerceMany; use crate::gather_locals::GatherLocalsVisitor; -use crate::{FnCtxt, Inherited}; +use crate::FnCtxt; use crate::{GeneratorTypes, UnsafetyState}; use rustc_hir as hir; use rustc_hir::def::DefKind; @@ -20,21 +20,16 @@ use std::cell::RefCell; /// /// * ... /// * inherited: other fields inherited from the enclosing fn (if any) -#[instrument(skip(inherited, body), level = "debug")] +#[instrument(skip(fcx, body), level = "debug")] pub(super) fn check_fn<'a, 'tcx>( - inherited: &'a Inherited<'tcx>, - param_env: ty::ParamEnv<'tcx>, + fcx: &mut FnCtxt<'a, 'tcx>, fn_sig: ty::FnSig<'tcx>, decl: &'tcx hir::FnDecl<'tcx>, fn_def_id: LocalDefId, body: &'tcx hir::Body<'tcx>, can_be_generator: Option, -) -> (FnCtxt<'a, 'tcx>, Option>) { - let fn_id = inherited.tcx.hir().local_def_id_to_hir_id(fn_def_id); - - // Create the function context. This is either derived from scratch or, - // in the case of closures, based on the outer context. - let mut fcx = FnCtxt::new(inherited, param_env, body.value.hir_id); +) -> Option> { + let fn_id = fcx.tcx.hir().local_def_id_to_hir_id(fn_def_id); fcx.ps.set(UnsafetyState::function(fn_sig.unsafety, fn_id)); let tcx = fcx.tcx; @@ -47,7 +42,7 @@ pub(super) fn check_fn<'a, 'tcx>( declared_ret_ty, body.value.hir_id, decl.output.span(), - param_env, + fcx.param_env, )); fcx.ret_coercion = Some(RefCell::new(CoerceMany::new(ret_ty))); @@ -105,7 +100,7 @@ pub(super) fn check_fn<'a, 'tcx>( fcx.write_ty(param.hir_id, param_ty); } - inherited.typeck_results.borrow_mut().liberated_fn_sigs_mut().insert(fn_id, fn_sig); + fcx.typeck_results.borrow_mut().liberated_fn_sigs_mut().insert(fn_id, fn_sig); if let ty::Dynamic(_, _, ty::Dyn) = declared_ret_ty.kind() { // FIXME: We need to verify that the return type is `Sized` after the return expression has @@ -174,7 +169,7 @@ pub(super) fn check_fn<'a, 'tcx>( check_panic_info_fn(tcx, panic_impl_did.expect_local(), fn_sig, decl, declared_ret_ty); } - (fcx, gen_ty) + gen_ty } fn check_panic_info_fn( diff --git a/compiler/rustc_hir_typeck/src/closure.rs b/compiler/rustc_hir_typeck/src/closure.rs index 33c87768ec1a3..5d3419b3b6e66 100644 --- a/compiler/rustc_hir_typeck/src/closure.rs +++ b/compiler/rustc_hir_typeck/src/closure.rs @@ -79,16 +79,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { debug!(?bound_sig, ?liberated_sig); + let mut fcx = FnCtxt::new(self, self.param_env.without_const(), body.value.hir_id); let generator_types = check_fn( - self, - self.param_env.without_const(), + &mut fcx, liberated_sig, closure.fn_decl, expr_def_id, body, closure.movability, - ) - .1; + ); let parent_substs = InternalSubsts::identity_for_item( self.tcx, @@ -797,10 +796,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ) -> ClosureSignatures<'tcx> { let liberated_sig = self.tcx().liberate_late_bound_regions(expr_def_id.to_def_id(), bound_sig); - let liberated_sig = self.normalize( - body.value.span, - liberated_sig, - ); + let liberated_sig = self.normalize(body.value.span, liberated_sig); ClosureSignatures { bound_sig, liberated_sig } } } diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index e93d95ee66d9b..f0b349f0c98dd 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -62,7 +62,9 @@ use rustc_span::{self, BytePos, DesugaringKind, Span}; use rustc_target::spec::abi::Abi; use rustc_trait_selection::infer::InferCtxtExt as _; use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _; -use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode, ObligationCtxt}; +use rustc_trait_selection::traits::{ + self, NormalizeExt, ObligationCause, ObligationCauseCode, ObligationCtxt, +}; use smallvec::{smallvec, SmallVec}; use std::ops::Deref; @@ -832,7 +834,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { let b = self.shallow_resolve(b); let InferOk { value: b, mut obligations } = - self.normalize_associated_types_in_as_infer_ok(self.cause.span, b); + self.at(&self.cause, self.param_env).normalize(b); debug!("coerce_from_fn_item(a={:?}, b={:?})", a, b); match b.kind() { @@ -854,7 +856,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { } let InferOk { value: a_sig, obligations: o1 } = - self.normalize_associated_types_in_as_infer_ok(self.cause.span, a_sig); + self.at(&self.cause, self.param_env).normalize(a_sig); obligations.extend(o1); let a_fn_pointer = self.tcx.mk_fn_ptr(a_sig); diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 7b101fc5c66c8..0c5bbb3e20be3 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -1748,9 +1748,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ty::Adt(adt, substs) if adt.is_struct() => variant .fields .iter() - .map(|f| { - self.normalize(expr_span, f.ty(self.tcx, substs)) - }) + .map(|f| self.normalize(expr_span, f.ty(self.tcx, substs))) .collect(), _ => { self.tcx diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index 8ce9e40cfdbf8..952d272625918 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -16,7 +16,7 @@ use rustc_hir_analysis::astconv::{ }; use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryResponse}; use rustc_infer::infer::error_reporting::TypeAnnotationNeeded::E0282; -use rustc_infer::infer::{InferOk, InferResult}; +use rustc_infer::infer::InferResult; use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability}; use rustc_middle::ty::error::TypeError; use rustc_middle::ty::fold::TypeFoldable; @@ -31,9 +31,7 @@ use rustc_span::hygiene::DesugaringKind; use rustc_span::symbol::{kw, sym, Ident}; use rustc_span::{Span, DUMMY_SP}; use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _; -use rustc_trait_selection::traits::{ - self, NormalizeExt, ObligationCause, ObligationCauseCode, ObligationCtxt, -}; +use rustc_trait_selection::traits::{self, NormalizeExt, ObligationCauseCode, ObligationCtxt}; use std::collections::hash_map::Entry; use std::slice; @@ -377,42 +375,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ) } - pub(in super::super) fn normalize_associated_types_in_as_infer_ok( - &self, - span: Span, - value: T, - ) -> InferOk<'tcx, T> - where - T: TypeFoldable<'tcx>, - { - self.at(&ObligationCause::misc(span, self.body_id), self.param_env).normalize(value) - } - - pub(in super::super) fn normalize_op_associated_types_in_as_infer_ok( - &self, - span: Span, - value: T, - opt_input_expr: Option<&hir::Expr<'_>>, - ) -> InferOk<'tcx, T> - where - T: TypeFoldable<'tcx>, - { - self.at( - &ObligationCause::new( - span, - self.body_id, - traits::BinOp { - rhs_span: opt_input_expr.map(|expr| expr.span), - is_lit: opt_input_expr - .map_or(false, |expr| matches!(expr.kind, ExprKind::Lit(_))), - output_ty: None, - }, - ), - self.param_env, - ) - .normalize(value) - } - pub fn require_type_meets( &self, ty: Ty<'tcx>, diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 10cc4e8181944..b9a8d16311c93 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -20,6 +20,7 @@ use rustc_span::Span; use rustc_trait_selection::infer::InferCtxtExt; use rustc_trait_selection::traits::error_reporting::DefIdOrName; use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _; +use rustc_trait_selection::traits::NormalizeExt; impl<'a, 'tcx> FnCtxt<'a, 'tcx> { pub(crate) fn body_fn_sig(&self) -> Option> { @@ -245,7 +246,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // implied by wf, but also because that would possibly result in // erroneous errors later on. let infer::InferOk { value: output, obligations: _ } = - self.normalize_associated_types_in_as_infer_ok(expr.span, output); + self.at(&self.misc(expr.span), self.param_env).normalize(output); if output.is_ty_var() { None } else { Some((def_id_or_name, output, inputs)) } } diff --git a/compiler/rustc_hir_typeck/src/lib.rs b/compiler/rustc_hir_typeck/src/lib.rs index 11c56b5fce61e..09bd123350dfd 100644 --- a/compiler/rustc_hir_typeck/src/lib.rs +++ b/compiler/rustc_hir_typeck/src/lib.rs @@ -48,8 +48,6 @@ pub use diverges::Diverges; pub use expectation::Expectation; pub use fn_ctxt::*; pub use inherited::{Inherited, InheritedBuilder}; -use rustc_infer::traits::ObligationCause; -use rustc_trait_selection::traits::NormalizeExt; use crate::check::check_fn; use crate::coercion::DynamicCoerceMany; @@ -235,9 +233,10 @@ fn typeck_with_fallback<'tcx>( let typeck_results = Inherited::build(tcx, def_id).enter(|inh| { let param_env = tcx.param_env(def_id); - let mut fcx = if let Some(hir::FnSig { header, decl, .. }) = fn_sig { + let mut fcx = FnCtxt::new(&inh, param_env, body.value.hir_id); + + if let Some(hir::FnSig { header, decl, .. }) = fn_sig { let fn_sig = if rustc_hir_analysis::collect::get_infer_ret_ty(&decl.output).is_some() { - let fcx = FnCtxt::new(&inh, param_env, body.value.hir_id); >::ty_of_fn(&fcx, id, header.unsafety, header.abi, decl, None, None) } else { tcx.fn_sig(def_id) @@ -247,16 +246,10 @@ fn typeck_with_fallback<'tcx>( // Compute the function signature from point of view of inside the fn. let fn_sig = tcx.liberate_late_bound_regions(def_id.to_def_id(), fn_sig); - // FIXME(compiler-errors): Remove - let fn_sig = inh - .register_infer_ok_obligations( - inh.at(&ObligationCause::misc(body.value.span, body_id.hir_id), - param_env, - ) - .normalize(fn_sig)); - check_fn(&inh, param_env, fn_sig, decl, def_id, body, None).0 + let fn_sig = fcx.normalize(body.value.span, fn_sig); + + check_fn(&mut fcx, fn_sig, decl, def_id, body, None); } else { - let fcx = FnCtxt::new(&inh, param_env, body.value.hir_id); let expected_type = body_ty .and_then(|ty| match ty.kind { hir::TyKind::Infer => Some(>::ast_ty_to_ty(&fcx, ty)), @@ -316,8 +309,6 @@ fn typeck_with_fallback<'tcx>( fcx.check_expr_coercable_to_type(&body.value, expected_type, None); fcx.write_ty(id, expected_type); - - fcx }; fcx.type_inference_fallback(); diff --git a/compiler/rustc_hir_typeck/src/method/mod.rs b/compiler/rustc_hir_typeck/src/method/mod.rs index 9c2de1763b080..ebbd5eb1e6478 100644 --- a/compiler/rustc_hir_typeck/src/method/mod.rs +++ b/compiler/rustc_hir_typeck/src/method/mod.rs @@ -23,8 +23,8 @@ use rustc_middle::ty::subst::{InternalSubsts, SubstsRef}; use rustc_middle::ty::{self, DefIdTree, GenericParamDefKind, Ty, TypeVisitable}; use rustc_span::symbol::Ident; use rustc_span::Span; -use rustc_trait_selection::traits; use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt; +use rustc_trait_selection::traits::{self, NormalizeExt}; use self::probe::{IsSuggestion, ProbeScope}; @@ -465,11 +465,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let fn_sig = fn_sig.subst(self.tcx, substs); let fn_sig = self.replace_bound_vars_with_fresh_vars(span, infer::FnCall, fn_sig); - let InferOk { value, obligations: o } = if is_op { - self.normalize_op_associated_types_in_as_infer_ok(span, fn_sig, opt_input_expr) + let cause = if is_op { + ObligationCause::new( + span, + self.body_id, + traits::BinOp { + rhs_span: opt_input_expr.map(|expr| expr.span), + is_lit: opt_input_expr + .map_or(false, |expr| matches!(expr.kind, hir::ExprKind::Lit(_))), + output_ty: None, + }, + ) } else { - self.normalize_associated_types_in_as_infer_ok(span, fn_sig) + traits::ObligationCause::misc(span, self.body_id) }; + + let InferOk { value, obligations: o } = self.at(&cause, self.param_env).normalize(fn_sig); let fn_sig = { obligations.extend(o); value @@ -485,11 +496,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // any late-bound regions appearing in its bounds. let bounds = self.tcx.predicates_of(def_id).instantiate(self.tcx, substs); - let InferOk { value, obligations: o } = if is_op { - self.normalize_op_associated_types_in_as_infer_ok(span, bounds, opt_input_expr) - } else { - self.normalize_associated_types_in_as_infer_ok(span, bounds) - }; + let InferOk { value, obligations: o } = self.at(&cause, self.param_env).normalize(bounds); let bounds = { obligations.extend(o); value @@ -497,20 +504,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { assert!(!bounds.has_escaping_bound_vars()); - let cause = if is_op { - ObligationCause::new( - span, - self.body_id, - traits::BinOp { - rhs_span: opt_input_expr.map(|expr| expr.span), - is_lit: opt_input_expr - .map_or(false, |expr| matches!(expr.kind, hir::ExprKind::Lit(_))), - output_ty: None, - }, - ) - } else { - traits::ObligationCause::misc(span, self.body_id) - }; let predicates_cause = cause.clone(); obligations.extend(traits::predicates_for_generics( move |_, _| predicates_cause.clone(), From 82c07f09e52379c8388abd8ca5ea6df246f686b1 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 25 Nov 2022 17:11:15 +0000 Subject: [PATCH 161/244] partially_normalize_... -> At::normalize --- clippy_utils/src/ty.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index f4459e3e6633a..82496f120e372 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -22,6 +22,7 @@ use rustc_span::symbol::Ident; use rustc_span::{sym, Span, Symbol, DUMMY_SP}; use rustc_target::abi::{Size, VariantIdx}; use rustc_trait_selection::infer::InferCtxtExt; +use rustc_trait_selection::traits::NormalizeExt; use rustc_trait_selection::traits::query::normalize::AtExt; use std::iter; From ce409b520045a72a806f88606bbef142989a233d Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 25 Nov 2022 18:12:51 +0000 Subject: [PATCH 162/244] Make normalize and normalize_to pub(crate) --- .../src/traits/error_reporting/mod.rs | 1 + compiler/rustc_trait_selection/src/traits/mod.rs | 3 ++- compiler/rustc_trait_selection/src/traits/project.rs | 10 +++++----- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index e8468567cba0f..f44d1d71fa66c 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -1595,6 +1595,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> { bound_predicate.rebind(data), ); let mut obligations = vec![]; + // FIXME(normalization): Change this to use `At::normalize` let normalized_ty = super::normalize_projection_type( &mut selcx, obligation.param_env, diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs index f038b294bd0fe..c6efbd6b684c0 100644 --- a/compiler/rustc_trait_selection/src/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/mod.rs @@ -56,7 +56,8 @@ pub use self::object_safety::astconv_object_safety_violations; pub use self::object_safety::is_vtable_safe_method; pub use self::object_safety::MethodViolationCode; pub use self::object_safety::ObjectSafetyViolation; -pub use self::project::{normalize, normalize_projection_type, normalize_to, NormalizeExt}; +pub use self::project::{NormalizeExt, normalize_projection_type}; +pub(crate) use self::project::{normalize, normalize_to}; pub use self::select::{EvaluationCache, SelectionCache, SelectionContext}; pub use self::select::{EvaluationResult, IntercrateAmbiguityCause, OverflowError}; pub use self::specialize::specialization_graph::FutureCompatOverlapError; diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 55db9277353e0..7483b160f333e 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -307,7 +307,7 @@ fn project_and_unify_type<'cx, 'tcx>( /// them with a fully resolved type where possible. The return value /// combines the normalized result and any additional obligations that /// were incurred as result. -pub fn normalize<'a, 'b, 'tcx, T>( +pub(crate) fn normalize<'a, 'b, 'tcx, T>( selcx: &'a mut SelectionContext<'b, 'tcx>, param_env: ty::ParamEnv<'tcx>, cause: ObligationCause<'tcx>, @@ -321,7 +321,7 @@ where Normalized { value, obligations } } -pub fn normalize_to<'a, 'b, 'tcx, T>( +pub(crate) fn normalize_to<'a, 'b, 'tcx, T>( selcx: &'a mut SelectionContext<'b, 'tcx>, param_env: ty::ParamEnv<'tcx>, cause: ObligationCause<'tcx>, @@ -335,7 +335,7 @@ where } /// As `normalize`, but with a custom depth. -pub fn normalize_with_depth<'a, 'b, 'tcx, T>( +pub(crate) fn normalize_with_depth<'a, 'b, 'tcx, T>( selcx: &'a mut SelectionContext<'b, 'tcx>, param_env: ty::ParamEnv<'tcx>, cause: ObligationCause<'tcx>, @@ -351,7 +351,7 @@ where } #[instrument(level = "info", skip(selcx, param_env, cause, obligations))] -pub fn normalize_with_depth_to<'a, 'b, 'tcx, T>( +pub(crate) fn normalize_with_depth_to<'a, 'b, 'tcx, T>( selcx: &'a mut SelectionContext<'b, 'tcx>, param_env: ty::ParamEnv<'tcx>, cause: ObligationCause<'tcx>, @@ -371,7 +371,7 @@ where } #[instrument(level = "info", skip(selcx, param_env, cause, obligations))] -pub fn try_normalize_with_depth_to<'a, 'b, 'tcx, T>( +pub(crate) fn try_normalize_with_depth_to<'a, 'b, 'tcx, T>( selcx: &'a mut SelectionContext<'b, 'tcx>, param_env: ty::ParamEnv<'tcx>, cause: ObligationCause<'tcx>, From a61e2a91f5071719f3ac4696ee3061c171a47798 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 25 Nov 2022 17:28:50 +0000 Subject: [PATCH 163/244] FnCtxt normalization stuff --- clippy_utils/src/ty.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index 82496f120e372..2ceda3511fe44 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -22,8 +22,7 @@ use rustc_span::symbol::Ident; use rustc_span::{sym, Span, Symbol, DUMMY_SP}; use rustc_target::abi::{Size, VariantIdx}; use rustc_trait_selection::infer::InferCtxtExt; -use rustc_trait_selection::traits::NormalizeExt; -use rustc_trait_selection::traits::query::normalize::AtExt; +use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt; use std::iter; use crate::{match_def_path, path_res, paths}; @@ -284,7 +283,7 @@ fn is_normalizable_helper<'tcx>( cache.insert(ty, false); let infcx = cx.tcx.infer_ctxt().build(); let cause = rustc_middle::traits::ObligationCause::dummy(); - let result = if infcx.at(&cause, param_env).normalize(ty).is_ok() { + let result = if infcx.at(&cause, param_env).query_normalize(ty).is_ok() { match ty.kind() { ty::Adt(def, substs) => def.variants().iter().all(|variant| { variant From 1e236acd05c045b186195aa0a0b44657ac039978 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 25 Nov 2022 18:40:52 +0000 Subject: [PATCH 164/244] Make ObligationCtxt::normalize take cause by borrow --- .../src/diagnostics/bound_region_errors.rs | 2 +- .../src/transform/check_consts/check.rs | 3 +-- .../src/util/compare_types.rs | 4 ++-- .../src/check/compare_method.rs | 20 +++++++++---------- .../rustc_hir_analysis/src/check/wfcheck.rs | 2 +- compiler/rustc_hir_analysis/src/lib.rs | 2 +- compiler/rustc_hir_typeck/src/inherited.rs | 2 +- .../src/traits/engine.rs | 5 ++--- .../src/traits/error_reporting/ambiguity.rs | 4 ++-- .../rustc_trait_selection/src/traits/mod.rs | 6 +++--- compiler/rustc_traits/src/type_op.rs | 6 +++--- 11 files changed, 27 insertions(+), 29 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs b/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs index 009787cc94cb8..e05566dc2c745 100644 --- a/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs @@ -295,7 +295,7 @@ where // the former fails to normalize the `nll/relate_tys/impl-fn-ignore-binder-via-bottom.rs` test. // Check after #85499 lands to see if its fixes have erased this difference. let (param_env, value) = key.into_parts(); - let _ = ocx.normalize(cause, param_env, value.value); + let _ = ocx.normalize(&cause, param_env, value.value); try_extract_error_from_fulfill_cx(&ocx, placeholder_region, error_region) } diff --git a/compiler/rustc_const_eval/src/transform/check_consts/check.rs b/compiler/rustc_const_eval/src/transform/check_consts/check.rs index d4cee305387dc..54213d55a2da7 100644 --- a/compiler/rustc_const_eval/src/transform/check_consts/check.rs +++ b/compiler/rustc_const_eval/src/transform/check_consts/check.rs @@ -761,8 +761,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> { hir_id, ObligationCauseCode::ItemObligation(callee), ); - let normalized_predicates = - ocx.normalize(cause.clone(), param_env, predicates); + let normalized_predicates = ocx.normalize(&cause, param_env, predicates); ocx.register_obligations(traits::predicates_for_generics( |_, _| cause.clone(), self.param_env, diff --git a/compiler/rustc_const_eval/src/util/compare_types.rs b/compiler/rustc_const_eval/src/util/compare_types.rs index a9cb191cc593f..be786569cde3f 100644 --- a/compiler/rustc_const_eval/src/util/compare_types.rs +++ b/compiler/rustc_const_eval/src/util/compare_types.rs @@ -46,8 +46,8 @@ pub fn is_subtype<'tcx>( let infcx = builder.build(); let ocx = ObligationCtxt::new(&infcx); let cause = ObligationCause::dummy(); - let src = ocx.normalize(cause.clone(), param_env, src); - let dest = ocx.normalize(cause.clone(), param_env, dest); + let src = ocx.normalize(&cause, param_env, src); + let dest = ocx.normalize(&cause, param_env, dest); match ocx.sub(&cause, param_env, src, dest) { Ok(()) => {} Err(_) => return false, diff --git a/compiler/rustc_hir_analysis/src/check/compare_method.rs b/compiler/rustc_hir_analysis/src/check/compare_method.rs index de386e2d13556..ba58672e7595a 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_method.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_method.rs @@ -221,7 +221,7 @@ fn compare_predicate_entailment<'tcx>( let impl_m_own_bounds = impl_m_predicates.instantiate_own(tcx, impl_to_placeholder_substs); for (predicate, span) in iter::zip(impl_m_own_bounds.predicates, impl_m_own_bounds.spans) { let normalize_cause = traits::ObligationCause::misc(span, impl_m_hir_id); - let predicate = ocx.normalize(normalize_cause, param_env, predicate); + let predicate = ocx.normalize(&normalize_cause, param_env, predicate); let cause = ObligationCause::new( span, @@ -260,7 +260,7 @@ fn compare_predicate_entailment<'tcx>( ); let norm_cause = ObligationCause::misc(impl_m_span, impl_m_hir_id); - let impl_sig = ocx.normalize(norm_cause.clone(), param_env, impl_sig); + let impl_sig = ocx.normalize(&norm_cause, param_env, impl_sig); let impl_fty = tcx.mk_fn_ptr(ty::Binder::dummy(impl_sig)); debug!("compare_impl_method: impl_fty={:?}", impl_fty); @@ -271,7 +271,7 @@ fn compare_predicate_entailment<'tcx>( // we have to do this before normalization, since the normalized ty may // not contain the input parameters. See issue #87748. wf_tys.extend(trait_sig.inputs_and_output.iter()); - let trait_sig = ocx.normalize(norm_cause, param_env, trait_sig); + let trait_sig = ocx.normalize(&norm_cause, param_env, trait_sig); // We also have to add the normalized trait signature // as we don't normalize during implied bounds computation. wf_tys.extend(trait_sig.inputs_and_output.iter()); @@ -366,7 +366,7 @@ pub fn collect_trait_impl_trait_tys<'tcx>( // Normalize the impl signature with fresh variables for lifetime inference. let norm_cause = ObligationCause::misc(return_span, impl_m_hir_id); let impl_sig = ocx.normalize( - norm_cause.clone(), + &norm_cause, param_env, infcx.replace_bound_vars_with_fresh_vars( return_span, @@ -387,7 +387,7 @@ pub fn collect_trait_impl_trait_tys<'tcx>( tcx.bound_fn_sig(trait_m.def_id).subst(tcx, trait_to_placeholder_substs), ) .fold_with(&mut collector); - let trait_sig = ocx.normalize(norm_cause.clone(), param_env, unnormalized_trait_sig); + let trait_sig = ocx.normalize(&norm_cause, param_env, unnormalized_trait_sig); let trait_return_ty = trait_sig.output(); let wf_tys = FxIndexSet::from_iter( @@ -592,7 +592,7 @@ impl<'tcx> TypeFolder<'tcx> for ImplTraitInTraitCollector<'_, 'tcx> { for (pred, pred_span) in self.tcx().bound_explicit_item_bounds(proj.item_def_id).subst_iter_copied(self.tcx(), proj.substs) { let pred = pred.fold_with(self); let pred = self.ocx.normalize( - ObligationCause::misc(self.span, self.body_id), + &ObligationCause::misc(self.span, self.body_id), self.param_env, pred, ); @@ -1403,11 +1403,11 @@ pub(crate) fn raw_compare_const_impl<'tcx>( ); // There is no "body" here, so just pass dummy id. - let impl_ty = ocx.normalize(cause.clone(), param_env, impl_ty); + let impl_ty = ocx.normalize(&cause, param_env, impl_ty); debug!("compare_const_impl: impl_ty={:?}", impl_ty); - let trait_ty = ocx.normalize(cause.clone(), param_env, trait_ty); + let trait_ty = ocx.normalize(&cause, param_env, trait_ty); debug!("compare_const_impl: trait_ty={:?}", trait_ty); @@ -1556,7 +1556,7 @@ fn compare_type_predicate_entailment<'tcx>( for (span, predicate) in std::iter::zip(impl_ty_own_bounds.spans, impl_ty_own_bounds.predicates) { let cause = ObligationCause::misc(span, impl_ty_hir_id); - let predicate = ocx.normalize(cause, param_env, predicate); + let predicate = ocx.normalize(&cause, param_env, predicate); let cause = ObligationCause::new( span, @@ -1778,7 +1778,7 @@ pub fn check_type_bounds<'tcx>( for mut obligation in util::elaborate_obligations(tcx, obligations) { let normalized_predicate = - ocx.normalize(normalize_cause.clone(), normalize_param_env, obligation.predicate); + ocx.normalize(&normalize_cause, normalize_param_env, obligation.predicate); debug!("compare_projection_bounds: normalized predicate = {:?}", normalized_predicate); obligation.predicate = normalized_predicate; diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index 894c0ba34155a..7daed74e9de83 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -60,7 +60,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> { T: TypeFoldable<'tcx>, { self.ocx.normalize( - ObligationCause::new(span, self.body_id, ObligationCauseCode::WellFormed(loc)), + &ObligationCause::new(span, self.body_id, ObligationCauseCode::WellFormed(loc)), self.param_env, value, ) diff --git a/compiler/rustc_hir_analysis/src/lib.rs b/compiler/rustc_hir_analysis/src/lib.rs index 664d3a3a1db84..2058832d5fdc1 100644 --- a/compiler/rustc_hir_analysis/src/lib.rs +++ b/compiler/rustc_hir_analysis/src/lib.rs @@ -332,7 +332,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) { ObligationCauseCode::MainFunctionType, ); let ocx = traits::ObligationCtxt::new(&infcx); - let norm_return_ty = ocx.normalize(cause.clone(), param_env, return_ty); + let norm_return_ty = ocx.normalize(&cause, param_env, return_ty); ocx.register_bound(cause, param_env, norm_return_ty, term_did); let errors = ocx.select_all_or_error(); if !errors.is_empty() { diff --git a/compiler/rustc_hir_typeck/src/inherited.rs b/compiler/rustc_hir_typeck/src/inherited.rs index e8a9e4980f613..869ad07c00d61 100644 --- a/compiler/rustc_hir_typeck/src/inherited.rs +++ b/compiler/rustc_hir_typeck/src/inherited.rs @@ -100,7 +100,7 @@ impl<'tcx> Inherited<'tcx> { infcx.probe(|_| { let ocx = ObligationCtxt::new_in_snapshot(infcx); let normalized_fn_sig = ocx.normalize( - ObligationCause::dummy(), + &ObligationCause::dummy(), // FIXME(compiler-errors): This is probably not the right param-env... infcx.tcx.param_env(def_id), fn_sig, diff --git a/compiler/rustc_trait_selection/src/traits/engine.rs b/compiler/rustc_trait_selection/src/traits/engine.rs index 86c877347c9e6..64d01ddb09a49 100644 --- a/compiler/rustc_trait_selection/src/traits/engine.rs +++ b/compiler/rustc_trait_selection/src/traits/engine.rs @@ -104,8 +104,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> { pub fn normalize>( &self, - // FIXME(compiler-errors): Make this borrow - cause: ObligationCause<'tcx>, + cause: &ObligationCause<'tcx>, param_env: ty::ParamEnv<'tcx>, value: T, ) -> T { @@ -186,7 +185,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> { // sound and then uncomment this line again. // implied_bounds.insert(ty); - let normalized = self.normalize(cause.clone(), param_env, ty); + let normalized = self.normalize(&cause, param_env, ty); implied_bounds.insert(normalized); } implied_bounds diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/ambiguity.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/ambiguity.rs index 6a5744f5f762b..752b53fbc3f9a 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/ambiguity.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/ambiguity.rs @@ -17,11 +17,11 @@ pub fn recompute_applicable_impls<'tcx>( let placeholder_obligation = infcx.replace_bound_vars_with_placeholders(obligation.predicate); let obligation_trait_ref = - ocx.normalize(dummy_cause.clone(), param_env, placeholder_obligation.trait_ref); + ocx.normalize(&dummy_cause, param_env, placeholder_obligation.trait_ref); let impl_substs = infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id); let impl_trait_ref = tcx.bound_impl_trait_ref(impl_def_id).unwrap().subst(tcx, impl_substs); - let impl_trait_ref = ocx.normalize(ObligationCause::dummy(), param_env, impl_trait_ref); + let impl_trait_ref = ocx.normalize(&ObligationCause::dummy(), param_env, impl_trait_ref); if let Err(_) = ocx.eq(&dummy_cause, param_env, obligation_trait_ref, impl_trait_ref) { return false; diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs index c6efbd6b684c0..371367f0deb3e 100644 --- a/compiler/rustc_trait_selection/src/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/mod.rs @@ -56,8 +56,8 @@ pub use self::object_safety::astconv_object_safety_violations; pub use self::object_safety::is_vtable_safe_method; pub use self::object_safety::MethodViolationCode; pub use self::object_safety::ObjectSafetyViolation; -pub use self::project::{NormalizeExt, normalize_projection_type}; pub(crate) use self::project::{normalize, normalize_to}; +pub use self::project::{normalize_projection_type, NormalizeExt}; pub use self::select::{EvaluationCache, SelectionCache, SelectionContext}; pub use self::select::{EvaluationResult, IntercrateAmbiguityCause, OverflowError}; pub use self::specialize::specialization_graph::FutureCompatOverlapError; @@ -387,7 +387,7 @@ where { let ocx = ObligationCtxt::new(infcx); debug!(?value); - let normalized_value = ocx.normalize(cause, param_env, value); + let normalized_value = ocx.normalize(&cause, param_env, value); debug!(?normalized_value); debug!("select_all_or_error start"); let errors = ocx.select_all_or_error(); @@ -454,7 +454,7 @@ pub fn impossible_predicates<'tcx>( let infcx = tcx.infer_ctxt().build(); let param_env = ty::ParamEnv::reveal_all(); let ocx = ObligationCtxt::new(&infcx); - let predicates = ocx.normalize(ObligationCause::dummy(), param_env, predicates); + let predicates = ocx.normalize(&ObligationCause::dummy(), param_env, predicates); for predicate in predicates { let obligation = Obligation::new(tcx, ObligationCause::dummy(), param_env, predicate); ocx.register_obligation(obligation); diff --git a/compiler/rustc_traits/src/type_op.rs b/compiler/rustc_traits/src/type_op.rs index 98141506298d4..7f964afde80fd 100644 --- a/compiler/rustc_traits/src/type_op.rs +++ b/compiler/rustc_traits/src/type_op.rs @@ -62,7 +62,7 @@ pub fn type_op_ascribe_user_type_with_span<'tcx>( let cause = ObligationCause::dummy_with_span(span); let ty = tcx.bound_type_of(def_id).subst(tcx, substs); - let ty = ocx.normalize(cause.clone(), param_env, ty); + let ty = ocx.normalize(&cause, param_env, ty); debug!("relate_type_and_user_type: ty of def-id is {:?}", ty); ocx.eq(&cause, param_env, mir_ty, ty)?; @@ -85,14 +85,14 @@ pub fn type_op_ascribe_user_type_with_span<'tcx>( ObligationCauseCode::AscribeUserTypeProvePredicate(predicate_span), ); let instantiated_predicate = - ocx.normalize(cause.clone(), param_env, instantiated_predicate); + ocx.normalize(&cause.clone(), param_env, instantiated_predicate); ocx.register_obligation(Obligation::new(tcx, cause, param_env, instantiated_predicate)); } if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty { let impl_self_ty = tcx.bound_type_of(impl_def_id).subst(tcx, substs); - let impl_self_ty = ocx.normalize(cause.clone(), param_env, impl_self_ty); + let impl_self_ty = ocx.normalize(&cause, param_env, impl_self_ty); ocx.eq(&cause, param_env, self_ty, impl_self_ty)?; From 19e020029670ca422978c98209d1ed712a041c45 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Mon, 28 Nov 2022 10:21:19 -0700 Subject: [PATCH 165/244] rustdoc: remove `fnname` CSS class that's styled exactly like `fn` It's not obvious why this was ever a separate class name, since even in c4219a478354b464079b1b7ab081e071e8e39765 when it was first added, it was styled identically to regular `fn` links. --- src/librustdoc/html/render/mod.rs | 4 ++-- src/librustdoc/html/static/css/rustdoc.css | 1 - src/test/rustdoc-gui/item-decl-colors.goml | 2 +- src/test/rustdoc-gui/notable-trait.goml | 4 ++-- src/test/rustdoc-gui/where-whitespace.goml | 4 ++-- src/test/rustdoc/anchors.no_method_anchor.html | 2 +- .../rustdoc/anchors.no_trait_method_anchor.html | 2 +- src/test/rustdoc/anchors.no_tymethod_anchor.html | 2 +- .../decl-trailing-whitespace.declaration.html | 10 +++++----- .../extern-default-method.no_href_on_anchor.html | 2 +- src/test/rustdoc/extern-default-method.rs | 6 +++--- src/test/rustdoc/foreigntype.rs | 2 +- .../issue-24183.method_no_where_self_sized.html | 2 +- .../rustdoc/trait-impl-items-links-and-anchors.rs | 14 +++++++------- .../rustdoc/where.SWhere_TraitWhere_item-decl.html | 4 ++-- .../whitespace-after-where-clause.trait.html | 4 ++-- .../whitespace-after-where-clause.trait2.html | 4 ++-- 17 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index ef28e2a855a04..08f8096b07bd6 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -847,10 +847,10 @@ fn assoc_method( render_attributes_in_code(w, meth); (0, "", Ending::Newline) }; - w.reserve(header_len + "{".len() + "".len()); + w.reserve(header_len + "{".len() + "".len()); write!( w, - "{indent}{vis}{constness}{asyncness}{unsafety}{defaultness}{abi}fn {name}\ + "{indent}{vis}{constness}{asyncness}{unsafety}{defaultness}{abi}fn {name}\ {generics}{decl}{notable_traits}{where_clause}", indent = indent_str, vis = vis, diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 98a5b761dedf5..33e9322586830 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -242,7 +242,6 @@ h1 a, } .content span.fn, .content a.fn, -.content .fnname, .content span.method, .content a.method, .content span.tymethod, .content a.tymethod { color: var(--function-link-color); diff --git a/src/test/rustdoc-gui/item-decl-colors.goml b/src/test/rustdoc-gui/item-decl-colors.goml index 9a46f256056f9..2e07f19b13d64 100644 --- a/src/test/rustdoc-gui/item-decl-colors.goml +++ b/src/test/rustdoc-gui/item-decl-colors.goml @@ -30,7 +30,7 @@ define-function: ( ("assert-css", (".item-decl .primitive", {"color": |primitive_color|}, ALL)), ("goto", "file://" + |DOC_PATH| + "/test_docs/trait.TraitWithoutGenerics.html"), ("assert-css", (".item-decl .constant", {"color": |constant_color|}, ALL)), - ("assert-css", (".item-decl .fnname", {"color": |fn_color|}, ALL)), + ("assert-css", (".item-decl .fn", {"color": |fn_color|}, ALL)), ("assert-css", (".item-decl .associatedtype", {"color": |assoc_type_color|}, ALL)), ], ) diff --git a/src/test/rustdoc-gui/notable-trait.goml b/src/test/rustdoc-gui/notable-trait.goml index 7e24af47ee817..7d4bd27d42d4d 100644 --- a/src/test/rustdoc-gui/notable-trait.goml +++ b/src/test/rustdoc-gui/notable-trait.goml @@ -226,7 +226,7 @@ assert: "#method\.create_an_iterator_from_read .notable-traits:focus" // Now we check that the focus isn't given back to the wrong item when opening // another popover. store-window-property: (scroll, "scrollY") -click: "#method\.create_an_iterator_from_read .fnname" +click: "#method\.create_an_iterator_from_read .fn" // We ensure that the scroll position changed. assert-window-property-false: {"scrollY": |scroll|} // Store the new position. @@ -240,7 +240,7 @@ assert-window-property-false: {"scrollY": |scroll|} // Same but with Escape handling. store-window-property: (scroll, "scrollY") -click: "#method\.create_an_iterator_from_read .fnname" +click: "#method\.create_an_iterator_from_read .fn" // We ensure that the scroll position changed. assert-window-property-false: {"scrollY": |scroll|} // Store the new position. diff --git a/src/test/rustdoc-gui/where-whitespace.goml b/src/test/rustdoc-gui/where-whitespace.goml index 776c8ec721e21..41596a9bcccf2 100644 --- a/src/test/rustdoc-gui/where-whitespace.goml +++ b/src/test/rustdoc-gui/where-whitespace.goml @@ -5,13 +5,13 @@ show-text: true // line than "pub trait Whitespace"). compare-elements-position-false: (".item-decl code", ".where.fmt-newline", ("y")) // And that the code following it isn't on the same line either. -compare-elements-position-false: (".item-decl .fnname", ".where.fmt-newline", ("y")) +compare-elements-position-false: (".item-decl .fn", ".where.fmt-newline", ("y")) goto: "file://" + |DOC_PATH| + "/lib2/struct.WhereWhitespace.html" // We make the screen a bit wider to ensure that the trait impl is on one line. size: (915, 915) -compare-elements-position-false: ("#method\.new .fnname", "#method\.new .where.fmt-newline", ("y")) +compare-elements-position-false: ("#method\.new .fn", "#method\.new .where.fmt-newline", ("y")) // We ensure that both the trait name and the struct name are on the same line in // "impl Whitespace<&K> for WhereWhitespace". compare-elements-position: ( diff --git a/src/test/rustdoc/anchors.no_method_anchor.html b/src/test/rustdoc/anchors.no_method_anchor.html index 521fdcb7877a7..b9ec8bf4c09a0 100644 --- a/src/test/rustdoc/anchors.no_method_anchor.html +++ b/src/test/rustdoc/anchors.no_method_anchor.html @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/src/test/rustdoc/anchors.no_trait_method_anchor.html b/src/test/rustdoc/anchors.no_trait_method_anchor.html index d7bd525ff0f53..4308ddad41206 100644 --- a/src/test/rustdoc/anchors.no_trait_method_anchor.html +++ b/src/test/rustdoc/anchors.no_trait_method_anchor.html @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/src/test/rustdoc/anchors.no_tymethod_anchor.html b/src/test/rustdoc/anchors.no_tymethod_anchor.html index e668e5e4db15c..91eed8a374292 100644 --- a/src/test/rustdoc/anchors.no_tymethod_anchor.html +++ b/src/test/rustdoc/anchors.no_tymethod_anchor.html @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/src/test/rustdoc/decl-trailing-whitespace.declaration.html b/src/test/rustdoc/decl-trailing-whitespace.declaration.html index e60caaeff383c..02b51b3446195 100644 --- a/src/test/rustdoc/decl-trailing-whitespace.declaration.html +++ b/src/test/rustdoc/decl-trailing-whitespace.declaration.html @@ -1,7 +1,7 @@ pub trait Write { - fn poll_write(
        self: Option<String>,
        cx: &mut Option<String>,
        buf: &mut [usize]
    ) -> Option<Result<usize, Error>>; - fn poll_flush(
        self: Option<String>,
        cx: &mut Option<String>
    ) -> Option<Result<(), Error>>; - fn poll_close(
        self: Option<String>,
        cx: &mut Option<String>
    ) -> Option<Result<(), Error>>; + fn poll_write(
        self: Option<String>,
        cx: &mut Option<String>,
        buf: &mut [usize]
    ) -> Option<Result<usize, Error>>; + fn poll_flush(
        self: Option<String>,
        cx: &mut Option<String>
    ) -> Option<Result<(), Error>>; + fn poll_close(
        self: Option<String>,
        cx: &mut Option<String>
    ) -> Option<Result<(), Error>>; - fn poll_write_vectored(
        self: Option<String>,
        cx: &mut Option<String>,
        bufs: &[usize]
    ) -> Option<Result<usize, Error>> { ... } -}
+ fn poll_write_vectored(
        self: Option<String>,
        cx: &mut Option<String>,
        bufs: &[usize]
    ) -> Option<Result<usize, Error>> { ... } +} \ No newline at end of file diff --git a/src/test/rustdoc/extern-default-method.no_href_on_anchor.html b/src/test/rustdoc/extern-default-method.no_href_on_anchor.html index dab0a64952955..ef14836ccb8c6 100644 --- a/src/test/rustdoc/extern-default-method.no_href_on_anchor.html +++ b/src/test/rustdoc/extern-default-method.no_href_on_anchor.html @@ -1 +1 @@ -provided(&self) \ No newline at end of file +provided(&self) \ No newline at end of file diff --git a/src/test/rustdoc/extern-default-method.rs b/src/test/rustdoc/extern-default-method.rs index 8139f5b2619b3..fc28b230a5f7e 100644 --- a/src/test/rustdoc/extern-default-method.rs +++ b/src/test/rustdoc/extern-default-method.rs @@ -11,13 +11,13 @@ extern crate rustdoc_extern_default_method as ext; // However, the method in the trait impl should *not* have a link (an `href` attribute) to // its corresponding item in the trait declaration since it would otherwise be broken. // -// In older versions of rustdoc, the impl item (`a[@class="fnname"]`) used to link to +// In older versions of rustdoc, the impl item (`a[@class="fn"]`) used to link to // `#method.provided` – i.e. "to itself". Put in quotes since that was actually incorrect in // general: If the type `Struct` also had an inherent method called `provided`, the impl item // would link to that one even though those two methods are distinct items! // @count extern_default_method/struct.Struct.html '//*[@id="method.provided"]' 1 -// @count extern_default_method/struct.Struct.html '//*[@id="method.provided"]//a[@class="fnname"]' 1 -// @snapshot no_href_on_anchor - '//*[@id="method.provided"]//a[@class="fnname"]' +// @count extern_default_method/struct.Struct.html '//*[@id="method.provided"]//a[@class="fn"]' 1 +// @snapshot no_href_on_anchor - '//*[@id="method.provided"]//a[@class="fn"]' // @has extern_default_method/struct.Struct.html '//*[@id="method.provided"]//a[@class="anchor"]/@href' #method.provided pub use ext::Struct; diff --git a/src/test/rustdoc/foreigntype.rs b/src/test/rustdoc/foreigntype.rs index 891cdd5fed76c..29f9c2926e9ec 100644 --- a/src/test/rustdoc/foreigntype.rs +++ b/src/test/rustdoc/foreigntype.rs @@ -6,7 +6,7 @@ extern "C" { } impl ExtType { - // @has - '//a[@class="fnname"]' 'do_something' + // @has - '//a[@class="fn"]' 'do_something' pub fn do_something(&self) {} } diff --git a/src/test/rustdoc/inline_cross/issue-24183.method_no_where_self_sized.html b/src/test/rustdoc/inline_cross/issue-24183.method_no_where_self_sized.html index 6955a961499ba..f3c1c045202b0 100644 --- a/src/test/rustdoc/inline_cross/issue-24183.method_no_where_self_sized.html +++ b/src/test/rustdoc/inline_cross/issue-24183.method_no_where_self_sized.html @@ -1 +1 @@ -

fn touch(&self)

\ No newline at end of file +

fn touch(&self)

\ No newline at end of file diff --git a/src/test/rustdoc/trait-impl-items-links-and-anchors.rs b/src/test/rustdoc/trait-impl-items-links-and-anchors.rs index fba594c38273c..a125fa036790b 100644 --- a/src/test/rustdoc/trait-impl-items-links-and-anchors.rs +++ b/src/test/rustdoc/trait-impl-items-links-and-anchors.rs @@ -13,10 +13,10 @@ impl MyTrait for String { // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//*[@id="associatedconstant.VALUE-1"]//a[@class="constant"]/@href' #associatedconstant.VALUE // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//*[@id="associatedconstant.VALUE-1"]//a[@class="anchor"]/@href' #associatedconstant.VALUE-1 const VALUE: u32 = 5; - // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//*[@id="method.trait_function"]//a[@class="fnname"]/@href' #tymethod.trait_function + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//*[@id="method.trait_function"]//a[@class="fn"]/@href' #tymethod.trait_function // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//*[@id="method.trait_function"]//a[@class="anchor"]/@href' #method.trait_function fn trait_function(&self) {} - // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//*[@id="method.defaulted_override-1"]//a[@class="fnname"]/@href' #method.defaulted_override + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//*[@id="method.defaulted_override-1"]//a[@class="fn"]/@href' #method.defaulted_override // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//*[@id="method.defaulted_override-1"]//a[@class="anchor"]/@href' #method.defaulted_override-1 fn defaulted_override(&self) {} } @@ -28,10 +28,10 @@ impl MyTrait for Vec { // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//*[@id="associatedconstant.VALUE-2"]//a[@class="constant"]/@href' #associatedconstant.VALUE // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//*[@id="associatedconstant.VALUE-2"]//a[@class="anchor"]/@href' #associatedconstant.VALUE-2 const VALUE: u32 = 5; - // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//*[@id="method.trait_function"]//a[@class="fnname"]/@href' #tymethod.trait_function + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//*[@id="method.trait_function"]//a[@class="fn"]/@href' #tymethod.trait_function // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//*[@id="method.trait_function-1"]//a[@class="anchor"]/@href' #method.trait_function-1 fn trait_function(&self) {} - // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//*[@id="method.defaulted_override-2"]//a[@class="fnname"]/@href' #method.defaulted_override + // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//*[@id="method.defaulted_override-2"]//a[@class="fn"]/@href' #method.defaulted_override // @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//*[@id="method.defaulted_override-2"]//a[@class="anchor"]/@href' #method.defaulted_override-2 fn defaulted_override(&self) {} } @@ -45,13 +45,13 @@ impl MyTrait for MyStruct { // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//*[@id="associatedconstant.VALUE"]//a[@class="constant"]/@href' trait.MyTrait.html#associatedconstant.VALUE // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//*[@id="associatedconstant.VALUE"]//a[@class="anchor"]/@href' #associatedconstant.VALUE const VALUE: u32 = 20; - // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//*[@id="method.trait_function"]//a[@class="fnname"]/@href' trait.MyTrait.html#tymethod.trait_function + // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//*[@id="method.trait_function"]//a[@class="fn"]/@href' trait.MyTrait.html#tymethod.trait_function // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//*[@id="method.trait_function"]//a[@class="anchor"]/@href' #method.trait_function fn trait_function(&self) {} - // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//*[@id="method.defaulted_override"]//a[@class="fnname"]/@href' trait.MyTrait.html#method.defaulted_override + // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//*[@id="method.defaulted_override"]//a[@class="fn"]/@href' trait.MyTrait.html#method.defaulted_override // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//*[@id="method.defaulted_override"]//a[@class="anchor"]/@href' #method.defaulted_override fn defaulted_override(&self) {} - // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//*[@id="method.defaulted"]//a[@class="fnname"]/@href' trait.MyTrait.html#method.defaulted + // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//*[@id="method.defaulted"]//a[@class="fn"]/@href' trait.MyTrait.html#method.defaulted // @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//*[@id="method.defaulted"]//a[@class="anchor"]/@href' #method.defaulted } diff --git a/src/test/rustdoc/where.SWhere_TraitWhere_item-decl.html b/src/test/rustdoc/where.SWhere_TraitWhere_item-decl.html index 24ab77703d107..d5d6c556d8001 100644 --- a/src/test/rustdoc/where.SWhere_TraitWhere_item-decl.html +++ b/src/test/rustdoc/where.SWhere_TraitWhere_item-decl.html @@ -1,8 +1,8 @@
pub trait TraitWhere {
     type Item<'a>
    where
        Self: 'a
; - fn func(self)
    where
        Self: Sized
, + fn func(self)
    where
        Self: Sized
, { ... } - fn lines(self) -> Lines<Self>
    where
        Self: Sized
, + fn lines(self) -> Lines<Self>
    where
        Self: Sized
, { ... } }
\ No newline at end of file diff --git a/src/test/rustdoc/whitespace-after-where-clause.trait.html b/src/test/rustdoc/whitespace-after-where-clause.trait.html index 16b5582370353..50cfe362328b6 100644 --- a/src/test/rustdoc/whitespace-after-where-clause.trait.html +++ b/src/test/rustdoc/whitespace-after-where-clause.trait.html @@ -1,6 +1,6 @@
pub trait ToOwned<T>where
    T: Clone,
{ type Owned; - fn to_owned(&self) -> Self::Owned; - fn whatever(&self) -> T; + fn to_owned(&self) -> Self::Owned; + fn whatever(&self) -> T; }
\ No newline at end of file diff --git a/src/test/rustdoc/whitespace-after-where-clause.trait2.html b/src/test/rustdoc/whitespace-after-where-clause.trait2.html index eeca6e1f50081..21eb89b75011b 100644 --- a/src/test/rustdoc/whitespace-after-where-clause.trait2.html +++ b/src/test/rustdoc/whitespace-after-where-clause.trait2.html @@ -1,6 +1,6 @@
pub trait ToOwned2<T: Clone> {
     type Owned;
 
-    fn to_owned(&self) -> Self::Owned;
-    fn whatever(&self) -> T;
+    fn to_owned(&self) -> Self::Owned;
+    fn whatever(&self) -> T;
 }
\ No newline at end of file From a44eb3c3662b39ca715c8d7bedbc45c18579779e Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 28 Nov 2022 18:04:29 +0000 Subject: [PATCH 166/244] Remove `Const::from_scalar_int` --- compiler/rustc_middle/src/ty/consts.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index eaeb08c7aed40..c2be08e497e5b 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -148,11 +148,6 @@ impl<'tcx> Const<'tcx> { } } - pub fn from_scalar_int(tcx: TyCtxt<'tcx>, i: ScalarInt, ty: Ty<'tcx>) -> Self { - let valtree = ty::ValTree::from_scalar_int(i); - tcx.mk_const(valtree, ty) - } - #[inline] /// Creates a constant with the given integer value and interns it. pub fn from_bits(tcx: TyCtxt<'tcx>, bits: u128, ty: ParamEnvAnd<'tcx, Ty<'tcx>>) -> Self { @@ -160,7 +155,10 @@ impl<'tcx> Const<'tcx> { .layout_of(ty) .unwrap_or_else(|e| panic!("could not compute layout for {:?}: {:?}", ty, e)) .size; - Self::from_scalar_int(tcx, ScalarInt::try_from_uint(bits, size).unwrap(), ty.value) + tcx.mk_const( + ty::ValTree::from_scalar_int(ScalarInt::try_from_uint(bits, size).unwrap()), + ty.value, + ) } #[inline] From 6bbf16660bf218b16d64d812b4c3d916cf827a88 Mon Sep 17 00:00:00 2001 From: xFrednet Date: Mon, 28 Nov 2022 20:23:09 +0100 Subject: [PATCH 167/244] Move `index_refutable_slice` to `pedantic` --- clippy_lints/src/index_refutable_slice.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/index_refutable_slice.rs b/clippy_lints/src/index_refutable_slice.rs index ee5f10da5b848..cf35b1f175c60 100644 --- a/clippy_lints/src/index_refutable_slice.rs +++ b/clippy_lints/src/index_refutable_slice.rs @@ -47,7 +47,7 @@ declare_clippy_lint! { /// ``` #[clippy::version = "1.59.0"] pub INDEX_REFUTABLE_SLICE, - nursery, + pedantic, "avoid indexing on slices which could be destructed" } From d958f3773ad04410b9fa316c2c7e242963e51ac5 Mon Sep 17 00:00:00 2001 From: Nixon Enraght-Moony Date: Mon, 28 Nov 2022 19:29:23 +0000 Subject: [PATCH 168/244] Rustdoc-Json: Add tests for linking to foreign variants. --- src/test/rustdoc-json/enums/auxiliary/color.rs | 5 +++++ .../rustdoc-json/enums/doc_link_to_foreign_variant.rs | 11 +++++++++++ src/test/rustdoc-json/enums/use_variant_foreign.rs | 9 +++++++++ 3 files changed, 25 insertions(+) create mode 100644 src/test/rustdoc-json/enums/auxiliary/color.rs create mode 100644 src/test/rustdoc-json/enums/doc_link_to_foreign_variant.rs create mode 100644 src/test/rustdoc-json/enums/use_variant_foreign.rs diff --git a/src/test/rustdoc-json/enums/auxiliary/color.rs b/src/test/rustdoc-json/enums/auxiliary/color.rs new file mode 100644 index 0000000000000..7188f79383259 --- /dev/null +++ b/src/test/rustdoc-json/enums/auxiliary/color.rs @@ -0,0 +1,5 @@ +pub enum Color { + Red, + Green, + Blue, +} diff --git a/src/test/rustdoc-json/enums/doc_link_to_foreign_variant.rs b/src/test/rustdoc-json/enums/doc_link_to_foreign_variant.rs new file mode 100644 index 0000000000000..470b195a29247 --- /dev/null +++ b/src/test/rustdoc-json/enums/doc_link_to_foreign_variant.rs @@ -0,0 +1,11 @@ +// aux-build: color.rs + +//! The purpose of this test it to have a link to [a foreign variant](Red). + +extern crate color; +use color::Color::Red; + +// @set red = "$.index[*][?(@.inner.is_crate == true)].links.Red" + +// @!has "$.index[*][?(@.name == 'Red')]" +// @!has "$.index[*][?(@.name == 'Color')]" diff --git a/src/test/rustdoc-json/enums/use_variant_foreign.rs b/src/test/rustdoc-json/enums/use_variant_foreign.rs new file mode 100644 index 0000000000000..11bb6ce1f3a0c --- /dev/null +++ b/src/test/rustdoc-json/enums/use_variant_foreign.rs @@ -0,0 +1,9 @@ +// aux-build: color.rs + +extern crate color; + +// @is "$.index[*][?(@.inner.name == 'Red')].kind" '"import"' +pub use color::Color::Red; + +// @!has "$.index[*][?(@.name == 'Red')]" +// @!has "$.index[*][?(@.name == 'Color')]" From 2db0dc32970a70bcf622e0854c944753a065cb15 Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Sat, 26 Nov 2022 21:33:12 +0100 Subject: [PATCH 169/244] Simplify checking for `GeneratorKind::Async` Adds a helper method around `generator_kind` that makes matching async constructs simpler. --- .../rustc_borrowck/src/diagnostics/region_errors.rs | 7 +------ compiler/rustc_lint/src/unused.rs | 5 +---- compiler/rustc_middle/src/ty/context.rs | 5 +++++ .../src/traits/error_reporting/suggestions.rs | 11 ++--------- .../src/traits/select/candidate_assembly.rs | 4 +--- 5 files changed, 10 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs index 534675f1dc042..7aa099433a76d 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs @@ -514,12 +514,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { span: *span, ty_err: match output_ty.kind() { ty::Closure(_, _) => FnMutReturnTypeErr::ReturnClosure { span: *span }, - ty::Generator(def, ..) - if matches!( - self.infcx.tcx.generator_kind(def), - Some(hir::GeneratorKind::Async(_)) - ) => - { + ty::Generator(def, ..) if self.infcx.tcx.generator_is_async(*def) => { FnMutReturnTypeErr::ReturnAsyncBlock { span: *span } } _ => FnMutReturnTypeErr::ReturnRef { span: *span }, diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index 43864ed45fae2..88ad4c67d93e4 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -322,10 +322,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { ty::Closure(..) => Some(MustUsePath::Closure(span)), ty::Generator(def_id, ..) => { // async fn should be treated as "implementor of `Future`" - let must_use = if matches!( - cx.tcx.generator_kind(def_id), - Some(hir::GeneratorKind::Async(..)) - ) { + let must_use = if cx.tcx.generator_is_async(def_id) { let def_id = cx.tcx.lang_items().future_trait().unwrap(); is_def_must_use(cx, def_id, span) .map(|inner| MustUsePath::Opaque(Box::new(inner))) diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index bf30a403d9b94..1628cca638ea5 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -1360,6 +1360,11 @@ impl<'tcx> TyCtxt<'tcx> { self.diagnostic_items(did.krate).name_to_id.get(&name) == Some(&did) } + /// Returns `true` if the node pointed to by `def_id` is a generator for an async construct. + pub fn generator_is_async(self, def_id: DefId) -> bool { + matches!(self.generator_kind(def_id), Some(hir::GeneratorKind::Async(_))) + } + pub fn stability(self) -> &'tcx stability::Index { self.stability_index(()) } diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 992ea1755163f..eeb4693eec3b6 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -1988,11 +1988,6 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { .as_local() .and_then(|def_id| hir.maybe_body_owned_by(def_id)) .map(|body_id| hir.body(body_id)); - let is_async = self - .tcx - .generator_kind(generator_did) - .map(|generator_kind| matches!(generator_kind, hir::GeneratorKind::Async(..))) - .unwrap_or(false); let mut visitor = AwaitsVisitor::default(); if let Some(body) = generator_body { visitor.visit_body(body); @@ -2069,6 +2064,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { debug!(?interior_or_upvar_span); if let Some(interior_or_upvar_span) = interior_or_upvar_span { + let is_async = self.tcx.generator_is_async(generator_did); let typeck_results = match generator_data { GeneratorData::Local(typeck_results) => Some(typeck_results), GeneratorData::Foreign(_) => None, @@ -2641,10 +2637,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { if is_future && obligated_types.last().map_or(false, |ty| match ty.kind() { ty::Generator(last_def_id, ..) => { - matches!( - tcx.generator_kind(last_def_id), - Some(GeneratorKind::Async(..)) - ) + tcx.generator_is_async(*last_def_id) } _ => false, }) diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs index 10854ede652b5..627ed4674b0e9 100644 --- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs +++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs @@ -430,9 +430,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { ) { let self_ty = obligation.self_ty().skip_binder(); if let ty::Generator(did, ..) = self_ty.kind() { - if let Some(rustc_hir::GeneratorKind::Async(_generator_kind)) = - self.tcx().generator_kind(did) - { + if self.tcx().generator_is_async(*did) { debug!(?self_ty, ?obligation, "assemble_future_candidates",); candidates.vec.push(FutureCandidate); From c9ae38c71e2838f1ac282803ddde47f21f4ca76e Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 24 Nov 2022 16:00:57 +1100 Subject: [PATCH 170/244] Avoid unnecessary `MetaItem`/`Attribute` conversions. `check_builtin_attribute` calls `parse_meta` to convert an `Attribute` to a `MetaItem`, which it then checks. However, many callers of `check_builtin_attribute` start with a `MetaItem`, and then convert it to an `Attribute` by calling `cx.attribute(meta_item)`. This `MetaItem` to `Attribute` to `MetaItem` conversion is silly. This commit adds a new function `check_builtin_meta_item`, which can be called instead from these call sites. `check_builtin_attribute` also now calls it. The commit also renames `check_meta` as `check_attr` to better match its arguments. --- .../rustc_ast_passes/src/ast_validation.rs | 2 +- .../src/cfg_accessible.rs | 6 +-- compiler/rustc_builtin_macros/src/derive.rs | 9 ++-- compiler/rustc_builtin_macros/src/util.rs | 11 +++-- compiler/rustc_expand/src/expand.rs | 2 +- compiler/rustc_parse/src/validate_attr.rs | 43 +++++++++++-------- 6 files changed, 44 insertions(+), 29 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index acd7eb69ffc37..29bf794877014 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -912,7 +912,7 @@ fn validate_generic_param_order( impl<'a> Visitor<'a> for AstValidator<'a> { fn visit_attribute(&mut self, attr: &Attribute) { - validate_attr::check_meta(&self.session.parse_sess, attr); + validate_attr::check_attr(&self.session.parse_sess, attr); } fn visit_expr(&mut self, expr: &'a Expr) { diff --git a/compiler/rustc_builtin_macros/src/cfg_accessible.rs b/compiler/rustc_builtin_macros/src/cfg_accessible.rs index 86df3c44eb334..4e4cafc71823a 100644 --- a/compiler/rustc_builtin_macros/src/cfg_accessible.rs +++ b/compiler/rustc_builtin_macros/src/cfg_accessible.rs @@ -37,10 +37,10 @@ impl MultiItemModifier for Expander { _is_derive_const: bool, ) -> ExpandResult, Annotatable> { let template = AttributeTemplate { list: Some("path"), ..Default::default() }; - let attr = &ecx.attribute(meta_item.clone()); - validate_attr::check_builtin_attribute( + validate_attr::check_builtin_meta_item( &ecx.sess.parse_sess, - attr, + &meta_item, + ast::AttrStyle::Outer, sym::cfg_accessible, template, ); diff --git a/compiler/rustc_builtin_macros/src/derive.rs b/compiler/rustc_builtin_macros/src/derive.rs index c8a2fca00e800..0358c0b1b5fab 100644 --- a/compiler/rustc_builtin_macros/src/derive.rs +++ b/compiler/rustc_builtin_macros/src/derive.rs @@ -33,14 +33,15 @@ impl MultiItemModifier for Expander { ecx.resolver.resolve_derives(ecx.current_expansion.id, ecx.force_mode, &|| { let template = AttributeTemplate { list: Some("Trait1, Trait2, ..."), ..Default::default() }; - let attr = - attr::mk_attr_outer(&sess.parse_sess.attr_id_generator, meta_item.clone()); - validate_attr::check_builtin_attribute( + validate_attr::check_builtin_meta_item( &sess.parse_sess, - &attr, + &meta_item, + ast::AttrStyle::Outer, sym::derive, template, ); + let attr = + attr::mk_attr_outer(&sess.parse_sess.attr_id_generator, meta_item.clone()); let mut resolutions: Vec<_> = attr .meta_item_list() diff --git a/compiler/rustc_builtin_macros/src/util.rs b/compiler/rustc_builtin_macros/src/util.rs index 527fe50eff0ce..83812631c2ff7 100644 --- a/compiler/rustc_builtin_macros/src/util.rs +++ b/compiler/rustc_builtin_macros/src/util.rs @@ -1,4 +1,4 @@ -use rustc_ast::{Attribute, MetaItem}; +use rustc_ast::{AttrStyle, Attribute, MetaItem}; use rustc_expand::base::{Annotatable, ExtCtxt}; use rustc_feature::AttributeTemplate; use rustc_lint_defs::builtin::DUPLICATE_MACRO_ATTRIBUTES; @@ -8,8 +8,13 @@ use rustc_span::Symbol; pub fn check_builtin_macro_attribute(ecx: &ExtCtxt<'_>, meta_item: &MetaItem, name: Symbol) { // All the built-in macro attributes are "words" at the moment. let template = AttributeTemplate { word: true, ..Default::default() }; - let attr = ecx.attribute(meta_item.clone()); - validate_attr::check_builtin_attribute(&ecx.sess.parse_sess, &attr, name, template); + validate_attr::check_builtin_meta_item( + &ecx.sess.parse_sess, + &meta_item, + AttrStyle::Outer, + name, + template, + ); } /// Emit a warning if the item is annotated with the given attribute. This is used to diagnose when diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs index 3e98b024c73b9..e799fa404f6fd 100644 --- a/compiler/rustc_expand/src/expand.rs +++ b/compiler/rustc_expand/src/expand.rs @@ -1644,7 +1644,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { let mut span: Option = None; while let Some(attr) = attrs.next() { rustc_ast_passes::feature_gate::check_attribute(attr, self.cx.sess, features); - validate_attr::check_meta(&self.cx.sess.parse_sess, attr); + validate_attr::check_attr(&self.cx.sess.parse_sess, attr); let current_span = if let Some(sp) = span { sp.to(attr.span) } else { attr.span }; span = Some(current_span); diff --git a/compiler/rustc_parse/src/validate_attr.rs b/compiler/rustc_parse/src/validate_attr.rs index 59e564114e5cf..72402a200907a 100644 --- a/compiler/rustc_parse/src/validate_attr.rs +++ b/compiler/rustc_parse/src/validate_attr.rs @@ -10,9 +10,9 @@ use rustc_errors::{Applicability, FatalError, PResult}; use rustc_feature::{AttributeTemplate, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP}; use rustc_session::lint::builtin::ILL_FORMED_ATTRIBUTE_INPUT; use rustc_session::parse::ParseSess; -use rustc_span::{sym, Symbol}; +use rustc_span::{sym, Span, Symbol}; -pub fn check_meta(sess: &ParseSess, attr: &Attribute) { +pub fn check_attr(sess: &ParseSess, attr: &Attribute) { if attr.is_doc_comment() { return; } @@ -115,25 +115,34 @@ pub fn check_builtin_attribute( name: Symbol, template: AttributeTemplate, ) { - // Some special attributes like `cfg` must be checked - // before the generic check, so we skip them here. - let should_skip = |name| name == sym::cfg; - match parse_meta(sess, attr) { - Ok(meta) => { - if !should_skip(name) && !is_attr_template_compatible(&template, &meta.kind) { - emit_malformed_attribute(sess, attr, name, template); - } - } + Ok(meta) => check_builtin_meta_item(sess, &meta, attr.style, name, template), Err(mut err) => { err.emit(); } } } +pub fn check_builtin_meta_item( + sess: &ParseSess, + meta: &MetaItem, + style: ast::AttrStyle, + name: Symbol, + template: AttributeTemplate, +) { + // Some special attributes like `cfg` must be checked + // before the generic check, so we skip them here. + let should_skip = |name| name == sym::cfg; + + if !should_skip(name) && !is_attr_template_compatible(&template, &meta.kind) { + emit_malformed_attribute(sess, style, meta.span, name, template); + } +} + fn emit_malformed_attribute( sess: &ParseSess, - attr: &Attribute, + style: ast::AttrStyle, + span: Span, name: Symbol, template: AttributeTemplate, ) { @@ -147,7 +156,7 @@ fn emit_malformed_attribute( let mut msg = "attribute must be of the form ".to_owned(); let mut suggestions = vec![]; let mut first = true; - let inner = if attr.style == ast::AttrStyle::Inner { "!" } else { "" }; + let inner = if style == ast::AttrStyle::Inner { "!" } else { "" }; if template.word { first = false; let code = format!("#{}[{}]", inner, name); @@ -172,12 +181,12 @@ fn emit_malformed_attribute( suggestions.push(code); } if should_warn(name) { - sess.buffer_lint(&ILL_FORMED_ATTRIBUTE_INPUT, attr.span, ast::CRATE_NODE_ID, &msg); + sess.buffer_lint(&ILL_FORMED_ATTRIBUTE_INPUT, span, ast::CRATE_NODE_ID, &msg); } else { sess.span_diagnostic - .struct_span_err(attr.span, &error_msg) + .struct_span_err(span, &error_msg) .span_suggestions( - attr.span, + span, if suggestions.len() == 1 { "must be of the form" } else { @@ -196,7 +205,7 @@ pub fn emit_fatal_malformed_builtin_attribute( name: Symbol, ) -> ! { let template = BUILTIN_ATTRIBUTE_MAP.get(&name).expect("builtin attr defined").template; - emit_malformed_attribute(sess, attr, name, template); + emit_malformed_attribute(sess, attr.style, attr.span, name, template); // This is fatal, otherwise it will likely cause a cascade of other errors // (and an error here is expected to be very rare). FatalError.raise() From 5a2fd1a47ffb72c060b77b717e8bfa85c7e0e5bd Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 25 Nov 2022 09:53:37 +1100 Subject: [PATCH 171/244] Improve comments about attributes and meta items. I have found the distinction confusing. --- compiler/rustc_ast/src/ast.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index d0bb05c36549b..9d23bb5c1596a 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -479,20 +479,24 @@ pub struct Crate { pub is_placeholder: bool, } -/// Possible values inside of compile-time attribute lists. +/// Values inside meta item lists. /// -/// E.g., the '..' in `#[name(..)]`. +/// E.g., each of `Clone`, `Copy` in `#[derive(Clone, Copy)]`. #[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)] pub enum NestedMetaItem { /// A full MetaItem, for recursive meta items. MetaItem(MetaItem), + /// A literal. /// /// E.g., `"foo"`, `64`, `true`. Lit(MetaItemLit), } -/// A spanned compile-time attribute item. +/// A semantic representation of a meta item. A meta item is a slightly +/// restricted form of an attribute -- it can only contain expressions in +/// certain leaf positions, rather than arbitrary token streams -- that is used +/// for most built-in attributes. /// /// E.g., `#[test]`, `#[derive(..)]`, `#[rustfmt::skip]` or `#[feature = "foo"]`. #[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)] @@ -502,22 +506,22 @@ pub struct MetaItem { pub span: Span, } -/// A compile-time attribute item. -/// -/// E.g., `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`. +/// The meta item kind, containing the data after the initial path. #[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)] pub enum MetaItemKind { /// Word meta item. /// - /// E.g., `test` as in `#[test]`. + /// E.g., `#[test]`, which lacks any arguments after `test`. Word, + /// List meta item. /// - /// E.g., `derive(..)` as in `#[derive(..)]`. + /// E.g., `#[derive(..)]`, where the field represents the `..`. List(Vec), + /// Name value meta item. /// - /// E.g., `feature = "foo"` as in `#[feature = "foo"]`. + /// E.g., `#[feature = "foo"]`, where the field represents the `"foo"`. NameValue(MetaItemLit), } @@ -2580,7 +2584,7 @@ pub struct AttrItem { /// A list of attributes. pub type AttrVec = ThinVec; -/// Metadata associated with an item. +/// A syntax-level representation of an attribute. #[derive(Clone, Encodable, Decodable, Debug)] pub struct Attribute { pub kind: AttrKind, From fc2a8a0c675bf7d111846b19fd3d5d7a13915425 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 25 Nov 2022 09:59:36 +1100 Subject: [PATCH 172/244] Reorder some types. So that `Attribute` and `MetaItem` are listed first, and then the component types are below them in a logical order. --- compiler/rustc_ast/src/ast.rs | 54 +++++++++++++++++------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 9d23bb5c1596a..6a2f1f0c5749c 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -479,20 +479,6 @@ pub struct Crate { pub is_placeholder: bool, } -/// Values inside meta item lists. -/// -/// E.g., each of `Clone`, `Copy` in `#[derive(Clone, Copy)]`. -#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)] -pub enum NestedMetaItem { - /// A full MetaItem, for recursive meta items. - MetaItem(MetaItem), - - /// A literal. - /// - /// E.g., `"foo"`, `64`, `true`. - Lit(MetaItemLit), -} - /// A semantic representation of a meta item. A meta item is a slightly /// restricted form of an attribute -- it can only contain expressions in /// certain leaf positions, rather than arbitrary token streams -- that is used @@ -525,6 +511,20 @@ pub enum MetaItemKind { NameValue(MetaItemLit), } +/// Values inside meta item lists. +/// +/// E.g., each of `Clone`, `Copy` in `#[derive(Clone, Copy)]`. +#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)] +pub enum NestedMetaItem { + /// A full MetaItem, for recursive meta items. + MetaItem(MetaItem), + + /// A literal. + /// + /// E.g., `"foo"`, `64`, `true`. + Lit(MetaItemLit), +} + /// A block (`{ .. }`). /// /// E.g., `{ .. }` as in `fn foo() { .. }`. @@ -2574,13 +2574,6 @@ impl Decodable for AttrId { } } -#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)] -pub struct AttrItem { - pub path: Path, - pub args: AttrArgs, - pub tokens: Option, -} - /// A list of attributes. pub type AttrVec = ThinVec; @@ -2595,12 +2588,6 @@ pub struct Attribute { pub span: Span, } -#[derive(Clone, Encodable, Decodable, Debug)] -pub struct NormalAttr { - pub item: AttrItem, - pub tokens: Option, -} - #[derive(Clone, Encodable, Decodable, Debug)] pub enum AttrKind { /// A normal attribute. @@ -2612,6 +2599,19 @@ pub enum AttrKind { DocComment(CommentKind, Symbol), } +#[derive(Clone, Encodable, Decodable, Debug)] +pub struct NormalAttr { + pub item: AttrItem, + pub tokens: Option, +} + +#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)] +pub struct AttrItem { + pub path: Path, + pub args: AttrArgs, + pub tokens: Option, +} + /// `TraitRef`s appear in impls. /// /// Resolution maps each `TraitRef`'s `ref_id` to its defining trait; that's all From a709f87be7d66e8a91c5ee2818cac6f077caac08 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 28 Nov 2022 11:17:45 +1100 Subject: [PATCH 173/244] Avoid more unnecessary `MetaItem`/`Attribute` conversions. In `Expander::expand` the code currently uses `mk_attr_outer` to convert a `MetaItem` to an `Attribute`, and then follows that with `meta_item_list` which converts back. This commit avoids the unnecessary conversions. There was one wrinkle: the existing conversions caused the bogus `<>` on `Default<>` to be removed. With the conversion gone, we get a second error message about the `<>`. This is a rare case, so I think it probably doesn't matter much. --- compiler/rustc_builtin_macros/src/derive.rs | 45 +++++++++++---------- src/test/ui/span/macro-ty-params.rs | 4 +- src/test/ui/span/macro-ty-params.stderr | 8 +++- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/derive.rs b/compiler/rustc_builtin_macros/src/derive.rs index 0358c0b1b5fab..fa5a45730ac7a 100644 --- a/compiler/rustc_builtin_macros/src/derive.rs +++ b/compiler/rustc_builtin_macros/src/derive.rs @@ -1,7 +1,7 @@ use crate::cfg_eval::cfg_eval; use rustc_ast as ast; -use rustc_ast::{attr, token, GenericParamKind, ItemKind, MetaItemKind, NestedMetaItem, StmtKind}; +use rustc_ast::{token, GenericParamKind, ItemKind, MetaItemKind, NestedMetaItem, StmtKind}; use rustc_errors::{struct_span_err, Applicability}; use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier}; use rustc_feature::AttributeTemplate; @@ -40,28 +40,29 @@ impl MultiItemModifier for Expander { sym::derive, template, ); - let attr = - attr::mk_attr_outer(&sess.parse_sess.attr_id_generator, meta_item.clone()); - let mut resolutions: Vec<_> = attr - .meta_item_list() - .unwrap_or_default() - .into_iter() - .filter_map(|nested_meta| match nested_meta { - NestedMetaItem::MetaItem(meta) => Some(meta), - NestedMetaItem::Lit(lit) => { - // Reject `#[derive("Debug")]`. - report_unexpected_meta_item_lit(sess, &lit); - None - } - }) - .map(|meta| { - // Reject `#[derive(Debug = "value", Debug(abc))]`, but recover the paths. - report_path_args(sess, &meta); - meta.path - }) - .map(|path| (path, dummy_annotatable(), None, self.0)) - .collect(); + let mut resolutions = match &meta_item.kind { + MetaItemKind::List(list) => { + list.iter() + .filter_map(|nested_meta| match nested_meta { + NestedMetaItem::MetaItem(meta) => Some(meta), + NestedMetaItem::Lit(lit) => { + // Reject `#[derive("Debug")]`. + report_unexpected_meta_item_lit(sess, &lit); + None + } + }) + .map(|meta| { + // Reject `#[derive(Debug = "value", Debug(abc))]`, but recover the + // paths. + report_path_args(sess, &meta); + meta.path.clone() + }) + .map(|path| (path, dummy_annotatable(), None, self.0)) + .collect() + } + _ => vec![], + }; // Do not configure or clone items unless necessary. match &mut resolutions[..] { diff --git a/src/test/ui/span/macro-ty-params.rs b/src/test/ui/span/macro-ty-params.rs index 0a93105b66477..cf28b0255d17d 100644 --- a/src/test/ui/span/macro-ty-params.rs +++ b/src/test/ui/span/macro-ty-params.rs @@ -9,5 +9,7 @@ macro_rules! foo { () => () } fn main() { foo::!(); //~ ERROR generic arguments in macro path foo::<>!(); //~ ERROR generic arguments in macro path - m!(Default<>); //~ ERROR unexpected generic arguments in path + m!(Default<>); + //~^ ERROR unexpected generic arguments in path + //~^^ ERROR generic arguments in macro path } diff --git a/src/test/ui/span/macro-ty-params.stderr b/src/test/ui/span/macro-ty-params.stderr index 138cd2598a1c5..7023ef8cd1c33 100644 --- a/src/test/ui/span/macro-ty-params.stderr +++ b/src/test/ui/span/macro-ty-params.stderr @@ -16,5 +16,11 @@ error: unexpected generic arguments in path LL | m!(Default<>); | ^^ -error: aborting due to 3 previous errors +error: generic arguments in macro path + --> $DIR/macro-ty-params.rs:12:15 + | +LL | m!(Default<>); + | ^^ + +error: aborting due to 4 previous errors From c95dceb63a5fdce2df3fc6c5ffe33d94223bb43d Mon Sep 17 00:00:00 2001 From: Rageking8 Date: Tue, 29 Nov 2022 08:56:43 +0800 Subject: [PATCH 174/244] clean up pr 104954 --- compiler/rustc_resolve/src/late/diagnostics.rs | 2 +- ...ggest_print_over_printf.rs => suggest_print_over_printf.rs} | 3 +-- ...int_over_printf.stderr => suggest_print_over_printf.stderr} | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) rename src/test/ui/suggestions/{seggest_print_over_printf.rs => suggest_print_over_printf.rs} (69%) rename src/test/ui/suggestions/{seggest_print_over_printf.stderr => suggest_print_over_printf.stderr} (88%) diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 5b9513ebc0da4..df59a350ea7c9 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -282,7 +282,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { "you may want to use a bool value instead", format!("{}", item_typo), )) - // FIXME(vicnenzopalazzo): make the check smarter, + // FIXME(vincenzopalazzo): make the check smarter, // and maybe expand with levenshtein distance checks } else if item_str.as_str() == "printf" { Some(( diff --git a/src/test/ui/suggestions/seggest_print_over_printf.rs b/src/test/ui/suggestions/suggest_print_over_printf.rs similarity index 69% rename from src/test/ui/suggestions/seggest_print_over_printf.rs rename to src/test/ui/suggestions/suggest_print_over_printf.rs index 25566cd7f2aeb..124ddec50cbbe 100644 --- a/src/test/ui/suggestions/seggest_print_over_printf.rs +++ b/src/test/ui/suggestions/suggest_print_over_printf.rs @@ -1,5 +1,4 @@ -// Suggest to a user to use the print macros -// instead to use the printf. +// Suggest print macro when user erroneously uses printf fn main() { let x = 4; diff --git a/src/test/ui/suggestions/seggest_print_over_printf.stderr b/src/test/ui/suggestions/suggest_print_over_printf.stderr similarity index 88% rename from src/test/ui/suggestions/seggest_print_over_printf.stderr rename to src/test/ui/suggestions/suggest_print_over_printf.stderr index 7b1ce047a9274..1214bec16ce03 100644 --- a/src/test/ui/suggestions/seggest_print_over_printf.stderr +++ b/src/test/ui/suggestions/suggest_print_over_printf.stderr @@ -1,5 +1,5 @@ error[E0425]: cannot find function `printf` in this scope - --> $DIR/seggest_print_over_printf.rs:6:5 + --> $DIR/suggest_print_over_printf.rs:5:5 | LL | printf("%d", x); | ^^^^^^ not found in this scope From 3980945ab1cd0815ba6bcd19143943b3a39c0593 Mon Sep 17 00:00:00 2001 From: yukang Date: Tue, 29 Nov 2022 12:38:39 +0800 Subject: [PATCH 175/244] fix #104884, Avoid Invalid code suggested when encountering unsatisfied trait bounds in derive macro code --- compiler/rustc_middle/src/ty/diagnostics.rs | 6 +++ .../ui/proc-macro/auxiliary/issue-104884.rs | 23 +++++++++ .../issue-104884-trait-impl-sugg-err.rs | 20 ++++++++ .../issue-104884-trait-impl-sugg-err.stderr | 48 +++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 src/test/ui/proc-macro/auxiliary/issue-104884.rs create mode 100644 src/test/ui/proc-macro/issue-104884-trait-impl-sugg-err.rs create mode 100644 src/test/ui/proc-macro/issue-104884-trait-impl-sugg-err.stderr diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs index 69f50df62350f..160ee73ec65ee 100644 --- a/compiler/rustc_middle/src/ty/diagnostics.rs +++ b/compiler/rustc_middle/src/ty/diagnostics.rs @@ -355,6 +355,12 @@ pub fn suggest_constraining_type_params<'a>( )); } + // FIXME: remove the suggestions that are from derive, as the span is not correct + suggestions = suggestions + .into_iter() + .filter(|(span, _, _)| !span.in_derive_expansion()) + .collect::>(); + if suggestions.len() == 1 { let (span, suggestion, msg) = suggestions.pop().unwrap(); diff --git a/src/test/ui/proc-macro/auxiliary/issue-104884.rs b/src/test/ui/proc-macro/auxiliary/issue-104884.rs new file mode 100644 index 0000000000000..0de59d005731d --- /dev/null +++ b/src/test/ui/proc-macro/auxiliary/issue-104884.rs @@ -0,0 +1,23 @@ +// force-host +// no-prefer-dynamic + +#![crate_type = "proc-macro"] + +extern crate proc_macro; + +use proc_macro::TokenStream; + +#[proc_macro_derive(AddImpl)] + +pub fn derive(input: TokenStream) -> TokenStream { + "use std::cmp::Ordering; + + impl Ord for PriorityQueue { + fn cmp(&self, other: &Self) -> Ordering { + self.0.cmp(&self.height) + } + } + " + .parse() + .unwrap() +} diff --git a/src/test/ui/proc-macro/issue-104884-trait-impl-sugg-err.rs b/src/test/ui/proc-macro/issue-104884-trait-impl-sugg-err.rs new file mode 100644 index 0000000000000..a0d619c456644 --- /dev/null +++ b/src/test/ui/proc-macro/issue-104884-trait-impl-sugg-err.rs @@ -0,0 +1,20 @@ +// aux-build:issue-104884.rs + +use std::collections::BinaryHeap; + +#[macro_use] +extern crate issue_104884; + +#[derive(PartialEq, Eq, PartialOrd, Ord)] +struct PriorityQueueEntry { + value: T, +} + +#[derive(PartialOrd, AddImpl)] +//~^ ERROR can't compare `PriorityQueue` with `PriorityQueue` +//~| ERROR the trait bound `PriorityQueue: Eq` is not satisfied +//~| ERROR can't compare `T` with `T` + +struct PriorityQueue(BinaryHeap>); + +fn main() {} diff --git a/src/test/ui/proc-macro/issue-104884-trait-impl-sugg-err.stderr b/src/test/ui/proc-macro/issue-104884-trait-impl-sugg-err.stderr new file mode 100644 index 0000000000000..ac49e04e3c0a9 --- /dev/null +++ b/src/test/ui/proc-macro/issue-104884-trait-impl-sugg-err.stderr @@ -0,0 +1,48 @@ +error[E0277]: can't compare `PriorityQueue` with `PriorityQueue` + --> $DIR/issue-104884-trait-impl-sugg-err.rs:13:10 + | +LL | #[derive(PartialOrd, AddImpl)] + | ^^^^^^^^^^ no implementation for `PriorityQueue == PriorityQueue` + | + = help: the trait `PartialEq` is not implemented for `PriorityQueue` +note: required by a bound in `PartialOrd` + --> $SRC_DIR/core/src/cmp.rs:LL:COL + | +LL | pub trait PartialOrd: PartialEq { + | ^^^^^^^^^^^^^^ required by this bound in `PartialOrd` + = note: this error originates in the derive macro `PartialOrd` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `PriorityQueue: Eq` is not satisfied + --> $DIR/issue-104884-trait-impl-sugg-err.rs:13:22 + | +LL | #[derive(PartialOrd, AddImpl)] + | ^^^^^^^ the trait `Eq` is not implemented for `PriorityQueue` + | +note: required by a bound in `Ord` + --> $SRC_DIR/core/src/cmp.rs:LL:COL + | +LL | pub trait Ord: Eq + PartialOrd { + | ^^ required by this bound in `Ord` + = note: this error originates in the derive macro `AddImpl` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: can't compare `T` with `T` + --> $DIR/issue-104884-trait-impl-sugg-err.rs:13:22 + | +LL | #[derive(PartialOrd, AddImpl)] + | ^^^^^^^ no implementation for `T < T` and `T > T` + | +note: required for `PriorityQueue` to implement `PartialOrd` + --> $DIR/issue-104884-trait-impl-sugg-err.rs:13:10 + | +LL | #[derive(PartialOrd, AddImpl)] + | ^^^^^^^^^^ +note: required by a bound in `Ord` + --> $SRC_DIR/core/src/cmp.rs:LL:COL + | +LL | pub trait Ord: Eq + PartialOrd { + | ^^^^^^^^^^^^^^^^ required by this bound in `Ord` + = note: this error originates in the derive macro `AddImpl` which comes from the expansion of the derive macro `PartialOrd` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0277`. From 0893322e5412f78e8214c3ebc8b5b79a8c976f38 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Mon, 28 Nov 2022 23:56:02 -0500 Subject: [PATCH 176/244] Don't lint `unnecessary_operation` in mixed macro contexts --- clippy_lints/src/no_effect.rs | 8 ++++++-- tests/ui/unnecessary_operation.fixed | 9 +++++++++ tests/ui/unnecessary_operation.rs | 9 +++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/no_effect.rs b/clippy_lints/src/no_effect.rs index 819646bb6780e..79c1ae4861e80 100644 --- a/clippy_lints/src/no_effect.rs +++ b/clippy_lints/src/no_effect.rs @@ -6,7 +6,8 @@ use clippy_utils::ty::has_drop; use rustc_errors::Applicability; use rustc_hir::def::{DefKind, Res}; use rustc_hir::{is_range_literal, BinOpKind, BlockCheckMode, Expr, ExprKind, PatKind, Stmt, StmtKind, UnsafeSource}; -use rustc_lint::{LateContext, LateLintPass}; +use rustc_lint::{LateContext, LateLintPass, LintContext}; +use rustc_middle::lint::in_external_macro; use rustc_session::{declare_lint_pass, declare_tool_lint}; use std::ops::Deref; @@ -159,8 +160,11 @@ fn has_no_effect(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { fn check_unnecessary_operation(cx: &LateContext<'_>, stmt: &Stmt<'_>) { if_chain! { if let StmtKind::Semi(expr) = stmt.kind; + let ctxt = stmt.span.ctxt(); + if expr.span.ctxt() == ctxt; if let Some(reduced) = reduce_expression(cx, expr); - if !&reduced.iter().any(|e| e.span.from_expansion()); + if !in_external_macro(cx.sess(), stmt.span); + if reduced.iter().all(|e| e.span.ctxt() == ctxt); then { if let ExprKind::Index(..) = &expr.kind { let snippet = if let (Some(arr), Some(func)) = diff --git a/tests/ui/unnecessary_operation.fixed b/tests/ui/unnecessary_operation.fixed index bf0ec8deb3458..d37163570abe0 100644 --- a/tests/ui/unnecessary_operation.fixed +++ b/tests/ui/unnecessary_operation.fixed @@ -76,4 +76,13 @@ fn main() { DropStruct { ..get_drop_struct() }; DropEnum::Tuple(get_number()); DropEnum::Struct { field: get_number() }; + + // Issue #9954 + fn one() -> i8 { + 1 + } + macro_rules! use_expr { + ($($e:expr),*) => {{ $($e;)* }} + } + use_expr!(isize::MIN / -(one() as isize), i8::MIN / -one()); } diff --git a/tests/ui/unnecessary_operation.rs b/tests/ui/unnecessary_operation.rs index 08cb9ab522ee0..a14fd4bca0efd 100644 --- a/tests/ui/unnecessary_operation.rs +++ b/tests/ui/unnecessary_operation.rs @@ -80,4 +80,13 @@ fn main() { DropStruct { ..get_drop_struct() }; DropEnum::Tuple(get_number()); DropEnum::Struct { field: get_number() }; + + // Issue #9954 + fn one() -> i8 { + 1 + } + macro_rules! use_expr { + ($($e:expr),*) => {{ $($e;)* }} + } + use_expr!(isize::MIN / -(one() as isize), i8::MIN / -one()); } From f44b7aa81eca5916a165f571b4621a9ec1dd22fb Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Mon, 28 Nov 2022 23:30:56 -0500 Subject: [PATCH 177/244] Don't lint `unnecessary_cast` in mixed macro context --- clippy_lints/src/casts/unnecessary_cast.rs | 11 +++++- tests/ui/unnecessary_cast.fixed | 11 ++++++ tests/ui/unnecessary_cast.rs | 11 ++++++ tests/ui/unnecessary_cast.stderr | 44 +++++++++++----------- 4 files changed, 54 insertions(+), 23 deletions(-) diff --git a/clippy_lints/src/casts/unnecessary_cast.rs b/clippy_lints/src/casts/unnecessary_cast.rs index c8596987e4d71..ecc8a8de97b93 100644 --- a/clippy_lints/src/casts/unnecessary_cast.rs +++ b/clippy_lints/src/casts/unnecessary_cast.rs @@ -1,7 +1,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg; -use clippy_utils::get_parent_expr; use clippy_utils::numeric_literal::NumericLiteral; use clippy_utils::source::snippet_opt; +use clippy_utils::{get_parent_expr, path_to_local}; use if_chain::if_chain; use rustc_ast::{LitFloatType, LitIntType, LitKind}; use rustc_errors::Applicability; @@ -75,6 +75,15 @@ pub(super) fn check<'tcx>( } if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) { + if let Some(id) = path_to_local(cast_expr) + && let Some(span) = cx.tcx.hir().opt_span(id) + && span.ctxt() != cast_expr.span.ctxt() + { + // Binding context is different than the identifiers context. + // Weird macro wizardry could be involved here. + return false; + } + span_lint_and_sugg( cx, UNNECESSARY_CAST, diff --git a/tests/ui/unnecessary_cast.fixed b/tests/ui/unnecessary_cast.fixed index ec8c6abfab91e..f234d4473c3e9 100644 --- a/tests/ui/unnecessary_cast.fixed +++ b/tests/ui/unnecessary_cast.fixed @@ -41,6 +41,17 @@ fn main() { // do not lint cast to alias type 1 as I32Alias; &1 as &I32Alias; + + // issue #9960 + macro_rules! bind_var { + ($id:ident, $e:expr) => {{ + let $id = 0usize; + let _ = $e != 0usize; + let $id = 0isize; + let _ = $e != 0usize; + }} + } + bind_var!(x, (x as usize) + 1); } type I32Alias = i32; diff --git a/tests/ui/unnecessary_cast.rs b/tests/ui/unnecessary_cast.rs index 5213cdc269bd4..855a4efa03411 100644 --- a/tests/ui/unnecessary_cast.rs +++ b/tests/ui/unnecessary_cast.rs @@ -41,6 +41,17 @@ fn main() { // do not lint cast to alias type 1 as I32Alias; &1 as &I32Alias; + + // issue #9960 + macro_rules! bind_var { + ($id:ident, $e:expr) => {{ + let $id = 0usize; + let _ = $e != 0usize; + let $id = 0isize; + let _ = $e != 0usize; + }} + } + bind_var!(x, (x as usize) + 1); } type I32Alias = i32; diff --git a/tests/ui/unnecessary_cast.stderr b/tests/ui/unnecessary_cast.stderr index e5c3dd5e53f87..934db0e86e662 100644 --- a/tests/ui/unnecessary_cast.stderr +++ b/tests/ui/unnecessary_cast.stderr @@ -49,133 +49,133 @@ LL | 1_f32 as f32; | ^^^^^^^^^^^^ help: try: `1_f32` error: casting integer literal to `f32` is unnecessary - --> $DIR/unnecessary_cast.rs:53:9 + --> $DIR/unnecessary_cast.rs:64:9 | LL | 100 as f32; | ^^^^^^^^^^ help: try: `100_f32` error: casting integer literal to `f64` is unnecessary - --> $DIR/unnecessary_cast.rs:54:9 + --> $DIR/unnecessary_cast.rs:65:9 | LL | 100 as f64; | ^^^^^^^^^^ help: try: `100_f64` error: casting integer literal to `f64` is unnecessary - --> $DIR/unnecessary_cast.rs:55:9 + --> $DIR/unnecessary_cast.rs:66:9 | LL | 100_i32 as f64; | ^^^^^^^^^^^^^^ help: try: `100_f64` error: casting integer literal to `f32` is unnecessary - --> $DIR/unnecessary_cast.rs:56:17 + --> $DIR/unnecessary_cast.rs:67:17 | LL | let _ = -100 as f32; | ^^^^^^^^^^^ help: try: `-100_f32` error: casting integer literal to `f64` is unnecessary - --> $DIR/unnecessary_cast.rs:57:17 + --> $DIR/unnecessary_cast.rs:68:17 | LL | let _ = -100 as f64; | ^^^^^^^^^^^ help: try: `-100_f64` error: casting integer literal to `f64` is unnecessary - --> $DIR/unnecessary_cast.rs:58:17 + --> $DIR/unnecessary_cast.rs:69:17 | LL | let _ = -100_i32 as f64; | ^^^^^^^^^^^^^^^ help: try: `-100_f64` error: casting float literal to `f32` is unnecessary - --> $DIR/unnecessary_cast.rs:59:9 + --> $DIR/unnecessary_cast.rs:70:9 | LL | 100. as f32; | ^^^^^^^^^^^ help: try: `100_f32` error: casting float literal to `f64` is unnecessary - --> $DIR/unnecessary_cast.rs:60:9 + --> $DIR/unnecessary_cast.rs:71:9 | LL | 100. as f64; | ^^^^^^^^^^^ help: try: `100_f64` error: casting integer literal to `u32` is unnecessary - --> $DIR/unnecessary_cast.rs:72:9 + --> $DIR/unnecessary_cast.rs:83:9 | LL | 1 as u32; | ^^^^^^^^ help: try: `1_u32` error: casting integer literal to `i32` is unnecessary - --> $DIR/unnecessary_cast.rs:73:9 + --> $DIR/unnecessary_cast.rs:84:9 | LL | 0x10 as i32; | ^^^^^^^^^^^ help: try: `0x10_i32` error: casting integer literal to `usize` is unnecessary - --> $DIR/unnecessary_cast.rs:74:9 + --> $DIR/unnecessary_cast.rs:85:9 | LL | 0b10 as usize; | ^^^^^^^^^^^^^ help: try: `0b10_usize` error: casting integer literal to `u16` is unnecessary - --> $DIR/unnecessary_cast.rs:75:9 + --> $DIR/unnecessary_cast.rs:86:9 | LL | 0o73 as u16; | ^^^^^^^^^^^ help: try: `0o73_u16` error: casting integer literal to `u32` is unnecessary - --> $DIR/unnecessary_cast.rs:76:9 + --> $DIR/unnecessary_cast.rs:87:9 | LL | 1_000_000_000 as u32; | ^^^^^^^^^^^^^^^^^^^^ help: try: `1_000_000_000_u32` error: casting float literal to `f64` is unnecessary - --> $DIR/unnecessary_cast.rs:78:9 + --> $DIR/unnecessary_cast.rs:89:9 | LL | 1.0 as f64; | ^^^^^^^^^^ help: try: `1.0_f64` error: casting float literal to `f32` is unnecessary - --> $DIR/unnecessary_cast.rs:79:9 + --> $DIR/unnecessary_cast.rs:90:9 | LL | 0.5 as f32; | ^^^^^^^^^^ help: try: `0.5_f32` error: casting integer literal to `i32` is unnecessary - --> $DIR/unnecessary_cast.rs:83:17 + --> $DIR/unnecessary_cast.rs:94:17 | LL | let _ = -1 as i32; | ^^^^^^^^^ help: try: `-1_i32` error: casting float literal to `f32` is unnecessary - --> $DIR/unnecessary_cast.rs:84:17 + --> $DIR/unnecessary_cast.rs:95:17 | LL | let _ = -1.0 as f32; | ^^^^^^^^^^^ help: try: `-1.0_f32` error: casting integer literal to `i32` is unnecessary - --> $DIR/unnecessary_cast.rs:93:22 + --> $DIR/unnecessary_cast.rs:104:22 | LL | let _: i32 = -(1) as i32; | ^^^^^^^^^^^ help: try: `-1_i32` error: casting integer literal to `i64` is unnecessary - --> $DIR/unnecessary_cast.rs:95:22 + --> $DIR/unnecessary_cast.rs:106:22 | LL | let _: i64 = -(1) as i64; | ^^^^^^^^^^^ help: try: `-1_i64` error: casting float literal to `f64` is unnecessary - --> $DIR/unnecessary_cast.rs:102:22 + --> $DIR/unnecessary_cast.rs:113:22 | LL | let _: f64 = (-8.0 as f64).exp(); | ^^^^^^^^^^^^^ help: try: `(-8.0_f64)` error: casting float literal to `f64` is unnecessary - --> $DIR/unnecessary_cast.rs:104:23 + --> $DIR/unnecessary_cast.rs:115:23 | LL | let _: f64 = -(8.0 as f64).exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior | ^^^^^^^^^^^^ help: try: `8.0_f64` error: casting to the same type is unnecessary (`f32` -> `f32`) - --> $DIR/unnecessary_cast.rs:112:20 + --> $DIR/unnecessary_cast.rs:123:20 | LL | let _num = foo() as f32; | ^^^^^^^^^^^^ help: try: `foo()` From ac7a7499df4c21de001475d8acf8bf43ee46498d Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 29 Nov 2022 17:13:38 +1100 Subject: [PATCH 178/244] Remove an out-of-date comment. This comment dates back to at least 2013, and is no longer relevant. (There used to be an `allow` attribute, but that's no longer present.) --- compiler/rustc_expand/src/build.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/compiler/rustc_expand/src/build.rs b/compiler/rustc_expand/src/build.rs index 234cf1b315a23..c2a2326d30123 100644 --- a/compiler/rustc_expand/src/build.rs +++ b/compiler/rustc_expand/src/build.rs @@ -579,8 +579,6 @@ impl<'a> ExtCtxt<'a> { attrs: ast::AttrVec, kind: ast::ItemKind, ) -> P { - // FIXME: Would be nice if our generated code didn't violate - // Rust coding conventions P(ast::Item { ident: name, attrs, From d1b61a31c5d28075f7c5a7f376315350672e2d93 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 29 Nov 2022 18:42:40 +1100 Subject: [PATCH 179/244] Inline and remove `MetaItemLit::from_lit_kind`. It has a single call site. --- compiler/rustc_ast/src/attr/mod.rs | 4 ++-- compiler/rustc_ast/src/util/literal.rs | 7 ------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_ast/src/attr/mod.rs b/compiler/rustc_ast/src/attr/mod.rs index 7a86b471ba298..e08be6970ebae 100644 --- a/compiler/rustc_ast/src/attr/mod.rs +++ b/compiler/rustc_ast/src/attr/mod.rs @@ -333,8 +333,8 @@ pub fn mk_name_value_item_str(ident: Ident, str: Symbol, str_span: Span) -> Meta mk_name_value_item(ident, lit_kind, str_span) } -pub fn mk_name_value_item(ident: Ident, lit_kind: LitKind, lit_span: Span) -> MetaItem { - let lit = MetaItemLit::from_lit_kind(lit_kind, lit_span); +pub fn mk_name_value_item(ident: Ident, kind: LitKind, lit_span: Span) -> MetaItem { + let lit = MetaItemLit { token_lit: kind.to_token_lit(), kind, span: lit_span }; let span = ident.span.to(lit_span); MetaItem { path: Path::from_ident(ident), span, kind: MetaItemKind::NameValue(lit) } } diff --git a/compiler/rustc_ast/src/util/literal.rs b/compiler/rustc_ast/src/util/literal.rs index 42cba07fcef8d..14ee4d481209c 100644 --- a/compiler/rustc_ast/src/util/literal.rs +++ b/compiler/rustc_ast/src/util/literal.rs @@ -207,13 +207,6 @@ impl MetaItemLit { .and_then(|token_lit| MetaItemLit::from_token_lit(token_lit, token.span).ok()) } - /// Attempts to create a meta item literal from a `LitKind`. - /// This function is used when the original token doesn't exist (e.g. the literal is created - /// by an AST-based macro) or unavailable (e.g. from HIR pretty-printing). - pub fn from_lit_kind(kind: LitKind, span: Span) -> MetaItemLit { - MetaItemLit { token_lit: kind.to_token_lit(), kind, span } - } - /// Losslessly convert a meta item literal into a token. pub fn to_token(&self) -> Token { let kind = match self.token_lit.kind { From ba1751a201190930a16ef20e6f7021c785d3891d Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 29 Nov 2022 18:43:44 +1100 Subject: [PATCH 180/244] Avoid more `MetaItem`-to-`Attribute` conversions. There is code for converting `Attribute` (syntactic) to `MetaItem` (semantic). There is also code for the reverse direction. The reverse direction isn't really necessary; it's currently only used when generating attributes, e.g. in `derive` code. This commit adds some new functions for creating `Attributes`s directly, without involving `MetaItem`s: `mk_attr_word`, `mk_attr_name_value_str`, `mk_attr_nested_word`, and `ExtCtxt::attr_{word,name_value_str,nested_word}`. These new methods replace the old functions for creating `Attribute`s: `mk_attr_inner`, `mk_attr_outer`, and `ExtCtxt::attribute`. Those functions took `MetaItem`s as input, and relied on many other functions that created `MetaItems`, which are also removed: `mk_name_value_item`, `mk_list_item`, `mk_word_item`, `mk_nested_word_item`, `{MetaItem,MetaItemKind,NestedMetaItem}::token_trees`, `MetaItemKind::attr_args`, `MetaItemLit::{from_lit_kind,to_token}`, `ExtCtxt::meta_word`. Overall this cuts more than 100 lines of code and makes thing simpler. --- compiler/rustc_ast/src/attr/mod.rs | 167 ++++++------------ compiler/rustc_ast/src/util/literal.rs | 9 - compiler/rustc_ast_lowering/src/expr.rs | 17 +- compiler/rustc_ast_pretty/src/pprust/state.rs | 19 +- .../src/alloc_error_handler.rs | 4 +- .../src/assert/context.rs | 6 +- .../src/deriving/clone.rs | 3 +- .../src/deriving/cmp/eq.rs | 12 +- .../src/deriving/cmp/ord.rs | 3 +- .../src/deriving/cmp/partial_eq.rs | 3 +- .../src/deriving/cmp/partial_ord.rs | 3 +- .../src/deriving/default.rs | 3 +- .../src/deriving/generic/mod.rs | 2 +- .../rustc_builtin_macros/src/deriving/mod.rs | 2 +- .../src/global_allocator.rs | 4 +- .../src/proc_macro_harness.rs | 14 +- .../src/standard_library_imports.rs | 4 +- compiler/rustc_builtin_macros/src/test.rs | 18 +- .../rustc_builtin_macros/src/test_harness.rs | 14 +- compiler/rustc_expand/src/build.rs | 20 ++- 20 files changed, 116 insertions(+), 211 deletions(-) diff --git a/compiler/rustc_ast/src/attr/mod.rs b/compiler/rustc_ast/src/attr/mod.rs index e08be6970ebae..057cc26b5799e 100644 --- a/compiler/rustc_ast/src/attr/mod.rs +++ b/compiler/rustc_ast/src/attr/mod.rs @@ -1,10 +1,10 @@ //! Functions dealing with attributes and meta items. use crate::ast; -use crate::ast::{AttrArgs, AttrArgsEq, AttrId, AttrItem, AttrKind, AttrStyle, Attribute}; -use crate::ast::{DelimArgs, LitKind, MetaItemLit}; -use crate::ast::{MacDelimiter, MetaItem, MetaItemKind, NestedMetaItem}; -use crate::ast::{Path, PathSegment}; +use crate::ast::{AttrArgs, AttrArgsEq, AttrId, AttrItem, AttrKind, AttrStyle, AttrVec, Attribute}; +use crate::ast::{DelimArgs, Expr, ExprKind, LitKind, MetaItemLit}; +use crate::ast::{MacDelimiter, MetaItem, MetaItemKind, NestedMetaItem, NormalAttr}; +use crate::ast::{Path, PathSegment, StrStyle, DUMMY_NODE_ID}; use crate::ptr::P; use crate::token::{self, CommentKind, Delimiter, Token}; use crate::tokenstream::{DelimSpan, Spacing, TokenTree}; @@ -12,7 +12,6 @@ use crate::tokenstream::{LazyAttrTokenStream, TokenStream}; use crate::util::comments; use rustc_data_structures::sync::WorkerLocal; use rustc_index::bit_set::GrowableBitSet; -use rustc_span::source_map::BytePos; use rustc_span::symbol::{sym, Ident, Symbol}; use rustc_span::Span; use std::cell::Cell; @@ -223,11 +222,7 @@ impl AttrItem { } pub fn meta(&self, span: Span) -> Option { - Some(MetaItem { - path: self.path.clone(), - kind: MetaItemKind::from_attr_args(&self.args)?, - span, - }) + Some(MetaItem { path: self.path.clone(), kind: self.meta_kind()?, span }) } pub fn meta_kind(&self) -> Option { @@ -329,26 +324,13 @@ impl Attribute { /* Constructors */ pub fn mk_name_value_item_str(ident: Ident, str: Symbol, str_span: Span) -> MetaItem { - let lit_kind = LitKind::Str(str, ast::StrStyle::Cooked); - mk_name_value_item(ident, lit_kind, str_span) + mk_name_value_item(ident, LitKind::Str(str, ast::StrStyle::Cooked), str_span) } pub fn mk_name_value_item(ident: Ident, kind: LitKind, lit_span: Span) -> MetaItem { let lit = MetaItemLit { token_lit: kind.to_token_lit(), kind, span: lit_span }; let span = ident.span.to(lit_span); - MetaItem { path: Path::from_ident(ident), span, kind: MetaItemKind::NameValue(lit) } -} - -pub fn mk_list_item(ident: Ident, items: Vec) -> MetaItem { - MetaItem { path: Path::from_ident(ident), span: ident.span, kind: MetaItemKind::List(items) } -} - -pub fn mk_word_item(ident: Ident) -> MetaItem { - MetaItem { path: Path::from_ident(ident), span: ident.span, kind: MetaItemKind::Word } -} - -pub fn mk_nested_word_item(ident: Ident) -> NestedMetaItem { - NestedMetaItem::MetaItem(mk_word_item(ident)) + MetaItem { path: Path::from_ident(ident), kind: MetaItemKind::NameValue(lit), span } } pub struct AttrIdGenerator(WorkerLocal>); @@ -406,21 +388,58 @@ pub fn mk_attr_from_item( span: Span, ) -> Attribute { Attribute { - kind: AttrKind::Normal(P(ast::NormalAttr { item, tokens })), + kind: AttrKind::Normal(P(NormalAttr { item, tokens })), id: g.mk_attr_id(), style, span, } } -/// Returns an inner attribute with the given value and span. -pub fn mk_attr_inner(g: &AttrIdGenerator, item: MetaItem) -> Attribute { - mk_attr(g, AttrStyle::Inner, item.path, item.kind.attr_args(item.span), item.span) +pub fn mk_attr_word(g: &AttrIdGenerator, style: AttrStyle, name: Symbol, span: Span) -> Attribute { + let path = Path::from_ident(Ident::new(name, span)); + let args = AttrArgs::Empty; + mk_attr(g, style, path, args, span) +} + +pub fn mk_attr_name_value_str( + g: &AttrIdGenerator, + style: AttrStyle, + name: Symbol, + val: Symbol, + span: Span, +) -> Attribute { + let lit = LitKind::Str(val, StrStyle::Cooked).to_token_lit(); + let expr = P(Expr { + id: DUMMY_NODE_ID, + kind: ExprKind::Lit(lit), + span, + attrs: AttrVec::new(), + tokens: None, + }); + let path = Path::from_ident(Ident::new(name, span)); + let args = AttrArgs::Eq(span, AttrArgsEq::Ast(expr)); + mk_attr(g, style, path, args, span) } -/// Returns an outer attribute with the given value and span. -pub fn mk_attr_outer(g: &AttrIdGenerator, item: MetaItem) -> Attribute { - mk_attr(g, AttrStyle::Outer, item.path, item.kind.attr_args(item.span), item.span) +pub fn mk_attr_nested_word( + g: &AttrIdGenerator, + style: AttrStyle, + outer: Symbol, + inner: Symbol, + span: Span, +) -> Attribute { + let inner_tokens = TokenStream::new(vec![TokenTree::Token( + Token::from_ast_ident(Ident::new(inner, span)), + Spacing::Alone, + )]); + let outer_ident = Ident::new(outer, span); + let path = Path::from_ident(outer_ident); + let attr_args = AttrArgs::Delimited(DelimArgs { + dspan: DelimSpan::from_single(span), + delim: MacDelimiter::Parenthesis, + tokens: inner_tokens, + }); + mk_attr(g, style, path, attr_args, span) } pub fn mk_doc_comment( @@ -438,23 +457,6 @@ pub fn list_contains_name(items: &[NestedMetaItem], name: Symbol) -> bool { } impl MetaItem { - fn token_trees(&self) -> Vec { - let mut idents = vec![]; - let mut last_pos = BytePos(0_u32); - for (i, segment) in self.path.segments.iter().enumerate() { - let is_first = i == 0; - if !is_first { - let mod_sep_span = - Span::new(last_pos, segment.ident.span.lo(), segment.ident.span.ctxt(), None); - idents.push(TokenTree::token_alone(token::ModSep, mod_sep_span)); - } - idents.push(TokenTree::Token(Token::from_ast_ident(segment.ident), Spacing::Alone)); - last_pos = segment.ident.span.hi(); - } - idents.extend(self.kind.token_trees(self.span)); - idents - } - fn from_tokens(tokens: &mut iter::Peekable) -> Option where I: Iterator, @@ -526,62 +528,6 @@ impl MetaItemKind { } } - pub fn attr_args(&self, span: Span) -> AttrArgs { - match self { - MetaItemKind::Word => AttrArgs::Empty, - MetaItemKind::NameValue(lit) => { - let expr = P(ast::Expr { - id: ast::DUMMY_NODE_ID, - kind: ast::ExprKind::Lit(lit.token_lit.clone()), - span: lit.span, - attrs: ast::AttrVec::new(), - tokens: None, - }); - AttrArgs::Eq(span, AttrArgsEq::Ast(expr)) - } - MetaItemKind::List(list) => { - let mut tts = Vec::new(); - for (i, item) in list.iter().enumerate() { - if i > 0 { - tts.push(TokenTree::token_alone(token::Comma, span)); - } - tts.extend(item.token_trees()) - } - AttrArgs::Delimited(DelimArgs { - dspan: DelimSpan::from_single(span), - delim: MacDelimiter::Parenthesis, - tokens: TokenStream::new(tts), - }) - } - } - } - - fn token_trees(&self, span: Span) -> Vec { - match self { - MetaItemKind::Word => vec![], - MetaItemKind::NameValue(lit) => { - vec![ - TokenTree::token_alone(token::Eq, span), - TokenTree::Token(lit.to_token(), Spacing::Alone), - ] - } - MetaItemKind::List(list) => { - let mut tokens = Vec::new(); - for (i, item) in list.iter().enumerate() { - if i > 0 { - tokens.push(TokenTree::token_alone(token::Comma, span)); - } - tokens.extend(item.token_trees()) - } - vec![TokenTree::Delimited( - DelimSpan::from_single(span), - Delimiter::Parenthesis, - TokenStream::new(tokens), - )] - } - } - } - fn list_from_tokens(tokens: TokenStream) -> Option { let mut tokens = tokens.into_trees().peekable(); let mut result = Vec::new(); @@ -620,7 +566,7 @@ impl MetaItemKind { }) => MetaItemKind::list_from_tokens(tokens.clone()), AttrArgs::Delimited(..) => None, AttrArgs::Eq(_, AttrArgsEq::Ast(expr)) => match expr.kind { - ast::ExprKind::Lit(token_lit) => { + ExprKind::Lit(token_lit) => { // Turn failures to `None`, we'll get parse errors elsewhere. MetaItemLit::from_token_lit(token_lit, expr.span) .ok() @@ -659,15 +605,6 @@ impl NestedMetaItem { } } - fn token_trees(&self) -> Vec { - match self { - NestedMetaItem::MetaItem(item) => item.token_trees(), - NestedMetaItem::Lit(lit) => { - vec![TokenTree::Token(lit.to_token(), Spacing::Alone)] - } - } - } - fn from_tokens(tokens: &mut iter::Peekable) -> Option where I: Iterator, diff --git a/compiler/rustc_ast/src/util/literal.rs b/compiler/rustc_ast/src/util/literal.rs index 14ee4d481209c..1d6e7914f3a5c 100644 --- a/compiler/rustc_ast/src/util/literal.rs +++ b/compiler/rustc_ast/src/util/literal.rs @@ -206,15 +206,6 @@ impl MetaItemLit { token::Lit::from_token(token) .and_then(|token_lit| MetaItemLit::from_token_lit(token_lit, token.span).ok()) } - - /// Losslessly convert a meta item literal into a token. - pub fn to_token(&self) -> Token { - let kind = match self.token_lit.kind { - token::Bool => token::Ident(self.token_lit.symbol, false), - _ => token::Literal(self.token_lit), - }; - Token::new(kind, self.span) - } } fn strip_underscores(symbol: Symbol) -> Symbol { diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index c14c591d3875d..82912a733d552 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -1606,16 +1606,13 @@ impl<'hir> LoweringContext<'_, 'hir> { }; // `#[allow(unreachable_code)]` - let attr = { - // `allow(unreachable_code)` - let allow = { - let allow_ident = Ident::new(sym::allow, self.lower_span(span)); - let uc_ident = Ident::new(sym::unreachable_code, self.lower_span(span)); - let uc_nested = attr::mk_nested_word_item(uc_ident); - attr::mk_list_item(allow_ident, vec![uc_nested]) - }; - attr::mk_attr_outer(&self.tcx.sess.parse_sess.attr_id_generator, allow) - }; + let attr = attr::mk_attr_nested_word( + &self.tcx.sess.parse_sess.attr_id_generator, + AttrStyle::Outer, + sym::allow, + sym::unreachable_code, + self.lower_span(span), + ); let attrs: AttrVec = thin_vec![attr]; // `ControlFlow::Continue(val) => #[allow(unreachable_code)] val,` diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index 7a9243c511b92..2d7fd83195d94 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -19,7 +19,7 @@ use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece}; use rustc_span::edition::Edition; use rustc_span::source_map::{SourceMap, Spanned}; use rustc_span::symbol::{kw, sym, Ident, IdentPrinter, Symbol}; -use rustc_span::{BytePos, FileName, Span}; +use rustc_span::{BytePos, FileName, Span, DUMMY_SP}; use rustc_ast::attr::AttrIdGenerator; use std::borrow::Cow; @@ -119,17 +119,20 @@ pub fn print_crate<'a>( // of the feature gate, so we fake them up here. // `#![feature(prelude_import)]` - let pi_nested = attr::mk_nested_word_item(Ident::with_dummy_span(sym::prelude_import)); - let list = attr::mk_list_item(Ident::with_dummy_span(sym::feature), vec![pi_nested]); - let fake_attr = attr::mk_attr_inner(g, list); + let fake_attr = attr::mk_attr_nested_word( + g, + ast::AttrStyle::Inner, + sym::feature, + sym::prelude_import, + DUMMY_SP, + ); s.print_attribute(&fake_attr); // Currently, in Rust 2018 we don't have `extern crate std;` at the crate // root, so this is not needed, and actually breaks things. if edition == Edition::Edition2015 { // `#![no_std]` - let no_std_meta = attr::mk_word_item(Ident::with_dummy_span(sym::no_std)); - let fake_attr = attr::mk_attr_inner(g, no_std_meta); + let fake_attr = attr::mk_attr_word(g, ast::AttrStyle::Inner, sym::no_std, DUMMY_SP); s.print_attribute(&fake_attr); } } @@ -1712,9 +1715,9 @@ impl<'a> State<'a> { where_clause: ast::WhereClause { has_where_token: false, predicates: Vec::new(), - span: rustc_span::DUMMY_SP, + span: DUMMY_SP, }, - span: rustc_span::DUMMY_SP, + span: DUMMY_SP, }; let header = ast::FnHeader { unsafety, ext, ..ast::FnHeader::default() }; self.print_fn(decl, header, name, &generics); diff --git a/compiler/rustc_builtin_macros/src/alloc_error_handler.rs b/compiler/rustc_builtin_macros/src/alloc_error_handler.rs index eaf1b1167cf21..460175ed2ac84 100644 --- a/compiler/rustc_builtin_macros/src/alloc_error_handler.rs +++ b/compiler/rustc_builtin_macros/src/alloc_error_handler.rs @@ -95,9 +95,7 @@ fn generate_handler(cx: &ExtCtxt<'_>, handler: Ident, span: Span, sig_span: Span body, })); - let special = sym::rustc_std_internal_symbol; - let special = cx.meta_word(span, special); - let attrs = thin_vec![cx.attribute(special)]; + let attrs = thin_vec![cx.attr_word(sym::rustc_std_internal_symbol, span)]; let item = cx.item(span, Ident::from_str_and_span("__rg_oom", span), attrs, kind); cx.stmt_item(sig_span, item) diff --git a/compiler/rustc_builtin_macros/src/assert/context.rs b/compiler/rustc_builtin_macros/src/assert/context.rs index 9f42a0c2d585c..bd415901ae34d 100644 --- a/compiler/rustc_builtin_macros/src/assert/context.rs +++ b/compiler/rustc_builtin_macros/src/assert/context.rs @@ -1,5 +1,4 @@ use rustc_ast::{ - attr, ptr::P, token, tokenstream::{DelimSpan, TokenStream, TokenTree}, @@ -118,10 +117,7 @@ impl<'cx, 'a> Context<'cx, 'a> { self.cx.item( self.span, Ident::empty(), - thin_vec![self.cx.attribute(attr::mk_list_item( - Ident::new(sym::allow, self.span), - vec![attr::mk_nested_word_item(Ident::new(sym::unused_imports, self.span))], - ))], + thin_vec![self.cx.attr_nested_word(sym::allow, sym::unused_imports, self.span)], ItemKind::Use(UseTree { prefix: self.cx.path(self.span, self.cx.std_path(&[sym::asserting])), kind: UseTreeKind::Nested(vec![ diff --git a/compiler/rustc_builtin_macros/src/deriving/clone.rs b/compiler/rustc_builtin_macros/src/deriving/clone.rs index 2f19fbcac7d72..23b96d4176d3a 100644 --- a/compiler/rustc_builtin_macros/src/deriving/clone.rs +++ b/compiler/rustc_builtin_macros/src/deriving/clone.rs @@ -68,8 +68,7 @@ pub fn expand_deriving_clone( _ => cx.span_bug(span, "`#[derive(Clone)]` on trait item or impl item"), } - let inline = cx.meta_word(span, sym::inline); - let attrs = thin_vec![cx.attribute(inline)]; + let attrs = thin_vec![cx.attr_word(sym::inline, span)]; let trait_def = TraitDef { span, path: path_std!(clone::Clone), diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs index a0b836171bea9..f861d47ed408e 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs @@ -5,7 +5,7 @@ use crate::deriving::path_std; use rustc_ast::{self as ast, MetaItem}; use rustc_data_structures::fx::FxHashSet; use rustc_expand::base::{Annotatable, ExtCtxt}; -use rustc_span::symbol::{sym, Ident}; +use rustc_span::symbol::sym; use rustc_span::Span; use thin_vec::thin_vec; @@ -18,11 +18,11 @@ pub fn expand_deriving_eq( is_const: bool, ) { let span = cx.with_def_site_ctxt(span); - let inline = cx.meta_word(span, sym::inline); - let hidden = rustc_ast::attr::mk_nested_word_item(Ident::new(sym::hidden, span)); - let doc = rustc_ast::attr::mk_list_item(Ident::new(sym::doc, span), vec![hidden]); - let no_coverage = cx.meta_word(span, sym::no_coverage); - let attrs = thin_vec![cx.attribute(inline), cx.attribute(doc), cx.attribute(no_coverage)]; + let attrs = thin_vec![ + cx.attr_word(sym::inline, span), + cx.attr_nested_word(sym::doc, sym::hidden, span), + cx.attr_word(sym::no_coverage, span) + ]; let trait_def = TraitDef { span, path: path_std!(cmp::Eq), diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs index 52780981248b9..96d18c7afb924 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs @@ -15,8 +15,7 @@ pub fn expand_deriving_ord( push: &mut dyn FnMut(Annotatable), is_const: bool, ) { - let inline = cx.meta_word(span, sym::inline); - let attrs = thin_vec![cx.attribute(inline)]; + let attrs = thin_vec![cx.attr_word(sym::inline, span)]; let trait_def = TraitDef { span, path: path_std!(cmp::Ord), diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs index 34de4a620b462..7f95551fc483a 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs @@ -68,8 +68,7 @@ pub fn expand_deriving_partial_eq( // No need to generate `ne`, the default suffices, and not generating it is // faster. - let inline = cx.meta_word(span, sym::inline); - let attrs = thin_vec![cx.attribute(inline)]; + let attrs = thin_vec![cx.attr_word(sym::inline, span)]; let methods = vec![MethodDef { name: sym::eq, generics: Bounds::empty(), diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs index 6cc8f26df559c..5c4e5b7f81675 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs @@ -19,8 +19,7 @@ pub fn expand_deriving_partial_ord( let ret_ty = Path(Path::new_(pathvec_std!(option::Option), vec![Box::new(ordering_ty)], PathKind::Std)); - let inline = cx.meta_word(span, sym::inline); - let attrs = thin_vec![cx.attribute(inline)]; + let attrs = thin_vec![cx.attr_word(sym::inline, span)]; let partial_cmp_def = MethodDef { name: sym::partial_cmp, diff --git a/compiler/rustc_builtin_macros/src/deriving/default.rs b/compiler/rustc_builtin_macros/src/deriving/default.rs index 93f297ad88b5f..e4e2435848dd2 100644 --- a/compiler/rustc_builtin_macros/src/deriving/default.rs +++ b/compiler/rustc_builtin_macros/src/deriving/default.rs @@ -20,8 +20,7 @@ pub fn expand_deriving_default( ) { item.visit_with(&mut DetectNonVariantDefaultAttr { cx }); - let inline = cx.meta_word(span, sym::inline); - let attrs = thin_vec![cx.attribute(inline)]; + let attrs = thin_vec![cx.attr_word(sym::inline, span)]; let trait_def = TraitDef { span, path: Path::new(vec![kw::Default, sym::Default]), diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index 1467d4eaec068..7fcaf0b436b90 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -718,7 +718,7 @@ impl<'a> TraitDef<'a> { let path = cx.path_all(self.span, false, vec![type_ident], self_params); let self_type = cx.ty_path(path); - let attr = cx.attribute(cx.meta_word(self.span, sym::automatically_derived)); + let attr = cx.attr_word(sym::automatically_derived, self.span); let attrs = thin_vec![attr]; let opt_trait_ref = Some(trait_ref); diff --git a/compiler/rustc_builtin_macros/src/deriving/mod.rs b/compiler/rustc_builtin_macros/src/deriving/mod.rs index 73a1df5d426d2..13fdd4fa68c43 100644 --- a/compiler/rustc_builtin_macros/src/deriving/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/mod.rs @@ -188,7 +188,7 @@ fn inject_impl_of_structural_trait( .cloned(), ); // Mark as `automatically_derived` to avoid some silly lints. - attrs.push(cx.attribute(cx.meta_word(span, sym::automatically_derived))); + attrs.push(cx.attr_word(sym::automatically_derived, span)); let newitem = cx.item( span, diff --git a/compiler/rustc_builtin_macros/src/global_allocator.rs b/compiler/rustc_builtin_macros/src/global_allocator.rs index 45b9b8ab6b641..0817aed037ef8 100644 --- a/compiler/rustc_builtin_macros/src/global_allocator.rs +++ b/compiler/rustc_builtin_macros/src/global_allocator.rs @@ -115,9 +115,7 @@ impl AllocFnFactory<'_, '_> { } fn attrs(&self) -> AttrVec { - let special = sym::rustc_std_internal_symbol; - let special = self.cx.meta_word(self.span, special); - thin_vec![self.cx.attribute(special)] + thin_vec![self.cx.attr_word(sym::rustc_std_internal_symbol, self.span)] } fn arg_ty( diff --git a/compiler/rustc_builtin_macros/src/proc_macro_harness.rs b/compiler/rustc_builtin_macros/src/proc_macro_harness.rs index ebe1c3663e3a7..ece660cf6f645 100644 --- a/compiler/rustc_builtin_macros/src/proc_macro_harness.rs +++ b/compiler/rustc_builtin_macros/src/proc_macro_harness.rs @@ -1,6 +1,3 @@ -use std::mem; - -use rustc_ast::attr; use rustc_ast::ptr::P; use rustc_ast::visit::{self, Visitor}; use rustc_ast::{self as ast, NodeId}; @@ -13,6 +10,7 @@ use rustc_span::source_map::SourceMap; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{Span, DUMMY_SP}; use smallvec::smallvec; +use std::mem; struct ProcMacroDerive { id: NodeId, @@ -365,14 +363,8 @@ fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> P { cx.expr_array_ref(span, decls), ) .map(|mut i| { - let attr = cx.meta_word(span, sym::rustc_proc_macro_decls); - i.attrs.push(cx.attribute(attr)); - - let deprecated_attr = attr::mk_nested_word_item(Ident::new(sym::deprecated, span)); - let allow_deprecated_attr = - attr::mk_list_item(Ident::new(sym::allow, span), vec![deprecated_attr]); - i.attrs.push(cx.attribute(allow_deprecated_attr)); - + i.attrs.push(cx.attr_word(sym::rustc_proc_macro_decls, span)); + i.attrs.push(cx.attr_nested_word(sym::allow, sym::deprecated, span)); i }); diff --git a/compiler/rustc_builtin_macros/src/standard_library_imports.rs b/compiler/rustc_builtin_macros/src/standard_library_imports.rs index 49ef538f04e14..f73f20c84a39d 100644 --- a/compiler/rustc_builtin_macros/src/standard_library_imports.rs +++ b/compiler/rustc_builtin_macros/src/standard_library_imports.rs @@ -52,7 +52,7 @@ pub fn inject( cx.item( span, ident, - thin_vec![cx.attribute(cx.meta_word(span, sym::macro_use))], + thin_vec![cx.attr_word(sym::macro_use, span)], ast::ItemKind::ExternCrate(None), ), ); @@ -79,7 +79,7 @@ pub fn inject( let use_item = cx.item( span, Ident::empty(), - thin_vec![cx.attribute(cx.meta_word(span, sym::prelude_import))], + thin_vec![cx.attr_word(sym::prelude_import, span)], ast::ItemKind::Use(ast::UseTree { prefix: cx.path(span, import_path), kind: ast::UseTreeKind::Glob, diff --git a/compiler/rustc_builtin_macros/src/test.rs b/compiler/rustc_builtin_macros/src/test.rs index 82baf1da28f2f..3bcb60478efbc 100644 --- a/compiler/rustc_builtin_macros/src/test.rs +++ b/compiler/rustc_builtin_macros/src/test.rs @@ -2,7 +2,6 @@ /// Ideally, this code would be in libtest but for efficiency and error messages it lives here. use crate::util::{check_builtin_macro_attribute, warn_on_duplicate_attribute}; use rustc_ast as ast; -use rustc_ast::attr; use rustc_ast::ptr::P; use rustc_ast_pretty::pprust; use rustc_errors::Applicability; @@ -47,11 +46,7 @@ pub fn expand_test_case( tokens: None, }; item.ident.span = item.ident.span.with_ctxt(sp.ctxt()); - item.attrs.push(ecx.attribute(attr::mk_name_value_item_str( - Ident::new(sym::rustc_test_marker, sp), - test_path_symbol, - sp, - ))); + item.attrs.push(ecx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, sp)); item }); @@ -241,16 +236,9 @@ pub fn expand_test_or_bench( Ident::new(item.ident.name, sp), thin_vec![ // #[cfg(test)] - cx.attribute(attr::mk_list_item( - Ident::new(sym::cfg, attr_sp), - vec![attr::mk_nested_word_item(Ident::new(sym::test, attr_sp))], - )), + cx.attr_nested_word(sym::cfg, sym::test, attr_sp), // #[rustc_test_marker = "test_case_sort_key"] - cx.attribute(attr::mk_name_value_item_str( - Ident::new(sym::rustc_test_marker, attr_sp), - test_path_symbol, - attr_sp, - )), + cx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, attr_sp), ] .into(), // const $ident: test::TestDescAndFn = diff --git a/compiler/rustc_builtin_macros/src/test_harness.rs b/compiler/rustc_builtin_macros/src/test_harness.rs index 3269f62b105b9..b5bce9278a907 100644 --- a/compiler/rustc_builtin_macros/src/test_harness.rs +++ b/compiler/rustc_builtin_macros/src/test_harness.rs @@ -185,13 +185,12 @@ impl<'a> MutVisitor for EntryPointCleaner<'a> { let item = match entry_point_type(self.sess, &item, self.depth) { EntryPointType::MainNamed | EntryPointType::RustcMainAttr | EntryPointType::Start => { item.map(|ast::Item { id, ident, attrs, kind, vis, span, tokens }| { - let allow_ident = Ident::new(sym::allow, self.def_site); - let dc_nested = - attr::mk_nested_word_item(Ident::new(sym::dead_code, self.def_site)); - let allow_dead_code_item = attr::mk_list_item(allow_ident, vec![dc_nested]); - let allow_dead_code = attr::mk_attr_outer( + let allow_dead_code = attr::mk_attr_nested_word( &self.sess.parse_sess.attr_id_generator, - allow_dead_code_item, + ast::AttrStyle::Outer, + sym::allow, + sym::dead_code, + self.def_site, ); let attrs = attrs .into_iter() @@ -309,8 +308,7 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P { ); // #[rustc_main] - let main_meta = ecx.meta_word(sp, sym::rustc_main); - let main_attr = ecx.attribute(main_meta); + let main_attr = ecx.attr_word(sym::rustc_main, sp); // pub fn main() { ... } let main_ret_ty = ecx.ty(sp, ast::TyKind::Tup(vec![])); diff --git a/compiler/rustc_expand/src/build.rs b/compiler/rustc_expand/src/build.rs index c2a2326d30123..c978297295d40 100644 --- a/compiler/rustc_expand/src/build.rs +++ b/compiler/rustc_expand/src/build.rs @@ -616,11 +616,23 @@ impl<'a> ExtCtxt<'a> { self.item(span, name, AttrVec::new(), ast::ItemKind::Const(def, ty, Some(expr))) } - pub fn attribute(&self, mi: ast::MetaItem) -> ast::Attribute { - attr::mk_attr_outer(&self.sess.parse_sess.attr_id_generator, mi) + // Builds `#[name]`. + pub fn attr_word(&self, name: Symbol, span: Span) -> ast::Attribute { + let g = &self.sess.parse_sess.attr_id_generator; + attr::mk_attr_word(g, ast::AttrStyle::Outer, name, span) } - pub fn meta_word(&self, sp: Span, w: Symbol) -> ast::MetaItem { - attr::mk_word_item(Ident::new(w, sp)) + // Builds `#[name = val]`. + // + // Note: `span` is used for both the identifer and the value. + pub fn attr_name_value_str(&self, name: Symbol, val: Symbol, span: Span) -> ast::Attribute { + let g = &self.sess.parse_sess.attr_id_generator; + attr::mk_attr_name_value_str(g, ast::AttrStyle::Outer, name, val, span) + } + + // Builds `#[outer(inner)]`. + pub fn attr_nested_word(&self, outer: Symbol, inner: Symbol, span: Span) -> ast::Attribute { + let g = &self.sess.parse_sess.attr_id_generator; + attr::mk_attr_nested_word(g, ast::AttrStyle::Outer, outer, inner, span) } } From 07b86a94c77483127a7b7371cb046e67d8e59279 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 28 Nov 2022 19:16:44 +0000 Subject: [PATCH 181/244] `rustc_ast_passes`: remove `ref` patterns --- .../rustc_ast_passes/src/ast_validation.rs | 95 +++++++++---------- compiler/rustc_ast_passes/src/feature_gate.rs | 20 ++-- 2 files changed, 55 insertions(+), 60 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index acd7eb69ffc37..e47c434454602 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -209,7 +209,7 @@ impl<'a> AstValidator<'a> { // Mirrors `visit::walk_ty`, but tracks relevant state. fn walk_ty(&mut self, t: &'a Ty) { - match t.kind { + match &t.kind { TyKind::ImplTrait(..) => { self.with_impl_trait(Some(t.span), |this| visit::walk_ty(this, t)) } @@ -217,7 +217,7 @@ impl<'a> AstValidator<'a> { .with_banned_tilde_const(DisallowTildeConstContext::TraitObject, |this| { visit::walk_ty(this, t) }), - TyKind::Path(ref qself, ref path) => { + TyKind::Path(qself, path) => { // We allow these: // - `Option` // - `option::Option` @@ -231,7 +231,7 @@ impl<'a> AstValidator<'a> { // (for cases like `::Foo>`) // but we allow `impl Trait` in `GenericArgs` // iff there are no more PathSegments. - if let Some(ref qself) = *qself { + if let Some(qself) = qself { // `impl Trait` in `qself` is always illegal self.with_banned_impl_trait(|this| this.visit_ty(&qself.ty)); } @@ -738,8 +738,8 @@ impl<'a> AstValidator<'a> { } fn visit_ty_common(&mut self, ty: &'a Ty) { - match ty.kind { - TyKind::BareFn(ref bfty) => { + match &ty.kind { + TyKind::BareFn(bfty) => { self.check_fn_decl(&bfty.decl, SelfSemantic::No); Self::check_decl_no_pat(&bfty.decl, |span, _, _| { struct_span_err!( @@ -756,10 +756,10 @@ impl<'a> AstValidator<'a> { self.maybe_lint_missing_abi(sig_span, ty.id); } } - TyKind::TraitObject(ref bounds, ..) => { + TyKind::TraitObject(bounds, ..) => { let mut any_lifetime_bounds = false; for bound in bounds { - if let GenericBound::Outlives(ref lifetime) = *bound { + if let GenericBound::Outlives(lifetime) = bound { if any_lifetime_bounds { struct_span_err!( self.session, @@ -774,7 +774,7 @@ impl<'a> AstValidator<'a> { } } } - TyKind::ImplTrait(_, ref bounds) => { + TyKind::ImplTrait(_, bounds) => { if self.is_impl_trait_banned { struct_span_err!( self.session, @@ -842,8 +842,8 @@ fn validate_generic_param_order( let (kind, bounds, span) = (¶m.kind, ¶m.bounds, ident.span); let (ord_kind, ident) = match ¶m.kind { GenericParamKind::Lifetime => (ParamKindOrd::Lifetime, ident.to_string()), - GenericParamKind::Type { default: _ } => (ParamKindOrd::TypeOrConst, ident.to_string()), - GenericParamKind::Const { ref ty, kw_span: _, default: _ } => { + GenericParamKind::Type { .. } => (ParamKindOrd::TypeOrConst, ident.to_string()), + GenericParamKind::Const { ty, .. } => { let ty = pprust::ty_to_string(ty); (ParamKindOrd::TypeOrConst, format!("const {}: {}", ident, ty)) } @@ -948,8 +948,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } ExprKind::Paren(local_expr) => { fn has_let_expr(expr: &Expr) -> bool { - match expr.kind { - ExprKind::Binary(_, ref lhs, ref rhs) => has_let_expr(lhs) || has_let_expr(rhs), + match &expr.kind { + ExprKind::Binary(_, lhs, rhs) => has_let_expr(lhs) || has_let_expr(rhs), ExprKind::Let(..) => true, _ => false, } @@ -1005,18 +1005,18 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self.check_nomangle_item_asciionly(item.ident, item.span); } - match item.kind { + match &item.kind { ItemKind::Impl(box Impl { unsafety, polarity, defaultness: _, constness, - ref generics, - of_trait: Some(ref t), - ref self_ty, - ref items, + generics, + of_trait: Some(t), + self_ty, + items, }) => { - self.with_in_trait_impl(true, Some(constness), |this| { + self.with_in_trait_impl(true, Some(*constness), |this| { this.invalid_visibility(&item.vis, None); if let TyKind::Err = self_ty.kind { this.err_handler() @@ -1027,7 +1027,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> { .help("use `auto trait Trait {}` instead") .emit(); } - if let (Unsafe::Yes(span), ImplPolarity::Negative(sp)) = (unsafety, polarity) { + if let (&Unsafe::Yes(span), &ImplPolarity::Negative(sp)) = (unsafety, polarity) + { struct_span_err!( this.session, sp.to(t.path.span), @@ -1061,7 +1062,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { constness, generics: _, of_trait: None, - ref self_ty, + self_ty, items: _, }) => { let error = |annotation_span, annotation| { @@ -1078,25 +1079,25 @@ impl<'a> Visitor<'a> for AstValidator<'a> { &item.vis, Some(InvalidVisibilityNote::IndividualImplItems), ); - if let Unsafe::Yes(span) = unsafety { + if let &Unsafe::Yes(span) = unsafety { error(span, "unsafe").code(error_code!(E0197)).emit(); } - if let ImplPolarity::Negative(span) = polarity { + if let &ImplPolarity::Negative(span) = polarity { error(span, "negative").emit(); } - if let Defaultness::Default(def_span) = defaultness { + if let &Defaultness::Default(def_span) = defaultness { error(def_span, "`default`") .note("only trait implementations may be annotated with `default`") .emit(); } - if let Const::Yes(span) = constness { + if let &Const::Yes(span) = constness { error(span, "`const`") .note("only trait implementations may be annotated with `const`") .emit(); } } - ItemKind::Fn(box Fn { defaultness, ref sig, ref generics, ref body }) => { - self.check_defaultness(item.span, defaultness); + ItemKind::Fn(box Fn { defaultness, sig, generics, body }) => { + self.check_defaultness(item.span, *defaultness); if body.is_none() { self.session.emit_err(FnWithoutBody { @@ -1132,7 +1133,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { &item.vis, Some(InvalidVisibilityNote::IndividualForeignItems), ); - if let Unsafe::Yes(span) = unsafety { + if let &Unsafe::Yes(span) = unsafety { self.err_handler().span_err(span, "extern block cannot be declared unsafe"); } if abi.is_none() { @@ -1142,7 +1143,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self.extern_mod = old_item; return; // Avoid visiting again. } - ItemKind::Enum(ref def, _) => { + ItemKind::Enum(def, _) => { for variant in &def.variants { self.invalid_visibility(&variant.vis, None); for field in variant.data.fields() { @@ -1150,8 +1151,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } } } - ItemKind::Trait(box Trait { is_auto, ref generics, ref bounds, ref items, .. }) => { - if is_auto == IsAuto::Yes { + ItemKind::Trait(box Trait { is_auto, generics, bounds, items, .. }) => { + if *is_auto == IsAuto::Yes { // Auto traits cannot have generics, super traits nor contain items. self.deny_generic_params(generics, item.ident.span); self.deny_super_traits(bounds, item.ident.span); @@ -1171,8 +1172,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> { walk_list!(self, visit_attribute, &item.attrs); return; // Avoid visiting again } - ItemKind::Mod(unsafety, ref mod_kind) => { - if let Unsafe::Yes(span) = unsafety { + ItemKind::Mod(unsafety, mod_kind) => { + if let &Unsafe::Yes(span) = unsafety { self.err_handler().span_err(span, "module cannot be declared unsafe"); } // Ensure that `path` attributes on modules are recorded as used (cf. issue #35584). @@ -1182,13 +1183,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self.check_mod_file_item_asciionly(item.ident); } } - ItemKind::Union(ref vdata, ..) => { + ItemKind::Union(vdata, ..) => { if vdata.fields().is_empty() { self.err_handler().span_err(item.span, "unions cannot have zero fields"); } } ItemKind::Const(def, .., None) => { - self.check_defaultness(item.span, def); + self.check_defaultness(item.span, *def); self.session.emit_err(ConstWithoutBody { span: item.span, replace_span: self.ending_semi_or_hi(item.span), @@ -1200,14 +1201,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> { replace_span: self.ending_semi_or_hi(item.span), }); } - ItemKind::TyAlias(box TyAlias { - defaultness, - where_clauses, - ref bounds, - ref ty, - .. - }) => { - self.check_defaultness(item.span, defaultness); + ItemKind::TyAlias(box TyAlias { defaultness, where_clauses, bounds, ty, .. }) => { + self.check_defaultness(item.span, *defaultness); if ty.is_none() { self.session.emit_err(TyAliasWithoutBody { span: item.span, @@ -1266,8 +1261,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> { // Mirrors `visit::walk_generic_args`, but tracks relevant state. fn visit_generic_args(&mut self, generic_args: &'a GenericArgs) { - match *generic_args { - GenericArgs::AngleBracketed(ref data) => { + match generic_args { + GenericArgs::AngleBracketed(data) => { self.check_generic_args_before_constraints(data); for arg in &data.args { @@ -1283,7 +1278,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } } } - GenericArgs::Parenthesized(ref data) => { + GenericArgs::Parenthesized(data) => { walk_list!(self, visit_ty, &data.inputs); if let FnRetTy::Ty(ty) = &data.output { // `-> Foo` syntax is essentially an associated type binding, @@ -1319,7 +1314,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { validate_generic_param_order(self.err_handler(), &generics.params, generics.span); for predicate in &generics.where_clause.predicates { - if let WherePredicate::EqPredicate(ref predicate) = *predicate { + if let WherePredicate::EqPredicate(predicate) = predicate { deny_equality_constraints(self, predicate, generics); } } @@ -1368,7 +1363,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } fn visit_param_bound(&mut self, bound: &'a GenericBound, ctxt: BoundKind) { - if let GenericBound::Trait(ref poly, modify) = *bound { + if let GenericBound::Trait(poly, modify) = bound { match (ctxt, modify) { (BoundKind::SuperTraits, TraitBoundModifier::Maybe) => { let mut err = self @@ -1573,8 +1568,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self.check_item_named(item.ident, "const"); } - match item.kind { - AssocItemKind::Type(box TyAlias { ref generics, ref bounds, ref ty, .. }) + match &item.kind { + AssocItemKind::Type(box TyAlias { generics, bounds, ty, .. }) if ctxt == AssocCtxt::Trait => { self.visit_vis(&item.vis); @@ -1586,7 +1581,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { }); walk_list!(self, visit_ty, ty); } - AssocItemKind::Fn(box Fn { ref sig, ref generics, ref body, .. }) + AssocItemKind::Fn(box Fn { sig, generics, body, .. }) if self.in_const_trait_impl || ctxt == AssocCtxt::Trait || matches!(sig.header.constness, Const::Yes(_)) => diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index 546010135a725..32f45f8b59e90 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -198,8 +198,8 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } fn visit_item(&mut self, i: &'a ast::Item) { - match i.kind { - ast::ItemKind::ForeignMod(ref foreign_module) => { + match &i.kind { + ast::ItemKind::ForeignMod(foreign_module) => { if let Some(abi) = foreign_module.abi { self.check_abi(abi, ast::Const::No); } @@ -233,8 +233,8 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } } - ast::ItemKind::Impl(box ast::Impl { polarity, defaultness, ref of_trait, .. }) => { - if let ast::ImplPolarity::Negative(span) = polarity { + ast::ItemKind::Impl(box ast::Impl { polarity, defaultness, of_trait, .. }) => { + if let &ast::ImplPolarity::Negative(span) = polarity { gate_feature_post!( &self, negative_impls, @@ -267,7 +267,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { gate_feature_post!(&self, decl_macro, i.span, msg); } - ast::ItemKind::TyAlias(box ast::TyAlias { ty: Some(ref ty), .. }) => { + ast::ItemKind::TyAlias(box ast::TyAlias { ty: Some(ty), .. }) => { self.check_impl_trait(&ty) } @@ -302,8 +302,8 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } fn visit_ty(&mut self, ty: &'a ast::Ty) { - match ty.kind { - ast::TyKind::BareFn(ref bare_fn_ty) => { + match &ty.kind { + ast::TyKind::BareFn(bare_fn_ty) => { // Function pointers cannot be `const` self.check_extern(bare_fn_ty.ext, ast::Const::No); } @@ -319,7 +319,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } fn visit_fn_ret_ty(&mut self, ret_ty: &'a ast::FnRetTy) { - if let ast::FnRetTy::Ty(ref output_ty) = *ret_ty { + if let ast::FnRetTy::Ty(output_ty) = ret_ty { if let ast::TyKind::Never = output_ty.kind { // Do nothing. } else { @@ -455,9 +455,9 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } fn visit_assoc_item(&mut self, i: &'a ast::AssocItem, ctxt: AssocCtxt) { - let is_fn = match i.kind { + let is_fn = match &i.kind { ast::AssocItemKind::Fn(_) => true, - ast::AssocItemKind::Type(box ast::TyAlias { ref ty, .. }) => { + ast::AssocItemKind::Type(box ast::TyAlias { ty, .. }) => { if let (Some(_), AssocCtxt::Trait) = (ty, ctxt) { gate_feature_post!( &self, From 56ae9a1746f266097eb51cd958f31c406b6eb233 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Tue, 29 Nov 2022 08:08:26 +0000 Subject: [PATCH 182/244] `rustc_ast_pretty`: remove `ref` patterns --- compiler/rustc_ast_pretty/src/pprust/state.rs | 175 +++++++++--------- .../rustc_ast_pretty/src/pprust/state/expr.rs | 144 +++++++------- .../rustc_ast_pretty/src/pprust/state/item.rs | 111 +++++------ 3 files changed, 214 insertions(+), 216 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index 7a9243c511b92..0fe1045c244cb 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -11,7 +11,7 @@ use rustc_ast::tokenstream::{TokenStream, TokenTree}; use rustc_ast::util::classify; use rustc_ast::util::comments::{gather_comments, Comment, CommentStyle}; use rustc_ast::util::parser; -use rustc_ast::{self as ast, AttrArgs, AttrArgsEq, BlockCheckMode, Mutability, PatKind}; +use rustc_ast::{self as ast, AttrArgs, AttrArgsEq, BlockCheckMode, PatKind}; use rustc_ast::{attr, BindingAnnotation, ByRef, DelimArgs, RangeEnd, RangeSyntax, Term}; use rustc_ast::{GenericArg, GenericBound, SelfKind, TraitBoundModifier}; use rustc_ast::{InlineAsmOperand, InlineAsmRegOrRegClass}; @@ -64,6 +64,7 @@ impl<'a> Comments<'a> { Comments { sm, comments, current: 0 } } + // This shouldn't probably clone lmao pub fn next(&self) -> Option { self.comments.get(self.current).cloned() } @@ -268,10 +269,10 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere fn maybe_print_comment(&mut self, pos: BytePos) -> bool { let mut has_comment = false; - while let Some(ref cmnt) = self.next_comment() { + while let Some(cmnt) = self.next_comment() { if cmnt.pos < pos { has_comment = true; - self.print_comment(cmnt); + self.print_comment(&cmnt); } else { break; } @@ -366,8 +367,8 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere if self.next_comment().is_none() { self.hardbreak(); } - while let Some(ref cmnt) = self.next_comment() { - self.print_comment(cmnt) + while let Some(cmnt) = self.next_comment() { + self.print_comment(&cmnt) } } @@ -446,8 +447,8 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere self.hardbreak_if_not_bol(); } self.maybe_print_comment(attr.span.lo()); - match attr.kind { - ast::AttrKind::Normal(ref normal) => { + match &attr.kind { + ast::AttrKind::Normal(normal) => { match attr.style { ast::AttrStyle::Inner => self.word("#!["), ast::AttrStyle::Outer => self.word("#["), @@ -456,7 +457,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere self.word("]"); } ast::AttrKind::DocComment(comment_kind, data) => { - self.word(doc_comment_to_string(comment_kind, attr.style, data)); + self.word(doc_comment_to_string(*comment_kind, attr.style, *data)); self.hardbreak() } } @@ -497,22 +498,22 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere fn print_meta_list_item(&mut self, item: &ast::NestedMetaItem) { match item { - ast::NestedMetaItem::MetaItem(ref mi) => self.print_meta_item(mi), - ast::NestedMetaItem::Lit(ref lit) => self.print_meta_item_lit(lit), + ast::NestedMetaItem::MetaItem(mi) => self.print_meta_item(mi), + ast::NestedMetaItem::Lit(lit) => self.print_meta_item_lit(lit), } } fn print_meta_item(&mut self, item: &ast::MetaItem) { self.ibox(INDENT_UNIT); - match item.kind { + match &item.kind { ast::MetaItemKind::Word => self.print_path(&item.path, false, 0), - ast::MetaItemKind::NameValue(ref value) => { + ast::MetaItemKind::NameValue(value) => { self.print_path(&item.path, false, 0); self.space(); self.word_space("="); self.print_meta_item_lit(value); } - ast::MetaItemKind::List(ref items) => { + ast::MetaItemKind::List(items) => { self.print_path(&item.path, false, 0); self.popen(); self.commasep(Consistent, &items, |s, i| s.print_meta_list_item(i)); @@ -657,7 +658,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere fn print_path_segment(&mut self, segment: &ast::PathSegment, colons_before_params: bool) { if segment.ident.name != kw::PathRoot { self.print_ident(segment.ident); - if let Some(ref args) = segment.args { + if let Some(args) = &segment.args { self.print_generic_args(args, colons_before_params); } } @@ -712,19 +713,19 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere } fn nonterminal_to_string(&self, nt: &Nonterminal) -> String { - match *nt { - token::NtExpr(ref e) => self.expr_to_string(e), - token::NtMeta(ref e) => self.attr_item_to_string(e), - token::NtTy(ref e) => self.ty_to_string(e), - token::NtPath(ref e) => self.path_to_string(e), - token::NtItem(ref e) => self.item_to_string(e), - token::NtBlock(ref e) => self.block_to_string(e), - token::NtStmt(ref e) => self.stmt_to_string(e), - token::NtPat(ref e) => self.pat_to_string(e), - token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(e, is_raw).to_string(), + match nt { + token::NtExpr(e) => self.expr_to_string(e), + token::NtMeta(e) => self.attr_item_to_string(e), + token::NtTy(e) => self.ty_to_string(e), + token::NtPath(e) => self.path_to_string(e), + token::NtItem(e) => self.item_to_string(e), + token::NtBlock(e) => self.block_to_string(e), + token::NtStmt(e) => self.stmt_to_string(e), + token::NtPat(e) => self.pat_to_string(e), + token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(*e, *is_raw).to_string(), token::NtLifetime(e) => e.to_string(), - token::NtLiteral(ref e) => self.expr_to_string(e), - token::NtVis(ref e) => self.vis_to_string(e), + token::NtLiteral(e) => self.expr_to_string(e), + token::NtVis(e) => self.vis_to_string(e), } } @@ -917,8 +918,8 @@ impl<'a> PrintState<'a> for State<'a> { self.word("::") } - match *args { - ast::GenericArgs::AngleBracketed(ref data) => { + match args { + ast::GenericArgs::AngleBracketed(data) => { self.word("<"); self.commasep(Inconsistent, &data.args, |s, arg| match arg { ast::AngleBracketedArg::Arg(a) => s.print_generic_arg(a), @@ -927,7 +928,7 @@ impl<'a> PrintState<'a> for State<'a> { self.word(">") } - ast::GenericArgs::Parenthesized(ref data) => { + ast::GenericArgs::Parenthesized(data) => { self.word("("); self.commasep(Inconsistent, &data.inputs, |s, ty| s.print_type(ty)); self.word(")"); @@ -1011,17 +1012,17 @@ impl<'a> State<'a> { pub fn print_type(&mut self, ty: &ast::Ty) { self.maybe_print_comment(ty.span.lo()); self.ibox(0); - match ty.kind { - ast::TyKind::Slice(ref ty) => { + match &ty.kind { + ast::TyKind::Slice(ty) => { self.word("["); self.print_type(ty); self.word("]"); } - ast::TyKind::Ptr(ref mt) => { + ast::TyKind::Ptr(mt) => { self.word("*"); self.print_mt(mt, true); } - ast::TyKind::Rptr(ref lifetime, ref mt) => { + ast::TyKind::Rptr(lifetime, mt) => { self.word("&"); self.print_opt_lifetime(lifetime); self.print_mt(mt, false); @@ -1029,7 +1030,7 @@ impl<'a> State<'a> { ast::TyKind::Never => { self.word("!"); } - ast::TyKind::Tup(ref elts) => { + ast::TyKind::Tup(elts) => { self.popen(); self.commasep(Inconsistent, &elts, |s, ty| s.print_type(ty)); if elts.len() == 1 { @@ -1037,36 +1038,36 @@ impl<'a> State<'a> { } self.pclose(); } - ast::TyKind::Paren(ref typ) => { + ast::TyKind::Paren(typ) => { self.popen(); self.print_type(typ); self.pclose(); } - ast::TyKind::BareFn(ref f) => { + ast::TyKind::BareFn(f) => { self.print_ty_fn(f.ext, f.unsafety, &f.decl, None, &f.generic_params); } - ast::TyKind::Path(None, ref path) => { + ast::TyKind::Path(None, path) => { self.print_path(path, false, 0); } - ast::TyKind::Path(Some(ref qself), ref path) => self.print_qpath(path, qself, false), - ast::TyKind::TraitObject(ref bounds, syntax) => { - if syntax == ast::TraitObjectSyntax::Dyn { + ast::TyKind::Path(Some(qself), path) => self.print_qpath(path, qself, false), + ast::TyKind::TraitObject(bounds, syntax) => { + if *syntax == ast::TraitObjectSyntax::Dyn { self.word_nbsp("dyn"); } self.print_type_bounds(bounds); } - ast::TyKind::ImplTrait(_, ref bounds) => { + ast::TyKind::ImplTrait(_, bounds) => { self.word_nbsp("impl"); self.print_type_bounds(bounds); } - ast::TyKind::Array(ref ty, ref length) => { + ast::TyKind::Array(ty, length) => { self.word("["); self.print_type(ty); self.word("; "); self.print_expr(&length.value); self.word("]"); } - ast::TyKind::Typeof(ref e) => { + ast::TyKind::Typeof(e) => { self.word("typeof("); self.print_expr(&e.value); self.word(")"); @@ -1082,7 +1083,7 @@ impl<'a> State<'a> { ast::TyKind::ImplicitSelf => { self.word("Self"); } - ast::TyKind::MacCall(ref m) => { + ast::TyKind::MacCall(m) => { self.print_mac(m); } ast::TyKind::CVarArgs => { @@ -1111,8 +1112,8 @@ impl<'a> State<'a> { pub(crate) fn print_stmt(&mut self, st: &ast::Stmt) { self.maybe_print_comment(st.span.lo()); - match st.kind { - ast::StmtKind::Local(ref loc) => { + match &st.kind { + ast::StmtKind::Local(loc) => { self.print_outer_attributes(&loc.attrs); self.space_if_not_bol(); self.ibox(INDENT_UNIT); @@ -1135,15 +1136,15 @@ impl<'a> State<'a> { self.word(";"); self.end(); // `let` ibox } - ast::StmtKind::Item(ref item) => self.print_item(item), - ast::StmtKind::Expr(ref expr) => { + ast::StmtKind::Item(item) => self.print_item(item), + ast::StmtKind::Expr(expr) => { self.space_if_not_bol(); self.print_expr_outer_attr_style(expr, false); if classify::expr_requires_semi_to_be_stmt(expr) { self.word(";"); } } - ast::StmtKind::Semi(ref expr) => { + ast::StmtKind::Semi(expr) => { self.space_if_not_bol(); self.print_expr_outer_attr_style(expr, false); self.word(";"); @@ -1152,7 +1153,7 @@ impl<'a> State<'a> { self.space_if_not_bol(); self.word(";"); } - ast::StmtKind::MacCall(ref mac) => { + ast::StmtKind::MacCall(mac) => { self.space_if_not_bol(); self.print_outer_attributes(&mac.attrs); self.print_mac(&mac.mac); @@ -1193,8 +1194,8 @@ impl<'a> State<'a> { let has_attrs = self.print_inner_attributes(attrs); for (i, st) in blk.stmts.iter().enumerate() { - match st.kind { - ast::StmtKind::Expr(ref expr) if i == blk.stmts.len() - 1 => { + match &st.kind { + ast::StmtKind::Expr(expr) if i == blk.stmts.len() - 1 => { self.maybe_print_comment(st.span.lo()); self.space_if_not_bol(); self.print_expr_outer_attr_style(expr, false); @@ -1362,7 +1363,7 @@ impl<'a> State<'a> { pub(crate) fn print_local_decl(&mut self, loc: &ast::Local) { self.print_pat(&loc.pat); - if let Some(ref ty) = loc.ty { + if let Some(ty) = &loc.ty { self.word_space(":"); self.print_type(ty); } @@ -1386,7 +1387,7 @@ impl<'a> State<'a> { for item_segment in &path.segments[qself.position..] { self.word("::"); self.print_ident(item_segment.ident); - if let Some(ref args) = item_segment.args { + if let Some(args) = &item_segment.args { self.print_generic_args(args, colons_before_params) } } @@ -1397,23 +1398,23 @@ impl<'a> State<'a> { self.ann.pre(self, AnnNode::Pat(pat)); /* Pat isn't normalized, but the beauty of it is that it doesn't matter */ - match pat.kind { + match &pat.kind { PatKind::Wild => self.word("_"), - PatKind::Ident(BindingAnnotation(by_ref, mutbl), ident, ref sub) => { - if by_ref == ByRef::Yes { + PatKind::Ident(BindingAnnotation(by_ref, mutbl), ident, sub) => { + if *by_ref == ByRef::Yes { self.word_nbsp("ref"); } - if mutbl == Mutability::Mut { + if mutbl.is_mut() { self.word_nbsp("mut"); } - self.print_ident(ident); - if let Some(ref p) = *sub { + self.print_ident(*ident); + if let Some(p) = sub { self.space(); self.word_space("@"); self.print_pat(p); } } - PatKind::TupleStruct(ref qself, ref path, ref elts) => { + PatKind::TupleStruct(qself, path, elts) => { if let Some(qself) = qself { self.print_qpath(path, qself, true); } else { @@ -1423,16 +1424,16 @@ impl<'a> State<'a> { self.commasep(Inconsistent, &elts, |s, p| s.print_pat(p)); self.pclose(); } - PatKind::Or(ref pats) => { + PatKind::Or(pats) => { self.strsep("|", true, Inconsistent, &pats, |s, p| s.print_pat(p)); } - PatKind::Path(None, ref path) => { + PatKind::Path(None, path) => { self.print_path(path, true, 0); } - PatKind::Path(Some(ref qself), ref path) => { + PatKind::Path(Some(qself), path) => { self.print_qpath(path, qself, false); } - PatKind::Struct(ref qself, ref path, ref fields, etc) => { + PatKind::Struct(qself, path, fields, etc) => { if let Some(qself) = qself { self.print_qpath(path, qself, true); } else { @@ -1458,7 +1459,7 @@ impl<'a> State<'a> { }, |f| f.pat.span, ); - if etc { + if *etc { if !fields.is_empty() { self.word_space(","); } @@ -1469,7 +1470,7 @@ impl<'a> State<'a> { } self.word("}"); } - PatKind::Tuple(ref elts) => { + PatKind::Tuple(elts) => { self.popen(); self.commasep(Inconsistent, &elts, |s, p| s.print_pat(p)); if elts.len() == 1 { @@ -1477,13 +1478,13 @@ impl<'a> State<'a> { } self.pclose(); } - PatKind::Box(ref inner) => { + PatKind::Box(inner) => { self.word("box "); self.print_pat(inner); } - PatKind::Ref(ref inner, mutbl) => { + PatKind::Ref(inner, mutbl) => { self.word("&"); - if mutbl == Mutability::Mut { + if mutbl.is_mut() { self.word("mut "); } if let PatKind::Ident(ast::BindingAnnotation::MUT, ..) = inner.kind { @@ -1494,12 +1495,12 @@ impl<'a> State<'a> { self.print_pat(inner); } } - PatKind::Lit(ref e) => self.print_expr(&**e), - PatKind::Range(ref begin, ref end, Spanned { node: ref end_kind, .. }) => { + PatKind::Lit(e) => self.print_expr(&**e), + PatKind::Range(begin, end, Spanned { node: end_kind, .. }) => { if let Some(e) = begin { self.print_expr(e); } - match *end_kind { + match end_kind { RangeEnd::Included(RangeSyntax::DotDotDot) => self.word("..."), RangeEnd::Included(RangeSyntax::DotDotEq) => self.word("..="), RangeEnd::Excluded => self.word(".."), @@ -1508,36 +1509,36 @@ impl<'a> State<'a> { self.print_expr(e); } } - PatKind::Slice(ref elts) => { + PatKind::Slice(elts) => { self.word("["); self.commasep(Inconsistent, &elts, |s, p| s.print_pat(p)); self.word("]"); } PatKind::Rest => self.word(".."), - PatKind::Paren(ref inner) => { + PatKind::Paren(inner) => { self.popen(); self.print_pat(inner); self.pclose(); } - PatKind::MacCall(ref m) => self.print_mac(m), + PatKind::MacCall(m) => self.print_mac(m), } self.ann.post(self, AnnNode::Pat(pat)) } fn print_explicit_self(&mut self, explicit_self: &ast::ExplicitSelf) { - match explicit_self.node { + match &explicit_self.node { SelfKind::Value(m) => { - self.print_mutability(m, false); + self.print_mutability(*m, false); self.word("self") } - SelfKind::Region(ref lt, m) => { + SelfKind::Region(lt, m) => { self.word("&"); self.print_opt_lifetime(lt); - self.print_mutability(m, false); + self.print_mutability(*m, false); self.word("self") } - SelfKind::Explicit(ref typ, m) => { - self.print_mutability(m, false); + SelfKind::Explicit(typ, m) => { + self.print_mutability(*m, false); self.word("self"); self.word_space(":"); self.print_type(typ) @@ -1599,7 +1600,7 @@ impl<'a> State<'a> { self.commasep(Inconsistent, &generic_params, |s, param| { s.print_outer_attributes_inline(¶m.attrs); - match param.kind { + match ¶m.kind { ast::GenericParamKind::Lifetime => { let lt = ast::Lifetime { id: param.id, ident: param.ident }; s.print_lifetime(lt); @@ -1608,19 +1609,19 @@ impl<'a> State<'a> { s.print_lifetime_bounds(¶m.bounds) } } - ast::GenericParamKind::Type { ref default } => { + ast::GenericParamKind::Type { default } => { s.print_ident(param.ident); if !param.bounds.is_empty() { s.word_nbsp(":"); s.print_type_bounds(¶m.bounds); } - if let Some(ref default) = default { + if let Some(default) = default { s.space(); s.word_space("="); s.print_type(default) } } - ast::GenericParamKind::Const { ref ty, kw_span: _, ref default } => { + ast::GenericParamKind::Const { ty, default, .. } => { s.word_space("const"); s.print_ident(param.ident); s.space(); @@ -1630,7 +1631,7 @@ impl<'a> State<'a> { s.word_nbsp(":"); s.print_type_bounds(¶m.bounds); } - if let Some(ref default) = default { + if let Some(default) = default { s.space(); s.word_space("="); s.print_expr(&default.value); diff --git a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs index f4d77549eff4c..81483ac30d1de 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs @@ -8,9 +8,9 @@ use rustc_ast::{self as ast, BlockCheckMode}; impl<'a> State<'a> { fn print_else(&mut self, els: Option<&ast::Expr>) { if let Some(_else) = els { - match _else.kind { + match &_else.kind { // Another `else if` block. - ast::ExprKind::If(ref i, ref then, ref e) => { + ast::ExprKind::If(i, then, e) => { self.cbox(INDENT_UNIT - 1); self.ibox(0); self.word(" else if "); @@ -20,7 +20,7 @@ impl<'a> State<'a> { self.print_else(e.as_deref()) } // Final `else` block. - ast::ExprKind::Block(ref b, _) => { + ast::ExprKind::Block(b, _) => { self.cbox(INDENT_UNIT - 1); self.ibox(0); self.word(" else "); @@ -202,7 +202,7 @@ impl<'a> State<'a> { self.print_expr_maybe_paren(receiver, parser::PREC_POSTFIX); self.word("."); self.print_ident(segment.ident); - if let Some(ref args) = segment.args { + if let Some(args) = &segment.args { self.print_generic_args(args, true); } self.print_call_post(base_args) @@ -284,73 +284,66 @@ impl<'a> State<'a> { self.ibox(INDENT_UNIT); self.ann.pre(self, AnnNode::Expr(expr)); - match expr.kind { - ast::ExprKind::Box(ref expr) => { + match &expr.kind { + ast::ExprKind::Box(expr) => { self.word_space("box"); self.print_expr_maybe_paren(expr, parser::PREC_PREFIX); } - ast::ExprKind::Array(ref exprs) => { + ast::ExprKind::Array(exprs) => { self.print_expr_vec(exprs); } - ast::ExprKind::ConstBlock(ref anon_const) => { + ast::ExprKind::ConstBlock(anon_const) => { self.print_expr_anon_const(anon_const, attrs); } - ast::ExprKind::Repeat(ref element, ref count) => { + ast::ExprKind::Repeat(element, count) => { self.print_expr_repeat(element, count); } - ast::ExprKind::Struct(ref se) => { + ast::ExprKind::Struct(se) => { self.print_expr_struct(&se.qself, &se.path, &se.fields, &se.rest); } - ast::ExprKind::Tup(ref exprs) => { + ast::ExprKind::Tup(exprs) => { self.print_expr_tup(exprs); } - ast::ExprKind::Call(ref func, ref args) => { + ast::ExprKind::Call(func, args) => { self.print_expr_call(func, &args); } - ast::ExprKind::MethodCall(box ast::MethodCall { - ref seg, - ref receiver, - ref args, - .. - }) => { + ast::ExprKind::MethodCall(box ast::MethodCall { seg, receiver, args, .. }) => { self.print_expr_method_call(seg, &receiver, &args); } - ast::ExprKind::Binary(op, ref lhs, ref rhs) => { - self.print_expr_binary(op, lhs, rhs); + ast::ExprKind::Binary(op, lhs, rhs) => { + self.print_expr_binary(*op, lhs, rhs); } - ast::ExprKind::Unary(op, ref expr) => { - self.print_expr_unary(op, expr); + ast::ExprKind::Unary(op, expr) => { + self.print_expr_unary(*op, expr); } - ast::ExprKind::AddrOf(k, m, ref expr) => { - self.print_expr_addr_of(k, m, expr); + ast::ExprKind::AddrOf(k, m, expr) => { + self.print_expr_addr_of(*k, *m, expr); } ast::ExprKind::Lit(token_lit) => { - self.print_token_literal(token_lit, expr.span); + self.print_token_literal(*token_lit, expr.span); } - ast::ExprKind::IncludedBytes(ref bytes) => { + ast::ExprKind::IncludedBytes(bytes) => { let lit = ast::LitKind::ByteStr(bytes.clone()).to_token_lit(); self.print_token_literal(lit, expr.span) } - ast::ExprKind::Cast(ref expr, ref ty) => { + ast::ExprKind::Cast(expr, ty) => { let prec = AssocOp::As.precedence() as i8; self.print_expr_maybe_paren(expr, prec); self.space(); self.word_space("as"); self.print_type(ty); } - ast::ExprKind::Type(ref expr, ref ty) => { + ast::ExprKind::Type(expr, ty) => { let prec = AssocOp::Colon.precedence() as i8; self.print_expr_maybe_paren(expr, prec); self.word_space(":"); self.print_type(ty); } - ast::ExprKind::Let(ref pat, ref scrutinee, _) => { + ast::ExprKind::Let(pat, scrutinee, _) => { self.print_let(pat, scrutinee); } - ast::ExprKind::If(ref test, ref blk, ref elseopt) => { - self.print_if(test, blk, elseopt.as_deref()) - } - ast::ExprKind::While(ref test, ref blk, opt_label) => { + ast::ExprKind::If(test, blk, elseopt) => self.print_if(test, blk, elseopt.as_deref()), + ast::ExprKind::While(test, blk, opt_label) => { if let Some(label) = opt_label { self.print_ident(label.ident); self.word_space(":"); @@ -362,7 +355,7 @@ impl<'a> State<'a> { self.space(); self.print_block_with_attrs(blk, attrs); } - ast::ExprKind::ForLoop(ref pat, ref iter, ref blk, opt_label) => { + ast::ExprKind::ForLoop(pat, iter, blk, opt_label) => { if let Some(label) = opt_label { self.print_ident(label.ident); self.word_space(":"); @@ -377,7 +370,7 @@ impl<'a> State<'a> { self.space(); self.print_block_with_attrs(blk, attrs); } - ast::ExprKind::Loop(ref blk, opt_label, _) => { + ast::ExprKind::Loop(blk, opt_label, _) => { if let Some(label) = opt_label { self.print_ident(label.ident); self.word_space(":"); @@ -387,7 +380,7 @@ impl<'a> State<'a> { self.word_nbsp("loop"); self.print_block_with_attrs(blk, attrs); } - ast::ExprKind::Match(ref expr, ref arms) => { + ast::ExprKind::Match(expr, arms) => { self.cbox(0); self.ibox(0); self.word_nbsp("match"); @@ -402,18 +395,18 @@ impl<'a> State<'a> { self.bclose(expr.span, empty); } ast::ExprKind::Closure(box ast::Closure { - ref binder, + binder, capture_clause, asyncness, movability, - ref fn_decl, - ref body, + fn_decl, + body, fn_decl_span: _, }) => { self.print_closure_binder(binder); - self.print_movability(movability); - self.print_asyncness(asyncness); - self.print_capture_clause(capture_clause); + self.print_movability(*movability); + self.print_asyncness(*asyncness); + self.print_capture_clause(*capture_clause); self.print_fn_params_and_ret(fn_decl, true); self.space(); @@ -425,7 +418,7 @@ impl<'a> State<'a> { // empty box to satisfy the close. self.ibox(0); } - ast::ExprKind::Block(ref blk, opt_label) => { + ast::ExprKind::Block(blk, opt_label) => { if let Some(label) = opt_label { self.print_ident(label.ident); self.word_space(":"); @@ -436,26 +429,26 @@ impl<'a> State<'a> { self.ibox(0); self.print_block_with_attrs(blk, attrs); } - ast::ExprKind::Async(capture_clause, _, ref blk) => { + ast::ExprKind::Async(capture_clause, _, blk) => { self.word_nbsp("async"); - self.print_capture_clause(capture_clause); + self.print_capture_clause(*capture_clause); // cbox/ibox in analogy to the `ExprKind::Block` arm above self.cbox(0); self.ibox(0); self.print_block_with_attrs(blk, attrs); } - ast::ExprKind::Await(ref expr) => { + ast::ExprKind::Await(expr) => { self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX); self.word(".await"); } - ast::ExprKind::Assign(ref lhs, ref rhs, _) => { + ast::ExprKind::Assign(lhs, rhs, _) => { let prec = AssocOp::Assign.precedence() as i8; self.print_expr_maybe_paren(lhs, prec + 1); self.space(); self.word_space("="); self.print_expr_maybe_paren(rhs, prec); } - ast::ExprKind::AssignOp(op, ref lhs, ref rhs) => { + ast::ExprKind::AssignOp(op, lhs, rhs) => { let prec = AssocOp::Assign.precedence() as i8; self.print_expr_maybe_paren(lhs, prec + 1); self.space(); @@ -463,45 +456,44 @@ impl<'a> State<'a> { self.word_space("="); self.print_expr_maybe_paren(rhs, prec); } - ast::ExprKind::Field(ref expr, ident) => { + ast::ExprKind::Field(expr, ident) => { self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX); self.word("."); - self.print_ident(ident); + self.print_ident(*ident); } - ast::ExprKind::Index(ref expr, ref index) => { + ast::ExprKind::Index(expr, index) => { self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX); self.word("["); self.print_expr(index); self.word("]"); } - ast::ExprKind::Range(ref start, ref end, limits) => { + ast::ExprKind::Range(start, end, limits) => { // Special case for `Range`. `AssocOp` claims that `Range` has higher precedence // than `Assign`, but `x .. x = x` gives a parse error instead of `x .. (x = x)`. // Here we use a fake precedence value so that any child with lower precedence than // a "normal" binop gets parenthesized. (`LOr` is the lowest-precedence binop.) let fake_prec = AssocOp::LOr.precedence() as i8; - if let Some(ref e) = *start { + if let Some(e) = start { self.print_expr_maybe_paren(e, fake_prec); } - if limits == ast::RangeLimits::HalfOpen { - self.word(".."); - } else { - self.word("..="); + match limits { + ast::RangeLimits::HalfOpen => self.word(".."), + ast::RangeLimits::Closed => self.word("..="), } - if let Some(ref e) = *end { + if let Some(e) = end { self.print_expr_maybe_paren(e, fake_prec); } } ast::ExprKind::Underscore => self.word("_"), - ast::ExprKind::Path(None, ref path) => self.print_path(path, true, 0), - ast::ExprKind::Path(Some(ref qself), ref path) => self.print_qpath(path, qself, true), - ast::ExprKind::Break(opt_label, ref opt_expr) => { + ast::ExprKind::Path(None, path) => self.print_path(path, true, 0), + ast::ExprKind::Path(Some(qself), path) => self.print_qpath(path, qself, true), + ast::ExprKind::Break(opt_label, opt_expr) => { self.word("break"); if let Some(label) = opt_label { self.space(); self.print_ident(label.ident); } - if let Some(ref expr) = *opt_expr { + if let Some(expr) = opt_expr { self.space(); self.print_expr_maybe_paren(expr, parser::PREC_JUMP); } @@ -513,45 +505,45 @@ impl<'a> State<'a> { self.print_ident(label.ident); } } - ast::ExprKind::Ret(ref result) => { + ast::ExprKind::Ret(result) => { self.word("return"); - if let Some(ref expr) = *result { + if let Some(expr) = result { self.word(" "); self.print_expr_maybe_paren(expr, parser::PREC_JUMP); } } - ast::ExprKind::Yeet(ref result) => { + ast::ExprKind::Yeet(result) => { self.word("do"); self.word(" "); self.word("yeet"); - if let Some(ref expr) = *result { + if let Some(expr) = result { self.word(" "); self.print_expr_maybe_paren(expr, parser::PREC_JUMP); } } - ast::ExprKind::InlineAsm(ref a) => { + ast::ExprKind::InlineAsm(a) => { self.word("asm!"); self.print_inline_asm(a); } - ast::ExprKind::MacCall(ref m) => self.print_mac(m), - ast::ExprKind::Paren(ref e) => { + ast::ExprKind::MacCall(m) => self.print_mac(m), + ast::ExprKind::Paren(e) => { self.popen(); self.print_expr(e); self.pclose(); } - ast::ExprKind::Yield(ref e) => { + ast::ExprKind::Yield(e) => { self.word("yield"); - if let Some(ref expr) = *e { + if let Some(expr) = e { self.space(); self.print_expr_maybe_paren(expr, parser::PREC_JUMP); } } - ast::ExprKind::Try(ref e) => { + ast::ExprKind::Try(e) => { self.print_expr_maybe_paren(e, parser::PREC_POSTFIX); self.word("?") } - ast::ExprKind::TryBlock(ref blk) => { + ast::ExprKind::TryBlock(blk) => { self.cbox(0); self.ibox(0); self.word_nbsp("try"); @@ -578,15 +570,15 @@ impl<'a> State<'a> { self.print_outer_attributes(&arm.attrs); self.print_pat(&arm.pat); self.space(); - if let Some(ref e) = arm.guard { + if let Some(e) = &arm.guard { self.word_space("if"); self.print_expr(e); self.space(); } self.word_space("=>"); - match arm.body.kind { - ast::ExprKind::Block(ref blk, opt_label) => { + match &arm.body.kind { + ast::ExprKind::Block(blk, opt_label) => { if let Some(label) = opt_label { self.print_ident(label.ident); self.word_space(":"); diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs index 9c4425701e039..c52f15401abe3 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/item.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs @@ -136,10 +136,10 @@ impl<'a> State<'a> { self.maybe_print_comment(item.span.lo()); self.print_outer_attributes(&item.attrs); self.ann.pre(self, AnnNode::Item(item)); - match item.kind { + match &item.kind { ast::ItemKind::ExternCrate(orig_name) => { self.head(visibility_qualified(&item.vis, "extern crate")); - if let Some(orig_name) = orig_name { + if let &Some(orig_name) = orig_name { self.print_name(orig_name); self.space(); self.word("as"); @@ -150,35 +150,41 @@ impl<'a> State<'a> { self.end(); // end inner head-block self.end(); // end outer head-block } - ast::ItemKind::Use(ref tree) => { + ast::ItemKind::Use(tree) => { self.print_visibility(&item.vis); self.word_nbsp("use"); self.print_use_tree(tree); self.word(";"); } - ast::ItemKind::Static(ref ty, mutbl, ref body) => { + ast::ItemKind::Static(ty, mutbl, body) => { let def = ast::Defaultness::Final; - self.print_item_const(item.ident, Some(mutbl), ty, body.as_deref(), &item.vis, def); + self.print_item_const( + item.ident, + Some(*mutbl), + ty, + body.as_deref(), + &item.vis, + def, + ); } - ast::ItemKind::Const(def, ref ty, ref body) => { - self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis, def); + ast::ItemKind::Const(def, ty, body) => { + self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis, *def); } - ast::ItemKind::Fn(box ast::Fn { defaultness, ref sig, ref generics, ref body }) => { - let body = body.as_deref(); + ast::ItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => { self.print_fn_full( sig, item.ident, generics, &item.vis, - defaultness, - body, + *defaultness, + body.as_deref(), &item.attrs, ); } - ast::ItemKind::Mod(unsafety, ref mod_kind) => { + ast::ItemKind::Mod(unsafety, mod_kind) => { self.head(Self::to_string(|s| { s.print_visibility(&item.vis); - s.print_unsafety(unsafety); + s.print_unsafety(*unsafety); s.word("mod"); })); self.print_ident(item.ident); @@ -201,7 +207,7 @@ impl<'a> State<'a> { } } } - ast::ItemKind::ForeignMod(ref nmod) => { + ast::ItemKind::ForeignMod(nmod) => { self.head(Self::to_string(|s| { s.print_unsafety(nmod.unsafety); s.word("extern"); @@ -215,7 +221,7 @@ impl<'a> State<'a> { let empty = item.attrs.is_empty() && nmod.items.is_empty(); self.bclose(item.span, empty); } - ast::ItemKind::GlobalAsm(ref asm) => { + ast::ItemKind::GlobalAsm(asm) => { self.head(visibility_qualified(&item.vis, "global_asm!")); self.print_inline_asm(asm); self.word(";"); @@ -224,32 +230,31 @@ impl<'a> State<'a> { } ast::ItemKind::TyAlias(box ast::TyAlias { defaultness, - ref generics, + generics, where_clauses, where_predicates_split, - ref bounds, - ref ty, + bounds, + ty, }) => { - let ty = ty.as_deref(); self.print_associated_type( item.ident, generics, - where_clauses, - where_predicates_split, + *where_clauses, + *where_predicates_split, bounds, - ty, + ty.as_deref(), &item.vis, - defaultness, + *defaultness, ); } - ast::ItemKind::Enum(ref enum_definition, ref params) => { + ast::ItemKind::Enum(enum_definition, params) => { self.print_enum_def(enum_definition, params, item.ident, item.span, &item.vis); } - ast::ItemKind::Struct(ref struct_def, ref generics) => { + ast::ItemKind::Struct(struct_def, generics) => { self.head(visibility_qualified(&item.vis, "struct")); self.print_struct(struct_def, generics, item.ident, item.span, true); } - ast::ItemKind::Union(ref struct_def, ref generics) => { + ast::ItemKind::Union(struct_def, generics) => { self.head(visibility_qualified(&item.vis, "union")); self.print_struct(struct_def, generics, item.ident, item.span, true); } @@ -258,15 +263,15 @@ impl<'a> State<'a> { polarity, defaultness, constness, - ref generics, - ref of_trait, - ref self_ty, - ref items, + generics, + of_trait, + self_ty, + items, }) => { self.head(""); self.print_visibility(&item.vis); - self.print_defaultness(defaultness); - self.print_unsafety(unsafety); + self.print_defaultness(*defaultness); + self.print_unsafety(*unsafety); self.word("impl"); if generics.params.is_empty() { @@ -276,13 +281,13 @@ impl<'a> State<'a> { self.space(); } - self.print_constness(constness); + self.print_constness(*constness); if let ast::ImplPolarity::Negative(_) = polarity { self.word("!"); } - if let Some(ref t) = *of_trait { + if let Some(t) = of_trait { self.print_trait_ref(t); self.space(); self.word_space("for"); @@ -303,21 +308,21 @@ impl<'a> State<'a> { ast::ItemKind::Trait(box ast::Trait { is_auto, unsafety, - ref generics, - ref bounds, - ref items, + generics, + bounds, + items, .. }) => { self.head(""); self.print_visibility(&item.vis); - self.print_unsafety(unsafety); - self.print_is_auto(is_auto); + self.print_unsafety(*unsafety); + self.print_is_auto(*is_auto); self.word_nbsp("trait"); self.print_ident(item.ident); self.print_generic_params(&generics.params); let mut real_bounds = Vec::with_capacity(bounds.len()); for b in bounds.iter() { - if let GenericBound::Trait(ref ptr, ast::TraitBoundModifier::Maybe) = *b { + if let GenericBound::Trait(ptr, ast::TraitBoundModifier::Maybe) = b { self.space(); self.word_space("for ?"); self.print_trait_ref(&ptr.trait_ref); @@ -339,14 +344,14 @@ impl<'a> State<'a> { let empty = item.attrs.is_empty() && items.is_empty(); self.bclose(item.span, empty); } - ast::ItemKind::TraitAlias(ref generics, ref bounds) => { + ast::ItemKind::TraitAlias(generics, bounds) => { self.head(visibility_qualified(&item.vis, "trait")); self.print_ident(item.ident); self.print_generic_params(&generics.params); let mut real_bounds = Vec::with_capacity(bounds.len()); // FIXME(durka) this seems to be some quite outdated syntax for b in bounds.iter() { - if let GenericBound::Trait(ref ptr, ast::TraitBoundModifier::Maybe) = *b { + if let GenericBound::Trait(ptr, ast::TraitBoundModifier::Maybe) = b { self.space(); self.word_space("for ?"); self.print_trait_ref(&ptr.trait_ref); @@ -364,13 +369,13 @@ impl<'a> State<'a> { self.end(); // end inner head-block self.end(); // end outer head-block } - ast::ItemKind::MacCall(ref mac) => { + ast::ItemKind::MacCall(mac) => { self.print_mac(mac); if mac.args.need_semicolon() { self.word(";"); } } - ast::ItemKind::MacroDef(ref macro_def) => { + ast::ItemKind::MacroDef(macro_def) => { self.print_mac_def(macro_def, &item.ident, item.span, |state| { state.print_visibility(&item.vis) }); @@ -412,11 +417,11 @@ impl<'a> State<'a> { } pub(crate) fn print_visibility(&mut self, vis: &ast::Visibility) { - match vis.kind { + match &vis.kind { ast::VisibilityKind::Public => self.word_nbsp("pub"), - ast::VisibilityKind::Restricted { ref path, id: _, shorthand } => { + ast::VisibilityKind::Restricted { path, shorthand, .. } => { let path = Self::to_string(|s| s.print_path(path, false, 0)); - if shorthand && (path == "crate" || path == "self" || path == "super") { + if *shorthand && (path == "crate" || path == "self" || path == "super") { self.word_nbsp(format!("pub({})", path)) } else { self.word_nbsp(format!("pub(in {})", path)) @@ -465,7 +470,7 @@ impl<'a> State<'a> { ) { self.print_ident(ident); self.print_generic_params(&generics.params); - match struct_def { + match &struct_def { ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => { if let ast::VariantData::Tuple(..) = struct_def { self.popen(); @@ -484,7 +489,7 @@ impl<'a> State<'a> { self.end(); self.end(); // Close the outer-box. } - ast::VariantData::Struct(ref fields, ..) => { + ast::VariantData::Struct(fields, ..) => { self.print_where_clause(&generics.where_clause); self.print_record_struct_body(fields, span); } @@ -496,7 +501,7 @@ impl<'a> State<'a> { self.print_visibility(&v.vis); let generics = ast::Generics::default(); self.print_struct(&v.data, &generics, v.ident, v.span, false); - if let Some(ref d) = v.disr_expr { + if let Some(d) = &v.disr_expr { self.space(); self.word_space("="); self.print_expr(&d.value) @@ -657,10 +662,10 @@ impl<'a> State<'a> { } fn print_use_tree(&mut self, tree: &ast::UseTree) { - match tree.kind { + match &tree.kind { ast::UseTreeKind::Simple(rename, ..) => { self.print_path(&tree.prefix, false, 0); - if let Some(rename) = rename { + if let &Some(rename) = rename { self.nbsp(); self.word_nbsp("as"); self.print_ident(rename); @@ -673,7 +678,7 @@ impl<'a> State<'a> { } self.word("*"); } - ast::UseTreeKind::Nested(ref items) => { + ast::UseTreeKind::Nested(items) => { if !tree.prefix.segments.is_empty() { self.print_path(&tree.prefix, false, 0); self.word("::"); From a0771bdabbd23738e73a7745d1b224d53db8c826 Mon Sep 17 00:00:00 2001 From: David Rheinsberg Date: Mon, 21 Nov 2022 16:47:02 +0100 Subject: [PATCH 183/244] codegen-llvm: never combine DSOLocal and DllImport Prevent DllImport from being attached to DSOLocal definitions in the LLVM IR. The combination makes no sense, since definitions local to the compilation unit will never be imported from external objects. Additionally, LLVM will refuse the IR if it encounters the combination (introduced in [1]): if (GV.hasDLLImportStorageClass()) Assert(!GV.isDSOLocal(), "GlobalValue with DLLImport Storage is dso_local!", &GV); Right now, codegen-llvm will only apply DllImport to constants and rely on call-stubs for functions. Hence, we simply extend the codegen of constants to skip DllImport for any local definitions. This was discovered when switching the EFI targets to the static relocation model [2]. With this fixed, we can start another attempt at this. [1] https://smlnj-gitlab.cs.uchicago.edu/manticore/llvm/commit/509132b368efed10bbdad825403f45e9cf1d6e38 [2] https://github.com/rust-lang/rust/issues/101656 --- compiler/rustc_codegen_llvm/src/consts.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/consts.rs b/compiler/rustc_codegen_llvm/src/consts.rs index 69434280b2144..3c324359565c1 100644 --- a/compiler/rustc_codegen_llvm/src/consts.rs +++ b/compiler/rustc_codegen_llvm/src/consts.rs @@ -295,8 +295,18 @@ impl<'ll> CodegenCx<'ll, '_> { llvm::set_thread_local_mode(g, self.tls_model); } + let dso_local = unsafe { self.should_assume_dso_local(g, true) }; + if dso_local { + unsafe { + llvm::LLVMRustSetDSOLocal(g, true); + } + } + if !def_id.is_local() { let needs_dll_storage_attr = self.use_dll_storage_attrs && !self.tcx.is_foreign_item(def_id) && + // Local definitions can never be imported, so we must not apply + // the DLLImport annotation. + !dso_local && // ThinLTO can't handle this workaround in all cases, so we don't // emit the attrs. Instead we make them unnecessary by disallowing // dynamic linking when linker plugin based LTO is enabled. @@ -340,12 +350,6 @@ impl<'ll> CodegenCx<'ll, '_> { } } - unsafe { - if self.should_assume_dso_local(g, true) { - llvm::LLVMRustSetDSOLocal(g, true); - } - } - self.instances.borrow_mut().insert(instance, g); g } From 7b9e1a95f0ca35bde8af529639dde501a0280425 Mon Sep 17 00:00:00 2001 From: David Rheinsberg Date: Tue, 22 Nov 2022 11:12:26 +0100 Subject: [PATCH 184/244] test/codegen: test inter-crate linkage with static relocation Add a codegen-test that verifies inter-crate linkage with the static relocation model. We expect all symbols that are part of a rust compilation to end up in the same DSO, thus we expect `dso_local` annotations. --- src/test/codegen/auxiliary/extern_decl.rs | 11 ++++++++ .../codegen/static-relocation-model-msvc.rs | 26 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/test/codegen/auxiliary/extern_decl.rs create mode 100644 src/test/codegen/static-relocation-model-msvc.rs diff --git a/src/test/codegen/auxiliary/extern_decl.rs b/src/test/codegen/auxiliary/extern_decl.rs new file mode 100644 index 0000000000000..edc48351869a6 --- /dev/null +++ b/src/test/codegen/auxiliary/extern_decl.rs @@ -0,0 +1,11 @@ +// Auxiliary crate that exports a function and static. Both always +// evaluate to `71`. We force mutability on the static to prevent +// it from being inlined as constant. + +#![crate_type = "lib"] + +#[no_mangle] +pub fn extern_fn() -> u8 { unsafe { extern_static } } + +#[no_mangle] +pub static mut extern_static: u8 = 71; diff --git a/src/test/codegen/static-relocation-model-msvc.rs b/src/test/codegen/static-relocation-model-msvc.rs new file mode 100644 index 0000000000000..b2afc7deb679a --- /dev/null +++ b/src/test/codegen/static-relocation-model-msvc.rs @@ -0,0 +1,26 @@ +// Verify linkage of external symbols in the static relocation model on MSVC. +// +// compile-flags: -O -C relocation-model=static +// aux-build: extern_decl.rs +// only-x86_64-pc-windows-msvc + +#![crate_type = "rlib"] + +extern crate extern_decl; + +// The `extern_decl` definitions are imported from a statically linked rust +// crate, thus they are expected to be marked `dso_local` without `dllimport`. +// +// The `access_extern()` symbol is from this compilation unit, thus we expect +// it to be marked `dso_local` as well, given the static relocation model. +// +// CHECK: @extern_static = external dso_local local_unnamed_addr global i8 +// CHECK: define dso_local i8 @access_extern() {{.*}} +// CHECK: declare dso_local i8 @extern_fn() {{.*}} + +#[no_mangle] +pub fn access_extern() -> u8 { + unsafe { + extern_decl::extern_fn() + extern_decl::extern_static + } +} From c8f3203c46c4b95a9eaff3ff8bb99982b682db0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Tue, 29 Nov 2022 11:26:40 +0100 Subject: [PATCH 185/244] hermit: Fix fuzzy_provenance_casts --- library/std/src/sys/hermit/thread.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/std/src/sys/hermit/thread.rs b/library/std/src/sys/hermit/thread.rs index e53a1fea6a0dc..8f65544a9e894 100644 --- a/library/std/src/sys/hermit/thread.rs +++ b/library/std/src/sys/hermit/thread.rs @@ -5,6 +5,7 @@ use crate::ffi::CStr; use crate::io; use crate::mem; use crate::num::NonZeroUsize; +use crate::ptr; use crate::sys::hermit::abi; use crate::sys::hermit::thread_local_dtor::run_dtors; use crate::time::Duration; @@ -47,7 +48,7 @@ impl Thread { extern "C" fn thread_start(main: usize) { unsafe { // Finally, let's run some code. - Box::from_raw(main as *mut Box)(); + Box::from_raw(ptr::from_exposed_addr::>(main).cast_mut())(); // run all destructors run_dtors(); From 0a4e5efe6f6e6d84147b3da689f99b2b9750834c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Tue, 29 Nov 2022 11:27:25 +0100 Subject: [PATCH 186/244] hermit: Remove unused exports --- library/std/src/sys/hermit/fs.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/library/std/src/sys/hermit/fs.rs b/library/std/src/sys/hermit/fs.rs index af297ff1ec75b..6fb92c037ee4f 100644 --- a/library/std/src/sys/hermit/fs.rs +++ b/library/std/src/sys/hermit/fs.rs @@ -1,10 +1,8 @@ -use crate::convert::TryFrom; -use crate::ffi::{CStr, CString, OsString}; +use crate::ffi::{CStr, OsString}; use crate::fmt; use crate::hash::{Hash, Hasher}; use crate::io::{self, Error, ErrorKind}; use crate::io::{BorrowedCursor, IoSlice, IoSliceMut, SeekFrom}; -use crate::os::unix::ffi::OsStrExt; use crate::path::{Path, PathBuf}; use crate::sys::common::small_c_string::run_path_with_cstr; use crate::sys::cvt; From 050cee48f83b39a0f532e4905d02f07716bf2ee3 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Tue, 29 Nov 2022 11:52:48 +0000 Subject: [PATCH 187/244] Replace a macro with a function --- compiler/rustc_serialize/src/leb128.rs | 19 ++++++++----------- compiler/rustc_serialize/src/opaque.rs | 10 +++++----- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_serialize/src/leb128.rs b/compiler/rustc_serialize/src/leb128.rs index a6cdd32f23b8e..c5297bff193c3 100644 --- a/compiler/rustc_serialize/src/leb128.rs +++ b/compiler/rustc_serialize/src/leb128.rs @@ -1,22 +1,19 @@ -#![macro_use] - -macro_rules! max_leb128_len { - ($int_ty:ty) => { - // The longest LEB128 encoding for an integer uses 7 bits per byte. - (std::mem::size_of::<$int_ty>() * 8 + 6) / 7 - }; +/// Returns the longest LEB128 encoding for `T`, assuming `T` is an integer type +pub const fn max_leb128_len() -> usize { + // The longest LEB128 encoding for an integer uses 7 bits per byte. + (std::mem::size_of::() * 8 + 6) / 7 } /// Returns the longest LEB128 encoding of all supported integer types. -pub const fn max_leb128_len() -> usize { - max_leb128_len!(u128) +pub const fn max_max_leb128_len() -> usize { + max_leb128_len::() } macro_rules! impl_write_unsigned_leb128 { ($fn_name:ident, $int_ty:ty) => { #[inline] pub fn $fn_name( - out: &mut [::std::mem::MaybeUninit; max_leb128_len!($int_ty)], + out: &mut [::std::mem::MaybeUninit; max_leb128_len::<$int_ty>()], mut value: $int_ty, ) -> &[u8] { let mut i = 0; @@ -90,7 +87,7 @@ macro_rules! impl_write_signed_leb128 { ($fn_name:ident, $int_ty:ty) => { #[inline] pub fn $fn_name( - out: &mut [::std::mem::MaybeUninit; max_leb128_len!($int_ty)], + out: &mut [::std::mem::MaybeUninit; max_leb128_len::<$int_ty>()], mut value: $int_ty, ) -> &[u8] { let mut i = 0; diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs index 7c54df809f179..7cd513e1eb925 100644 --- a/compiler/rustc_serialize/src/opaque.rs +++ b/compiler/rustc_serialize/src/opaque.rs @@ -1,4 +1,4 @@ -use crate::leb128::{self, max_leb128_len}; +use crate::leb128::{self, max_max_leb128_len}; use crate::serialize::{Decodable, Decoder, Encodable, Encoder}; use std::convert::TryInto; use std::fs::File; @@ -32,7 +32,7 @@ impl MemEncoder { macro_rules! write_leb128 { ($enc:expr, $value:expr, $int_ty:ty, $fun:ident) => {{ - const MAX_ENCODED_LEN: usize = max_leb128_len!($int_ty); + const MAX_ENCODED_LEN: usize = $crate::leb128::max_leb128_len::<$int_ty>(); let old_len = $enc.data.len(); if MAX_ENCODED_LEN > $enc.data.capacity() - old_len { @@ -186,12 +186,12 @@ impl FileEncoder { pub fn with_capacity>(path: P, capacity: usize) -> io::Result { // Require capacity at least as large as the largest LEB128 encoding // here, so that we don't have to check or handle this on every write. - assert!(capacity >= max_leb128_len()); + assert!(capacity >= max_max_leb128_len()); // Require capacity small enough such that some capacity checks can be // done using guaranteed non-overflowing add rather than sub, which // shaves an instruction off those code paths (on x86 at least). - assert!(capacity <= usize::MAX - max_leb128_len()); + assert!(capacity <= usize::MAX - max_max_leb128_len()); // Create the file for reading and writing, because some encoders do both // (e.g. the metadata encoder when -Zmeta-stats is enabled) @@ -411,7 +411,7 @@ impl Drop for FileEncoder { macro_rules! file_encoder_write_leb128 { ($enc:expr, $value:expr, $int_ty:ty, $fun:ident) => {{ - const MAX_ENCODED_LEN: usize = max_leb128_len!($int_ty); + const MAX_ENCODED_LEN: usize = $crate::leb128::max_leb128_len::<$int_ty>(); // We ensure this during `FileEncoder` construction. debug_assert!($enc.capacity() >= MAX_ENCODED_LEN); From 8759f33bb338e9d9a2ebe36ee3e11f4643acdc8a Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 29 Nov 2022 05:18:43 -0800 Subject: [PATCH 188/244] Add S-waiting-on-review autolabel. --- triagebot.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/triagebot.toml b/triagebot.toml index c615d18f8687e..acb476ee69628 100644 --- a/triagebot.toml +++ b/triagebot.toml @@ -9,6 +9,9 @@ allow-unauthenticated = [ # See https://github.com/rust-lang/triagebot/wiki/Shortcuts [shortcut] +[autolabel."S-waiting-on-review"] +new_pr = true + [assign] contributing_url = "https://github.com/rust-lang/rust-clippy/blob/master/CONTRIBUTING.md" From 537488efd6f0be7240b5e2d08cae4af5db7162ab Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 25 Nov 2022 16:35:27 -0300 Subject: [PATCH 189/244] Make inferred_outlives_crate return Clause --- compiler/rustc_hir_analysis/src/collect.rs | 12 +++++---- .../rustc_hir_analysis/src/outlives/mod.rs | 26 ++++++------------- compiler/rustc_lint/src/builtin.rs | 20 ++++++-------- compiler/rustc_metadata/src/rmeta/mod.rs | 2 +- compiler/rustc_middle/src/query/mod.rs | 2 +- compiler/rustc_middle/src/ty/codec.rs | 8 ++++++ compiler/rustc_middle/src/ty/mod.rs | 9 ++++++- compiler/rustc_middle/src/ty/parameterized.rs | 3 ++- .../rustc_query_impl/src/on_disk_cache.rs | 6 +++++ 9 files changed, 49 insertions(+), 39 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 638dd6d756b58..d623e72613944 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -33,7 +33,7 @@ use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs} use rustc_middle::mir::mono::Linkage; use rustc_middle::ty::query::Providers; use rustc_middle::ty::util::{Discr, IntTypeExt}; -use rustc_middle::ty::{self, AdtKind, Const, DefIdTree, IsSuggestable, Ty, TyCtxt}; +use rustc_middle::ty::{self, AdtKind, Const, DefIdTree, IsSuggestable, ToPredicate, Ty, TyCtxt}; use rustc_session::lint; use rustc_session::parse::feature_err; use rustc_span::symbol::{kw, sym, Ident, Symbol}; @@ -1366,12 +1366,14 @@ fn predicates_defined_on(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicate "predicates_defined_on: inferred_outlives_of({:?}) = {:?}", def_id, inferred_outlives, ); + let inferred_outlives_iter = + inferred_outlives.iter().map(|(clause, span)| ((*clause).to_predicate(tcx), *span)); if result.predicates.is_empty() { - result.predicates = inferred_outlives; + result.predicates = tcx.arena.alloc_from_iter(inferred_outlives_iter); } else { - result.predicates = tcx - .arena - .alloc_from_iter(result.predicates.iter().chain(inferred_outlives).copied()); + result.predicates = tcx.arena.alloc_from_iter( + result.predicates.into_iter().copied().chain(inferred_outlives_iter), + ); } } diff --git a/compiler/rustc_hir_analysis/src/outlives/mod.rs b/compiler/rustc_hir_analysis/src/outlives/mod.rs index c8f37176836c5..81fe32000d307 100644 --- a/compiler/rustc_hir_analysis/src/outlives/mod.rs +++ b/compiler/rustc_hir_analysis/src/outlives/mod.rs @@ -3,7 +3,7 @@ use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_middle::ty::query::Providers; use rustc_middle::ty::subst::GenericArgKind; -use rustc_middle::ty::{self, CratePredicatesMap, ToPredicate, TyCtxt}; +use rustc_middle::ty::{self, CratePredicatesMap, TyCtxt}; use rustc_span::symbol::sym; use rustc_span::Span; @@ -17,7 +17,7 @@ pub fn provide(providers: &mut Providers) { *providers = Providers { inferred_outlives_of, inferred_outlives_crate, ..*providers }; } -fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[(ty::Predicate<'_>, Span)] { +fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[(ty::Clause<'_>, Span)] { let id = tcx.hir().local_def_id_to_hir_id(item_def_id.expect_local()); if matches!(tcx.def_kind(item_def_id), hir::def::DefKind::AnonConst) && tcx.lazy_normalization() @@ -50,12 +50,10 @@ fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[(ty::Predicate if tcx.has_attr(item_def_id, sym::rustc_outlives) { let mut pred: Vec = predicates .iter() - .map(|(out_pred, _)| match out_pred.kind().skip_binder() { - ty::PredicateKind::Clause(ty::Clause::RegionOutlives(p)) => { - p.to_string() - } - ty::PredicateKind::Clause(ty::Clause::TypeOutlives(p)) => p.to_string(), - err => bug!("unexpected predicate {:?}", err), + .map(|(out_pred, _)| match out_pred { + ty::Clause::RegionOutlives(p) => p.to_string(), + ty::Clause::TypeOutlives(p) => p.to_string(), + err => bug!("unexpected clause {:?}", err), }) .collect(); pred.sort(); @@ -103,19 +101,11 @@ fn inferred_outlives_crate(tcx: TyCtxt<'_>, (): ()) -> CratePredicatesMap<'_> { |(ty::OutlivesPredicate(kind1, region2), &span)| { match kind1.unpack() { GenericArgKind::Type(ty1) => Some(( - ty::Binder::dummy(ty::PredicateKind::Clause(ty::Clause::TypeOutlives( - ty::OutlivesPredicate(ty1, *region2), - ))) - .to_predicate(tcx), + ty::Clause::TypeOutlives(ty::OutlivesPredicate(ty1, *region2)), span, )), GenericArgKind::Lifetime(region1) => Some(( - ty::Binder::dummy(ty::PredicateKind::Clause( - ty::Clause::RegionOutlives(ty::OutlivesPredicate( - region1, *region2, - )), - )) - .to_predicate(tcx), + ty::Clause::RegionOutlives(ty::OutlivesPredicate(region1, *region2)), span, )), GenericArgKind::Const(_) => { diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 93d81125f4885..825093384fba7 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -2046,16 +2046,13 @@ declare_lint_pass!(ExplicitOutlivesRequirements => [EXPLICIT_OUTLIVES_REQUIREMEN impl ExplicitOutlivesRequirements { fn lifetimes_outliving_lifetime<'tcx>( - inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)], + inferred_outlives: &'tcx [(ty::Clause<'tcx>, Span)], def_id: DefId, ) -> Vec> { inferred_outlives .iter() - .filter_map(|(pred, _)| match pred.kind().skip_binder() { - ty::PredicateKind::Clause(ty::Clause::RegionOutlives(ty::OutlivesPredicate( - a, - b, - ))) => match *a { + .filter_map(|(clause, _)| match *clause { + ty::Clause::RegionOutlives(ty::OutlivesPredicate(a, b)) => match *a { ty::ReEarlyBound(ebr) if ebr.def_id == def_id => Some(b), _ => None, }, @@ -2065,16 +2062,15 @@ impl ExplicitOutlivesRequirements { } fn lifetimes_outliving_type<'tcx>( - inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)], + inferred_outlives: &'tcx [(ty::Clause<'tcx>, Span)], index: u32, ) -> Vec> { inferred_outlives .iter() - .filter_map(|(pred, _)| match pred.kind().skip_binder() { - ty::PredicateKind::Clause(ty::Clause::TypeOutlives(ty::OutlivesPredicate( - a, - b, - ))) => a.is_param(index).then_some(b), + .filter_map(|(clause, _)| match *clause { + ty::Clause::TypeOutlives(ty::OutlivesPredicate(a, b)) => { + a.is_param(index).then_some(b) + } _ => None, }) .collect() diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index c51b8f96c7151..6b60577c9023f 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -353,7 +353,7 @@ define_tables! { explicit_predicates_of: Table>>, generics_of: Table>, // As an optimization, a missing entry indicates an empty `&[]`. - inferred_outlives_of: Table, Span)>>, + inferred_outlives_of: Table, Span)>>, super_predicates_of: Table>>, type_of: Table>>, variances_of: Table>, diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 38b72ec923193..e1220320eea1b 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -562,7 +562,7 @@ rustc_queries! { /// Returns the inferred outlives predicates (e.g., for `struct /// Foo<'a, T> { x: &'a T }`, this would return `T: 'a`). - query inferred_outlives_of(key: DefId) -> &'tcx [(ty::Predicate<'tcx>, Span)] { + query inferred_outlives_of(key: DefId) -> &'tcx [(ty::Clause<'tcx>, Span)] { desc { |tcx| "computing inferred outlives predicates of `{}`", tcx.def_path_str(key) } cache_on_disk_if { key.is_local() } separate_provide_extern diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index b22b3961f34ea..75f2d45eadb81 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -345,6 +345,14 @@ impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> } } +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [(ty::Clause<'tcx>, Span)] { + fn decode(decoder: &mut D) -> &'tcx Self { + decoder.interner().arena.alloc_from_iter( + (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::>(), + ) + } +} + impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for ty::List { diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 595b73986a87b..7be9fee3b0cf6 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -734,7 +734,7 @@ pub struct CratePredicatesMap<'tcx> { /// For each struct with outlive bounds, maps to a vector of the /// predicate of its outlive bounds. If an item has no outlives /// bounds, it will have no entry. - pub predicates: FxHashMap, Span)]>, + pub predicates: FxHashMap, Span)]>, } impl<'tcx> Predicate<'tcx> { @@ -1167,6 +1167,13 @@ impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for Binder<'tcx, PredicateKind<'tc } } +impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for Clause<'tcx> { + #[inline(always)] + fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { + tcx.mk_predicate(ty::Binder::dummy(ty::PredicateKind::Clause(self))) + } +} + impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for Binder<'tcx, TraitRef<'tcx>> { #[inline(always)] fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { diff --git a/compiler/rustc_middle/src/ty/parameterized.rs b/compiler/rustc_middle/src/ty/parameterized.rs index b2bcf0e29cd9d..c7d6c6abd1c22 100644 --- a/compiler/rustc_middle/src/ty/parameterized.rs +++ b/compiler/rustc_middle/src/ty/parameterized.rs @@ -5,7 +5,7 @@ use rustc_index::vec::{Idx, IndexVec}; use crate::middle::exported_symbols::ExportedSymbol; use crate::mir::Body; use crate::ty::{ - self, Const, FnSig, GeneratorDiagnosticData, GenericPredicates, Predicate, TraitRef, Ty, + self, Clause, Const, FnSig, GeneratorDiagnosticData, GenericPredicates, Predicate, TraitRef, Ty, }; pub trait ParameterizedOverTcx: 'static { @@ -121,6 +121,7 @@ parameterized_over_tcx! { TraitRef, Const, Predicate, + Clause, GeneratorDiagnosticData, Body, ExportedSymbol, diff --git a/compiler/rustc_query_impl/src/on_disk_cache.rs b/compiler/rustc_query_impl/src/on_disk_cache.rs index c61d2a9c2d0c6..ac9653b90071c 100644 --- a/compiler/rustc_query_impl/src/on_disk_cache.rs +++ b/compiler/rustc_query_impl/src/on_disk_cache.rs @@ -818,6 +818,12 @@ impl<'a, 'tcx> Decodable> for &'tcx [(ty::Predicate<'tcx> } } +impl<'a, 'tcx> Decodable> for &'tcx [(ty::Clause<'tcx>, Span)] { + fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self { + RefDecodable::decode(d) + } +} + impl<'a, 'tcx> Decodable> for &'tcx [rustc_ast::InlineAsmTemplatePiece] { fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self { RefDecodable::decode(d) From 8c0951511b25dbe911dd7b1f880deaf06aad0351 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Tue, 29 Nov 2022 17:58:09 +0000 Subject: [PATCH 190/244] rename `{max=>largest}_max_leb128_len` --- compiler/rustc_serialize/src/leb128.rs | 6 +++--- compiler/rustc_serialize/src/opaque.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_serialize/src/leb128.rs b/compiler/rustc_serialize/src/leb128.rs index c5297bff193c3..7dad9aa01fafd 100644 --- a/compiler/rustc_serialize/src/leb128.rs +++ b/compiler/rustc_serialize/src/leb128.rs @@ -1,11 +1,11 @@ -/// Returns the longest LEB128 encoding for `T`, assuming `T` is an integer type +/// Returns the length of the longest LEB128 encoding for `T`, assuming `T` is an integer type pub const fn max_leb128_len() -> usize { // The longest LEB128 encoding for an integer uses 7 bits per byte. (std::mem::size_of::() * 8 + 6) / 7 } -/// Returns the longest LEB128 encoding of all supported integer types. -pub const fn max_max_leb128_len() -> usize { +/// Returns the length of the longest LEB128 encoding of all supported integer types. +pub const fn largest_max_leb128_len() -> usize { max_leb128_len::() } diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs index 7cd513e1eb925..0afeb86fceb24 100644 --- a/compiler/rustc_serialize/src/opaque.rs +++ b/compiler/rustc_serialize/src/opaque.rs @@ -1,4 +1,4 @@ -use crate::leb128::{self, max_max_leb128_len}; +use crate::leb128::{self, largest_max_leb128_len}; use crate::serialize::{Decodable, Decoder, Encodable, Encoder}; use std::convert::TryInto; use std::fs::File; @@ -186,12 +186,12 @@ impl FileEncoder { pub fn with_capacity>(path: P, capacity: usize) -> io::Result { // Require capacity at least as large as the largest LEB128 encoding // here, so that we don't have to check or handle this on every write. - assert!(capacity >= max_max_leb128_len()); + assert!(capacity >= largest_max_leb128_len()); // Require capacity small enough such that some capacity checks can be // done using guaranteed non-overflowing add rather than sub, which // shaves an instruction off those code paths (on x86 at least). - assert!(capacity <= usize::MAX - max_max_leb128_len()); + assert!(capacity <= usize::MAX - largest_max_leb128_len()); // Create the file for reading and writing, because some encoders do both // (e.g. the metadata encoder when -Zmeta-stats is enabled) From 1b4012e3043c2431c57435d0017832fa3f4bb0c6 Mon Sep 17 00:00:00 2001 From: Waffle Maybe Date: Tue, 29 Nov 2022 22:01:49 +0400 Subject: [PATCH 191/244] add `FIXME:` where it belongs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit suggestion from a friend!! 🐸 Co-authored-by: Michael Goulet --- compiler/rustc_ast_pretty/src/pprust/state.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index 0fe1045c244cb..374e0a97063e3 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -64,7 +64,7 @@ impl<'a> Comments<'a> { Comments { sm, comments, current: 0 } } - // This shouldn't probably clone lmao + // FIXME: This shouldn't probably clone lmao pub fn next(&self) -> Option { self.comments.get(self.current).cloned() } From edf5cce5a40202df78ecf47cf52057e0eb93c2ff Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Tue, 29 Nov 2022 11:13:44 -0700 Subject: [PATCH 192/244] rustdoc: use shorthand background for rustdoc toggle CSS --- src/librustdoc/html/static/css/rustdoc.css | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index cf5592da43205..de882e66f43de 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -1511,13 +1511,11 @@ details.rustdoc-toggle > summary.hideme > span { } details.rustdoc-toggle > summary::before { - background-image: url("toggle-plus-1092eb4930d581b0.svg"); + background: url("toggle-plus-1092eb4930d581b0.svg") no-repeat top left; content: ""; cursor: pointer; width: 16px; height: 16px; - background-repeat: no-repeat; - background-position: top left; display: inline-block; vertical-align: middle; opacity: .5; @@ -1598,11 +1596,9 @@ details.rustdoc-toggle[open] > summary.hideme > span { details.rustdoc-toggle[open] > summary::before, details.rustdoc-toggle[open] > summary.hideme::before { - background-image: url("toggle-minus-31bbd6e4c77f5c96.svg"); + background: url("toggle-minus-31bbd6e4c77f5c96.svg") no-repeat top left; width: 16px; height: 16px; - background-repeat: no-repeat; - background-position: top left; display: inline-block; content: ""; } From 928622812c8f885d8d802ccee36bbe538a47e801 Mon Sep 17 00:00:00 2001 From: Nixon Enraght-Moony Date: Tue, 29 Nov 2022 18:27:16 +0000 Subject: [PATCH 193/244] Don't assume that core::fmt::Debug will always have one item. --- src/test/rustdoc-json/traits/uses_extern_trait.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/test/rustdoc-json/traits/uses_extern_trait.rs b/src/test/rustdoc-json/traits/uses_extern_trait.rs index 430dd1543f561..a4add43c6a174 100644 --- a/src/test/rustdoc-json/traits/uses_extern_trait.rs +++ b/src/test/rustdoc-json/traits/uses_extern_trait.rs @@ -3,5 +3,10 @@ pub fn drop_default(_x: T) {} // FIXME(adotinthevoid): Theses shouldn't be here // @has "$.index[*][?(@.name=='Debug')]" -// @set Debug_fmt = "$.index[*][?(@.name=='Debug')].inner.items[*]" + +// Debug may have several items. All we depend on here the that `fmt` is first. See +// https://github.com/rust-lang/rust/pull/104525#issuecomment-1331087852 for why we +// can't use [*]. + +// @set Debug_fmt = "$.index[*][?(@.name=='Debug')].inner.items[0]" // @has "$.index[*][?(@.name=='fmt')].id" $Debug_fmt From 7c45772bc996cac15c090cdeb13ac14f713486d0 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 8 May 2022 15:00:03 +0200 Subject: [PATCH 194/244] Make verbose query description more useful. --- compiler/rustc_query_impl/src/plumbing.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index 99edaa0416274..e69f6aa3a93a6 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -309,7 +309,7 @@ pub(crate) fn create_query_frame< ty::print::with_forced_impl_filename_line!(do_describe(tcx.tcx, key)) ); let description = - if tcx.sess.verbose() { format!("{} [{}]", description, name) } else { description }; + if tcx.sess.verbose() { format!("{} [{:?}]", description, name) } else { description }; let span = if kind == dep_graph::DepKind::def_span { // The `def_span` query is used to calculate `default_span`, // so exit to avoid infinite recursion. From ca42dd67167575daa95a9f3c0d084d44f6c2ad2a Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 8 May 2022 14:42:12 +0200 Subject: [PATCH 195/244] Sanity check fingerprints in the dep-graph. --- .../rustc_query_system/src/dep_graph/graph.rs | 54 ++++++++++++------- .../rustc_query_system/src/query/caches.rs | 4 ++ 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_query_system/src/dep_graph/graph.rs b/compiler/rustc_query_system/src/dep_graph/graph.rs index d0f35b27c19fc..e4f2b87e78f12 100644 --- a/compiler/rustc_query_system/src/dep_graph/graph.rs +++ b/compiler/rustc_query_system/src/dep_graph/graph.rs @@ -916,6 +916,11 @@ pub(super) struct CurrentDepGraph { new_node_to_index: Sharded, DepNodeIndex>>, prev_index_to_index: Lock>>, + /// This is used to verify that fingerprints do not change between the creation of a node + /// and its recomputation. + #[cfg(debug_assertions)] + fingerprints: Lock, Fingerprint>>, + /// Used to trap when a specific edge is added to the graph. /// This is used for debug purposes and is only active with `debug_assertions`. #[cfg(debug_assertions)] @@ -999,6 +1004,8 @@ impl CurrentDepGraph { anon_id_seed, #[cfg(debug_assertions)] forbidden_edge, + #[cfg(debug_assertions)] + fingerprints: Lock::new(Default::default()), total_read_count: AtomicU64::new(0), total_duplicate_read_count: AtomicU64::new(0), node_intern_event_id, @@ -1006,10 +1013,18 @@ impl CurrentDepGraph { } #[cfg(debug_assertions)] - fn record_edge(&self, dep_node_index: DepNodeIndex, key: DepNode) { + fn record_edge(&self, dep_node_index: DepNodeIndex, key: DepNode, fingerprint: Fingerprint) { if let Some(forbidden_edge) = &self.forbidden_edge { forbidden_edge.index_to_node.lock().insert(dep_node_index, key); } + match self.fingerprints.lock().entry(key) { + Entry::Vacant(v) => { + v.insert(fingerprint); + } + Entry::Occupied(o) => { + assert_eq!(*o.get(), fingerprint, "Unstable fingerprints for {:?}", key); + } + } } /// Writes the node to the current dep-graph and allocates a `DepNodeIndex` for it. @@ -1021,17 +1036,21 @@ impl CurrentDepGraph { edges: EdgesVec, current_fingerprint: Fingerprint, ) -> DepNodeIndex { - match self.new_node_to_index.get_shard_by_value(&key).lock().entry(key) { + let dep_node_index = match self.new_node_to_index.get_shard_by_value(&key).lock().entry(key) + { Entry::Occupied(entry) => *entry.get(), Entry::Vacant(entry) => { let dep_node_index = self.encoder.borrow().send(profiler, key, current_fingerprint, edges); entry.insert(dep_node_index); - #[cfg(debug_assertions)] - self.record_edge(dep_node_index, key); dep_node_index } - } + }; + + #[cfg(debug_assertions)] + self.record_edge(dep_node_index, key, current_fingerprint); + + dep_node_index } fn intern_node( @@ -1072,7 +1091,7 @@ impl CurrentDepGraph { }; #[cfg(debug_assertions)] - self.record_edge(dep_node_index, key); + self.record_edge(dep_node_index, key, fingerprint); (dep_node_index, Some((prev_index, DepNodeColor::Green(dep_node_index)))) } else { if print_status { @@ -1094,7 +1113,7 @@ impl CurrentDepGraph { }; #[cfg(debug_assertions)] - self.record_edge(dep_node_index, key); + self.record_edge(dep_node_index, key, fingerprint); (dep_node_index, Some((prev_index, DepNodeColor::Red))) } } else { @@ -1119,7 +1138,7 @@ impl CurrentDepGraph { }; #[cfg(debug_assertions)] - self.record_edge(dep_node_index, key); + self.record_edge(dep_node_index, key, Fingerprint::ZERO); (dep_node_index, Some((prev_index, DepNodeColor::Red))) } } else { @@ -1150,19 +1169,16 @@ impl CurrentDepGraph { Some(dep_node_index) => dep_node_index, None => { let key = prev_graph.index_to_node(prev_index); - let dep_node_index = self.encoder.borrow().send( - profiler, - key, - prev_graph.fingerprint_by_index(prev_index), - prev_graph - .edge_targets_from(prev_index) - .iter() - .map(|i| prev_index_to_index[*i].unwrap()) - .collect(), - ); + let edges = prev_graph + .edge_targets_from(prev_index) + .iter() + .map(|i| prev_index_to_index[*i].unwrap()) + .collect(); + let fingerprint = prev_graph.fingerprint_by_index(prev_index); + let dep_node_index = self.encoder.borrow().send(profiler, key, fingerprint, edges); prev_index_to_index[prev_index] = Some(dep_node_index); #[cfg(debug_assertions)] - self.record_edge(dep_node_index, key); + self.record_edge(dep_node_index, key, fingerprint); dep_node_index } } diff --git a/compiler/rustc_query_system/src/query/caches.rs b/compiler/rustc_query_system/src/query/caches.rs index cdd4357242215..4c4680b5d8ea8 100644 --- a/compiler/rustc_query_system/src/query/caches.rs +++ b/compiler/rustc_query_system/src/query/caches.rs @@ -117,6 +117,8 @@ where let mut lock = self.cache.get_shard_by_value(&key).lock(); #[cfg(not(parallel_compiler))] let mut lock = self.cache.lock(); + // We may be overwriting another value. This is all right, since the dep-graph + // will check that the fingerprint matches. lock.insert(key, (value.clone(), index)); value } @@ -202,6 +204,8 @@ where let mut lock = self.cache.get_shard_by_value(&key).lock(); #[cfg(not(parallel_compiler))] let mut lock = self.cache.lock(); + // We may be overwriting another value. This is all right, since the dep-graph + // will check that the fingerprint matches. lock.insert(key, value); &value.0 } From 547138134992bfcf9171a781e4a4283cef350a89 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Tue, 22 Mar 2022 21:45:32 +0100 Subject: [PATCH 196/244] Allow to set a query's result as a side effect. --- compiler/rustc_macros/src/query.rs | 21 ++++- compiler/rustc_middle/src/query/mod.rs | 1 + compiler/rustc_middle/src/ty/query.rs | 68 +++++++++++++++ .../rustc_query_system/src/dep_graph/graph.rs | 82 +++++++++++++++++++ 4 files changed, 171 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_macros/src/query.rs b/compiler/rustc_macros/src/query.rs index 7cefafef9d978..30c42757dbe8c 100644 --- a/compiler/rustc_macros/src/query.rs +++ b/compiler/rustc_macros/src/query.rs @@ -114,6 +114,9 @@ struct QueryModifiers { /// Always remap the ParamEnv's constness before hashing. remap_env_constness: Option, + + /// Generate a `feed` method to set the query's value from another query. + feedable: Option, } fn parse_query_modifiers(input: ParseStream<'_>) -> Result { @@ -128,6 +131,7 @@ fn parse_query_modifiers(input: ParseStream<'_>) -> Result { let mut depth_limit = None; let mut separate_provide_extern = None; let mut remap_env_constness = None; + let mut feedable = None; while !input.is_empty() { let modifier: Ident = input.parse()?; @@ -187,6 +191,8 @@ fn parse_query_modifiers(input: ParseStream<'_>) -> Result { try_insert!(separate_provide_extern = modifier); } else if modifier == "remap_env_constness" { try_insert!(remap_env_constness = modifier); + } else if modifier == "feedable" { + try_insert!(feedable = modifier); } else { return Err(Error::new(modifier.span(), "unknown query modifier")); } @@ -206,6 +212,7 @@ fn parse_query_modifiers(input: ParseStream<'_>) -> Result { depth_limit, separate_provide_extern, remap_env_constness, + feedable, }) } @@ -296,6 +303,7 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream { let mut query_stream = quote! {}; let mut query_description_stream = quote! {}; let mut query_cached_stream = quote! {}; + let mut feedable_queries = quote! {}; for query in queries.0 { let Query { name, arg, modifiers, .. } = &query; @@ -350,6 +358,13 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream { [#attribute_stream] fn #name(#arg) #result, }); + if modifiers.feedable.is_some() { + feedable_queries.extend(quote! { + #(#doc_comments)* + [#attribute_stream] fn #name(#arg) #result, + }); + } + add_query_desc_cached_impl(&query, &mut query_description_stream, &mut query_cached_stream); } @@ -363,7 +378,11 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream { } } } - + macro_rules! rustc_feedable_queries { + ( $macro:ident! ) => { + $macro!(#feedable_queries); + } + } pub mod descs { use super::*; #query_description_stream diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 38b72ec923193..f94fc34ec4786 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -165,6 +165,7 @@ rustc_queries! { } cache_on_disk_if { key.is_local() } separate_provide_extern + feedable } query collect_trait_impl_trait_tys(key: DefId) diff --git a/compiler/rustc_middle/src/ty/query.rs b/compiler/rustc_middle/src/ty/query.rs index afbc9eb0512be..f44039879a613 100644 --- a/compiler/rustc_middle/src/ty/query.rs +++ b/compiler/rustc_middle/src/ty/query.rs @@ -85,6 +85,11 @@ pub struct TyCtxtEnsure<'tcx> { pub tcx: TyCtxt<'tcx>, } +#[derive(Copy, Clone)] +pub struct TyCtxtFeed<'tcx> { + pub tcx: TyCtxt<'tcx>, +} + impl<'tcx> TyCtxt<'tcx> { /// Returns a transparent wrapper for `TyCtxt`, which ensures queries /// are executed instead of just returning their results. @@ -93,6 +98,12 @@ impl<'tcx> TyCtxt<'tcx> { TyCtxtEnsure { tcx: self } } + /// Returns a transparent wrapper for `TyCtxt`, for setting a result into a query. + #[inline(always)] + pub fn feed(self) -> TyCtxtFeed<'tcx> { + TyCtxtFeed { tcx: self } + } + /// Returns a transparent wrapper for `TyCtxt` which uses /// `span` as the location of queries performed through it. #[inline(always)] @@ -175,6 +186,18 @@ macro_rules! opt_remap_env_constness { }; } +macro_rules! hash_result { + ([]) => {{ + Some(dep_graph::hash_result) + }}; + ([(no_hash) $($rest:tt)*]) => {{ + None + }}; + ([$other:tt $($modifiers:tt)*]) => { + hash_result!([$($modifiers)*]) + }; +} + macro_rules! define_callbacks { ( $($(#[$attr:meta])* @@ -327,6 +350,50 @@ macro_rules! define_callbacks { }; } +macro_rules! define_feedable { + ($($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => { + impl<'tcx> TyCtxtFeed<'tcx> { + $($(#[$attr])* + #[inline(always)] + pub fn $name( + self, + key: query_helper_param_ty!($($K)*), + value: $V, + ) -> query_stored::$name<'tcx> { + let key = key.into_query_param(); + opt_remap_env_constness!([$($modifiers)*][key]); + + let tcx = self.tcx; + let cache = &tcx.query_caches.$name; + + let cached = try_get_cached(tcx, cache, &key, copy); + + match cached { + Ok(old) => { + assert_eq!( + value, old, + "Trying to feed an already recorded value for query {} key={key:?}", + stringify!($name), + ); + return old; + } + Err(()) => (), + } + + let dep_node = dep_graph::DepNode::construct(tcx, dep_graph::DepKind::$name, &key); + let dep_node_index = tcx.dep_graph.with_feed_task( + dep_node, + tcx, + key, + &value, + hash_result!([$($modifiers)*]).unwrap(), + ); + cache.complete(key, value, dep_node_index) + })* + } + } +} + // Each of these queries corresponds to a function pointer field in the // `Providers` struct for requesting a value of that type, and a method // on `tcx: TyCtxt` (and `tcx.at(span)`) for doing that request in a way @@ -340,6 +407,7 @@ macro_rules! define_callbacks { // as they will raise an fatal error on query cycles instead. rustc_query_append! { define_callbacks! } +rustc_feedable_queries! { define_feedable! } mod sealed { use super::{DefId, LocalDefId, OwnerId}; diff --git a/compiler/rustc_query_system/src/dep_graph/graph.rs b/compiler/rustc_query_system/src/dep_graph/graph.rs index e4f2b87e78f12..d3d2ac256601f 100644 --- a/compiler/rustc_query_system/src/dep_graph/graph.rs +++ b/compiler/rustc_query_system/src/dep_graph/graph.rs @@ -489,6 +489,88 @@ impl DepGraph { } } + /// Create a node when we force-feed a value into the query cache. + /// This is used to remove cycles during type-checking const generic parameters. + /// + /// As usual in the query system, we consider the current state of the calling query + /// only depends on the list of dependencies up to now. As a consequence, the value + /// that this query gives us can only depend on those dependencies too. Therefore, + /// it is sound to use the current dependency set for the created node. + /// + /// During replay, the order of the nodes is relevant in the dependency graph. + /// So the unchanged replay will mark the caller query before trying to mark this one. + /// If there is a change to report, the caller query will be re-executed before this one. + /// + /// FIXME: If the code is changed enough for this node to be marked before requiring the + /// caller's node, we suppose that those changes will be enough to mark this node red and + /// force a recomputation using the "normal" way. + pub fn with_feed_task, A: Debug, R: Debug>( + &self, + node: DepNode, + cx: Ctxt, + key: A, + result: &R, + hash_result: fn(&mut StableHashingContext<'_>, &R) -> Fingerprint, + ) -> DepNodeIndex { + if let Some(data) = self.data.as_ref() { + if let Some(dep_node_index) = self.dep_node_index_of_opt(&node) { + #[cfg(debug_assertions)] + { + let hashing_timer = cx.profiler().incr_result_hashing(); + let current_fingerprint = + cx.with_stable_hashing_context(|mut hcx| hash_result(&mut hcx, result)); + hashing_timer.finish_with_query_invocation_id(dep_node_index.into()); + data.current.record_edge(dep_node_index, node, current_fingerprint); + } + + return dep_node_index; + } + + let mut edges = SmallVec::new(); + K::read_deps(|task_deps| match task_deps { + TaskDepsRef::Allow(deps) => edges.extend(deps.lock().reads.iter().copied()), + TaskDepsRef::Ignore | TaskDepsRef::Forbid => { + panic!("Cannot summarize when dependencies are not recorded.") + } + }); + + let hashing_timer = cx.profiler().incr_result_hashing(); + let current_fingerprint = + cx.with_stable_hashing_context(|mut hcx| hash_result(&mut hcx, result)); + + let print_status = cfg!(debug_assertions) && cx.sess().opts.unstable_opts.dep_tasks; + + // Intern the new `DepNode` with the dependencies up-to-now. + let (dep_node_index, prev_and_color) = data.current.intern_node( + cx.profiler(), + &data.previous, + node, + edges, + Some(current_fingerprint), + print_status, + ); + + hashing_timer.finish_with_query_invocation_id(dep_node_index.into()); + + if let Some((prev_index, color)) = prev_and_color { + debug_assert!( + data.colors.get(prev_index).is_none(), + "DepGraph::with_task() - Duplicate DepNodeColor insertion for {key:?}", + ); + + data.colors.insert(prev_index, color); + } + + dep_node_index + } else { + // Incremental compilation is turned off. We just execute the task + // without tracking. We still provide a dep-node index that uniquely + // identifies the task so that we have a cheap way of referring to + // the query for self-profiling. + self.next_virtual_depnode_index() + } + } + #[inline] pub fn dep_node_index_of(&self, dep_node: &DepNode) -> DepNodeIndex { self.dep_node_index_of_opt(dep_node).unwrap() From ee7a9a8641b79329ed4c221a2ae0e1e0c3d3d75d Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 4 Sep 2022 22:42:05 +0200 Subject: [PATCH 197/244] Expand hash check. --- .../rustc_query_system/src/dep_graph/graph.rs | 17 ++++++---- .../rustc_query_system/src/query/plumbing.rs | 32 +++++++++++-------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/compiler/rustc_query_system/src/dep_graph/graph.rs b/compiler/rustc_query_system/src/dep_graph/graph.rs index d3d2ac256601f..e44857a023857 100644 --- a/compiler/rustc_query_system/src/dep_graph/graph.rs +++ b/compiler/rustc_query_system/src/dep_graph/graph.rs @@ -513,15 +513,18 @@ impl DepGraph { hash_result: fn(&mut StableHashingContext<'_>, &R) -> Fingerprint, ) -> DepNodeIndex { if let Some(data) = self.data.as_ref() { + // The caller query has more dependencies than the node we are creating. We may + // encounter a case where this created node is marked as green, but the caller query is + // subsequently marked as red or recomputed. In this case, we will end up feeding a + // value to an existing node. + // + // For sanity, we still check that the loaded stable hash and the new one match. if let Some(dep_node_index) = self.dep_node_index_of_opt(&node) { + let _current_fingerprint = + crate::query::incremental_verify_ich(cx, result, &node, Some(hash_result)); + #[cfg(debug_assertions)] - { - let hashing_timer = cx.profiler().incr_result_hashing(); - let current_fingerprint = - cx.with_stable_hashing_context(|mut hcx| hash_result(&mut hcx, result)); - hashing_timer.finish_with_query_invocation_id(dep_node_index.into()); - data.current.record_edge(dep_node_index, node, current_fingerprint); - } + data.current.record_edge(dep_node_index, node, _current_fingerprint); return dep_node_index; } diff --git a/compiler/rustc_query_system/src/query/plumbing.rs b/compiler/rustc_query_system/src/query/plumbing.rs index f8d93a27d1c2b..eb5a35b34491b 100644 --- a/compiler/rustc_query_system/src/query/plumbing.rs +++ b/compiler/rustc_query_system/src/query/plumbing.rs @@ -3,6 +3,7 @@ //! manage the caches, and so forth. use crate::dep_graph::{DepContext, DepNode, DepNodeIndex, DepNodeParams}; +use crate::ich::StableHashingContext; use crate::query::caches::QueryCache; use crate::query::config::QueryVTable; use crate::query::job::{report_cycle, QueryInfo, QueryJob, QueryJobId, QueryJobInfo}; @@ -525,7 +526,7 @@ where if std::intrinsics::unlikely( try_verify || qcx.dep_context().sess().opts.unstable_opts.incremental_verify_ich, ) { - incremental_verify_ich(*qcx.dep_context(), &result, dep_node, query); + incremental_verify_ich(*qcx.dep_context(), &result, dep_node, query.hash_result); } return Some((result, dep_node_index)); @@ -558,39 +559,42 @@ where // // See issue #82920 for an example of a miscompilation that would get turned into // an ICE by this check - incremental_verify_ich(*qcx.dep_context(), &result, dep_node, query); + incremental_verify_ich(*qcx.dep_context(), &result, dep_node, query.hash_result); Some((result, dep_node_index)) } -#[instrument(skip(qcx, result, query), level = "debug")] -fn incremental_verify_ich( - qcx: Qcx::DepContext, +#[instrument(skip(tcx, result, hash_result), level = "debug")] +pub(crate) fn incremental_verify_ich( + tcx: Tcx, result: &V, - dep_node: &DepNode, - query: &QueryVTable, -) where - Qcx: QueryContext, + dep_node: &DepNode, + hash_result: Option, &V) -> Fingerprint>, +) -> Fingerprint +where + Tcx: DepContext, { assert!( - qcx.dep_graph().is_green(dep_node), + tcx.dep_graph().is_green(dep_node), "fingerprint for green query instance not loaded from cache: {:?}", dep_node, ); - let new_hash = query.hash_result.map_or(Fingerprint::ZERO, |f| { - qcx.with_stable_hashing_context(|mut hcx| f(&mut hcx, result)) + let new_hash = hash_result.map_or(Fingerprint::ZERO, |f| { + tcx.with_stable_hashing_context(|mut hcx| f(&mut hcx, result)) }); - let old_hash = qcx.dep_graph().prev_fingerprint_of(dep_node); + let old_hash = tcx.dep_graph().prev_fingerprint_of(dep_node); if Some(new_hash) != old_hash { incremental_verify_ich_failed( - qcx.sess(), + tcx.sess(), DebugArg::from(&dep_node), DebugArg::from(&result), ); } + + new_hash } // This DebugArg business is largely a mirror of std::fmt::ArgumentV1, which is From 9f2c6b0b09c1f93f922f6fcd46649c3e2110f42b Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 29 Oct 2022 13:04:38 +0000 Subject: [PATCH 198/244] Sanity check computed value for feeable queries. --- compiler/rustc_macros/src/query.rs | 9 +++++++++ compiler/rustc_query_impl/src/plumbing.rs | 13 +++++++++++++ .../rustc_query_system/src/query/config.rs | 5 +++-- .../rustc_query_system/src/query/plumbing.rs | 18 +++++++++++++++++- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_macros/src/query.rs b/compiler/rustc_macros/src/query.rs index 30c42757dbe8c..4047969724aa9 100644 --- a/compiler/rustc_macros/src/query.rs +++ b/compiler/rustc_macros/src/query.rs @@ -359,6 +359,15 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream { }); if modifiers.feedable.is_some() { + assert!(modifiers.anon.is_none(), "Query {name} cannot be both `feedable` and `anon`."); + assert!( + modifiers.eval_always.is_none(), + "Query {name} cannot be both `feedable` and `eval_always`." + ); + assert!( + modifiers.no_hash.is_none(), + "Query {name} cannot be both `feedable` and `no_hash`." + ); feedable_queries.extend(quote! { #(#doc_comments)* [#attribute_stream] fn #name(#arg) #result, diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index e69f6aa3a93a6..8d5d84c5db48a 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -252,6 +252,18 @@ macro_rules! depth_limit { }; } +macro_rules! feedable { + ([]) => {{ + false + }}; + ([(feedable) $($rest:tt)*]) => {{ + true + }}; + ([$other:tt $($modifiers:tt)*]) => { + feedable!([$($modifiers)*]) + }; +} + macro_rules! hash_result { ([]) => {{ Some(dep_graph::hash_result) @@ -491,6 +503,7 @@ macro_rules! define_queries { anon: is_anon!([$($modifiers)*]), eval_always: is_eval_always!([$($modifiers)*]), depth_limit: depth_limit!([$($modifiers)*]), + feedable: feedable!([$($modifiers)*]), dep_kind: dep_graph::DepKind::$name, hash_result: hash_result!([$($modifiers)*]), handle_cycle_error: handle_cycle_error!([$($modifiers)*]), diff --git a/compiler/rustc_query_system/src/query/config.rs b/compiler/rustc_query_system/src/query/config.rs index f40e174b7e79b..7d1b62ab10237 100644 --- a/compiler/rustc_query_system/src/query/config.rs +++ b/compiler/rustc_query_system/src/query/config.rs @@ -15,8 +15,8 @@ pub trait QueryConfig { const NAME: &'static str; type Key: Eq + Hash + Clone + Debug; - type Value; - type Stored: Clone; + type Value: Debug; + type Stored: Debug + Clone + std::borrow::Borrow; type Cache: QueryCache; @@ -45,6 +45,7 @@ pub struct QueryVTable { pub dep_kind: Qcx::DepKind, pub eval_always: bool, pub depth_limit: bool, + pub feedable: bool, pub compute: fn(Qcx::DepContext, K) -> V, pub hash_result: Option, &V) -> Fingerprint>, diff --git a/compiler/rustc_query_system/src/query/plumbing.rs b/compiler/rustc_query_system/src/query/plumbing.rs index eb5a35b34491b..848fa67e3df25 100644 --- a/compiler/rustc_query_system/src/query/plumbing.rs +++ b/compiler/rustc_query_system/src/query/plumbing.rs @@ -20,6 +20,7 @@ use rustc_data_structures::sync::Lock; use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed, FatalError}; use rustc_session::Session; use rustc_span::{Span, DUMMY_SP}; +use std::borrow::Borrow; use std::cell::Cell; use std::collections::hash_map::Entry; use std::fmt::Debug; @@ -370,11 +371,26 @@ where C: QueryCache, C::Key: Clone + DepNodeParams, C::Value: Value, + C::Stored: Debug + std::borrow::Borrow, Qcx: QueryContext, { match JobOwner::<'_, C::Key>::try_start(&qcx, state, span, key.clone()) { TryGetJob::NotYetStarted(job) => { - let (result, dep_node_index) = execute_job(qcx, key, dep_node, query, job.id); + let (result, dep_node_index) = execute_job(qcx, key.clone(), dep_node, query, job.id); + if query.feedable { + // We may have put a value inside the cache from inside the execution. + // Verify that it has the same hash as what we have now, to ensure consistency. + let _ = cache.lookup(&key, |cached_result, _| { + let hasher = query.hash_result.expect("feedable forbids no_hash"); + let old_hash = qcx.dep_context().with_stable_hashing_context(|mut hcx| hasher(&mut hcx, cached_result.borrow())); + let new_hash = qcx.dep_context().with_stable_hashing_context(|mut hcx| hasher(&mut hcx, &result)); + debug_assert_eq!( + old_hash, new_hash, + "Computed query value for {:?}({:?}) is inconsistent with fed value,\ncomputed={:#?}\nfed={:#?}", + query.dep_kind, key, result, cached_result, + ); + }); + } let result = job.complete(cache, result, dep_node_index); (result, Some(dep_node_index)) } From 731c002b2754bb028fcd28019a441cd962313f82 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 26 Nov 2022 13:54:45 +0000 Subject: [PATCH 199/244] Only allow feeding a value to newly created definitions. --- compiler/rustc_ast_lowering/src/lib.rs | 2 +- compiler/rustc_hir/src/definitions.rs | 8 ++--- compiler/rustc_middle/src/lib.rs | 2 ++ compiler/rustc_middle/src/query/mod.rs | 1 - compiler/rustc_middle/src/ty/context.rs | 47 +++++++++++++++++++------ compiler/rustc_middle/src/ty/query.rs | 20 ++--------- 6 files changed, 47 insertions(+), 33 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index ad6e72d015695..266d653b46dd3 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -497,7 +497,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { self.tcx.hir().def_key(self.local_def_id(node_id)), ); - let def_id = self.tcx.create_def(parent, data); + let def_id = self.tcx.create_def(parent, data).def_id; debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id); self.resolver.node_id_to_def_id.insert(node_id, def_id); diff --git a/compiler/rustc_hir/src/definitions.rs b/compiler/rustc_hir/src/definitions.rs index d85ac960f9b2f..dd37efb6983b4 100644 --- a/compiler/rustc_hir/src/definitions.rs +++ b/compiler/rustc_hir/src/definitions.rs @@ -368,10 +368,6 @@ impl Definitions { LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) } } - pub fn iter_local_def_id(&self) -> impl Iterator + '_ { - self.table.def_path_hashes.indices().map(|local_def_index| LocalDefId { local_def_index }) - } - #[inline(always)] pub fn local_def_path_hash_to_def_id( &self, @@ -389,6 +385,10 @@ impl Definitions { pub fn def_path_hash_to_def_index_map(&self) -> &DefPathHashMap { &self.table.def_path_hash_to_index } + + pub fn num_definitions(&self) -> usize { + self.table.def_path_hashes.len() + } } #[derive(Copy, Clone, PartialEq, Debug)] diff --git a/compiler/rustc_middle/src/lib.rs b/compiler/rustc_middle/src/lib.rs index 6bdf591fdd792..7e4063c2ffd78 100644 --- a/compiler/rustc_middle/src/lib.rs +++ b/compiler/rustc_middle/src/lib.rs @@ -30,8 +30,10 @@ #![feature(core_intrinsics)] #![feature(discriminant_kind)] #![feature(exhaustive_patterns)] +#![feature(generators)] #![feature(get_mut_unchecked)] #![feature(if_let_guard)] +#![feature(iter_from_generator)] #![feature(negative_impls)] #![feature(never_type)] #![feature(extern_types)] diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index f94fc34ec4786..38b72ec923193 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -165,7 +165,6 @@ rustc_queries! { } cache_on_disk_if { key.is_local() } separate_provide_extern - feedable } query collect_trait_impl_trait_tys(key: DefId) diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 3590aae51c3df..60e600f22a2c6 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -53,6 +53,7 @@ use rustc_hir::{ use rustc_index::vec::{Idx, IndexVec}; use rustc_macros::HashStable; use rustc_middle::mir::FakeReadCause; +use rustc_query_system::dep_graph::DepNodeIndex; use rustc_query_system::ich::StableHashingContext; use rustc_serialize::opaque::{FileEncodeResult, FileEncoder}; use rustc_session::config::{CrateType, OutputFilenames}; @@ -1009,6 +1010,14 @@ pub struct FreeRegionInfo { pub is_impl_item: bool, } +#[derive(Copy, Clone)] +pub struct TyCtxtFeed<'tcx> { + pub tcx: TyCtxt<'tcx>, + pub def_id: LocalDefId, + /// This struct should only be created by `create_def`. + _priv: (), +} + /// The central data structure of the compiler. It stores references /// to the various **arenas** and also houses the results of the /// various **compiler queries** that have been performed. See the @@ -1471,12 +1480,15 @@ impl<'tcx> TyCtxt<'tcx> { } /// Create a new definition within the incr. comp. engine. - pub fn create_def(self, parent: LocalDefId, data: hir::definitions::DefPathData) -> LocalDefId { + pub fn create_def( + self, + parent: LocalDefId, + data: hir::definitions::DefPathData, + ) -> TyCtxtFeed<'tcx> { // This function modifies `self.definitions` using a side-effect. // We need to ensure that these side effects are re-run by the incr. comp. engine. // Depending on the forever-red node will tell the graph that the calling query // needs to be re-evaluated. - use rustc_query_system::dep_graph::DepNodeIndex; self.dep_graph.read_index(DepNodeIndex::FOREVER_RED_NODE); // The following call has the side effect of modifying the tables inside `definitions`. @@ -1493,23 +1505,38 @@ impl<'tcx> TyCtxt<'tcx> { // This is fine because: // - those queries are `eval_always` so we won't miss their result changing; // - this write will have happened before these queries are called. - self.definitions.write().create_def(parent, data) + let def_id = self.definitions.write().create_def(parent, data); + + TyCtxtFeed { tcx: self, def_id, _priv: () } } pub fn iter_local_def_id(self) -> impl Iterator + 'tcx { - // Create a dependency to the crate to be sure we re-execute this when the amount of + // Create a dependency to the red node to be sure we re-execute this when the amount of // definitions change. - self.ensure().hir_crate(()); - // Leak a read lock once we start iterating on definitions, to prevent adding new ones - // while iterating. If some query needs to add definitions, it should be `ensure`d above. - let definitions = self.definitions.leak(); - definitions.iter_local_def_id() + self.dep_graph.read_index(DepNodeIndex::FOREVER_RED_NODE); + + let definitions = &self.definitions; + std::iter::from_generator(|| { + let mut i = 0; + + // Recompute the number of definitions each time, because our caller may be creating + // new ones. + while i < { definitions.read().num_definitions() } { + let local_def_index = rustc_span::def_id::DefIndex::from_usize(i); + yield LocalDefId { local_def_index }; + i += 1; + } + + // Leak a read lock once we finish iterating on definitions, to prevent adding new ones. + definitions.leak(); + }) } pub fn def_path_table(self) -> &'tcx rustc_hir::definitions::DefPathTable { // Create a dependency to the crate to be sure we re-execute this when the amount of // definitions change. - self.ensure().hir_crate(()); + self.dep_graph.read_index(DepNodeIndex::FOREVER_RED_NODE); + // Leak a read lock once we start iterating on definitions, to prevent adding new ones // while iterating. If some query needs to add definitions, it should be `ensure`d above. let definitions = self.definitions.leak(); diff --git a/compiler/rustc_middle/src/ty/query.rs b/compiler/rustc_middle/src/ty/query.rs index f44039879a613..ba85d5c849823 100644 --- a/compiler/rustc_middle/src/ty/query.rs +++ b/compiler/rustc_middle/src/ty/query.rs @@ -28,6 +28,7 @@ use crate::traits::query::{ }; use crate::traits::specialization_graph; use crate::traits::{self, ImplSource}; +use crate::ty::context::TyCtxtFeed; use crate::ty::fast_reject::SimplifiedType; use crate::ty::layout::TyAndLayout; use crate::ty::subst::{GenericArg, SubstsRef}; @@ -85,11 +86,6 @@ pub struct TyCtxtEnsure<'tcx> { pub tcx: TyCtxt<'tcx>, } -#[derive(Copy, Clone)] -pub struct TyCtxtFeed<'tcx> { - pub tcx: TyCtxt<'tcx>, -} - impl<'tcx> TyCtxt<'tcx> { /// Returns a transparent wrapper for `TyCtxt`, which ensures queries /// are executed instead of just returning their results. @@ -98,12 +94,6 @@ impl<'tcx> TyCtxt<'tcx> { TyCtxtEnsure { tcx: self } } - /// Returns a transparent wrapper for `TyCtxt`, for setting a result into a query. - #[inline(always)] - pub fn feed(self) -> TyCtxtFeed<'tcx> { - TyCtxtFeed { tcx: self } - } - /// Returns a transparent wrapper for `TyCtxt` which uses /// `span` as the location of queries performed through it. #[inline(always)] @@ -355,12 +345,8 @@ macro_rules! define_feedable { impl<'tcx> TyCtxtFeed<'tcx> { $($(#[$attr])* #[inline(always)] - pub fn $name( - self, - key: query_helper_param_ty!($($K)*), - value: $V, - ) -> query_stored::$name<'tcx> { - let key = key.into_query_param(); + pub fn $name(self, value: $V) -> query_stored::$name<'tcx> { + let key = self.def_id.into_query_param(); opt_remap_env_constness!([$($modifiers)*][key]); let tcx = self.tcx; From a0c38807cf5567cd380e80aa7c3ec8611d7e6604 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 27 Nov 2022 10:28:28 +0000 Subject: [PATCH 200/244] Feedable queries must allow hashing. --- compiler/rustc_middle/src/ty/query.rs | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/compiler/rustc_middle/src/ty/query.rs b/compiler/rustc_middle/src/ty/query.rs index ba85d5c849823..ae246c8195990 100644 --- a/compiler/rustc_middle/src/ty/query.rs +++ b/compiler/rustc_middle/src/ty/query.rs @@ -176,18 +176,6 @@ macro_rules! opt_remap_env_constness { }; } -macro_rules! hash_result { - ([]) => {{ - Some(dep_graph::hash_result) - }}; - ([(no_hash) $($rest:tt)*]) => {{ - None - }}; - ([$other:tt $($modifiers:tt)*]) => { - hash_result!([$($modifiers)*]) - }; -} - macro_rules! define_callbacks { ( $($(#[$attr:meta])* @@ -372,7 +360,7 @@ macro_rules! define_feedable { tcx, key, &value, - hash_result!([$($modifiers)*]).unwrap(), + dep_graph::hash_result, ); cache.complete(key, value, dep_node_index) })* From 6477fd8fc3f30af2f691a69ab9ba772c65ee4f0b Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Tue, 29 Nov 2022 18:49:37 +0000 Subject: [PATCH 201/244] Make TyCtxtFeed::def_id private. --- compiler/rustc_ast_lowering/src/lib.rs | 2 +- compiler/rustc_middle/src/ty/context.rs | 15 +++++++++++---- compiler/rustc_middle/src/ty/query.rs | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 266d653b46dd3..1d27970627854 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -497,7 +497,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { self.tcx.hir().def_key(self.local_def_id(node_id)), ); - let def_id = self.tcx.create_def(parent, data).def_id; + let def_id = self.tcx.create_def(parent, data).def_id(); debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id); self.resolver.node_id_to_def_id.insert(node_id, def_id); diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 60e600f22a2c6..62c00db2da1c7 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -1010,12 +1010,19 @@ pub struct FreeRegionInfo { pub is_impl_item: bool, } +/// This struct should only be created by `create_def`. #[derive(Copy, Clone)] pub struct TyCtxtFeed<'tcx> { pub tcx: TyCtxt<'tcx>, - pub def_id: LocalDefId, - /// This struct should only be created by `create_def`. - _priv: (), + // Do not allow direct access, as downstream code must not mutate this field. + def_id: LocalDefId, +} + +impl<'tcx> TyCtxtFeed<'tcx> { + #[inline(always)] + pub fn def_id(&self) -> LocalDefId { + self.def_id + } } /// The central data structure of the compiler. It stores references @@ -1507,7 +1514,7 @@ impl<'tcx> TyCtxt<'tcx> { // - this write will have happened before these queries are called. let def_id = self.definitions.write().create_def(parent, data); - TyCtxtFeed { tcx: self, def_id, _priv: () } + TyCtxtFeed { tcx: self, def_id } } pub fn iter_local_def_id(self) -> impl Iterator + 'tcx { diff --git a/compiler/rustc_middle/src/ty/query.rs b/compiler/rustc_middle/src/ty/query.rs index ae246c8195990..47c1379b308eb 100644 --- a/compiler/rustc_middle/src/ty/query.rs +++ b/compiler/rustc_middle/src/ty/query.rs @@ -334,7 +334,7 @@ macro_rules! define_feedable { $($(#[$attr])* #[inline(always)] pub fn $name(self, value: $V) -> query_stored::$name<'tcx> { - let key = self.def_id.into_query_param(); + let key = self.def_id().into_query_param(); opt_remap_env_constness!([$($modifiers)*][key]); let tcx = self.tcx; From 9512446a00dd203196dc0e716bd34d107299bc1d Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 14 Nov 2022 23:25:41 +0000 Subject: [PATCH 202/244] Explain why rematch_impl fails to be infallible --- compiler/rustc_trait_selection/src/traits/select/mod.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index a2d2d44fbc28f..a3c182d4becc9 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -2140,6 +2140,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { match self.match_impl(impl_def_id, impl_trait_ref, obligation) { Ok(substs) => substs, Err(()) => { + // FIXME: A rematch may fail when a candidate cache hit occurs + // on thefreshened form of the trait predicate, but the match + // fails for some reason that is not captured in the freshened + // cache key. For example, equating an impl trait ref against + // the placeholder trait ref may fail due the Generalizer relation + // raising a CyclicalTy error due to a sub_root_var relation + // for a variable being generalized... self.infcx.tcx.sess.delay_span_bug( obligation.cause.span, &format!( From 43f5d2b91071b4fe8c475fa1adc003b8005452f4 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Tue, 29 Nov 2022 11:37:32 -0700 Subject: [PATCH 203/244] rustdoc: add comment to confusing CSS `main { min-width: 0 }` This CSS was added in cad0fce2053d52b7ba04c458f4c124c8b5c6141e, but the reason why it was necessary was unclear. This comment makes it clear. --- src/librustdoc/html/static/css/rustdoc.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index cf5592da43205..5c9468392425a 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -314,7 +314,7 @@ main { position: relative; flex-grow: 1; padding: 10px 15px 40px 45px; - min-width: 0; + min-width: 0; /* avoid growing beyond the size limit */ } .source main { From bb982df771d02d2b6d2c30bd56cd2d25ba423a2b Mon Sep 17 00:00:00 2001 From: lcnr Date: Tue, 29 Nov 2022 20:32:48 +0000 Subject: [PATCH 204/244] move `candidate_from_obligation` out of assembly it doesn't belong there as it also does winnowing --- .../src/traits/select/candidate_assembly.rs | 213 +----------------- .../src/traits/select/mod.rs | 204 +++++++++++++++++ 2 files changed, 207 insertions(+), 210 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs index 627ed4674b0e9..fe5135661b5f8 100644 --- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs +++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs @@ -9,225 +9,18 @@ use hir::LangItem; use rustc_hir as hir; use rustc_infer::traits::ObligationCause; use rustc_infer::traits::{Obligation, SelectionError, TraitObligation}; -use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_middle::ty::{self, Ty, TypeVisitable}; use rustc_target::spec::abi::Abi; use crate::traits; -use crate::traits::coherence::Conflict; use crate::traits::query::evaluate_obligation::InferCtxtExt; -use crate::traits::{util, SelectionResult}; -use crate::traits::{ErrorReporting, Overflow, Unimplemented}; +use crate::traits::util; use super::BuiltinImplConditions; -use super::IntercrateAmbiguityCause; -use super::OverflowError; -use super::SelectionCandidate::{self, *}; -use super::{EvaluatedCandidate, SelectionCandidateSet, SelectionContext, TraitObligationStack}; +use super::SelectionCandidate::*; +use super::{SelectionCandidateSet, SelectionContext, TraitObligationStack}; impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { - #[instrument(level = "debug", skip(self), ret)] - pub(super) fn candidate_from_obligation<'o>( - &mut self, - stack: &TraitObligationStack<'o, 'tcx>, - ) -> SelectionResult<'tcx, SelectionCandidate<'tcx>> { - // Watch out for overflow. This intentionally bypasses (and does - // not update) the cache. - self.check_recursion_limit(&stack.obligation, &stack.obligation)?; - - // Check the cache. Note that we freshen the trait-ref - // separately rather than using `stack.fresh_trait_ref` -- - // this is because we want the unbound variables to be - // replaced with fresh types starting from index 0. - let cache_fresh_trait_pred = self.infcx.freshen(stack.obligation.predicate); - debug!(?cache_fresh_trait_pred); - debug_assert!(!stack.obligation.predicate.has_escaping_bound_vars()); - - if let Some(c) = - self.check_candidate_cache(stack.obligation.param_env, cache_fresh_trait_pred) - { - debug!("CACHE HIT"); - return c; - } - - // If no match, compute result and insert into cache. - // - // FIXME(nikomatsakis) -- this cache is not taking into - // account cycles that may have occurred in forming the - // candidate. I don't know of any specific problems that - // result but it seems awfully suspicious. - let (candidate, dep_node) = - self.in_task(|this| this.candidate_from_obligation_no_cache(stack)); - - debug!("CACHE MISS"); - self.insert_candidate_cache( - stack.obligation.param_env, - cache_fresh_trait_pred, - dep_node, - candidate.clone(), - ); - candidate - } - - fn candidate_from_obligation_no_cache<'o>( - &mut self, - stack: &TraitObligationStack<'o, 'tcx>, - ) -> SelectionResult<'tcx, SelectionCandidate<'tcx>> { - if let Err(conflict) = self.is_knowable(stack) { - debug!("coherence stage: not knowable"); - if self.intercrate_ambiguity_causes.is_some() { - debug!("evaluate_stack: intercrate_ambiguity_causes is some"); - // Heuristics: show the diagnostics when there are no candidates in crate. - if let Ok(candidate_set) = self.assemble_candidates(stack) { - let mut no_candidates_apply = true; - - for c in candidate_set.vec.iter() { - if self.evaluate_candidate(stack, &c)?.may_apply() { - no_candidates_apply = false; - break; - } - } - - if !candidate_set.ambiguous && no_candidates_apply { - let trait_ref = stack.obligation.predicate.skip_binder().trait_ref; - let self_ty = trait_ref.self_ty(); - let (trait_desc, self_desc) = with_no_trimmed_paths!({ - let trait_desc = trait_ref.print_only_trait_path().to_string(); - let self_desc = if self_ty.has_concrete_skeleton() { - Some(self_ty.to_string()) - } else { - None - }; - (trait_desc, self_desc) - }); - let cause = if let Conflict::Upstream = conflict { - IntercrateAmbiguityCause::UpstreamCrateUpdate { trait_desc, self_desc } - } else { - IntercrateAmbiguityCause::DownstreamCrate { trait_desc, self_desc } - }; - debug!(?cause, "evaluate_stack: pushing cause"); - self.intercrate_ambiguity_causes.as_mut().unwrap().insert(cause); - } - } - } - return Ok(None); - } - - let candidate_set = self.assemble_candidates(stack)?; - - if candidate_set.ambiguous { - debug!("candidate set contains ambig"); - return Ok(None); - } - - let candidates = candidate_set.vec; - - debug!(?stack, ?candidates, "assembled {} candidates", candidates.len()); - - // At this point, we know that each of the entries in the - // candidate set is *individually* applicable. Now we have to - // figure out if they contain mutual incompatibilities. This - // frequently arises if we have an unconstrained input type -- - // for example, we are looking for `$0: Eq` where `$0` is some - // unconstrained type variable. In that case, we'll get a - // candidate which assumes $0 == int, one that assumes `$0 == - // usize`, etc. This spells an ambiguity. - - let mut candidates = self.filter_impls(candidates, stack.obligation); - - // If there is more than one candidate, first winnow them down - // by considering extra conditions (nested obligations and so - // forth). We don't winnow if there is exactly one - // candidate. This is a relatively minor distinction but it - // can lead to better inference and error-reporting. An - // example would be if there was an impl: - // - // impl Vec { fn push_clone(...) { ... } } - // - // and we were to see some code `foo.push_clone()` where `boo` - // is a `Vec` and `Bar` does not implement `Clone`. If - // we were to winnow, we'd wind up with zero candidates. - // Instead, we select the right impl now but report "`Bar` does - // not implement `Clone`". - if candidates.len() == 1 { - return self.filter_reservation_impls(candidates.pop().unwrap(), stack.obligation); - } - - // Winnow, but record the exact outcome of evaluation, which - // is needed for specialization. Propagate overflow if it occurs. - let mut candidates = candidates - .into_iter() - .map(|c| match self.evaluate_candidate(stack, &c) { - Ok(eval) if eval.may_apply() => { - Ok(Some(EvaluatedCandidate { candidate: c, evaluation: eval })) - } - Ok(_) => Ok(None), - Err(OverflowError::Canonical) => Err(Overflow(OverflowError::Canonical)), - Err(OverflowError::ErrorReporting) => Err(ErrorReporting), - Err(OverflowError::Error(e)) => Err(Overflow(OverflowError::Error(e))), - }) - .flat_map(Result::transpose) - .collect::, _>>()?; - - debug!(?stack, ?candidates, "winnowed to {} candidates", candidates.len()); - - let needs_infer = stack.obligation.predicate.has_non_region_infer(); - - // If there are STILL multiple candidates, we can further - // reduce the list by dropping duplicates -- including - // resolving specializations. - if candidates.len() > 1 { - let mut i = 0; - while i < candidates.len() { - let is_dup = (0..candidates.len()).filter(|&j| i != j).any(|j| { - self.candidate_should_be_dropped_in_favor_of( - &candidates[i], - &candidates[j], - needs_infer, - ) - }); - if is_dup { - debug!(candidate = ?candidates[i], "Dropping candidate #{}/{}", i, candidates.len()); - candidates.swap_remove(i); - } else { - debug!(candidate = ?candidates[i], "Retaining candidate #{}/{}", i, candidates.len()); - i += 1; - - // If there are *STILL* multiple candidates, give up - // and report ambiguity. - if i > 1 { - debug!("multiple matches, ambig"); - return Ok(None); - } - } - } - } - - // If there are *NO* candidates, then there are no impls -- - // that we know of, anyway. Note that in the case where there - // are unbound type variables within the obligation, it might - // be the case that you could still satisfy the obligation - // from another crate by instantiating the type variables with - // a type from another crate that does have an impl. This case - // is checked for in `evaluate_stack` (and hence users - // who might care about this case, like coherence, should use - // that function). - if candidates.is_empty() { - // If there's an error type, 'downgrade' our result from - // `Err(Unimplemented)` to `Ok(None)`. This helps us avoid - // emitting additional spurious errors, since we're guaranteed - // to have emitted at least one. - if stack.obligation.predicate.references_error() { - debug!(?stack.obligation.predicate, "found error type in predicate, treating as ambiguous"); - return Ok(None); - } - return Err(Unimplemented); - } - - // Just one candidate left. - self.filter_reservation_impls(candidates.pop().unwrap().candidate, stack.obligation) - } - #[instrument(skip(self, stack), level = "debug")] pub(super) fn assemble_candidates<'o>( &mut self, diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index a2d2d44fbc28f..48f94fc11a636 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -30,6 +30,7 @@ use crate::traits::error_reporting::TypeErrCtxtExt; use crate::traits::project::ProjectAndUnifyResult; use crate::traits::project::ProjectionCacheKeyExt; use crate::traits::ProjectionCacheKey; +use crate::traits::Unimplemented; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::{FxHashSet, FxIndexSet}; use rustc_data_structures::stack::ensure_sufficient_stack; @@ -55,6 +56,7 @@ use std::fmt::{self, Display}; use std::iter; pub use rustc_middle::traits::select::*; +use rustc_middle::ty::print::with_no_trimmed_paths; mod candidate_assembly; mod confirmation; @@ -305,6 +307,208 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { self.candidate_from_obligation(&stack) } + #[instrument(level = "debug", skip(self), ret)] + fn candidate_from_obligation<'o>( + &mut self, + stack: &TraitObligationStack<'o, 'tcx>, + ) -> SelectionResult<'tcx, SelectionCandidate<'tcx>> { + // Watch out for overflow. This intentionally bypasses (and does + // not update) the cache. + self.check_recursion_limit(&stack.obligation, &stack.obligation)?; + + // Check the cache. Note that we freshen the trait-ref + // separately rather than using `stack.fresh_trait_ref` -- + // this is because we want the unbound variables to be + // replaced with fresh types starting from index 0. + let cache_fresh_trait_pred = self.infcx.freshen(stack.obligation.predicate); + debug!(?cache_fresh_trait_pred); + debug_assert!(!stack.obligation.predicate.has_escaping_bound_vars()); + + if let Some(c) = + self.check_candidate_cache(stack.obligation.param_env, cache_fresh_trait_pred) + { + debug!("CACHE HIT"); + return c; + } + + // If no match, compute result and insert into cache. + // + // FIXME(nikomatsakis) -- this cache is not taking into + // account cycles that may have occurred in forming the + // candidate. I don't know of any specific problems that + // result but it seems awfully suspicious. + let (candidate, dep_node) = + self.in_task(|this| this.candidate_from_obligation_no_cache(stack)); + + debug!("CACHE MISS"); + self.insert_candidate_cache( + stack.obligation.param_env, + cache_fresh_trait_pred, + dep_node, + candidate.clone(), + ); + candidate + } + + fn candidate_from_obligation_no_cache<'o>( + &mut self, + stack: &TraitObligationStack<'o, 'tcx>, + ) -> SelectionResult<'tcx, SelectionCandidate<'tcx>> { + if let Err(conflict) = self.is_knowable(stack) { + debug!("coherence stage: not knowable"); + if self.intercrate_ambiguity_causes.is_some() { + debug!("evaluate_stack: intercrate_ambiguity_causes is some"); + // Heuristics: show the diagnostics when there are no candidates in crate. + if let Ok(candidate_set) = self.assemble_candidates(stack) { + let mut no_candidates_apply = true; + + for c in candidate_set.vec.iter() { + if self.evaluate_candidate(stack, &c)?.may_apply() { + no_candidates_apply = false; + break; + } + } + + if !candidate_set.ambiguous && no_candidates_apply { + let trait_ref = stack.obligation.predicate.skip_binder().trait_ref; + let self_ty = trait_ref.self_ty(); + let (trait_desc, self_desc) = with_no_trimmed_paths!({ + let trait_desc = trait_ref.print_only_trait_path().to_string(); + let self_desc = if self_ty.has_concrete_skeleton() { + Some(self_ty.to_string()) + } else { + None + }; + (trait_desc, self_desc) + }); + let cause = if let Conflict::Upstream = conflict { + IntercrateAmbiguityCause::UpstreamCrateUpdate { trait_desc, self_desc } + } else { + IntercrateAmbiguityCause::DownstreamCrate { trait_desc, self_desc } + }; + debug!(?cause, "evaluate_stack: pushing cause"); + self.intercrate_ambiguity_causes.as_mut().unwrap().insert(cause); + } + } + } + return Ok(None); + } + + let candidate_set = self.assemble_candidates(stack)?; + + if candidate_set.ambiguous { + debug!("candidate set contains ambig"); + return Ok(None); + } + + let candidates = candidate_set.vec; + + debug!(?stack, ?candidates, "assembled {} candidates", candidates.len()); + + // At this point, we know that each of the entries in the + // candidate set is *individually* applicable. Now we have to + // figure out if they contain mutual incompatibilities. This + // frequently arises if we have an unconstrained input type -- + // for example, we are looking for `$0: Eq` where `$0` is some + // unconstrained type variable. In that case, we'll get a + // candidate which assumes $0 == int, one that assumes `$0 == + // usize`, etc. This spells an ambiguity. + + let mut candidates = self.filter_impls(candidates, stack.obligation); + + // If there is more than one candidate, first winnow them down + // by considering extra conditions (nested obligations and so + // forth). We don't winnow if there is exactly one + // candidate. This is a relatively minor distinction but it + // can lead to better inference and error-reporting. An + // example would be if there was an impl: + // + // impl Vec { fn push_clone(...) { ... } } + // + // and we were to see some code `foo.push_clone()` where `boo` + // is a `Vec` and `Bar` does not implement `Clone`. If + // we were to winnow, we'd wind up with zero candidates. + // Instead, we select the right impl now but report "`Bar` does + // not implement `Clone`". + if candidates.len() == 1 { + return self.filter_reservation_impls(candidates.pop().unwrap(), stack.obligation); + } + + // Winnow, but record the exact outcome of evaluation, which + // is needed for specialization. Propagate overflow if it occurs. + let mut candidates = candidates + .into_iter() + .map(|c| match self.evaluate_candidate(stack, &c) { + Ok(eval) if eval.may_apply() => { + Ok(Some(EvaluatedCandidate { candidate: c, evaluation: eval })) + } + Ok(_) => Ok(None), + Err(OverflowError::Canonical) => Err(Overflow(OverflowError::Canonical)), + Err(OverflowError::ErrorReporting) => Err(ErrorReporting), + Err(OverflowError::Error(e)) => Err(Overflow(OverflowError::Error(e))), + }) + .flat_map(Result::transpose) + .collect::, _>>()?; + + debug!(?stack, ?candidates, "winnowed to {} candidates", candidates.len()); + + let needs_infer = stack.obligation.predicate.has_non_region_infer(); + + // If there are STILL multiple candidates, we can further + // reduce the list by dropping duplicates -- including + // resolving specializations. + if candidates.len() > 1 { + let mut i = 0; + while i < candidates.len() { + let is_dup = (0..candidates.len()).filter(|&j| i != j).any(|j| { + self.candidate_should_be_dropped_in_favor_of( + &candidates[i], + &candidates[j], + needs_infer, + ) + }); + if is_dup { + debug!(candidate = ?candidates[i], "Dropping candidate #{}/{}", i, candidates.len()); + candidates.swap_remove(i); + } else { + debug!(candidate = ?candidates[i], "Retaining candidate #{}/{}", i, candidates.len()); + i += 1; + + // If there are *STILL* multiple candidates, give up + // and report ambiguity. + if i > 1 { + debug!("multiple matches, ambig"); + return Ok(None); + } + } + } + } + + // If there are *NO* candidates, then there are no impls -- + // that we know of, anyway. Note that in the case where there + // are unbound type variables within the obligation, it might + // be the case that you could still satisfy the obligation + // from another crate by instantiating the type variables with + // a type from another crate that does have an impl. This case + // is checked for in `evaluate_stack` (and hence users + // who might care about this case, like coherence, should use + // that function). + if candidates.is_empty() { + // If there's an error type, 'downgrade' our result from + // `Err(Unimplemented)` to `Ok(None)`. This helps us avoid + // emitting additional spurious errors, since we're guaranteed + // to have emitted at least one. + if stack.obligation.predicate.references_error() { + debug!(?stack.obligation.predicate, "found error type in predicate, treating as ambiguous"); + return Ok(None); + } + return Err(Unimplemented); + } + + // Just one candidate left. + self.filter_reservation_impls(candidates.pop().unwrap().candidate, stack.obligation) + } + /////////////////////////////////////////////////////////////////////////// // EVALUATION // From 66180dfad8999661068ab7065b379fa044a114c6 Mon Sep 17 00:00:00 2001 From: Joe Neeman Date: Tue, 29 Nov 2022 14:33:55 -0600 Subject: [PATCH 205/244] Run patchelf also on rust-analyzer-proc-macro-srv. --- src/bootstrap/bootstrap.py | 1 + src/bootstrap/download.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/bootstrap/bootstrap.py b/src/bootstrap/bootstrap.py index 57128685d9110..2d5018d934e2e 100644 --- a/src/bootstrap/bootstrap.py +++ b/src/bootstrap/bootstrap.py @@ -441,6 +441,7 @@ def download_toolchain(self): self.fix_bin_or_dylib("{}/bin/rustc".format(bin_root)) self.fix_bin_or_dylib("{}/bin/rustdoc".format(bin_root)) + self.fix_bin_or_dylib("{}/libexec/rust-analyzer-proc-macro-srv".format(bin_root)) lib_dir = "{}/lib".format(bin_root) for lib in os.listdir(lib_dir): if lib.endswith(".so"): diff --git a/src/bootstrap/download.rs b/src/bootstrap/download.rs index d0f389df97344..6ae283f32a5f3 100644 --- a/src/bootstrap/download.rs +++ b/src/bootstrap/download.rs @@ -360,6 +360,7 @@ impl Config { self.fix_bin_or_dylib(&bin_root.join("bin").join("rustc")); self.fix_bin_or_dylib(&bin_root.join("bin").join("rustdoc")); + self.fix_bin_or_dylib(&bin_root.join("libexec").join("rust-analyzer-proc-macro-srv")); let lib_dir = bin_root.join("lib"); for lib in t!(fs::read_dir(&lib_dir), lib_dir.display().to_string()) { let lib = t!(lib); From b788cb00716b099abf1fe4c5ae3cb3342911af15 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 30 Nov 2022 08:51:43 +1100 Subject: [PATCH 206/244] Add Nicholas Nethercote to `.mailmap`. Before: ``` $ git shortlog -se | grep Nethercote 517 Nicholas Nethercote 2 Nicholas Nethercote 560 Nicholas Nethercote ``` After ``` $ git shortlog -se | grep Nethercote 1079 Nicholas Nethercote ``` --- .mailmap | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.mailmap b/.mailmap index 4ef723e51468a..6816169948652 100644 --- a/.mailmap +++ b/.mailmap @@ -396,6 +396,8 @@ Nathaniel Herman Nathaniel Herman Ngo Iok Ui (Wu Yu Wei) Nicholas Baron +Nicholas Nethercote +Nicholas Nethercote Nick Platt Niclas Schwarzlose <15schnic@gmail.com> Nicolas Abram From bf4a62c381db9adb252143dacf6738d74ed0ba58 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 29 Nov 2022 11:50:03 +1100 Subject: [PATCH 207/244] Fix an ICE parsing a malformed literal in `concat_bytes!`. Fixes #104769. --- .../rustc_builtin_macros/src/concat_bytes.rs | 6 +++++- .../issue-104769-concat_bytes-invalid-literal.rs | 8 ++++++++ ...ue-104769-concat_bytes-invalid-literal.stderr | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/macros/issue-104769-concat_bytes-invalid-literal.rs create mode 100644 src/test/ui/macros/issue-104769-concat_bytes-invalid-literal.stderr diff --git a/compiler/rustc_builtin_macros/src/concat_bytes.rs b/compiler/rustc_builtin_macros/src/concat_bytes.rs index 87658e60e9d4e..161e3499584e2 100644 --- a/compiler/rustc_builtin_macros/src/concat_bytes.rs +++ b/compiler/rustc_builtin_macros/src/concat_bytes.rs @@ -2,6 +2,7 @@ use rustc_ast as ast; use rustc_ast::{ptr::P, tokenstream::TokenStream}; use rustc_errors::Applicability; use rustc_expand::base::{self, DummyResult}; +use rustc_session::errors::report_lit_error; use rustc_span::Span; /// Emits errors for literal expressions that are invalid inside and outside of an array. @@ -68,7 +69,10 @@ fn invalid_type_err( Ok(ast::LitKind::Int(_, _)) => { cx.span_err(span, "numeric literal is not a `u8`"); } - _ => unreachable!(), + Ok(ast::LitKind::ByteStr(_) | ast::LitKind::Byte(_)) => unreachable!(), + Err(err) => { + report_lit_error(&cx.sess.parse_sess, err, token_lit, span); + } } } diff --git a/src/test/ui/macros/issue-104769-concat_bytes-invalid-literal.rs b/src/test/ui/macros/issue-104769-concat_bytes-invalid-literal.rs new file mode 100644 index 0000000000000..24150376ef0b7 --- /dev/null +++ b/src/test/ui/macros/issue-104769-concat_bytes-invalid-literal.rs @@ -0,0 +1,8 @@ +#![feature(concat_bytes)] + +fn main() { + concat_bytes!(7Y); + //~^ ERROR invalid suffix `Y` for number literal + concat_bytes!(888888888888888888888888888888888888888); + //~^ ERROR integer literal is too large +} diff --git a/src/test/ui/macros/issue-104769-concat_bytes-invalid-literal.stderr b/src/test/ui/macros/issue-104769-concat_bytes-invalid-literal.stderr new file mode 100644 index 0000000000000..8d70faa494dbe --- /dev/null +++ b/src/test/ui/macros/issue-104769-concat_bytes-invalid-literal.stderr @@ -0,0 +1,16 @@ +error: invalid suffix `Y` for number literal + --> $DIR/issue-104769-concat_bytes-invalid-literal.rs:4:19 + | +LL | concat_bytes!(7Y); + | ^^ invalid suffix `Y` + | + = help: the suffix must be one of the numeric types (`u32`, `isize`, `f32`, etc.) + +error: integer literal is too large + --> $DIR/issue-104769-concat_bytes-invalid-literal.rs:6:19 + | +LL | concat_bytes!(888888888888888888888888888888888888888); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 2 previous errors + From b2e05f6400ca6b0fd0d6b09670547e7796cceb24 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 30 Nov 2022 13:01:17 +1100 Subject: [PATCH 208/244] Add bors to `.mailmap`. Before: ``` $ git shortlog -se | grep "\" 27755 bors 5729 bors[bot] <26634292+bors[bot]@users.noreply.github.com> 995 bors[bot] ``` After: ``` $ git shortlog -se | grep "\" 34479 bors ``` --- .mailmap | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.mailmap b/.mailmap index 4ef723e51468a..7ba7a9c07f836 100644 --- a/.mailmap +++ b/.mailmap @@ -73,6 +73,8 @@ Björn Steinbrink blake2-ppc boolean_coercion Boris Egorov +bors bors[bot] <26634292+bors[bot]@users.noreply.github.com> +bors bors[bot] Braden Nelson Brandon Sanderson Brandon Sanderson Brett Cannon Brett Cannon From 3593a5d74ec751cd93f0a12be0801fa2de173216 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 30 Nov 2022 13:08:43 +1100 Subject: [PATCH 209/244] Add dependabot to `.mailmap`. Before: ``` $ git shortlog -se | grep "depend" 44 dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> 6 dependabot-preview[bot] 4 dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 74 dependabot[bot] 93 dependabot[bot] ``` After: ``` $ git shortlog -se | grep "depend" 221 dependabot[bot] ``` --- .mailmap | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.mailmap b/.mailmap index 7ba7a9c07f836..13317ab61fb0a 100644 --- a/.mailmap +++ b/.mailmap @@ -141,6 +141,10 @@ David Ross David Wood Deadbeef Deadbeef +dependabot[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> +dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> +dependabot[bot] +dependabot[bot] Derek Chiang Derek Chiang (Enchi Jiang) DeveloperC Devin Ragotzy From a98254179b312f3d03c7ef57c53cdc590fc5c0b2 Mon Sep 17 00:00:00 2001 From: Jakob Degen Date: Sun, 27 Nov 2022 02:32:48 -0800 Subject: [PATCH 210/244] Support arbitrary `let` statements in custom mir --- library/core/src/intrinsics/mir.rs | 143 +++++++++++++++++- ...rbitrary_let.arbitrary_let.built.after.mir | 22 +++ .../mir-opt/building/custom/arbitrary_let.rs | 28 ++++ 3 files changed, 189 insertions(+), 4 deletions(-) create mode 100644 src/test/mir-opt/building/custom/arbitrary_let.arbitrary_let.built.after.mir create mode 100644 src/test/mir-opt/building/custom/arbitrary_let.rs diff --git a/library/core/src/intrinsics/mir.rs b/library/core/src/intrinsics/mir.rs index 1bacdc39148a1..6e3cf974119bf 100644 --- a/library/core/src/intrinsics/mir.rs +++ b/library/core/src/intrinsics/mir.rs @@ -90,10 +90,14 @@ pub macro mir { ( $(let $local_decl:ident $(: $local_decl_ty:ty)? ;)* - $entry_block:block + { + $($entry:tt)* + } $( - $block_name:ident = $block:block + $block_name:ident = { + $($block:tt)* + } )* ) => {{ // First, we declare all basic blocks. @@ -109,11 +113,22 @@ pub macro mir { let $local_decl $(: $local_decl_ty)? ; )* + ::core::intrinsics::mir::__internal_extract_let!($($entry)*); + $( + ::core::intrinsics::mir::__internal_extract_let!($($block)*); + )* + { // Finally, the contents of the basic blocks - $entry_block; + ::core::intrinsics::mir::__internal_remove_let!({ + {} + { $($entry)* } + }); $( - $block; + ::core::intrinsics::mir::__internal_remove_let!({ + {} + { $($block)* } + }); )* RET @@ -121,3 +136,123 @@ pub macro mir { } }} } + +/// Helper macro that extracts the `let` declarations out of a bunch of statements. +/// +/// This macro is written using the "statement muncher" strategy. Each invocation parses the first +/// statement out of the input, does the appropriate thing with it, and then recursively calls the +/// same macro on the remainder of the input. +#[doc(hidden)] +pub macro __internal_extract_let { + // If it's a `let` like statement, keep the `let` + ( + let $var:ident $(: $ty:ty)? = $expr:expr; $($rest:tt)* + ) => { + let $var $(: $ty)?; + ::core::intrinsics::mir::__internal_extract_let!($($rest)*); + }, + // Otherwise, output nothing + ( + $stmt:stmt; $($rest:tt)* + ) => { + ::core::intrinsics::mir::__internal_extract_let!($($rest)*); + }, + ( + $expr:expr + ) => {} +} + +/// Helper macro that removes the `let` declarations from a bunch of statements. +/// +/// Because expression position macros cannot expand to statements + expressions, we need to be +/// slightly creative here. The general strategy is also statement munching as above, but the output +/// of the macro is "stored" in the subsequent macro invocation. Easiest understood via example: +/// ```text +/// invoke!( +/// { +/// { +/// x = 5; +/// } +/// { +/// let d = e; +/// Call() +/// } +/// } +/// ) +/// ``` +/// becomes +/// ```text +/// invoke!( +/// { +/// { +/// x = 5; +/// d = e; +/// } +/// { +/// Call() +/// } +/// } +/// ) +/// ``` +#[doc(hidden)] +pub macro __internal_remove_let { + // If it's a `let` like statement, remove the `let` + ( + { + { + $($already_parsed:tt)* + } + { + let $var:ident $(: $ty:ty)? = $expr:expr; + $($rest:tt)* + } + } + ) => { ::core::intrinsics::mir::__internal_remove_let!( + { + { + $($already_parsed)* + $var = $expr; + } + { + $($rest)* + } + } + )}, + // Otherwise, keep going + ( + { + { + $($already_parsed:tt)* + } + { + $stmt:stmt; + $($rest:tt)* + } + } + ) => { ::core::intrinsics::mir::__internal_remove_let!( + { + { + $($already_parsed)* + $stmt; + } + { + $($rest)* + } + } + )}, + ( + { + { + $($already_parsed:tt)* + } + { + $expr:expr + } + } + ) => { + { + $($already_parsed)* + $expr + } + }, +} diff --git a/src/test/mir-opt/building/custom/arbitrary_let.arbitrary_let.built.after.mir b/src/test/mir-opt/building/custom/arbitrary_let.arbitrary_let.built.after.mir new file mode 100644 index 0000000000000..d8cef6244f408 --- /dev/null +++ b/src/test/mir-opt/building/custom/arbitrary_let.arbitrary_let.built.after.mir @@ -0,0 +1,22 @@ +// MIR for `arbitrary_let` after built + +fn arbitrary_let(_1: i32) -> i32 { + let mut _0: i32; // return place in scope 0 at $DIR/arbitrary_let.rs:+0:29: +0:32 + let mut _2: i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL + let mut _3: i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL + + bb0: { + _2 = _1; // scope 0 at $DIR/arbitrary_let.rs:+0:1: +0:32 + goto -> bb2; // scope 0 at $DIR/arbitrary_let.rs:+0:1: +0:32 + } + + bb1: { + _0 = _3; // scope 0 at $DIR/arbitrary_let.rs:+0:1: +0:32 + return; // scope 0 at $DIR/arbitrary_let.rs:+0:1: +0:32 + } + + bb2: { + _3 = _2; // scope 0 at $DIR/arbitrary_let.rs:+0:1: +0:32 + goto -> bb1; // scope 0 at $DIR/arbitrary_let.rs:+0:1: +0:32 + } +} diff --git a/src/test/mir-opt/building/custom/arbitrary_let.rs b/src/test/mir-opt/building/custom/arbitrary_let.rs new file mode 100644 index 0000000000000..776df3151ffd7 --- /dev/null +++ b/src/test/mir-opt/building/custom/arbitrary_let.rs @@ -0,0 +1,28 @@ +#![feature(custom_mir, core_intrinsics)] + +extern crate core; +use core::intrinsics::mir::*; +use core::ptr::{addr_of, addr_of_mut}; + +// EMIT_MIR arbitrary_let.arbitrary_let.built.after.mir +#[custom_mir(dialect = "built")] +fn arbitrary_let(x: i32) -> i32 { + mir!( + { + let y = x; + Goto(second) + } + third = { + RET = z; + Return() + } + second = { + let z = y; + Goto(third) + } + ) +} + +fn main() { + assert_eq!(arbitrary_let(5), 5); +} From 757810031764e4a0fce2a21eb5c8ba208ff7f6c1 Mon Sep 17 00:00:00 2001 From: Jakob Degen Date: Sun, 27 Nov 2022 18:48:04 -0800 Subject: [PATCH 211/244] Support most constant kinds in custom mir --- .../rustc_mir_build/src/build/custom/parse.rs | 13 +- .../src/build/custom/parse/instruction.rs | 24 ++- .../src/build/expr/as_constant.rs | 137 +++++++++--------- library/core/src/intrinsics/mir.rs | 29 ++++ .../custom/consts.consts.built.after.mir | 22 +++ src/test/mir-opt/building/custom/consts.rs | 23 +++ 6 files changed, 170 insertions(+), 78 deletions(-) create mode 100644 src/test/mir-opt/building/custom/consts.consts.built.after.mir create mode 100644 src/test/mir-opt/building/custom/consts.rs diff --git a/compiler/rustc_mir_build/src/build/custom/parse.rs b/compiler/rustc_mir_build/src/build/custom/parse.rs index 52cb0a4826d07..b2c5aead430da 100644 --- a/compiler/rustc_mir_build/src/build/custom/parse.rs +++ b/compiler/rustc_mir_build/src/build/custom/parse.rs @@ -23,6 +23,7 @@ macro_rules! parse_by_kind { ( $self:ident, $expr_id:expr, + $expr_name:pat, $expected:literal, $( @call($name:literal, $args:ident) => $call_expr:expr, @@ -33,6 +34,8 @@ macro_rules! parse_by_kind { ) => {{ let expr_id = $self.preparse($expr_id); let expr = &$self.thir[expr_id]; + debug!("Trying to parse {:?} as {}", expr.kind, $expected); + let $expr_name = expr; match &expr.kind { $( ExprKind::Call { ty, fun: _, args: $args, .. } if { @@ -137,10 +140,10 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> { /// This allows us to easily parse the basic blocks declarations, local declarations, and /// basic block definitions in order. pub fn parse_body(&mut self, expr_id: ExprId) -> PResult<()> { - let body = parse_by_kind!(self, expr_id, "whole body", + let body = parse_by_kind!(self, expr_id, _, "whole body", ExprKind::Block { block } => self.thir[*block].expr.unwrap(), ); - let (block_decls, rest) = parse_by_kind!(self, body, "body with block decls", + let (block_decls, rest) = parse_by_kind!(self, body, _, "body with block decls", ExprKind::Block { block } => { let block = &self.thir[*block]; (&block.stmts, block.expr.unwrap()) @@ -148,7 +151,7 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> { ); self.parse_block_decls(block_decls.iter().copied())?; - let (local_decls, rest) = parse_by_kind!(self, rest, "body with local decls", + let (local_decls, rest) = parse_by_kind!(self, rest, _, "body with local decls", ExprKind::Block { block } => { let block = &self.thir[*block]; (&block.stmts, block.expr.unwrap()) @@ -156,7 +159,7 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> { ); self.parse_local_decls(local_decls.iter().copied())?; - let block_defs = parse_by_kind!(self, rest, "body with block defs", + let block_defs = parse_by_kind!(self, rest, _, "body with block defs", ExprKind::Block { block } => &self.thir[*block].stmts, ); for (i, block_def) in block_defs.iter().enumerate() { @@ -223,7 +226,7 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> { } fn parse_block_def(&self, expr_id: ExprId) -> PResult> { - let block = parse_by_kind!(self, expr_id, "basic block", + let block = parse_by_kind!(self, expr_id, _, "basic block", ExprKind::Block { block } => &self.thir[*block], ); diff --git a/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs b/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs index 6d6176584f5f4..cd809807a1267 100644 --- a/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs +++ b/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs @@ -4,7 +4,7 @@ use super::{parse_by_kind, PResult, ParseCtxt}; impl<'tcx, 'body> ParseCtxt<'tcx, 'body> { pub fn parse_statement(&self, expr_id: ExprId) -> PResult> { - parse_by_kind!(self, expr_id, "statement", + parse_by_kind!(self, expr_id, _, "statement", @call("mir_retag", args) => { Ok(StatementKind::Retag(RetagKind::Default, Box::new(self.parse_place(args[0])?))) }, @@ -20,7 +20,7 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> { } pub fn parse_terminator(&self, expr_id: ExprId) -> PResult> { - parse_by_kind!(self, expr_id, "terminator", + parse_by_kind!(self, expr_id, _, "terminator", @call("mir_return", _args) => { Ok(TerminatorKind::Return) }, @@ -31,7 +31,7 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> { } fn parse_rvalue(&self, expr_id: ExprId) -> PResult> { - parse_by_kind!(self, expr_id, "rvalue", + parse_by_kind!(self, expr_id, _, "rvalue", ExprKind::Borrow { borrow_kind, arg } => Ok( Rvalue::Ref(self.tcx.lifetimes.re_erased, *borrow_kind, self.parse_place(*arg)?) ), @@ -43,14 +43,24 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> { } fn parse_operand(&self, expr_id: ExprId) -> PResult> { - parse_by_kind!(self, expr_id, "operand", + parse_by_kind!(self, expr_id, expr, "operand", @call("mir_move", args) => self.parse_place(args[0]).map(Operand::Move), + ExprKind::Literal { .. } + | ExprKind::NamedConst { .. } + | ExprKind::NonHirLiteral { .. } + | ExprKind::ZstLiteral { .. } + | ExprKind::ConstParam { .. } + | ExprKind::ConstBlock { .. } => { + Ok(Operand::Constant(Box::new( + crate::build::expr::as_constant::as_constant_inner(expr, |_| None, self.tcx) + ))) + }, _ => self.parse_place(expr_id).map(Operand::Copy), ) } fn parse_place(&self, expr_id: ExprId) -> PResult> { - parse_by_kind!(self, expr_id, "place", + parse_by_kind!(self, expr_id, _, "place", ExprKind::Deref { arg } => Ok( self.parse_place(*arg)?.project_deeper(&[PlaceElem::Deref], self.tcx) ), @@ -59,13 +69,13 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> { } fn parse_local(&self, expr_id: ExprId) -> PResult { - parse_by_kind!(self, expr_id, "local", + parse_by_kind!(self, expr_id, _, "local", ExprKind::VarRef { id } => Ok(self.local_map[id]), ) } fn parse_block(&self, expr_id: ExprId) -> PResult { - parse_by_kind!(self, expr_id, "basic block", + parse_by_kind!(self, expr_id, _, "basic block", ExprKind::VarRef { id } => Ok(self.block_map[id]), ) } diff --git a/compiler/rustc_mir_build/src/build/expr/as_constant.rs b/compiler/rustc_mir_build/src/build/expr/as_constant.rs index 32c0207cb680c..717c62315745b 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_constant.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_constant.rs @@ -8,7 +8,9 @@ use rustc_middle::mir::interpret::{ }; use rustc_middle::mir::*; use rustc_middle::thir::*; -use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, TyCtxt}; +use rustc_middle::ty::{ + self, CanonicalUserType, CanonicalUserTypeAnnotation, TyCtxt, UserTypeAnnotationIndex, +}; use rustc_span::DUMMY_SP; use rustc_target::abi::Size; @@ -19,84 +21,87 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let this = self; let tcx = this.tcx; let Expr { ty, temp_lifetime: _, span, ref kind } = *expr; - match *kind { + match kind { ExprKind::Scope { region_scope: _, lint_level: _, value } => { - this.as_constant(&this.thir[value]) - } - ExprKind::Literal { lit, neg } => { - let literal = - match lit_to_mir_constant(tcx, LitToConstInput { lit: &lit.node, ty, neg }) { - Ok(c) => c, - Err(LitToConstError::Reported(guar)) => { - ConstantKind::Ty(tcx.const_error_with_guaranteed(ty, guar)) - } - Err(LitToConstError::TypeError) => { - bug!("encountered type error in `lit_to_mir_constant") - } - }; - - Constant { span, user_ty: None, literal } + this.as_constant(&this.thir[*value]) } - ExprKind::NonHirLiteral { lit, ref user_ty } => { - let user_ty = user_ty.as_ref().map(|user_ty| { - this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { + _ => as_constant_inner( + expr, + |user_ty| { + Some(this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { span, user_ty: user_ty.clone(), inferred_ty: ty, - }) - }); - let literal = ConstantKind::Val(ConstValue::Scalar(Scalar::Int(lit)), ty); + })) + }, + tcx, + ), + } + } +} - Constant { span, user_ty: user_ty, literal } - } - ExprKind::ZstLiteral { ref user_ty } => { - let user_ty = user_ty.as_ref().map(|user_ty| { - this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { - span, - user_ty: user_ty.clone(), - inferred_ty: ty, - }) - }); - let literal = ConstantKind::Val(ConstValue::ZeroSized, ty); +pub fn as_constant_inner<'tcx>( + expr: &Expr<'tcx>, + push_cuta: impl FnMut(&Box>) -> Option, + tcx: TyCtxt<'tcx>, +) -> Constant<'tcx> { + let Expr { ty, temp_lifetime: _, span, ref kind } = *expr; + match *kind { + ExprKind::Literal { lit, neg } => { + let literal = + match lit_to_mir_constant(tcx, LitToConstInput { lit: &lit.node, ty, neg }) { + Ok(c) => c, + Err(LitToConstError::Reported(guar)) => { + ConstantKind::Ty(tcx.const_error_with_guaranteed(ty, guar)) + } + Err(LitToConstError::TypeError) => { + bug!("encountered type error in `lit_to_mir_constant") + } + }; - Constant { span, user_ty: user_ty, literal } - } - ExprKind::NamedConst { def_id, substs, ref user_ty } => { - let user_ty = user_ty.as_ref().map(|user_ty| { - this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { - span, - user_ty: user_ty.clone(), - inferred_ty: ty, - }) - }); + Constant { span, user_ty: None, literal } + } + ExprKind::NonHirLiteral { lit, ref user_ty } => { + let user_ty = user_ty.as_ref().map(push_cuta).flatten(); - let uneval = - mir::UnevaluatedConst::new(ty::WithOptConstParam::unknown(def_id), substs); - let literal = ConstantKind::Unevaluated(uneval, ty); + let literal = ConstantKind::Val(ConstValue::Scalar(Scalar::Int(lit)), ty); - Constant { user_ty, span, literal } - } - ExprKind::ConstParam { param, def_id: _ } => { - let const_param = tcx.mk_const(param, expr.ty); - let literal = ConstantKind::Ty(const_param); + Constant { span, user_ty: user_ty, literal } + } + ExprKind::ZstLiteral { ref user_ty } => { + let user_ty = user_ty.as_ref().map(push_cuta).flatten(); - Constant { user_ty: None, span, literal } - } - ExprKind::ConstBlock { did: def_id, substs } => { - let uneval = - mir::UnevaluatedConst::new(ty::WithOptConstParam::unknown(def_id), substs); - let literal = ConstantKind::Unevaluated(uneval, ty); + let literal = ConstantKind::Val(ConstValue::ZeroSized, ty); - Constant { user_ty: None, span, literal } - } - ExprKind::StaticRef { alloc_id, ty, .. } => { - let const_val = ConstValue::Scalar(Scalar::from_pointer(alloc_id.into(), &tcx)); - let literal = ConstantKind::Val(const_val, ty); + Constant { span, user_ty: user_ty, literal } + } + ExprKind::NamedConst { def_id, substs, ref user_ty } => { + let user_ty = user_ty.as_ref().map(push_cuta).flatten(); - Constant { span, user_ty: None, literal } - } - _ => span_bug!(span, "expression is not a valid constant {:?}", kind), + let uneval = mir::UnevaluatedConst::new(ty::WithOptConstParam::unknown(def_id), substs); + let literal = ConstantKind::Unevaluated(uneval, ty); + + Constant { user_ty, span, literal } + } + ExprKind::ConstParam { param, def_id: _ } => { + let const_param = tcx.mk_const(ty::ConstKind::Param(param), expr.ty); + let literal = ConstantKind::Ty(const_param); + + Constant { user_ty: None, span, literal } + } + ExprKind::ConstBlock { did: def_id, substs } => { + let uneval = mir::UnevaluatedConst::new(ty::WithOptConstParam::unknown(def_id), substs); + let literal = ConstantKind::Unevaluated(uneval, ty); + + Constant { user_ty: None, span, literal } + } + ExprKind::StaticRef { alloc_id, ty, .. } => { + let const_val = ConstValue::Scalar(Scalar::from_pointer(alloc_id.into(), &tcx)); + let literal = ConstantKind::Val(const_val, ty); + + Constant { span, user_ty: None, literal } } + _ => span_bug!(span, "expression is not a valid constant {:?}", kind), } } diff --git a/library/core/src/intrinsics/mir.rs b/library/core/src/intrinsics/mir.rs index 6e3cf974119bf..18011aa5e98a1 100644 --- a/library/core/src/intrinsics/mir.rs +++ b/library/core/src/intrinsics/mir.rs @@ -151,6 +151,13 @@ pub macro __internal_extract_let { let $var $(: $ty)?; ::core::intrinsics::mir::__internal_extract_let!($($rest)*); }, + // Due to #86730, we have to handle const blocks separately + ( + let $var:ident $(: $ty:ty)? = const $block:block; $($rest:tt)* + ) => { + let $var $(: $ty)?; + ::core::intrinsics::mir::__internal_extract_let!($($rest)*); + }, // Otherwise, output nothing ( $stmt:stmt; $($rest:tt)* @@ -218,6 +225,28 @@ pub macro __internal_remove_let { } } )}, + // Due to #86730 , we have to handle const blocks separately + ( + { + { + $($already_parsed:tt)* + } + { + let $var:ident $(: $ty:ty)? = const $block:block; + $($rest:tt)* + } + } + ) => { ::core::intrinsics::mir::__internal_remove_let!( + { + { + $($already_parsed)* + $var = const $block; + } + { + $($rest)* + } + } + )}, // Otherwise, keep going ( { diff --git a/src/test/mir-opt/building/custom/consts.consts.built.after.mir b/src/test/mir-opt/building/custom/consts.consts.built.after.mir new file mode 100644 index 0000000000000..e384cdeb465b9 --- /dev/null +++ b/src/test/mir-opt/building/custom/consts.consts.built.after.mir @@ -0,0 +1,22 @@ +// MIR for `consts` after built + +fn consts() -> () { + let mut _0: (); // return place in scope 0 at $DIR/consts.rs:10:27: 10:27 + let mut _1: u8; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL + let mut _2: i8; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL + let mut _3: u32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL + let mut _4: i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL + let mut _5: fn() {consts::<10>}; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL + + bb0: { + _1 = const 5_u8; // scope 0 at $DIR/consts.rs:+0:1: +0:26 + _2 = const _; // scope 0 at $DIR/consts.rs:+0:1: +0:26 + _3 = const C; // scope 0 at $DIR/consts.rs:+0:1: +0:26 + _4 = const _; // scope 0 at $DIR/consts.rs:+0:1: +0:26 + _5 = consts::<10>; // scope 0 at $DIR/consts.rs:+0:1: +0:26 + // mir::Constant + // + span: $DIR/consts.rs:16:18: 16:30 + // + literal: Const { ty: fn() {consts::<10>}, val: Value() } + return; // scope 0 at $DIR/consts.rs:+0:1: +0:26 + } +} diff --git a/src/test/mir-opt/building/custom/consts.rs b/src/test/mir-opt/building/custom/consts.rs new file mode 100644 index 0000000000000..98b087f1e585a --- /dev/null +++ b/src/test/mir-opt/building/custom/consts.rs @@ -0,0 +1,23 @@ +#![feature(custom_mir, core_intrinsics, inline_const)] + +extern crate core; +use core::intrinsics::mir::*; + +const D: i32 = 5; + +// EMIT_MIR consts.consts.built.after.mir +#[custom_mir(dialect = "built")] +fn consts() { + mir!({ + let _a = 5_u8; + let _b = const { 5_i8 }; + let _c = C; + let _d = D; + let _e = consts::<10>; + Return() + }) +} + +fn main() { + consts::<5>(); +} From 52ce1f7697924685ca7abf09be540bb21df46104 Mon Sep 17 00:00:00 2001 From: Jakob Degen Date: Sun, 27 Nov 2022 19:25:55 -0800 Subject: [PATCH 212/244] Support statics in custom mir --- .../src/build/custom/parse/instruction.rs | 23 ++++++++++++++++ library/core/src/intrinsics/mir.rs | 2 ++ src/test/mir-opt/building/custom/consts.rs | 13 +++++++++ .../custom/consts.statics.built.after.mir | 27 +++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 src/test/mir-opt/building/custom/consts.statics.built.after.mir diff --git a/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs b/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs index cd809807a1267..03206af33bfb5 100644 --- a/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs +++ b/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs @@ -1,3 +1,4 @@ +use rustc_middle::mir::interpret::{ConstValue, Scalar}; use rustc_middle::{mir::*, thir::*, ty}; use super::{parse_by_kind, PResult, ParseCtxt}; @@ -45,6 +46,8 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> { fn parse_operand(&self, expr_id: ExprId) -> PResult> { parse_by_kind!(self, expr_id, expr, "operand", @call("mir_move", args) => self.parse_place(args[0]).map(Operand::Move), + @call("mir_static", args) => self.parse_static(args[0]), + @call("mir_static_mut", args) => self.parse_static(args[0]), ExprKind::Literal { .. } | ExprKind::NamedConst { .. } | ExprKind::NonHirLiteral { .. } @@ -79,4 +82,24 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> { ExprKind::VarRef { id } => Ok(self.block_map[id]), ) } + + fn parse_static(&self, expr_id: ExprId) -> PResult> { + let expr_id = parse_by_kind!(self, expr_id, _, "static", + ExprKind::Deref { arg } => *arg, + ); + + parse_by_kind!(self, expr_id, expr, "static", + ExprKind::StaticRef { alloc_id, ty, .. } => { + let const_val = + ConstValue::Scalar(Scalar::from_pointer((*alloc_id).into(), &self.tcx)); + let literal = ConstantKind::Val(const_val, *ty); + + Ok(Operand::Constant(Box::new(Constant { + span: expr.span, + user_ty: None, + literal + }))) + }, + ) + } } diff --git a/library/core/src/intrinsics/mir.rs b/library/core/src/intrinsics/mir.rs index 18011aa5e98a1..8ba1c122884ca 100644 --- a/library/core/src/intrinsics/mir.rs +++ b/library/core/src/intrinsics/mir.rs @@ -80,6 +80,8 @@ define!("mir_goto", fn Goto(destination: BasicBlock) -> BasicBlock); define!("mir_retag", fn Retag(place: T)); define!("mir_retag_raw", fn RetagRaw(place: T)); define!("mir_move", fn Move(place: T) -> T); +define!("mir_static", fn Static(s: T) -> &'static T); +define!("mir_static_mut", fn StaticMut(s: T) -> *mut T); /// Convenience macro for generating custom MIR. /// diff --git a/src/test/mir-opt/building/custom/consts.rs b/src/test/mir-opt/building/custom/consts.rs index 98b087f1e585a..ff4fe1a93246e 100644 --- a/src/test/mir-opt/building/custom/consts.rs +++ b/src/test/mir-opt/building/custom/consts.rs @@ -18,6 +18,19 @@ fn consts() { }) } +static S: i32 = 5; +static mut T: i32 = 10; +// EMIT_MIR consts.statics.built.after.mir +#[custom_mir(dialect = "built")] +fn statics() { + mir!({ + let _a: &i32 = Static(S); + let _b: *mut i32 = StaticMut(T); + Return() + }) +} + fn main() { consts::<5>(); + statics(); } diff --git a/src/test/mir-opt/building/custom/consts.statics.built.after.mir b/src/test/mir-opt/building/custom/consts.statics.built.after.mir new file mode 100644 index 0000000000000..a193af729d565 --- /dev/null +++ b/src/test/mir-opt/building/custom/consts.statics.built.after.mir @@ -0,0 +1,27 @@ +// MIR for `statics` after built + +fn statics() -> () { + let mut _0: (); // return place in scope 0 at $DIR/consts.rs:25:14: 25:14 + let mut _1: &i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL + let mut _2: *mut i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL + + bb0: { + _1 = const {alloc1: &i32}; // scope 0 at $DIR/consts.rs:+0:1: +0:13 + // mir::Constant + // + span: $DIR/consts.rs:27:31: 27:32 + // + literal: Const { ty: &i32, val: Value(Scalar(alloc1)) } + _2 = const {alloc2: *mut i32}; // scope 0 at $DIR/consts.rs:+0:1: +0:13 + // mir::Constant + // + span: $DIR/consts.rs:28:38: 28:39 + // + literal: Const { ty: *mut i32, val: Value(Scalar(alloc2)) } + return; // scope 0 at $DIR/consts.rs:+0:1: +0:13 + } +} + +alloc2 (static: T, size: 4, align: 4) { + 0a 00 00 00 │ .... +} + +alloc1 (static: S, size: 4, align: 4) { + 05 00 00 00 │ .... +} From 5a34dbf193ac8cfb7dbe53b354614f2622f5682c Mon Sep 17 00:00:00 2001 From: Jakob Degen Date: Sun, 27 Nov 2022 21:23:39 -0800 Subject: [PATCH 213/244] Improve spans in custom mir --- compiler/rustc_mir_build/src/build/custom/mod.rs | 4 ++-- compiler/rustc_mir_build/src/build/custom/parse.rs | 12 ++++++++++-- compiler/rustc_mir_build/src/build/mod.rs | 2 +- .../arbitrary_let.arbitrary_let.built.after.mir | 12 ++++++------ .../building/custom/consts.consts.built.after.mir | 14 +++++++------- .../building/custom/consts.statics.built.after.mir | 8 ++++---- .../custom/references.immut_ref.built.after.mir | 10 +++++----- .../custom/references.mut_ref.built.after.mir | 10 +++++----- .../custom/simple_assign.simple.built.after.mir | 10 +++++----- .../simple_assign.simple_ref.built.after.mir | 4 ++-- 10 files changed, 47 insertions(+), 39 deletions(-) diff --git a/compiler/rustc_mir_build/src/build/custom/mod.rs b/compiler/rustc_mir_build/src/build/custom/mod.rs index 68d8766c90734..2412824efeb75 100644 --- a/compiler/rustc_mir_build/src/build/custom/mod.rs +++ b/compiler/rustc_mir_build/src/build/custom/mod.rs @@ -74,7 +74,7 @@ pub(super) fn build_custom_mir<'tcx>( let mut pctxt = ParseCtxt { tcx, thir, - source_info: SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE }, + source_scope: OUTERMOST_SOURCE_SCOPE, body: &mut body, local_map: FxHashMap::default(), block_map: FxHashMap::default(), @@ -128,7 +128,7 @@ fn parse_attribute(attr: &Attribute) -> MirPhase { struct ParseCtxt<'tcx, 'body> { tcx: TyCtxt<'tcx>, thir: &'body Thir<'tcx>, - source_info: SourceInfo, + source_scope: SourceScope, body: &'body mut Body<'tcx>, local_map: FxHashMap, diff --git a/compiler/rustc_mir_build/src/build/custom/parse.rs b/compiler/rustc_mir_build/src/build/custom/parse.rs index b2c5aead430da..d72770e70c7ec 100644 --- a/compiler/rustc_mir_build/src/build/custom/parse.rs +++ b/compiler/rustc_mir_build/src/build/custom/parse.rs @@ -233,15 +233,23 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> { let mut data = BasicBlockData::new(None); for stmt_id in &*block.stmts { let stmt = self.statement_as_expr(*stmt_id)?; + let span = self.thir[stmt].span; let statement = self.parse_statement(stmt)?; - data.statements.push(Statement { source_info: self.source_info, kind: statement }); + data.statements.push(Statement { + source_info: SourceInfo { span, scope: self.source_scope }, + kind: statement, + }); } let Some(trailing) = block.expr else { return Err(self.expr_error(expr_id, "terminator")) }; + let span = self.thir[trailing].span; let terminator = self.parse_terminator(trailing)?; - data.terminator = Some(Terminator { source_info: self.source_info, kind: terminator }); + data.terminator = Some(Terminator { + source_info: SourceInfo { span, scope: self.source_scope }, + kind: terminator, + }); Ok(data) } diff --git a/compiler/rustc_mir_build/src/build/mod.rs b/compiler/rustc_mir_build/src/build/mod.rs index 0b76122913ebe..b456e2aa37a27 100644 --- a/compiler/rustc_mir_build/src/build/mod.rs +++ b/compiler/rustc_mir_build/src/build/mod.rs @@ -492,7 +492,7 @@ fn construct_fn<'tcx>( arguments, return_ty, return_ty_span, - span, + span_with_body, custom_mir_attr, ); } diff --git a/src/test/mir-opt/building/custom/arbitrary_let.arbitrary_let.built.after.mir b/src/test/mir-opt/building/custom/arbitrary_let.arbitrary_let.built.after.mir index d8cef6244f408..20dd251e7e308 100644 --- a/src/test/mir-opt/building/custom/arbitrary_let.arbitrary_let.built.after.mir +++ b/src/test/mir-opt/building/custom/arbitrary_let.arbitrary_let.built.after.mir @@ -6,17 +6,17 @@ fn arbitrary_let(_1: i32) -> i32 { let mut _3: i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL bb0: { - _2 = _1; // scope 0 at $DIR/arbitrary_let.rs:+0:1: +0:32 - goto -> bb2; // scope 0 at $DIR/arbitrary_let.rs:+0:1: +0:32 + _2 = _1; // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL + goto -> bb2; // scope 0 at $DIR/arbitrary_let.rs:+4:13: +4:25 } bb1: { - _0 = _3; // scope 0 at $DIR/arbitrary_let.rs:+0:1: +0:32 - return; // scope 0 at $DIR/arbitrary_let.rs:+0:1: +0:32 + _0 = _3; // scope 0 at $DIR/arbitrary_let.rs:+7:13: +7:20 + return; // scope 0 at $DIR/arbitrary_let.rs:+8:13: +8:21 } bb2: { - _3 = _2; // scope 0 at $DIR/arbitrary_let.rs:+0:1: +0:32 - goto -> bb1; // scope 0 at $DIR/arbitrary_let.rs:+0:1: +0:32 + _3 = _2; // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL + goto -> bb1; // scope 0 at $DIR/arbitrary_let.rs:+12:13: +12:24 } } diff --git a/src/test/mir-opt/building/custom/consts.consts.built.after.mir b/src/test/mir-opt/building/custom/consts.consts.built.after.mir index e384cdeb465b9..ba753cfc20ca2 100644 --- a/src/test/mir-opt/building/custom/consts.consts.built.after.mir +++ b/src/test/mir-opt/building/custom/consts.consts.built.after.mir @@ -1,7 +1,7 @@ // MIR for `consts` after built fn consts() -> () { - let mut _0: (); // return place in scope 0 at $DIR/consts.rs:10:27: 10:27 + let mut _0: (); // return place in scope 0 at $DIR/consts.rs:+0:27: +0:27 let mut _1: u8; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL let mut _2: i8; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL let mut _3: u32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL @@ -9,14 +9,14 @@ fn consts() -> () { let mut _5: fn() {consts::<10>}; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL bb0: { - _1 = const 5_u8; // scope 0 at $DIR/consts.rs:+0:1: +0:26 - _2 = const _; // scope 0 at $DIR/consts.rs:+0:1: +0:26 - _3 = const C; // scope 0 at $DIR/consts.rs:+0:1: +0:26 - _4 = const _; // scope 0 at $DIR/consts.rs:+0:1: +0:26 - _5 = consts::<10>; // scope 0 at $DIR/consts.rs:+0:1: +0:26 + _1 = const 5_u8; // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL + _2 = const _; // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL + _3 = const C; // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL + _4 = const _; // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL + _5 = consts::<10>; // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL // mir::Constant // + span: $DIR/consts.rs:16:18: 16:30 // + literal: Const { ty: fn() {consts::<10>}, val: Value() } - return; // scope 0 at $DIR/consts.rs:+0:1: +0:26 + return; // scope 0 at $DIR/consts.rs:+7:9: +7:17 } } diff --git a/src/test/mir-opt/building/custom/consts.statics.built.after.mir b/src/test/mir-opt/building/custom/consts.statics.built.after.mir index a193af729d565..ee768e263ecdf 100644 --- a/src/test/mir-opt/building/custom/consts.statics.built.after.mir +++ b/src/test/mir-opt/building/custom/consts.statics.built.after.mir @@ -1,20 +1,20 @@ // MIR for `statics` after built fn statics() -> () { - let mut _0: (); // return place in scope 0 at $DIR/consts.rs:25:14: 25:14 + let mut _0: (); // return place in scope 0 at $DIR/consts.rs:+0:14: +0:14 let mut _1: &i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL let mut _2: *mut i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL bb0: { - _1 = const {alloc1: &i32}; // scope 0 at $DIR/consts.rs:+0:1: +0:13 + _1 = const {alloc1: &i32}; // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL // mir::Constant // + span: $DIR/consts.rs:27:31: 27:32 // + literal: Const { ty: &i32, val: Value(Scalar(alloc1)) } - _2 = const {alloc2: *mut i32}; // scope 0 at $DIR/consts.rs:+0:1: +0:13 + _2 = const {alloc2: *mut i32}; // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL // mir::Constant // + span: $DIR/consts.rs:28:38: 28:39 // + literal: Const { ty: *mut i32, val: Value(Scalar(alloc2)) } - return; // scope 0 at $DIR/consts.rs:+0:1: +0:13 + return; // scope 0 at $DIR/consts.rs:+4:9: +4:17 } } diff --git a/src/test/mir-opt/building/custom/references.immut_ref.built.after.mir b/src/test/mir-opt/building/custom/references.immut_ref.built.after.mir index 4a5ddde4081e2..4d38d45c0f479 100644 --- a/src/test/mir-opt/building/custom/references.immut_ref.built.after.mir +++ b/src/test/mir-opt/building/custom/references.immut_ref.built.after.mir @@ -5,10 +5,10 @@ fn immut_ref(_1: &i32) -> &i32 { let mut _2: *const i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL bb0: { - _2 = &raw const (*_1); // scope 0 at $DIR/references.rs:+0:1: +0:34 - Retag([raw] _2); // scope 0 at $DIR/references.rs:+0:1: +0:34 - _0 = &(*_2); // scope 0 at $DIR/references.rs:+0:1: +0:34 - Retag(_0); // scope 0 at $DIR/references.rs:+0:1: +0:34 - return; // scope 0 at $DIR/references.rs:+0:1: +0:34 + _2 = &raw const (*_1); // scope 0 at $DIR/references.rs:+5:13: +5:29 + Retag([raw] _2); // scope 0 at $DIR/references.rs:+6:13: +6:24 + _0 = &(*_2); // scope 0 at $DIR/references.rs:+7:13: +7:23 + Retag(_0); // scope 0 at $DIR/references.rs:+8:13: +8:23 + return; // scope 0 at $DIR/references.rs:+9:13: +9:21 } } diff --git a/src/test/mir-opt/building/custom/references.mut_ref.built.after.mir b/src/test/mir-opt/building/custom/references.mut_ref.built.after.mir index ec8509f69d14e..01bc8a9cd3580 100644 --- a/src/test/mir-opt/building/custom/references.mut_ref.built.after.mir +++ b/src/test/mir-opt/building/custom/references.mut_ref.built.after.mir @@ -5,10 +5,10 @@ fn mut_ref(_1: &mut i32) -> &mut i32 { let mut _2: *mut i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL bb0: { - _2 = &raw mut (*_1); // scope 0 at $DIR/references.rs:+0:1: +0:40 - Retag([raw] _2); // scope 0 at $DIR/references.rs:+0:1: +0:40 - _0 = &mut (*_2); // scope 0 at $DIR/references.rs:+0:1: +0:40 - Retag(_0); // scope 0 at $DIR/references.rs:+0:1: +0:40 - return; // scope 0 at $DIR/references.rs:+0:1: +0:40 + _2 = &raw mut (*_1); // scope 0 at $DIR/references.rs:+5:13: +5:33 + Retag([raw] _2); // scope 0 at $DIR/references.rs:+6:13: +6:24 + _0 = &mut (*_2); // scope 0 at $DIR/references.rs:+7:13: +7:26 + Retag(_0); // scope 0 at $DIR/references.rs:+8:13: +8:23 + return; // scope 0 at $DIR/references.rs:+9:13: +9:21 } } diff --git a/src/test/mir-opt/building/custom/simple_assign.simple.built.after.mir b/src/test/mir-opt/building/custom/simple_assign.simple.built.after.mir index a5a2834c2e1bf..d7560fde69c95 100644 --- a/src/test/mir-opt/building/custom/simple_assign.simple.built.after.mir +++ b/src/test/mir-opt/building/custom/simple_assign.simple.built.after.mir @@ -6,13 +6,13 @@ fn simple(_1: i32) -> i32 { let mut _3: i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL bb0: { - _2 = _1; // scope 0 at $DIR/simple_assign.rs:+0:1: +0:29 - goto -> bb1; // scope 0 at $DIR/simple_assign.rs:+0:1: +0:29 + _2 = _1; // scope 0 at $DIR/simple_assign.rs:+6:13: +6:22 + goto -> bb1; // scope 0 at $DIR/simple_assign.rs:+7:13: +7:23 } bb1: { - _3 = move _2; // scope 0 at $DIR/simple_assign.rs:+0:1: +0:29 - _0 = _3; // scope 0 at $DIR/simple_assign.rs:+0:1: +0:29 - return; // scope 0 at $DIR/simple_assign.rs:+0:1: +0:29 + _3 = move _2; // scope 0 at $DIR/simple_assign.rs:+11:13: +11:32 + _0 = _3; // scope 0 at $DIR/simple_assign.rs:+12:13: +12:24 + return; // scope 0 at $DIR/simple_assign.rs:+13:13: +13:21 } } diff --git a/src/test/mir-opt/building/custom/simple_assign.simple_ref.built.after.mir b/src/test/mir-opt/building/custom/simple_assign.simple_ref.built.after.mir index 6c90f0130a2e6..2b0e8f1047b53 100644 --- a/src/test/mir-opt/building/custom/simple_assign.simple_ref.built.after.mir +++ b/src/test/mir-opt/building/custom/simple_assign.simple_ref.built.after.mir @@ -4,7 +4,7 @@ fn simple_ref(_1: &mut i32) -> &mut i32 { let mut _0: &mut i32; // return place in scope 0 at $DIR/simple_assign.rs:+0:35: +0:43 bb0: { - _0 = move _1; // scope 0 at $DIR/simple_assign.rs:+0:1: +0:43 - return; // scope 0 at $DIR/simple_assign.rs:+0:1: +0:43 + _0 = move _1; // scope 0 at $DIR/simple_assign.rs:+2:9: +2:22 + return; // scope 0 at $DIR/simple_assign.rs:+3:9: +3:17 } } From 97f0c58b37ba7e1bd32ddf1c4558884302f68194 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Wed, 30 Nov 2022 13:31:11 +0900 Subject: [PATCH 214/244] report literal errors when `token_lit` has errors --- compiler/rustc_expand/src/base.rs | 6 +++++- src/test/ui/macros/issue-105011.rs | 3 +++ src/test/ui/macros/issue-105011.stderr | 8 ++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/macros/issue-105011.rs create mode 100644 src/test/ui/macros/issue-105011.stderr diff --git a/compiler/rustc_expand/src/base.rs b/compiler/rustc_expand/src/base.rs index 8955abebf1e0f..fc74182b87256 100644 --- a/compiler/rustc_expand/src/base.rs +++ b/compiler/rustc_expand/src/base.rs @@ -16,6 +16,7 @@ use rustc_errors::{ use rustc_lint_defs::builtin::PROC_MACRO_BACK_COMPAT; use rustc_lint_defs::{BufferedEarlyLint, BuiltinLintDiagnostics}; use rustc_parse::{self, parser, MACRO_ARGUMENTS}; +use rustc_session::errors::report_lit_error; use rustc_session::{parse::ParseSess, Limit, Session}; use rustc_span::def_id::{CrateNum, DefId, LocalDefId}; use rustc_span::edition::Edition; @@ -1245,7 +1246,10 @@ pub fn expr_to_spanned_string<'a>( Some((err, true)) } Ok(ast::LitKind::Err) => None, - Err(_) => None, + Err(err) => { + report_lit_error(&cx.sess.parse_sess, err, token_lit, expr.span); + None + } _ => Some((cx.struct_span_err(expr.span, err_msg), false)), }, ast::ExprKind::Err => None, diff --git a/src/test/ui/macros/issue-105011.rs b/src/test/ui/macros/issue-105011.rs new file mode 100644 index 0000000000000..da12c381464e1 --- /dev/null +++ b/src/test/ui/macros/issue-105011.rs @@ -0,0 +1,3 @@ +fn main() { + println!(""y); //~ ERROR suffixes on string literals are invalid +} diff --git a/src/test/ui/macros/issue-105011.stderr b/src/test/ui/macros/issue-105011.stderr new file mode 100644 index 0000000000000..e898af7faa30a --- /dev/null +++ b/src/test/ui/macros/issue-105011.stderr @@ -0,0 +1,8 @@ +error: suffixes on string literals are invalid + --> $DIR/issue-105011.rs:2:14 + | +LL | println!(""y); + | ^^^ invalid suffix `y` + +error: aborting due to previous error + From 02eaecc767b5f8deb1f1e3ddc7c0a793b6e7ff2d Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Wed, 30 Nov 2022 13:31:35 +0900 Subject: [PATCH 215/244] avoid an unnecessary `&str` to `String` conversion --- compiler/rustc_session/src/errors.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_session/src/errors.rs b/compiler/rustc_session/src/errors.rs index 2f7055e3cc5e8..9aa8a06c6d36e 100644 --- a/compiler/rustc_session/src/errors.rs +++ b/compiler/rustc_session/src/errors.rs @@ -197,12 +197,12 @@ pub enum UnleashedFeatureHelp { #[derive(Diagnostic)] #[diag(session_invalid_literal_suffix)] -pub(crate) struct InvalidLiteralSuffix { +pub(crate) struct InvalidLiteralSuffix<'a> { #[primary_span] #[label] pub span: Span, // FIXME(#100717) - pub kind: String, + pub kind: &'a str, pub suffix: Symbol, } @@ -311,11 +311,7 @@ pub fn report_lit_error(sess: &ParseSess, err: LitError, lit: token::Lit, span: LitError::LexerError => {} LitError::InvalidSuffix => { if let Some(suffix) = suffix { - sess.emit_err(InvalidLiteralSuffix { - span, - kind: format!("{}", kind.descr()), - suffix, - }); + sess.emit_err(InvalidLiteralSuffix { span, kind: kind.descr(), suffix }); } } LitError::InvalidIntSuffix => { From c502dee41ec38e6e0096abaa50a48e792e4c9d40 Mon Sep 17 00:00:00 2001 From: dswij Date: Wed, 30 Nov 2022 14:50:13 +0800 Subject: [PATCH 216/244] Use `snippet_with_context` instead of `_with_macro_callsite` --- clippy_lints/src/manual_let_else.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/clippy_lints/src/manual_let_else.rs b/clippy_lints/src/manual_let_else.rs index 91f0dc2a7170c..874d36ca9f4e3 100644 --- a/clippy_lints/src/manual_let_else.rs +++ b/clippy_lints/src/manual_let_else.rs @@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::higher::IfLetOrMatch; use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::peel_blocks; -use clippy_utils::source::{snippet, snippet_with_macro_callsite}; +use clippy_utils::source::snippet_with_context; use clippy_utils::ty::is_type_diagnostic_item; use clippy_utils::visitors::{for_each_expr, Descend}; use if_chain::if_chain; @@ -141,16 +141,10 @@ fn emit_manual_let_else(cx: &LateContext<'_>, span: Span, expr: &Expr<'_>, pat: // * unused binding collision detection with existing ones // * putting patterns with at the top level | inside () // for this to be machine applicable. - let app = Applicability::HasPlaceholders; - - let snippet_fn = if span.from_expansion() { - snippet - } else { - snippet_with_macro_callsite - }; - let sn_pat = snippet_fn(cx, pat.span, ""); - let sn_expr = snippet_fn(cx, expr.span, ""); - let sn_else = snippet_fn(cx, else_body.span, ""); + let mut app = Applicability::HasPlaceholders; + let (sn_pat, _) = snippet_with_context(cx, pat.span, span.ctxt(), "", &mut app); + let (sn_expr, _) = snippet_with_context(cx, expr.span, span.ctxt(), "", &mut app); + let (sn_else, _) = snippet_with_context(cx, else_body.span, span.ctxt(), "", &mut app); let else_bl = if matches!(else_body.kind, ExprKind::Block(..)) { sn_else.into_owned() From cc43c3d25d4c0a6cf3dc3df7d5b560656c652bab Mon Sep 17 00:00:00 2001 From: xFrednet Date: Tue, 29 Nov 2022 18:22:02 +0100 Subject: [PATCH 217/244] Move `unnecessary_unsafety_doc` to `pedantic` --- clippy_lints/src/doc.rs | 2 +- ...ssary_unsafe.rs => unnecessary_unsafety_doc.rs} | 1 + ...safe.stderr => unnecessary_unsafety_doc.stderr} | 14 +++++++------- 3 files changed, 9 insertions(+), 8 deletions(-) rename tests/ui/{doc_unnecessary_unsafe.rs => unnecessary_unsafety_doc.rs} (98%) rename tests/ui/{doc_unnecessary_unsafe.stderr => unnecessary_unsafety_doc.stderr} (81%) diff --git a/clippy_lints/src/doc.rs b/clippy_lints/src/doc.rs index 4557e43288542..49b5d8b60d8ba 100644 --- a/clippy_lints/src/doc.rs +++ b/clippy_lints/src/doc.rs @@ -253,7 +253,7 @@ declare_clippy_lint! { /// ``` #[clippy::version = "1.66.0"] pub UNNECESSARY_SAFETY_DOC, - style, + restriction, "`pub fn` or `pub trait` with `# Safety` docs" } diff --git a/tests/ui/doc_unnecessary_unsafe.rs b/tests/ui/unnecessary_unsafety_doc.rs similarity index 98% rename from tests/ui/doc_unnecessary_unsafe.rs rename to tests/ui/unnecessary_unsafety_doc.rs index d9e9363b0f4bd..c160e31afd33b 100644 --- a/tests/ui/doc_unnecessary_unsafe.rs +++ b/tests/ui/unnecessary_unsafety_doc.rs @@ -1,6 +1,7 @@ // aux-build:doc_unsafe_macros.rs #![allow(clippy::let_unit_value)] +#![warn(clippy::unnecessary_safety_doc)] #[macro_use] extern crate doc_unsafe_macros; diff --git a/tests/ui/doc_unnecessary_unsafe.stderr b/tests/ui/unnecessary_unsafety_doc.stderr similarity index 81% rename from tests/ui/doc_unnecessary_unsafe.stderr rename to tests/ui/unnecessary_unsafety_doc.stderr index 83b2efbb346be..72898c93fa113 100644 --- a/tests/ui/doc_unnecessary_unsafe.stderr +++ b/tests/ui/unnecessary_unsafety_doc.stderr @@ -1,5 +1,5 @@ error: safe function's docs have unnecessary `# Safety` section - --> $DIR/doc_unnecessary_unsafe.rs:18:1 + --> $DIR/unnecessary_unsafety_doc.rs:19:1 | LL | pub fn apocalypse(universe: &mut ()) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -7,31 +7,31 @@ LL | pub fn apocalypse(universe: &mut ()) { = note: `-D clippy::unnecessary-safety-doc` implied by `-D warnings` error: safe function's docs have unnecessary `# Safety` section - --> $DIR/doc_unnecessary_unsafe.rs:44:5 + --> $DIR/unnecessary_unsafety_doc.rs:45:5 | LL | pub fn republished() { | ^^^^^^^^^^^^^^^^^^^^ error: safe function's docs have unnecessary `# Safety` section - --> $DIR/doc_unnecessary_unsafe.rs:57:5 + --> $DIR/unnecessary_unsafety_doc.rs:58:5 | LL | fn documented(self); | ^^^^^^^^^^^^^^^^^^^^ error: docs for safe trait have unnecessary `# Safety` section - --> $DIR/doc_unnecessary_unsafe.rs:67:1 + --> $DIR/unnecessary_unsafety_doc.rs:68:1 | LL | pub trait DocumentedSafeTrait { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: safe function's docs have unnecessary `# Safety` section - --> $DIR/doc_unnecessary_unsafe.rs:95:5 + --> $DIR/unnecessary_unsafety_doc.rs:96:5 | LL | pub fn documented() -> Self { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: safe function's docs have unnecessary `# Safety` section - --> $DIR/doc_unnecessary_unsafe.rs:122:9 + --> $DIR/unnecessary_unsafety_doc.rs:123:9 | LL | pub fn drive() { | ^^^^^^^^^^^^^^ @@ -42,7 +42,7 @@ LL | very_safe!(); = note: this error originates in the macro `very_safe` (in Nightly builds, run with -Z macro-backtrace for more info) error: docs for safe trait have unnecessary `# Safety` section - --> $DIR/doc_unnecessary_unsafe.rs:146:1 + --> $DIR/unnecessary_unsafety_doc.rs:147:1 | LL | pub trait DocumentedSafeTraitWithImplementationHeader { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ From 42ff718d0fbc6f3c9a3386e65a16ab6c4ae89480 Mon Sep 17 00:00:00 2001 From: Georg Semmler Date: Wed, 30 Nov 2022 07:46:01 +0100 Subject: [PATCH 218/244] Add a regression test for #104322 --- src/test/ui/traits/issue-104322.rs | 80 ++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/test/ui/traits/issue-104322.rs diff --git a/src/test/ui/traits/issue-104322.rs b/src/test/ui/traits/issue-104322.rs new file mode 100644 index 0000000000000..dcc27f1f03ae1 --- /dev/null +++ b/src/test/ui/traits/issue-104322.rs @@ -0,0 +1,80 @@ +// build-pass +// +// Tests that overflows do not occur in certain situations +// related to generic diesel code + +use mini_diesel::*; + +pub trait HandleDelete {} + +pub fn handle_delete() +where + R: HasTable, + R::Table: HandleDelete + 'static, +{ +} + +impl HandleDelete for T +where + T: Table + HasTable + 'static, + K: 'static, + &'static K: Identifiable
, + T::PrimaryKey: EqAll<<&'static K as Identifiable>::Id>, + T::Query: FilterDsl<::Id>>::Output>, + Filter::Id>>::Output>: + IntoUpdateTarget
, +{ +} + +mod mini_diesel { + pub trait HasTable { + type Table: Table; + } + + pub trait Identifiable: HasTable { + type Id; + } + + pub trait EqAll { + type Output; + } + + pub trait IntoUpdateTarget: HasTable { + type WhereClause; + } + + pub trait Query { + type SqlType; + } + + pub trait AsQuery { + type Query: Query; + } + impl AsQuery for T { + type Query = Self; + } + + pub trait FilterDsl { + type Output; + } + + impl FilterDsl for T + where + T: Table, + T::Query: FilterDsl, + { + type Output = Filter; + } + + pub trait QuerySource { + type FromClause; + } + + pub trait Table: QuerySource + AsQuery + Sized { + type PrimaryKey; + } + + pub type Filter = >::Output; +} + +fn main() {} From 3a61ab61169984c3efbf5c4c3e3765591f83db72 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Wed, 30 Nov 2022 07:51:18 -0700 Subject: [PATCH 219/244] rustdoc: clean up sidebar link CSS Group `text-overflow: ellipses` along with `white-space: nowrap`. It makes no sense to try to apply it to links with `overflow-wrap: anywhere`, because it can't actually make ellipses when that's turned on. Simplify the selector for the 25rem left padding on sidebar links, to match up with the style for the container left padding that makes room for it. --- src/librustdoc/html/static/css/rustdoc.css | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index de882e66f43de..a8c75a6f0639d 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -480,15 +480,11 @@ ul.block, .block li { list-style: none; } -.block a, -.sidebar h2 a, -.sidebar h3 a { +.sidebar-elems a, +.sidebar > h2 a { display: block; - padding: 0.25rem; + padding: 0.25rem; /* 4px */ margin-left: -0.25rem; - - text-overflow: ellipsis; - overflow: hidden; } .sidebar h2 { @@ -522,6 +518,8 @@ ul.block, .block li { .sidebar-elems .block li a { white-space: nowrap; + text-overflow: ellipsis; + overflow: hidden; } .mobile-topbar { From e0eba9cafcc8aaf3821f4b0b9777954caf049498 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Tue, 29 Nov 2022 10:36:43 -0500 Subject: [PATCH 220/244] Don't cross contexts while building the suggestion for `redundant_closure_call` --- clippy_lints/src/collapsible_if.rs | 10 +-- clippy_lints/src/redundant_closure_call.rs | 4 +- clippy_utils/src/source.rs | 38 +++++++++-- clippy_utils/src/sugg.rs | 65 ++++++++++--------- tests/ui/redundant_closure_call_fixable.fixed | 12 ++++ tests/ui/redundant_closure_call_fixable.rs | 12 ++++ .../ui/redundant_closure_call_fixable.stderr | 24 ++++++- 7 files changed, 122 insertions(+), 43 deletions(-) diff --git a/clippy_lints/src/collapsible_if.rs b/clippy_lints/src/collapsible_if.rs index 90430b71a0ef9..b38e09dc09f46 100644 --- a/clippy_lints/src/collapsible_if.rs +++ b/clippy_lints/src/collapsible_if.rs @@ -160,11 +160,13 @@ fn check_collapsible_no_if_let(cx: &EarlyContext<'_>, expr: &ast::Expr, check: & if let ast::ExprKind::If(ref check_inner, ref content, None) = inner.kind; // Prevent triggering on `if c { if let a = b { .. } }`. if !matches!(check_inner.kind, ast::ExprKind::Let(..)); - if expr.span.ctxt() == inner.span.ctxt(); + let ctxt = expr.span.ctxt(); + if inner.span.ctxt() == ctxt; then { span_lint_and_then(cx, COLLAPSIBLE_IF, expr.span, "this `if` statement can be collapsed", |diag| { - let lhs = Sugg::ast(cx, check, ".."); - let rhs = Sugg::ast(cx, check_inner, ".."); + let mut app = Applicability::MachineApplicable; + let lhs = Sugg::ast(cx, check, "..", ctxt, &mut app); + let rhs = Sugg::ast(cx, check_inner, "..", ctxt, &mut app); diag.span_suggestion( expr.span, "collapse nested if block", @@ -173,7 +175,7 @@ fn check_collapsible_no_if_let(cx: &EarlyContext<'_>, expr: &ast::Expr, check: & lhs.and(&rhs), snippet_block(cx, content.span, "..", Some(expr.span)), ), - Applicability::MachineApplicable, // snippet + app, // snippet ); }); } diff --git a/clippy_lints/src/redundant_closure_call.rs b/clippy_lints/src/redundant_closure_call.rs index 8e675d34a1836..2a42e73488f19 100644 --- a/clippy_lints/src/redundant_closure_call.rs +++ b/clippy_lints/src/redundant_closure_call.rs @@ -81,8 +81,8 @@ impl EarlyLintPass for RedundantClosureCall { "try not to call a closure in the expression where it is declared", |diag| { if fn_decl.inputs.is_empty() { - let app = Applicability::MachineApplicable; - let mut hint = Sugg::ast(cx, body, ".."); + let mut app = Applicability::MachineApplicable; + let mut hint = Sugg::ast(cx, body, "..", closure.span.ctxt(), &mut app); if asyncness.is_async() { // `async x` is a syntax error, so it becomes `async { x }` diff --git a/clippy_utils/src/source.rs b/clippy_utils/src/source.rs index eacfa91ba556d..cd5dcfdaca34b 100644 --- a/clippy_utils/src/source.rs +++ b/clippy_utils/src/source.rs @@ -5,6 +5,7 @@ use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LintContext}; +use rustc_session::Session; use rustc_span::hygiene; use rustc_span::source_map::{original_sp, SourceMap}; use rustc_span::{BytePos, Pos, Span, SpanData, SyntaxContext, DUMMY_SP}; @@ -204,11 +205,20 @@ pub fn snippet_with_applicability<'a, T: LintContext>( span: Span, default: &'a str, applicability: &mut Applicability, +) -> Cow<'a, str> { + snippet_with_applicability_sess(cx.sess(), span, default, applicability) +} + +fn snippet_with_applicability_sess<'a>( + sess: &Session, + span: Span, + default: &'a str, + applicability: &mut Applicability, ) -> Cow<'a, str> { if *applicability != Applicability::Unspecified && span.from_expansion() { *applicability = Applicability::MaybeIncorrect; } - snippet_opt(cx, span).map_or_else( + snippet_opt_sess(sess, span).map_or_else( || { if *applicability == Applicability::MachineApplicable { *applicability = Applicability::HasPlaceholders; @@ -226,8 +236,12 @@ pub fn snippet_with_macro_callsite<'a, T: LintContext>(cx: &T, span: Span, defau } /// Converts a span to a code snippet. Returns `None` if not available. -pub fn snippet_opt(cx: &T, span: Span) -> Option { - cx.sess().source_map().span_to_snippet(span).ok() +pub fn snippet_opt(cx: &impl LintContext, span: Span) -> Option { + snippet_opt_sess(cx.sess(), span) +} + +fn snippet_opt_sess(sess: &Session, span: Span) -> Option { + sess.source_map().span_to_snippet(span).ok() } /// Converts a span (from a block) to a code snippet if available, otherwise use default. @@ -277,8 +291,8 @@ pub fn snippet_block<'a, T: LintContext>( /// Same as `snippet_block`, but adapts the applicability level by the rules of /// `snippet_with_applicability`. -pub fn snippet_block_with_applicability<'a, T: LintContext>( - cx: &T, +pub fn snippet_block_with_applicability<'a>( + cx: &impl LintContext, span: Span, default: &'a str, indent_relative_to: Option, @@ -299,7 +313,17 @@ pub fn snippet_block_with_applicability<'a, T: LintContext>( /// /// This will also return whether or not the snippet is a macro call. pub fn snippet_with_context<'a>( - cx: &LateContext<'_>, + cx: &impl LintContext, + span: Span, + outer: SyntaxContext, + default: &'a str, + applicability: &mut Applicability, +) -> (Cow<'a, str>, bool) { + snippet_with_context_sess(cx.sess(), span, outer, default, applicability) +} + +fn snippet_with_context_sess<'a>( + sess: &Session, span: Span, outer: SyntaxContext, default: &'a str, @@ -318,7 +342,7 @@ pub fn snippet_with_context<'a>( ); ( - snippet_with_applicability(cx, span, default, applicability), + snippet_with_applicability_sess(sess, span, default, applicability), is_macro_call, ) } diff --git a/clippy_utils/src/sugg.rs b/clippy_utils/src/sugg.rs index 3cacdb4937721..b66604f33db17 100644 --- a/clippy_utils/src/sugg.rs +++ b/clippy_utils/src/sugg.rs @@ -176,25 +176,28 @@ impl<'a> Sugg<'a> { } /// Prepare a suggestion from an expression. - pub fn ast(cx: &EarlyContext<'_>, expr: &ast::Expr, default: &'a str) -> Self { + pub fn ast( + cx: &EarlyContext<'_>, + expr: &ast::Expr, + default: &'a str, + ctxt: SyntaxContext, + app: &mut Applicability, + ) -> Self { use rustc_ast::ast::RangeLimits; - let snippet_without_expansion = |cx, span: Span, default| { - if span.from_expansion() { - snippet_with_macro_callsite(cx, span, default) - } else { - snippet(cx, span, default) - } - }; - + #[expect(clippy::match_wildcard_for_single_variants)] match expr.kind { + _ if expr.span.ctxt() != ctxt => Sugg::NonParen(snippet_with_context(cx, expr.span, ctxt, default, app).0), ast::ExprKind::AddrOf(..) | ast::ExprKind::Box(..) | ast::ExprKind::Closure { .. } | ast::ExprKind::If(..) | ast::ExprKind::Let(..) | ast::ExprKind::Unary(..) - | ast::ExprKind::Match(..) => Sugg::MaybeParen(snippet_without_expansion(cx, expr.span, default)), + | ast::ExprKind::Match(..) => match snippet_with_context(cx, expr.span, ctxt, default, app) { + (snip, false) => Sugg::MaybeParen(snip), + (snip, true) => Sugg::NonParen(snip), + }, ast::ExprKind::Async(..) | ast::ExprKind::Block(..) | ast::ExprKind::Break(..) @@ -224,45 +227,49 @@ impl<'a> Sugg<'a> { | ast::ExprKind::Array(..) | ast::ExprKind::While(..) | ast::ExprKind::Await(..) - | ast::ExprKind::Err => Sugg::NonParen(snippet_without_expansion(cx, expr.span, default)), + | ast::ExprKind::Err => Sugg::NonParen(snippet_with_context(cx, expr.span, ctxt, default, app).0), ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::HalfOpen) => Sugg::BinOp( AssocOp::DotDot, - lhs.as_ref() - .map_or("".into(), |lhs| snippet_without_expansion(cx, lhs.span, default)), - rhs.as_ref() - .map_or("".into(), |rhs| snippet_without_expansion(cx, rhs.span, default)), + lhs.as_ref().map_or("".into(), |lhs| { + snippet_with_context(cx, lhs.span, ctxt, default, app).0 + }), + rhs.as_ref().map_or("".into(), |rhs| { + snippet_with_context(cx, rhs.span, ctxt, default, app).0 + }), ), ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::Closed) => Sugg::BinOp( AssocOp::DotDotEq, - lhs.as_ref() - .map_or("".into(), |lhs| snippet_without_expansion(cx, lhs.span, default)), - rhs.as_ref() - .map_or("".into(), |rhs| snippet_without_expansion(cx, rhs.span, default)), + lhs.as_ref().map_or("".into(), |lhs| { + snippet_with_context(cx, lhs.span, ctxt, default, app).0 + }), + rhs.as_ref().map_or("".into(), |rhs| { + snippet_with_context(cx, rhs.span, ctxt, default, app).0 + }), ), ast::ExprKind::Assign(ref lhs, ref rhs, _) => Sugg::BinOp( AssocOp::Assign, - snippet_without_expansion(cx, lhs.span, default), - snippet_without_expansion(cx, rhs.span, default), + snippet_with_context(cx, lhs.span, ctxt, default, app).0, + snippet_with_context(cx, rhs.span, ctxt, default, app).0, ), ast::ExprKind::AssignOp(op, ref lhs, ref rhs) => Sugg::BinOp( astbinop2assignop(op), - snippet_without_expansion(cx, lhs.span, default), - snippet_without_expansion(cx, rhs.span, default), + snippet_with_context(cx, lhs.span, ctxt, default, app).0, + snippet_with_context(cx, rhs.span, ctxt, default, app).0, ), ast::ExprKind::Binary(op, ref lhs, ref rhs) => Sugg::BinOp( AssocOp::from_ast_binop(op.node), - snippet_without_expansion(cx, lhs.span, default), - snippet_without_expansion(cx, rhs.span, default), + snippet_with_context(cx, lhs.span, ctxt, default, app).0, + snippet_with_context(cx, rhs.span, ctxt, default, app).0, ), ast::ExprKind::Cast(ref lhs, ref ty) => Sugg::BinOp( AssocOp::As, - snippet_without_expansion(cx, lhs.span, default), - snippet_without_expansion(cx, ty.span, default), + snippet_with_context(cx, lhs.span, ctxt, default, app).0, + snippet_with_context(cx, ty.span, ctxt, default, app).0, ), ast::ExprKind::Type(ref lhs, ref ty) => Sugg::BinOp( AssocOp::Colon, - snippet_without_expansion(cx, lhs.span, default), - snippet_without_expansion(cx, ty.span, default), + snippet_with_context(cx, lhs.span, ctxt, default, app).0, + snippet_with_context(cx, ty.span, ctxt, default, app).0, ), } } diff --git a/tests/ui/redundant_closure_call_fixable.fixed b/tests/ui/redundant_closure_call_fixable.fixed index 7cd687c95a003..c0e49ff4caa74 100644 --- a/tests/ui/redundant_closure_call_fixable.fixed +++ b/tests/ui/redundant_closure_call_fixable.fixed @@ -25,4 +25,16 @@ fn main() { x * y }; let d = async { something().await }; + + macro_rules! m { + () => { + 0 + }; + } + macro_rules! m2 { + () => { + m!() + }; + } + m2!(); } diff --git a/tests/ui/redundant_closure_call_fixable.rs b/tests/ui/redundant_closure_call_fixable.rs index 37e4d22386415..9e6e54348a8c2 100644 --- a/tests/ui/redundant_closure_call_fixable.rs +++ b/tests/ui/redundant_closure_call_fixable.rs @@ -25,4 +25,16 @@ fn main() { x * y })(); let d = (async || something().await)(); + + macro_rules! m { + () => { + (|| 0)() + }; + } + macro_rules! m2 { + () => { + (|| m!())() + }; + } + m2!(); } diff --git a/tests/ui/redundant_closure_call_fixable.stderr b/tests/ui/redundant_closure_call_fixable.stderr index 56a8e57c0c362..d71bcba2a8200 100644 --- a/tests/ui/redundant_closure_call_fixable.stderr +++ b/tests/ui/redundant_closure_call_fixable.stderr @@ -52,5 +52,27 @@ error: try not to call a closure in the expression where it is declared LL | let d = (async || something().await)(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `async { something().await }` -error: aborting due to 4 previous errors +error: try not to call a closure in the expression where it is declared + --> $DIR/redundant_closure_call_fixable.rs:36:13 + | +LL | (|| m!())() + | ^^^^^^^^^^^ help: try doing something like: `m!()` +... +LL | m2!(); + | ----- in this macro invocation + | + = note: this error originates in the macro `m2` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: try not to call a closure in the expression where it is declared + --> $DIR/redundant_closure_call_fixable.rs:31:13 + | +LL | (|| 0)() + | ^^^^^^^^ help: try doing something like: `0` +... +LL | m2!(); + | ----- in this macro invocation + | + = note: this error originates in the macro `m` which comes from the expansion of the macro `m2` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 6 previous errors From 73f4546d24bc00f032d3761fa966f9f5927a8f76 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Wed, 30 Nov 2022 10:48:53 -0500 Subject: [PATCH 221/244] Fix `unnecessary_cast` suggestion when taking a reference --- clippy_lints/src/casts/unnecessary_cast.rs | 6 +++++- tests/ui/unnecessary_cast.fixed | 3 +++ tests/ui/unnecessary_cast.rs | 3 +++ tests/ui/unnecessary_cast.stderr | 18 ++++++++++++------ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/clippy_lints/src/casts/unnecessary_cast.rs b/clippy_lints/src/casts/unnecessary_cast.rs index ecc8a8de97b93..7e23318076cf2 100644 --- a/clippy_lints/src/casts/unnecessary_cast.rs +++ b/clippy_lints/src/casts/unnecessary_cast.rs @@ -90,7 +90,11 @@ pub(super) fn check<'tcx>( expr.span, &format!("casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)"), "try", - cast_str, + if get_parent_expr(cx, expr).map_or(false, |e| matches!(e.kind, ExprKind::AddrOf(..))) { + format!("{{ {cast_str} }}") + } else { + cast_str + }, Applicability::MachineApplicable, ); return true; diff --git a/tests/ui/unnecessary_cast.fixed b/tests/ui/unnecessary_cast.fixed index f234d4473c3e9..2f7e2997e739d 100644 --- a/tests/ui/unnecessary_cast.fixed +++ b/tests/ui/unnecessary_cast.fixed @@ -96,6 +96,9 @@ mod fixable { let _ = 1 as I32Alias; let _ = &1 as &I32Alias; + + let x = 1i32; + let _ = &{ x }; } type I32Alias = i32; diff --git a/tests/ui/unnecessary_cast.rs b/tests/ui/unnecessary_cast.rs index 855a4efa03411..54dd46ba59f10 100644 --- a/tests/ui/unnecessary_cast.rs +++ b/tests/ui/unnecessary_cast.rs @@ -96,6 +96,9 @@ mod fixable { let _ = 1 as I32Alias; let _ = &1 as &I32Alias; + + let x = 1i32; + let _ = &(x as i32); } type I32Alias = i32; diff --git a/tests/ui/unnecessary_cast.stderr b/tests/ui/unnecessary_cast.stderr index 934db0e86e662..fcee4ee2a65cc 100644 --- a/tests/ui/unnecessary_cast.stderr +++ b/tests/ui/unnecessary_cast.stderr @@ -150,35 +150,41 @@ error: casting float literal to `f32` is unnecessary LL | let _ = -1.0 as f32; | ^^^^^^^^^^^ help: try: `-1.0_f32` +error: casting to the same type is unnecessary (`i32` -> `i32`) + --> $DIR/unnecessary_cast.rs:101:18 + | +LL | let _ = &(x as i32); + | ^^^^^^^^^^ help: try: `{ x }` + error: casting integer literal to `i32` is unnecessary - --> $DIR/unnecessary_cast.rs:104:22 + --> $DIR/unnecessary_cast.rs:107:22 | LL | let _: i32 = -(1) as i32; | ^^^^^^^^^^^ help: try: `-1_i32` error: casting integer literal to `i64` is unnecessary - --> $DIR/unnecessary_cast.rs:106:22 + --> $DIR/unnecessary_cast.rs:109:22 | LL | let _: i64 = -(1) as i64; | ^^^^^^^^^^^ help: try: `-1_i64` error: casting float literal to `f64` is unnecessary - --> $DIR/unnecessary_cast.rs:113:22 + --> $DIR/unnecessary_cast.rs:116:22 | LL | let _: f64 = (-8.0 as f64).exp(); | ^^^^^^^^^^^^^ help: try: `(-8.0_f64)` error: casting float literal to `f64` is unnecessary - --> $DIR/unnecessary_cast.rs:115:23 + --> $DIR/unnecessary_cast.rs:118:23 | LL | let _: f64 = -(8.0 as f64).exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior | ^^^^^^^^^^^^ help: try: `8.0_f64` error: casting to the same type is unnecessary (`f32` -> `f32`) - --> $DIR/unnecessary_cast.rs:123:20 + --> $DIR/unnecessary_cast.rs:126:20 | LL | let _num = foo() as f32; | ^^^^^^^^^^^^ help: try: `foo()` -error: aborting due to 30 previous errors +error: aborting due to 31 previous errors From 2405e607696698e5d9cf0d2eec46bd3fbc339e5b Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Wed, 30 Nov 2022 09:00:02 -0700 Subject: [PATCH 222/244] rustdoc: remove redundant CSS `div.desc { display: block }` DIV tags have block display by default. It is from when this rule used to target a SPAN tag, but became redundant in 4bd6748bb9b73c210558498070ae0b7ed8193ddf. --- src/librustdoc/html/static/css/rustdoc.css | 1 - 1 file changed, 1 deletion(-) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index de882e66f43de..85bd73db52472 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -896,7 +896,6 @@ so that we can apply CSS-filters to change the arrow color in themes */ white-space: nowrap; text-overflow: ellipsis; overflow: hidden; - display: block; } .search-results a:hover, From 2d32b403596d6ff152ccce3f1136b695698a3cc8 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Wed, 30 Nov 2022 11:15:49 -0500 Subject: [PATCH 223/244] Don't lint `explicit_auto_deref` when the initial type is neither a reference, nor a receiver --- clippy_lints/src/dereference.rs | 17 +++++++++++------ tests/ui/explicit_auto_deref.fixed | 4 ++++ tests/ui/explicit_auto_deref.rs | 4 ++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/dereference.rs b/clippy_lints/src/dereference.rs index 7de117d554917..57c30661e8823 100644 --- a/clippy_lints/src/dereference.rs +++ b/clippy_lints/src/dereference.rs @@ -289,23 +289,24 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> { let (position, adjustments) = walk_parents(cx, &mut self.possible_borrowers, expr, &self.msrv); match kind { RefOp::Deref => { + let sub_ty = typeck.expr_ty(sub_expr); if let Position::FieldAccess { name, of_union: false, } = position - && !ty_contains_field(typeck.expr_ty(sub_expr), name) + && !ty_contains_field(sub_ty, name) { self.state = Some(( State::ExplicitDerefField { name }, StateData { span: expr.span, hir_id: expr.hir_id, position }, )); - } else if position.is_deref_stable() { + } else if position.is_deref_stable() && sub_ty.is_ref() { self.state = Some(( State::ExplicitDeref { mutability: None }, StateData { span: expr.span, hir_id: expr.hir_id, position }, )); } - } + }, RefOp::Method(target_mut) if !is_lint_allowed(cx, EXPLICIT_DEREF_METHODS, expr.hir_id) && position.lint_explicit_deref() => @@ -320,7 +321,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> { StateData { span: expr.span, hir_id: expr.hir_id, - position + position, }, )); }, @@ -394,7 +395,11 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> { msg, snip_expr, }), - StateData { span: expr.span, hir_id: expr.hir_id, position }, + StateData { + span: expr.span, + hir_id: expr.hir_id, + position, + }, )); } else if position.is_deref_stable() // Auto-deref doesn't combine with other adjustments @@ -406,7 +411,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> { StateData { span: expr.span, hir_id: expr.hir_id, - position + position, }, )); } diff --git a/tests/ui/explicit_auto_deref.fixed b/tests/ui/explicit_auto_deref.fixed index 59ff5e4040a3d..475fae5e823b3 100644 --- a/tests/ui/explicit_auto_deref.fixed +++ b/tests/ui/explicit_auto_deref.fixed @@ -277,4 +277,8 @@ fn main() { unimplemented!() } let _: String = takes_assoc(&*String::new()); + + // Issue #9901 + fn takes_ref(_: &i32) {} + takes_ref(*Box::new(&0i32)); } diff --git a/tests/ui/explicit_auto_deref.rs b/tests/ui/explicit_auto_deref.rs index bcfb60c327886..c1894258f4d84 100644 --- a/tests/ui/explicit_auto_deref.rs +++ b/tests/ui/explicit_auto_deref.rs @@ -277,4 +277,8 @@ fn main() { unimplemented!() } let _: String = takes_assoc(&*String::new()); + + // Issue #9901 + fn takes_ref(_: &i32) {} + takes_ref(*Box::new(&0i32)); } From f6e8580d638c87fd3698e14e481e3df50adbc0a8 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Tue, 29 Nov 2022 18:01:58 -0500 Subject: [PATCH 224/244] Add Nicholas Bishop to `.mailmap` --- .mailmap | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.mailmap b/.mailmap index 11f6cdec9528c..82892d57688ea 100644 --- a/.mailmap +++ b/.mailmap @@ -402,6 +402,8 @@ Nathaniel Herman Nathaniel Herman Ngo Iok Ui (Wu Yu Wei) Nicholas Baron +Nicholas Bishop +Nicholas Bishop Nicholas Nethercote Nicholas Nethercote Nick Platt From fe6d9e951a4e85b34096663cad17c5becd0f6781 Mon Sep 17 00:00:00 2001 From: Tshepang Mbambo Date: Wed, 30 Nov 2022 19:29:41 +0200 Subject: [PATCH 225/244] add Tshepang Mbambo to .mailmap --- .mailmap | 1 + 1 file changed, 1 insertion(+) diff --git a/.mailmap b/.mailmap index 11f6cdec9528c..395512cce437b 100644 --- a/.mailmap +++ b/.mailmap @@ -530,6 +530,7 @@ Tomas Koutsky Torsten Weber Torsten Weber Trevor Spiteri +Tshepang Mbambo Ty Overby Tyler Mandry Tyler Ruckinger From 56126fb149ea810db234e210893833e97a5c8e36 Mon Sep 17 00:00:00 2001 From: hkalbasi Date: Wed, 30 Nov 2022 19:13:09 +0330 Subject: [PATCH 226/244] Extract llvm datalayout parsing out of spec module --- compiler/rustc_abi/src/lib.rs | 96 +++++++++++++++++++++++++++ compiler/rustc_target/src/spec/mod.rs | 92 +------------------------ 2 files changed, 98 insertions(+), 90 deletions(-) diff --git a/compiler/rustc_abi/src/lib.rs b/compiler/rustc_abi/src/lib.rs index 4f4a4bf314f14..85693259cd015 100644 --- a/compiler/rustc_abi/src/lib.rs +++ b/compiler/rustc_abi/src/lib.rs @@ -211,6 +211,102 @@ pub enum TargetDataLayoutErrors<'a> { } impl TargetDataLayout { + /// Parse data layout from an [llvm data layout string](https://llvm.org/docs/LangRef.html#data-layout) + /// + /// This function doesn't fill `c_enum_min_size` and it will always be `I32` since it can not be + /// determined from llvm string. + pub fn parse_from_llvm_datalayout_string<'a>( + input: &'a str, + ) -> Result> { + // Parse an address space index from a string. + let parse_address_space = |s: &'a str, cause: &'a str| { + s.parse::().map(AddressSpace).map_err(|err| { + TargetDataLayoutErrors::InvalidAddressSpace { addr_space: s, cause, err } + }) + }; + + // Parse a bit count from a string. + let parse_bits = |s: &'a str, kind: &'a str, cause: &'a str| { + s.parse::().map_err(|err| TargetDataLayoutErrors::InvalidBits { + kind, + bit: s, + cause, + err, + }) + }; + + // Parse a size string. + let size = |s: &'a str, cause: &'a str| parse_bits(s, "size", cause).map(Size::from_bits); + + // Parse an alignment string. + let align = |s: &[&'a str], cause: &'a str| { + if s.is_empty() { + return Err(TargetDataLayoutErrors::MissingAlignment { cause }); + } + let align_from_bits = |bits| { + Align::from_bits(bits) + .map_err(|err| TargetDataLayoutErrors::InvalidAlignment { cause, err }) + }; + let abi = parse_bits(s[0], "alignment", cause)?; + let pref = s.get(1).map_or(Ok(abi), |pref| parse_bits(pref, "alignment", cause))?; + Ok(AbiAndPrefAlign { abi: align_from_bits(abi)?, pref: align_from_bits(pref)? }) + }; + + let mut dl = TargetDataLayout::default(); + let mut i128_align_src = 64; + for spec in input.split('-') { + let spec_parts = spec.split(':').collect::>(); + + match &*spec_parts { + ["e"] => dl.endian = Endian::Little, + ["E"] => dl.endian = Endian::Big, + [p] if p.starts_with('P') => { + dl.instruction_address_space = parse_address_space(&p[1..], "P")? + } + ["a", ref a @ ..] => dl.aggregate_align = align(a, "a")?, + ["f32", ref a @ ..] => dl.f32_align = align(a, "f32")?, + ["f64", ref a @ ..] => dl.f64_align = align(a, "f64")?, + [p @ "p", s, ref a @ ..] | [p @ "p0", s, ref a @ ..] => { + dl.pointer_size = size(s, p)?; + dl.pointer_align = align(a, p)?; + } + [s, ref a @ ..] if s.starts_with('i') => { + let Ok(bits) = s[1..].parse::() else { + size(&s[1..], "i")?; // For the user error. + continue; + }; + let a = align(a, s)?; + match bits { + 1 => dl.i1_align = a, + 8 => dl.i8_align = a, + 16 => dl.i16_align = a, + 32 => dl.i32_align = a, + 64 => dl.i64_align = a, + _ => {} + } + if bits >= i128_align_src && bits <= 128 { + // Default alignment for i128 is decided by taking the alignment of + // largest-sized i{64..=128}. + i128_align_src = bits; + dl.i128_align = a; + } + } + [s, ref a @ ..] if s.starts_with('v') => { + let v_size = size(&s[1..], "v")?; + let a = align(a, s)?; + if let Some(v) = dl.vector_align.iter_mut().find(|v| v.0 == v_size) { + v.1 = a; + continue; + } + // No existing entry, add a new one. + dl.vector_align.push((v_size, a)); + } + _ => {} // Ignore everything else. + } + } + Ok(dl) + } + /// Returns exclusive upper bound on object size. /// /// The theoretical maximum object size is defined as the maximum positive `isize` value. diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 0f8cfd7f5385c..78315afa75956 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -35,10 +35,7 @@ //! to the list specified by the target, rather than replace. use crate::abi::call::Conv; -use crate::abi::{ - AbiAndPrefAlign, AddressSpace, Align, Endian, Integer, Size, TargetDataLayout, - TargetDataLayoutErrors, -}; +use crate::abi::{Endian, Integer, Size, TargetDataLayout, TargetDataLayoutErrors}; use crate::json::{Json, ToJson}; use crate::spec::abi::{lookup as lookup_abi, Abi}; use crate::spec::crt_objects::{CrtObjects, LinkSelfContainedDefault}; @@ -1322,92 +1319,7 @@ pub struct Target { impl Target { pub fn parse_data_layout<'a>(&'a self) -> Result> { - // Parse an address space index from a string. - let parse_address_space = |s: &'a str, cause: &'a str| { - s.parse::().map(AddressSpace).map_err(|err| { - TargetDataLayoutErrors::InvalidAddressSpace { addr_space: s, cause, err } - }) - }; - - // Parse a bit count from a string. - let parse_bits = |s: &'a str, kind: &'a str, cause: &'a str| { - s.parse::().map_err(|err| TargetDataLayoutErrors::InvalidBits { - kind, - bit: s, - cause, - err, - }) - }; - - // Parse a size string. - let size = |s: &'a str, cause: &'a str| parse_bits(s, "size", cause).map(Size::from_bits); - - // Parse an alignment string. - let align = |s: &[&'a str], cause: &'a str| { - if s.is_empty() { - return Err(TargetDataLayoutErrors::MissingAlignment { cause }); - } - let align_from_bits = |bits| { - Align::from_bits(bits) - .map_err(|err| TargetDataLayoutErrors::InvalidAlignment { cause, err }) - }; - let abi = parse_bits(s[0], "alignment", cause)?; - let pref = s.get(1).map_or(Ok(abi), |pref| parse_bits(pref, "alignment", cause))?; - Ok(AbiAndPrefAlign { abi: align_from_bits(abi)?, pref: align_from_bits(pref)? }) - }; - - let mut dl = TargetDataLayout::default(); - let mut i128_align_src = 64; - for spec in self.data_layout.split('-') { - let spec_parts = spec.split(':').collect::>(); - - match &*spec_parts { - ["e"] => dl.endian = Endian::Little, - ["E"] => dl.endian = Endian::Big, - [p] if p.starts_with('P') => { - dl.instruction_address_space = parse_address_space(&p[1..], "P")? - } - ["a", ref a @ ..] => dl.aggregate_align = align(a, "a")?, - ["f32", ref a @ ..] => dl.f32_align = align(a, "f32")?, - ["f64", ref a @ ..] => dl.f64_align = align(a, "f64")?, - [p @ "p", s, ref a @ ..] | [p @ "p0", s, ref a @ ..] => { - dl.pointer_size = size(s, p)?; - dl.pointer_align = align(a, p)?; - } - [s, ref a @ ..] if s.starts_with('i') => { - let Ok(bits) = s[1..].parse::() else { - size(&s[1..], "i")?; // For the user error. - continue; - }; - let a = align(a, s)?; - match bits { - 1 => dl.i1_align = a, - 8 => dl.i8_align = a, - 16 => dl.i16_align = a, - 32 => dl.i32_align = a, - 64 => dl.i64_align = a, - _ => {} - } - if bits >= i128_align_src && bits <= 128 { - // Default alignment for i128 is decided by taking the alignment of - // largest-sized i{64..=128}. - i128_align_src = bits; - dl.i128_align = a; - } - } - [s, ref a @ ..] if s.starts_with('v') => { - let v_size = size(&s[1..], "v")?; - let a = align(a, s)?; - if let Some(v) = dl.vector_align.iter_mut().find(|v| v.0 == v_size) { - v.1 = a; - continue; - } - // No existing entry, add a new one. - dl.vector_align.push((v_size, a)); - } - _ => {} // Ignore everything else. - } - } + let mut dl = TargetDataLayout::parse_from_llvm_datalayout_string(&self.data_layout)?; // Perform consistency checks against the Target information. if dl.endian != self.endian { From c1b8bc66e999eda7dfa2887aab2a8b06914ed875 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Wed, 30 Nov 2022 12:31:38 -0500 Subject: [PATCH 227/244] Fix ICE in `unused_rounding` --- clippy_lints/src/unused_rounding.rs | 21 ++++++++------------- tests/ui/unused_rounding.fixed | 3 +++ tests/ui/unused_rounding.rs | 3 +++ tests/ui/unused_rounding.stderr | 8 +++++++- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/clippy_lints/src/unused_rounding.rs b/clippy_lints/src/unused_rounding.rs index aac6719a8dc0f..097568cd1f700 100644 --- a/clippy_lints/src/unused_rounding.rs +++ b/clippy_lints/src/unused_rounding.rs @@ -1,4 +1,5 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::source::snippet; use rustc_ast::ast::{Expr, ExprKind, MethodCall}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass}; @@ -29,22 +30,16 @@ declare_clippy_lint! { } declare_lint_pass!(UnusedRounding => [UNUSED_ROUNDING]); -fn is_useless_rounding(expr: &Expr) -> Option<(&str, String)> { +fn is_useless_rounding<'a>(cx: &EarlyContext<'_>, expr: &'a Expr) -> Option<(&'a str, String)> { if let ExprKind::MethodCall(box MethodCall { seg:name_ident, receiver, .. }) = &expr.kind && let method_name = name_ident.ident.name.as_str() && (method_name == "ceil" || method_name == "round" || method_name == "floor") && let ExprKind::Lit(token_lit) = &receiver.kind - && token_lit.is_semantic_float() { - let mut f_str = token_lit.symbol.to_string(); - let f = f_str.trim_end_matches('_').parse::().unwrap(); - if let Some(suffix) = token_lit.suffix { - f_str.push_str(suffix.as_str()); - } - if f.fract() == 0.0 { - Some((method_name, f_str)) - } else { - None - } + && token_lit.is_semantic_float() + && let Ok(f) = token_lit.symbol.as_str().replace('_', "").parse::() { + (f.fract() == 0.0).then(|| + (method_name, snippet(cx, receiver.span, "..").to_string()) + ) } else { None } @@ -52,7 +47,7 @@ fn is_useless_rounding(expr: &Expr) -> Option<(&str, String)> { impl EarlyLintPass for UnusedRounding { fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { - if let Some((method_name, float)) = is_useless_rounding(expr) { + if let Some((method_name, float)) = is_useless_rounding(cx, expr) { span_lint_and_sugg( cx, UNUSED_ROUNDING, diff --git a/tests/ui/unused_rounding.fixed b/tests/ui/unused_rounding.fixed index 38fe6c34cfec1..f6f734c05ed59 100644 --- a/tests/ui/unused_rounding.fixed +++ b/tests/ui/unused_rounding.fixed @@ -11,4 +11,7 @@ fn main() { let _ = 3.3_f32.round(); let _ = 3.3_f64.round(); let _ = 3.0_f32; + + let _ = 3_3.0_0_f32; + let _ = 3_3.0_1_f64.round(); } diff --git a/tests/ui/unused_rounding.rs b/tests/ui/unused_rounding.rs index a5cac64d023ae..a0267d8144aab 100644 --- a/tests/ui/unused_rounding.rs +++ b/tests/ui/unused_rounding.rs @@ -11,4 +11,7 @@ fn main() { let _ = 3.3_f32.round(); let _ = 3.3_f64.round(); let _ = 3.0_f32.round(); + + let _ = 3_3.0_0_f32.round(); + let _ = 3_3.0_1_f64.round(); } diff --git a/tests/ui/unused_rounding.stderr b/tests/ui/unused_rounding.stderr index 1eeb5d1de8832..b867996fe5763 100644 --- a/tests/ui/unused_rounding.stderr +++ b/tests/ui/unused_rounding.stderr @@ -24,5 +24,11 @@ error: used the `round` method with a whole number float LL | let _ = 3.0_f32.round(); | ^^^^^^^^^^^^^^^ help: remove the `round` method call: `3.0_f32` -error: aborting due to 4 previous errors +error: used the `round` method with a whole number float + --> $DIR/unused_rounding.rs:15:13 + | +LL | let _ = 3_3.0_0_f32.round(); + | ^^^^^^^^^^^^^^^^^^^ help: remove the `round` method call: `3_3.0_0_f32` + +error: aborting due to 5 previous errors From ab264ae61217df6a2b66c8f2bbff294b793a1f94 Mon Sep 17 00:00:00 2001 From: Jacob Pratt Date: Wed, 30 Nov 2022 21:17:00 +0000 Subject: [PATCH 228/244] Fix ICE from #105101 --- .../src/deriving/default.rs | 2 +- src/test/ui/deriving/issue-105101.rs | 9 ++++++ src/test/ui/deriving/issue-105101.stderr | 29 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/deriving/issue-105101.rs create mode 100644 src/test/ui/deriving/issue-105101.stderr diff --git a/compiler/rustc_builtin_macros/src/deriving/default.rs b/compiler/rustc_builtin_macros/src/deriving/default.rs index 93f297ad88b5f..bd5c6e42a19f3 100644 --- a/compiler/rustc_builtin_macros/src/deriving/default.rs +++ b/compiler/rustc_builtin_macros/src/deriving/default.rs @@ -146,7 +146,7 @@ fn extract_default_variant<'a>( let suggestion = default_variants .iter() .filter_map(|v| { - if v.ident == variant.ident { + if v.span == variant.span { None } else { Some((cx.sess.find_by_name(&v.attrs, kw::Default)?.span, String::new())) diff --git a/src/test/ui/deriving/issue-105101.rs b/src/test/ui/deriving/issue-105101.rs new file mode 100644 index 0000000000000..1a377feb91948 --- /dev/null +++ b/src/test/ui/deriving/issue-105101.rs @@ -0,0 +1,9 @@ +// compile-flags: --crate-type=lib + +#[derive(Default)] //~ ERROR multiple declared defaults +enum E { + #[default] + A, + #[default] + A, //~ ERROR defined multiple times +} diff --git a/src/test/ui/deriving/issue-105101.stderr b/src/test/ui/deriving/issue-105101.stderr new file mode 100644 index 0000000000000..0f6f67043f32f --- /dev/null +++ b/src/test/ui/deriving/issue-105101.stderr @@ -0,0 +1,29 @@ +error: multiple declared defaults + --> $DIR/issue-105101.rs:3:10 + | +LL | #[derive(Default)] + | ^^^^^^^ +... +LL | A, + | - first default +LL | #[default] +LL | A, + | - additional default + | + = note: only one variant can be default + = note: this error originates in the derive macro `Default` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0428]: the name `A` is defined multiple times + --> $DIR/issue-105101.rs:8:5 + | +LL | A, + | - previous definition of the type `A` here +LL | #[default] +LL | A, + | ^ `A` redefined here + | + = note: `A` must be defined only once in the type namespace of this enum + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0428`. From 7ae5c81e9f9046a547757a1521e83c23da0bb67e Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Wed, 30 Nov 2022 21:44:18 -0500 Subject: [PATCH 229/244] Fix ICE in `result large_err` with uninhabited enums --- clippy_lints/src/functions/result.rs | 8 +++++--- tests/ui/result_large_err.rs | 6 ++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/functions/result.rs b/clippy_lints/src/functions/result.rs index f7e30b051a694..23da145d03825 100644 --- a/clippy_lints/src/functions/result.rs +++ b/clippy_lints/src/functions/result.rs @@ -94,7 +94,9 @@ fn check_result_large_err<'tcx>(cx: &LateContext<'tcx>, err_ty: Ty<'tcx>, hir_ty if let hir::ItemKind::Enum(ref def, _) = item.kind; then { let variants_size = AdtVariantInfo::new(cx, *adt, subst); - if variants_size[0].size >= large_err_threshold { + if let Some((first_variant, variants)) = variants_size.split_first() + && first_variant.size >= large_err_threshold + { span_lint_and_then( cx, RESULT_LARGE_ERR, @@ -102,11 +104,11 @@ fn check_result_large_err<'tcx>(cx: &LateContext<'tcx>, err_ty: Ty<'tcx>, hir_ty "the `Err`-variant returned from this function is very large", |diag| { diag.span_label( - def.variants[variants_size[0].ind].span, + def.variants[first_variant.ind].span, format!("the largest variant contains at least {} bytes", variants_size[0].size), ); - for variant in &variants_size[1..] { + for variant in variants { if variant.size >= large_err_threshold { let variant_def = &def.variants[variant.ind]; diag.span_label( diff --git a/tests/ui/result_large_err.rs b/tests/ui/result_large_err.rs index 9dd27d6dc01aa..1c12cebfd971a 100644 --- a/tests/ui/result_large_err.rs +++ b/tests/ui/result_large_err.rs @@ -108,4 +108,10 @@ pub fn array_error() -> Result<(), ArrayError<(i32, T), U>> { Ok(()) } +// Issue #10005 +enum Empty {} +fn _empty_error() -> Result<(), Empty> { + Ok(()) +} + fn main() {} From efea79ca80289be333bf6052015c645b19a32132 Mon Sep 17 00:00:00 2001 From: nils <48135649+Nilstrieb@users.noreply.github.com> Date: Thu, 1 Dec 2022 11:16:18 +0100 Subject: [PATCH 230/244] Gate macros behind `#[cfg(not(bootstrap))]` Co-authored-by: Takayuki Maeda --- library/core/src/macros/mod.rs | 11 ----------- library/core/src/prelude/v1.rs | 1 + library/std/src/prelude/v1.rs | 1 + 3 files changed, 2 insertions(+), 11 deletions(-) diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs index 0cd9426209fd2..f29cd357d6bfd 100644 --- a/library/core/src/macros/mod.rs +++ b/library/core/src/macros/mod.rs @@ -1558,17 +1558,6 @@ pub(crate) mod builtin { /* compiler built-in */ } - /// Unstable placeholder for type ascription. - #[unstable( - feature = "type_ascription", - issue = "23416", - reason = "placeholder syntax for type ascription" - )] - #[cfg(bootstrap)] - pub macro type_ascribe($expr:expr, $ty:ty) { - $expr: $ty - } - /// Unstable implementation detail of the `rustc` compiler, do not use. #[rustc_builtin_macro] #[stable(feature = "rust1", since = "1.0.0")] diff --git a/library/core/src/prelude/v1.rs b/library/core/src/prelude/v1.rs index a28d14b14a8e6..2d67d742c689b 100644 --- a/library/core/src/prelude/v1.rs +++ b/library/core/src/prelude/v1.rs @@ -104,4 +104,5 @@ pub use crate::macros::builtin::cfg_eval; issue = "23416", reason = "placeholder syntax for type ascription" )] +#[cfg(not(bootstrap))] pub use crate::macros::builtin::type_ascribe; diff --git a/library/std/src/prelude/v1.rs b/library/std/src/prelude/v1.rs index 4ab4229598ecc..a5a798078eb98 100644 --- a/library/std/src/prelude/v1.rs +++ b/library/std/src/prelude/v1.rs @@ -91,6 +91,7 @@ pub use core::prelude::v1::cfg_eval; issue = "23416", reason = "placeholder syntax for type ascription" )] +#[cfg(not(bootstrap))] pub use core::prelude::v1::type_ascribe; // The file so far is equivalent to src/libcore/prelude/v1.rs, From 929003aacf27e23d72264bc8fcb15cf3a68aee83 Mon Sep 17 00:00:00 2001 From: Markus Everling Date: Thu, 1 Dec 2022 12:15:29 +0100 Subject: [PATCH 231/244] Make `VecDeque::new_in` unstably const --- library/alloc/src/collections/vec_deque/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 86d77182bccee..f39223f6a2d23 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -566,10 +566,9 @@ impl VecDeque { /// /// let deque: VecDeque = VecDeque::new(); /// ``` - // FIXME: This should probably be const #[inline] #[unstable(feature = "allocator_api", issue = "32838")] - pub fn new_in(alloc: A) -> VecDeque { + pub const fn new_in(alloc: A) -> VecDeque { VecDeque { head: 0, len: 0, buf: RawVec::new_in(alloc) } } From c959fbe771b5f1f7d8473805bb17e988c54d45d3 Mon Sep 17 00:00:00 2001 From: Markus Everling Date: Thu, 1 Dec 2022 12:44:29 +0100 Subject: [PATCH 232/244] Fix typo in comment --- library/alloc/src/collections/vec_deque/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index f39223f6a2d23..ee8032ad6f0f7 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -2151,7 +2151,7 @@ impl VecDeque { self.head = tail; } else { - // ´free` is smaller than both `head_len` and `tail_len`. + // `free` is smaller than both `head_len` and `tail_len`. // the general algorithm for this first moves the slices // right next to each other and then uses `slice::rotate` // to rotate them into place: From 4063712bf45fcb600d23dcaea4540d95311aaa29 Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Thu, 1 Dec 2022 12:55:29 +0100 Subject: [PATCH 233/244] Bump nightly version -> 2022-12-01 --- rust-toolchain | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-toolchain b/rust-toolchain index a806c1564796a..19fee38db46e6 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1,3 +1,3 @@ [toolchain] -channel = "nightly-2022-11-21" +channel = "nightly-2022-12-01" components = ["cargo", "llvm-tools", "rust-src", "rust-std", "rustc", "rustc-dev", "rustfmt"] From c04d67444fcbfccc5db848417c1dbeac568a1a23 Mon Sep 17 00:00:00 2001 From: Florian Bartels Date: Thu, 1 Dec 2022 14:12:55 +0100 Subject: [PATCH 234/244] Ignore `gnu` systems (`windows-msvc` only) --- src/test/codegen/dllimports/main.rs | 1 + src/test/codegen/panic-abort-windows.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/codegen/dllimports/main.rs b/src/test/codegen/dllimports/main.rs index f7978749a9173..ab599992ffd79 100644 --- a/src/test/codegen/dllimports/main.rs +++ b/src/test/codegen/dllimports/main.rs @@ -1,5 +1,6 @@ // This test is for *-windows-msvc only. // only-windows +// ignore-gnu // aux-build:dummy.rs // aux-build:wrapper.rs diff --git a/src/test/codegen/panic-abort-windows.rs b/src/test/codegen/panic-abort-windows.rs index d22df6200afae..2ee29762dcdf4 100644 --- a/src/test/codegen/panic-abort-windows.rs +++ b/src/test/codegen/panic-abort-windows.rs @@ -1,4 +1,4 @@ -// This test is for *-windows-msvc only. +// This test is for *-windows only. // only-windows // compile-flags: -C no-prepopulate-passes -C panic=abort -O From 6cd4dd3091dfe0ce4a728bd9ae177361fba23736 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Fri, 25 Nov 2022 11:26:36 +0300 Subject: [PATCH 235/244] rustc_hir: Relax lifetime requirements on `Visitor::visit_path` --- compiler/rustc_hir/src/intravisit.rs | 4 ++-- compiler/rustc_hir_analysis/src/collect/lifetimes.rs | 2 +- compiler/rustc_lint/src/internal.rs | 2 +- compiler/rustc_lint/src/late.rs | 2 +- compiler/rustc_lint/src/passes.rs | 2 +- compiler/rustc_passes/src/dead.rs | 2 +- compiler/rustc_passes/src/hir_stats.rs | 2 +- compiler/rustc_passes/src/stability.rs | 4 ++-- compiler/rustc_passes/src/upvars.rs | 2 +- src/librustdoc/core.rs | 2 +- src/librustdoc/html/render/span_map.rs | 2 +- src/tools/clippy/clippy_lints/src/from_over_into.rs | 2 +- .../clippy/clippy_lints/src/methods/option_map_unwrap_or.rs | 4 ++-- .../src/utils/internal_lints/lint_without_lint_pass.rs | 2 +- .../src/utils/internal_lints/metadata_collector.rs | 2 +- src/tools/clippy/clippy_utils/src/usage.rs | 2 +- 16 files changed, 19 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index 957f8c1058e98..9eeb04c8e0102 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -422,7 +422,7 @@ pub trait Visitor<'v>: Sized { fn visit_qpath(&mut self, qpath: &'v QPath<'v>, id: HirId, _span: Span) { walk_qpath(self, qpath, id) } - fn visit_path(&mut self, path: &'v Path<'v>, _id: HirId) { + fn visit_path(&mut self, path: &Path<'v>, _id: HirId) { walk_path(self, path) } fn visit_path_segment(&mut self, path_segment: &'v PathSegment<'v>) { @@ -1126,7 +1126,7 @@ pub fn walk_qpath<'v, V: Visitor<'v>>(visitor: &mut V, qpath: &'v QPath<'v>, id: } } -pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path<'v>) { +pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &Path<'v>) { for segment in path.segments { visitor.visit_path_segment(segment); } diff --git a/compiler/rustc_hir_analysis/src/collect/lifetimes.rs b/compiler/rustc_hir_analysis/src/collect/lifetimes.rs index c11eed7ad9e57..9a7b261fffd4d 100644 --- a/compiler/rustc_hir_analysis/src/collect/lifetimes.rs +++ b/compiler/rustc_hir_analysis/src/collect/lifetimes.rs @@ -814,7 +814,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { } } - fn visit_path(&mut self, path: &'tcx hir::Path<'tcx>, _: hir::HirId) { + fn visit_path(&mut self, path: &hir::Path<'tcx>, _: hir::HirId) { for (i, segment) in path.segments.iter().enumerate() { let depth = path.segments.len() - i - 1; if let Some(ref args) = segment.args { diff --git a/compiler/rustc_lint/src/internal.rs b/compiler/rustc_lint/src/internal.rs index a6c7e819482c2..4f92661dbd334 100644 --- a/compiler/rustc_lint/src/internal.rs +++ b/compiler/rustc_lint/src/internal.rs @@ -117,7 +117,7 @@ impl<'tcx> LateLintPass<'tcx> for TyTyKind { fn check_path( &mut self, cx: &LateContext<'tcx>, - path: &'tcx rustc_hir::Path<'tcx>, + path: &rustc_hir::Path<'tcx>, _: rustc_hir::HirId, ) { if let Some(segment) = path.segments.iter().nth_back(1) diff --git a/compiler/rustc_lint/src/late.rs b/compiler/rustc_lint/src/late.rs index f484e31ba1508..1d0b3f34d5d78 100644 --- a/compiler/rustc_lint/src/late.rs +++ b/compiler/rustc_lint/src/late.rs @@ -292,7 +292,7 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas hir_visit::walk_lifetime(self, lt); } - fn visit_path(&mut self, p: &'tcx hir::Path<'tcx>, id: hir::HirId) { + fn visit_path(&mut self, p: &hir::Path<'tcx>, id: hir::HirId) { lint_callback!(self, check_path, p, id); hir_visit::walk_path(self, p); } diff --git a/compiler/rustc_lint/src/passes.rs b/compiler/rustc_lint/src/passes.rs index 1c6a057d1a85b..fc11d092ccb4b 100644 --- a/compiler/rustc_lint/src/passes.rs +++ b/compiler/rustc_lint/src/passes.rs @@ -44,7 +44,7 @@ macro_rules! late_lint_methods { fn check_struct_def(a: &$hir hir::VariantData<$hir>); fn check_field_def(a: &$hir hir::FieldDef<$hir>); fn check_variant(a: &$hir hir::Variant<$hir>); - fn check_path(a: &$hir hir::Path<$hir>, b: hir::HirId); + fn check_path(a: &hir::Path<$hir>, b: hir::HirId); fn check_attribute(a: &$hir ast::Attribute); /// Called when entering a syntax node that can have lint attributes such diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index 5d0224c35f364..da023fcf4c3b5 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -433,7 +433,7 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> { self.in_pat = false; } - fn visit_path(&mut self, path: &'tcx hir::Path<'tcx>, _: hir::HirId) { + fn visit_path(&mut self, path: &hir::Path<'tcx>, _: hir::HirId) { self.handle_res(path.res); intravisit::walk_path(self, path); } diff --git a/compiler/rustc_passes/src/hir_stats.rs b/compiler/rustc_passes/src/hir_stats.rs index 140f02c046a66..20951878e0032 100644 --- a/compiler/rustc_passes/src/hir_stats.rs +++ b/compiler/rustc_passes/src/hir_stats.rs @@ -442,7 +442,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> { hir_visit::walk_lifetime(self, lifetime) } - fn visit_path(&mut self, path: &'v hir::Path<'v>, _id: hir::HirId) { + fn visit_path(&mut self, path: &hir::Path<'v>, _id: hir::HirId) { self.record("Path", Id::None, path); hir_visit::walk_path(self, path) } diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index 88bd655d8d384..da715523474f9 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -787,7 +787,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> { intravisit::walk_item(self, item); } - fn visit_path(&mut self, path: &'tcx hir::Path<'tcx>, id: hir::HirId) { + fn visit_path(&mut self, path: &hir::Path<'tcx>, id: hir::HirId) { if let Some(def_id) = path.res.opt_def_id() { let method_span = path.segments.last().map(|s| s.ident.span); let item_is_allowed = self.tcx.check_stability_allow_unstable( @@ -880,7 +880,7 @@ struct CheckTraitImplStable<'tcx> { } impl<'tcx> Visitor<'tcx> for CheckTraitImplStable<'tcx> { - fn visit_path(&mut self, path: &'tcx hir::Path<'tcx>, _id: hir::HirId) { + fn visit_path(&mut self, path: &hir::Path<'tcx>, _id: hir::HirId) { if let Some(def_id) = path.res.opt_def_id() { if let Some(stab) = self.tcx.lookup_stability(def_id) { self.fully_stable &= stab.level.is_stable(); diff --git a/compiler/rustc_passes/src/upvars.rs b/compiler/rustc_passes/src/upvars.rs index 9e41efce9ceaf..605cf0a93b877 100644 --- a/compiler/rustc_passes/src/upvars.rs +++ b/compiler/rustc_passes/src/upvars.rs @@ -66,7 +66,7 @@ impl CaptureCollector<'_, '_> { } impl<'tcx> Visitor<'tcx> for CaptureCollector<'_, 'tcx> { - fn visit_path(&mut self, path: &'tcx hir::Path<'tcx>, _: hir::HirId) { + fn visit_path(&mut self, path: &hir::Path<'tcx>, _: hir::HirId) { if let Res::Local(var_id) = path.res { self.visit_local_use(var_id, path.span); } diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 893249e88cf7b..da0df596c41e3 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -489,7 +489,7 @@ impl<'tcx> Visitor<'tcx> for EmitIgnoredResolutionErrors<'tcx> { self.tcx.hir() } - fn visit_path(&mut self, path: &'tcx Path<'_>, _id: HirId) { + fn visit_path(&mut self, path: &Path<'tcx>, _id: HirId) { debug!("visiting path {:?}", path); if path.res == Res::Err { // We have less context here than in rustc_resolve, diff --git a/src/librustdoc/html/render/span_map.rs b/src/librustdoc/html/render/span_map.rs index 151ec2b28adc9..b898db242464a 100644 --- a/src/librustdoc/html/render/span_map.rs +++ b/src/librustdoc/html/render/span_map.rs @@ -140,7 +140,7 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> { self.tcx.hir() } - fn visit_path(&mut self, path: &'tcx rustc_hir::Path<'tcx>, _id: HirId) { + fn visit_path(&mut self, path: &rustc_hir::Path<'tcx>, _id: HirId) { if self.handle_macro(path.span) { return; } diff --git a/src/tools/clippy/clippy_lints/src/from_over_into.rs b/src/tools/clippy/clippy_lints/src/from_over_into.rs index 8b24a4962fb29..6d9ede5f73bbe 100644 --- a/src/tools/clippy/clippy_lints/src/from_over_into.rs +++ b/src/tools/clippy/clippy_lints/src/from_over_into.rs @@ -126,7 +126,7 @@ impl<'a, 'tcx> Visitor<'tcx> for SelfFinder<'a, 'tcx> { self.cx.tcx.hir() } - fn visit_path(&mut self, path: &'tcx Path<'tcx>, _id: HirId) { + fn visit_path(&mut self, path: &Path<'tcx>, _id: HirId) { for segment in path.segments { match segment.ident.name { kw::SelfLower => self.lower.push(segment.ident.span), diff --git a/src/tools/clippy/clippy_lints/src/methods/option_map_unwrap_or.rs b/src/tools/clippy/clippy_lints/src/methods/option_map_unwrap_or.rs index 30421a6dd5afb..910ee14855e23 100644 --- a/src/tools/clippy/clippy_lints/src/methods/option_map_unwrap_or.rs +++ b/src/tools/clippy/clippy_lints/src/methods/option_map_unwrap_or.rs @@ -97,7 +97,7 @@ struct UnwrapVisitor<'a, 'tcx> { impl<'a, 'tcx> Visitor<'tcx> for UnwrapVisitor<'a, 'tcx> { type NestedFilter = nested_filter::All; - fn visit_path(&mut self, path: &'tcx Path<'_>, _id: HirId) { + fn visit_path(&mut self, path: &Path<'tcx>, _id: HirId) { self.identifiers.insert(ident(path)); walk_path(self, path); } @@ -116,7 +116,7 @@ struct MapExprVisitor<'a, 'tcx> { impl<'a, 'tcx> Visitor<'tcx> for MapExprVisitor<'a, 'tcx> { type NestedFilter = nested_filter::All; - fn visit_path(&mut self, path: &'tcx Path<'_>, _id: HirId) { + fn visit_path(&mut self, path: &Path<'tcx>, _id: HirId) { if self.identifiers.contains(&ident(path)) { self.found_identifier = true; return; diff --git a/src/tools/clippy/clippy_lints/src/utils/internal_lints/lint_without_lint_pass.rs b/src/tools/clippy/clippy_lints/src/utils/internal_lints/lint_without_lint_pass.rs index 1aebb8b3104ba..786d9608c851e 100644 --- a/src/tools/clippy/clippy_lints/src/utils/internal_lints/lint_without_lint_pass.rs +++ b/src/tools/clippy/clippy_lints/src/utils/internal_lints/lint_without_lint_pass.rs @@ -330,7 +330,7 @@ struct LintCollector<'a, 'tcx> { impl<'a, 'tcx> Visitor<'tcx> for LintCollector<'a, 'tcx> { type NestedFilter = nested_filter::All; - fn visit_path(&mut self, path: &'tcx Path<'_>, _: HirId) { + fn visit_path(&mut self, path: &Path<'_>, _: HirId) { if path.segments.len() == 1 { self.output.insert(path.segments[0].ident.name); } diff --git a/src/tools/clippy/clippy_lints/src/utils/internal_lints/metadata_collector.rs b/src/tools/clippy/clippy_lints/src/utils/internal_lints/metadata_collector.rs index d06a616e4b30b..857abe77e21f2 100644 --- a/src/tools/clippy/clippy_lints/src/utils/internal_lints/metadata_collector.rs +++ b/src/tools/clippy/clippy_lints/src/utils/internal_lints/metadata_collector.rs @@ -1019,7 +1019,7 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for ApplicabilityResolver<'a, 'hir> { self.cx.tcx.hir() } - fn visit_path(&mut self, path: &'hir hir::Path<'hir>, _id: hir::HirId) { + fn visit_path(&mut self, path: &hir::Path<'hir>, _id: hir::HirId) { for (index, enum_value) in paths::APPLICABILITY_VALUES.iter().enumerate() { if match_path(path, enum_value) { self.add_new_index(index); diff --git a/src/tools/clippy/clippy_utils/src/usage.rs b/src/tools/clippy/clippy_utils/src/usage.rs index 797722cfc1fcc..ab3976a13bdbb 100644 --- a/src/tools/clippy/clippy_utils/src/usage.rs +++ b/src/tools/clippy/clippy_utils/src/usage.rs @@ -128,7 +128,7 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for BindingUsageFinder<'a, 'tcx> { } } - fn visit_path(&mut self, path: &'tcx hir::Path<'tcx>, _: hir::HirId) { + fn visit_path(&mut self, path: &hir::Path<'tcx>, _: hir::HirId) { if let hir::def::Res::Local(id) = path.res { if self.binding_ids.contains(&id) { self.usage_found = true; From f76c4b84c9d3def13d4514db0d13201693552274 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 1 Dec 2022 15:55:09 +0100 Subject: [PATCH 236/244] Migrate summary toggle filter to CSS variable --- src/librustdoc/html/static/css/rustdoc.css | 1 + src/librustdoc/html/static/css/themes/ayu.css | 5 +---- src/librustdoc/html/static/css/themes/dark.css | 5 +---- src/librustdoc/html/static/css/themes/light.css | 1 + 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 98a5b761dedf5..fc688e285719c 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -1521,6 +1521,7 @@ details.rustdoc-toggle > summary::before { display: inline-block; vertical-align: middle; opacity: .5; + filter: var(--toggle-filter); } details.rustdoc-toggle > summary.hideme > span, diff --git a/src/librustdoc/html/static/css/themes/ayu.css b/src/librustdoc/html/static/css/themes/ayu.css index de7db7d438c9f..6e0905e730d08 100644 --- a/src/librustdoc/html/static/css/themes/ayu.css +++ b/src/librustdoc/html/static/css/themes/ayu.css @@ -21,6 +21,7 @@ Original by Dempfi (https://github.com/dempfi/ayu) --right-side-color: grey; --code-attribute-color: #999; --toggles-color: #999; + --toggle-filter: invert(100%); --search-input-focused-border-color: #5c6773; /* Same as `--border-color`. */ --copy-path-button-color: #fff; --copy-path-img-filter: invert(70%); @@ -158,10 +159,6 @@ body.source .example-wrap pre.rust a { background: #333; } -details.rustdoc-toggle > summary::before { - filter: invert(100%); -} - .module-item .stab, .import-item .stab { color: #000; diff --git a/src/librustdoc/html/static/css/themes/dark.css b/src/librustdoc/html/static/css/themes/dark.css index d8929f3233864..334fc3de5616d 100644 --- a/src/librustdoc/html/static/css/themes/dark.css +++ b/src/librustdoc/html/static/css/themes/dark.css @@ -16,6 +16,7 @@ --right-side-color: grey; --code-attribute-color: #999; --toggles-color: #999; + --toggle-filter: invert(100%); --search-input-focused-border-color: #008dfd; --copy-path-button-color: #999; --copy-path-img-filter: invert(50%); @@ -89,10 +90,6 @@ body.source .example-wrap pre.rust a { background: #333; } -details.rustdoc-toggle > summary::before { - filter: invert(100%); -} - #titles > button:not(.selected) { background-color: #252525; border-top-color: #252525; diff --git a/src/librustdoc/html/static/css/themes/light.css b/src/librustdoc/html/static/css/themes/light.css index 78a0cdcc3bc05..453e7508af4ba 100644 --- a/src/librustdoc/html/static/css/themes/light.css +++ b/src/librustdoc/html/static/css/themes/light.css @@ -16,6 +16,7 @@ --right-side-color: grey; --code-attribute-color: #999; --toggles-color: #999; + --toggle-filter: none; --search-input-focused-border-color: #66afe9; --copy-path-button-color: #999; --copy-path-img-filter: invert(50%); From 188f31eba0c190425bb1fde67005cfd535686c36 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 1 Dec 2022 15:58:16 +0100 Subject: [PATCH 237/244] Add GUI test for toggle filter and opacity --- src/test/rustdoc-gui/toggle-docs.goml | 29 +++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/test/rustdoc-gui/toggle-docs.goml b/src/test/rustdoc-gui/toggle-docs.goml index 8c9fd0a8866d5..b7d1072376758 100644 --- a/src/test/rustdoc-gui/toggle-docs.goml +++ b/src/test/rustdoc-gui/toggle-docs.goml @@ -40,3 +40,32 @@ assert-attribute-false: ( click: "#toggle-all-docs" wait-for-text: ("#toggle-all-docs", "[−]") assert-attribute: ("details.rustdoc-toggle", {"open": ""}, ALL) + +// Checking the toggles style. +show-text: true +define-function: ( + "check-color", + (theme, filter), + [ + // Setting the theme. + ("local-storage", {"rustdoc-theme": |theme|, "rustdoc-use-system-theme": "false"}), + // We reload the page so the local storage settings are being used. + ("reload"), + + ("assert-css", ("details.rustdoc-toggle > summary::before", { + "opacity": "0.5", + "filter": |filter|, + }, ALL)), + ("move-cursor-to", "details.rustdoc-toggle summary"), + ("assert-css", ("details.rustdoc-toggle > summary:hover::before", { + "opacity": "1", + "filter": |filter|, + })), + // moving the cursor somewhere else to not mess with next function calls. + ("move-cursor-to", ".search-input"), + ] +) + +call-function: ("check-color", {"theme": "ayu", "filter": "invert(1)"}) +call-function: ("check-color", {"theme": "dark", "filter": "invert(1)"}) +call-function: ("check-color", {"theme": "light", "filter": "none"}) From 0af5b7265d8d2bf1f13d2ab3e005601782ae5170 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20Puk=C5=A1ta?= <54238857+yjhn@users.noreply.github.com> Date: Thu, 1 Dec 2022 17:42:31 +0200 Subject: [PATCH 238/244] Add tracking issue for file_create_new --- library/std/src/fs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index 188ff00e1f8dd..0660e03c1a821 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -401,7 +401,7 @@ impl File { /// Ok(()) /// } /// ``` - #[unstable(feature = "file_create_new", issue = "none")] + #[unstable(feature = "file_create_new", issue = "105135")] pub fn create_new>(path: P) -> io::Result { OpenOptions::new().read(true).write(true).create_new(true).open(path.as_ref()) } From b7f2b1cd5cd08e4805bb3df0e8153445acfb04c7 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 1 Dec 2022 16:41:49 +0100 Subject: [PATCH 239/244] clarify comment on Deref promotion --- compiler/rustc_const_eval/src/transform/promote_consts.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_const_eval/src/transform/promote_consts.rs b/compiler/rustc_const_eval/src/transform/promote_consts.rs index f48bcd9080966..6777fae74f1ca 100644 --- a/compiler/rustc_const_eval/src/transform/promote_consts.rs +++ b/compiler/rustc_const_eval/src/transform/promote_consts.rs @@ -318,14 +318,14 @@ impl<'tcx> Validator<'_, 'tcx> { match elem { ProjectionElem::Deref => { let mut promotable = false; + // When a static is used by-value, that gets desugared to `*STATIC_ADDR`, + // and we need to be able to promote this. So check if this deref matches + // that specific pattern. + // We need to make sure this is a `Deref` of a local with no further projections. // Discussion can be found at // https://github.com/rust-lang/rust/pull/74945#discussion_r463063247 if let Some(local) = place_base.as_local() { - // This is a special treatment for cases like *&STATIC where STATIC is a - // global static variable. - // This pattern is generated only when global static variables are directly - // accessed and is qualified for promotion safely. if let TempState::Defined { location, .. } = self.temps[local] { let def_stmt = self.body[location.block] .statements From 1f259ae6799e7e0a08e5b84fc5686e404b17eef0 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Fri, 25 Nov 2022 17:39:38 +0300 Subject: [PATCH 240/244] rustc_hir: Change representation of import paths to support multiple resolutions --- compiler/rustc_ast_lowering/src/item.rs | 21 ++++--- compiler/rustc_ast_lowering/src/path.rs | 21 ++----- .../src/stable_hasher.rs | 2 +- compiler/rustc_hir/src/arena.rs | 1 + compiler/rustc_hir/src/hir.rs | 9 ++- compiler/rustc_hir/src/intravisit.rs | 9 ++- compiler/rustc_hir_pretty/src/lib.rs | 2 +- compiler/rustc_passes/src/hir_stats.rs | 2 +- .../rustc_save_analysis/src/dump_visitor.rs | 4 +- compiler/rustc_save_analysis/src/lib.rs | 4 +- src/librustdoc/clean/mod.rs | 20 +++++++ src/librustdoc/clean/types.rs | 11 ++-- src/librustdoc/html/render/span_map.rs | 8 --- src/librustdoc/visit_ast.rs | 59 ++++++++++--------- .../clippy_lints/src/disallowed_types.rs | 4 +- .../clippy/clippy_lints/src/macro_use.rs | 5 +- .../src/missing_enforced_import_rename.rs | 59 ++++++++++--------- .../clippy_lints/src/redundant_pub_crate.rs | 2 +- .../clippy_lints/src/wildcard_imports.rs | 3 +- 19 files changed, 134 insertions(+), 112 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 2b47e90891291..b26b37b0466b1 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -508,7 +508,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let mut resolutions = self.expect_full_res_from_use(id).fuse(); // We want to return *something* from this function, so hold onto the first item // for later. - let ret_res = self.lower_res(resolutions.next().unwrap_or(Res::Err)); + let ret_res = smallvec![self.lower_res(resolutions.next().unwrap_or(Res::Err))]; // Here, we are looping over namespaces, if they exist for the definition // being imported. We only handle type and value namespaces because we @@ -538,8 +538,8 @@ impl<'hir> LoweringContext<'_, 'hir> { let span = path.span; self.with_hir_id_owner(new_node_id, |this| { - let res = this.lower_res(res); - let path = this.lower_path_extra(res, &path, ParamMode::Explicit); + let res = smallvec![this.lower_res(res)]; + let path = this.lower_use_path(res, &path, ParamMode::Explicit); let kind = hir::ItemKind::Use(path, hir::UseKind::Single); if let Some(attrs) = attrs { this.attrs.insert(hir::ItemLocalId::new(0), attrs); @@ -556,15 +556,14 @@ impl<'hir> LoweringContext<'_, 'hir> { }); } - let path = self.lower_path_extra(ret_res, &path, ParamMode::Explicit); + let path = self.lower_use_path(ret_res, &path, ParamMode::Explicit); hir::ItemKind::Use(path, hir::UseKind::Single) } UseTreeKind::Glob => { - let path = self.lower_path( - id, - &Path { segments, span: path.span, tokens: None }, - ParamMode::Explicit, - ); + let res = self.expect_full_res(id); + let res = smallvec![self.lower_res(res)]; + let path = Path { segments, span: path.span, tokens: None }; + let path = self.lower_use_path(res, &path, ParamMode::Explicit); hir::ItemKind::Use(path, hir::UseKind::Glob) } UseTreeKind::Nested(ref trees) => { @@ -635,8 +634,8 @@ impl<'hir> LoweringContext<'_, 'hir> { } let res = self.expect_full_res_from_use(id).next().unwrap_or(Res::Err); - let res = self.lower_res(res); - let path = self.lower_path_extra(res, &prefix, ParamMode::Explicit); + let res = smallvec![self.lower_res(res)]; + let path = self.lower_use_path(res, &prefix, ParamMode::Explicit); hir::ItemKind::Use(path, hir::UseKind::ListStem) } } diff --git a/compiler/rustc_ast_lowering/src/path.rs b/compiler/rustc_ast_lowering/src/path.rs index dc85b5e95ea86..8d23c26e603b7 100644 --- a/compiler/rustc_ast_lowering/src/path.rs +++ b/compiler/rustc_ast_lowering/src/path.rs @@ -12,7 +12,7 @@ use rustc_hir::GenericArg; use rustc_span::symbol::{kw, Ident}; use rustc_span::{BytePos, Span, DUMMY_SP}; -use smallvec::smallvec; +use smallvec::{smallvec, SmallVec}; impl<'a, 'hir> LoweringContext<'a, 'hir> { #[instrument(level = "trace", skip(self))] @@ -144,13 +144,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ); } - pub(crate) fn lower_path_extra( + pub(crate) fn lower_use_path( &mut self, - res: Res, + res: SmallVec<[Res; 3]>, p: &Path, param_mode: ParamMode, - ) -> &'hir hir::Path<'hir> { - self.arena.alloc(hir::Path { + ) -> &'hir hir::UsePath<'hir> { + self.arena.alloc(hir::UsePath { res, segments: self.arena.alloc_from_iter(p.segments.iter().map(|segment| { self.lower_path_segment( @@ -165,17 +165,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { }) } - pub(crate) fn lower_path( - &mut self, - id: NodeId, - p: &Path, - param_mode: ParamMode, - ) -> &'hir hir::Path<'hir> { - let res = self.expect_full_res(id); - let res = self.lower_res(res); - self.lower_path_extra(res, p, param_mode) - } - pub(crate) fn lower_path_segment( &mut self, path_span: Span, diff --git a/compiler/rustc_data_structures/src/stable_hasher.rs b/compiler/rustc_data_structures/src/stable_hasher.rs index ce85917341880..e2c33e7e06268 100644 --- a/compiler/rustc_data_structures/src/stable_hasher.rs +++ b/compiler/rustc_data_structures/src/stable_hasher.rs @@ -399,7 +399,7 @@ where } } -impl HashStable for SmallVec<[A; 1]> +impl HashStable for SmallVec<[A; N]> where A: HashStable, { diff --git a/compiler/rustc_hir/src/arena.rs b/compiler/rustc_hir/src/arena.rs index 44335b7f42ef9..c89e7eb75f8f0 100644 --- a/compiler/rustc_hir/src/arena.rs +++ b/compiler/rustc_hir/src/arena.rs @@ -39,6 +39,7 @@ macro_rules! arena_types { [] param: rustc_hir::Param<'tcx>, [] pat: rustc_hir::Pat<'tcx>, [] path: rustc_hir::Path<'tcx>, + [] use_path: rustc_hir::UsePath<'tcx>, [] path_segment: rustc_hir::PathSegment<'tcx>, [] poly_trait_ref: rustc_hir::PolyTraitRef<'tcx>, [] qpath: rustc_hir::QPath<'tcx>, diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 81aedcce87728..118eafe2910f0 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -183,14 +183,17 @@ impl Lifetime { /// `std::cmp::PartialEq`. It's represented as a sequence of identifiers, /// along with a bunch of supporting information. #[derive(Debug, HashStable_Generic)] -pub struct Path<'hir> { +pub struct Path<'hir, R = Res> { pub span: Span, /// The resolution for the path. - pub res: Res, + pub res: R, /// The segments in the path: the things separated by `::`. pub segments: &'hir [PathSegment<'hir>], } +/// Up to three resolutions for type, value and macro namespaces. +pub type UsePath<'hir> = Path<'hir, SmallVec<[Res; 3]>>; + impl Path<'_> { pub fn is_global(&self) -> bool { !self.segments.is_empty() && self.segments[0].ident.name == kw::PathRoot @@ -3068,7 +3071,7 @@ pub enum ItemKind<'hir> { /// or just /// /// `use foo::bar::baz;` (with `as baz` implicitly on the right). - Use(&'hir Path<'hir>, UseKind), + Use(&'hir UsePath<'hir>, UseKind), /// A `static` item. Static(&'hir Ty<'hir>, Mutability, BodyId), diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index 9eeb04c8e0102..cbb530424ca5d 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -367,7 +367,7 @@ pub trait Visitor<'v>: Sized { fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl<'v>, b: BodyId, _: Span, id: HirId) { walk_fn(self, fk, fd, b, id) } - fn visit_use(&mut self, path: &'v Path<'v>, hir_id: HirId) { + fn visit_use(&mut self, path: &'v UsePath<'v>, hir_id: HirId) { walk_use(self, path, hir_id) } fn visit_trait_item(&mut self, ti: &'v TraitItem<'v>) { @@ -938,9 +938,12 @@ pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<' } } -pub fn walk_use<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path<'v>, hir_id: HirId) { +pub fn walk_use<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v UsePath<'v>, hir_id: HirId) { visitor.visit_id(hir_id); - visitor.visit_path(path, hir_id); + let UsePath { segments, ref res, span } = *path; + for &res in res { + visitor.visit_path(&Path { segments, res, span }, hir_id); + } } pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem<'v>) { diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index 95729822677bd..2460a23bb3f34 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -1591,7 +1591,7 @@ impl<'a> State<'a> { self.print_ident(Ident::with_dummy_span(name)) } - pub fn print_path(&mut self, path: &hir::Path<'_>, colons_before_params: bool) { + pub fn print_path(&mut self, path: &hir::Path<'_, R>, colons_before_params: bool) { self.maybe_print_comment(path.span.lo()); for (i, segment) in path.segments.iter().enumerate() { diff --git a/compiler/rustc_passes/src/hir_stats.rs b/compiler/rustc_passes/src/hir_stats.rs index 20951878e0032..a7854cd49988f 100644 --- a/compiler/rustc_passes/src/hir_stats.rs +++ b/compiler/rustc_passes/src/hir_stats.rs @@ -369,7 +369,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> { hir_visit::walk_fn(self, fk, fd, b, id) } - fn visit_use(&mut self, p: &'v hir::Path<'v>, hir_id: hir::HirId) { + fn visit_use(&mut self, p: &'v hir::UsePath<'v>, hir_id: hir::HirId) { // This is `visit_use`, but the type is `Path` so record it that way. self.record("Path", Id::None, p); hir_visit::walk_use(self, p, hir_id) diff --git a/compiler/rustc_save_analysis/src/dump_visitor.rs b/compiler/rustc_save_analysis/src/dump_visitor.rs index fae20c2ba5fb7..b45288538256f 100644 --- a/compiler/rustc_save_analysis/src/dump_visitor.rs +++ b/compiler/rustc_save_analysis/src/dump_visitor.rs @@ -185,13 +185,13 @@ impl<'tcx> DumpVisitor<'tcx> { } } - fn write_sub_paths(&mut self, path: &'tcx hir::Path<'tcx>) { + fn write_sub_paths(&mut self, path: &'tcx hir::Path<'tcx, R>) { self.write_segments(path.segments) } // As write_sub_paths, but does not process the last ident in the path (assuming it // will be processed elsewhere). See note on write_sub_paths about global. - fn write_sub_paths_truncated(&mut self, path: &'tcx hir::Path<'tcx>) { + fn write_sub_paths_truncated(&mut self, path: &'tcx hir::Path<'tcx, R>) { if let [segments @ .., _] = path.segments { self.write_segments(segments) } diff --git a/compiler/rustc_save_analysis/src/lib.rs b/compiler/rustc_save_analysis/src/lib.rs index 4fa0c14715e01..f05eb2b7432b5 100644 --- a/compiler/rustc_save_analysis/src/lib.rs +++ b/compiler/rustc_save_analysis/src/lib.rs @@ -594,7 +594,9 @@ impl<'tcx> SaveContext<'tcx> { match self.tcx.hir().get(hir_id) { Node::TraitRef(tr) => tr.path.res, - Node::Item(&hir::Item { kind: hir::ItemKind::Use(path, _), .. }) => path.res, + Node::Item(&hir::Item { kind: hir::ItemKind::Use(path, _), .. }) => { + path.res.get(0).copied().unwrap_or(Res::Err) + } Node::PathSegment(seg) => { if seg.res != Res::Err { seg.res diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index b18788a033fb6..80909919ba2b6 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2231,6 +2231,26 @@ fn clean_extern_crate<'tcx>( } fn clean_use_statement<'tcx>( + import: &hir::Item<'tcx>, + name: Symbol, + path: &hir::UsePath<'tcx>, + kind: hir::UseKind, + cx: &mut DocContext<'tcx>, + inlined_names: &mut FxHashSet<(ItemType, Symbol)>, +) -> Vec { + let mut items = Vec::new(); + let hir::UsePath { segments, ref res, span } = *path; + for &res in res { + if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = res { + continue; + } + let path = hir::Path { segments, res, span }; + items.append(&mut clean_use_statement_inner(import, name, &path, kind, cx, inlined_names)); + } + items +} + +fn clean_use_statement_inner<'tcx>( import: &hir::Item<'tcx>, name: Symbol, path: &hir::Path<'tcx>, diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index ed4e9508f4309..2590bb0df3f0e 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -242,7 +242,9 @@ impl ExternalCrate { hir::ItemKind::Use(path, hir::UseKind::Single) if tcx.visibility(id.owner_id).is_public() => { - as_keyword(path.res.expect_non_local()) + path.res + .iter() + .find_map(|res| as_keyword(res.expect_non_local())) .map(|(_, prim)| (id.owner_id.to_def_id(), prim)) } _ => None, @@ -310,10 +312,11 @@ impl ExternalCrate { hir::ItemKind::Use(path, hir::UseKind::Single) if tcx.visibility(id.owner_id).is_public() => { - as_primitive(path.res.expect_non_local()).map(|(_, prim)| { + path.res + .iter() + .find_map(|res| as_primitive(res.expect_non_local())) // Pretend the primitive is local. - (id.owner_id.to_def_id(), prim) - }) + .map(|(_, prim)| (id.owner_id.to_def_id(), prim)) } _ => None, } diff --git a/src/librustdoc/html/render/span_map.rs b/src/librustdoc/html/render/span_map.rs index b898db242464a..4514894cabe0f 100644 --- a/src/librustdoc/html/render/span_map.rs +++ b/src/librustdoc/html/render/span_map.rs @@ -190,12 +190,4 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> { } intravisit::walk_expr(self, expr); } - - fn visit_use(&mut self, path: &'tcx rustc_hir::Path<'tcx>, id: HirId) { - if self.handle_macro(path.span) { - return; - } - self.handle_path(path); - intravisit::walk_use(self, path, id); - } } diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index c788b9f4093fe..22068ebe041c7 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -301,39 +301,40 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { hir::ItemKind::GlobalAsm(..) => {} hir::ItemKind::Use(_, hir::UseKind::ListStem) => {} hir::ItemKind::Use(path, kind) => { - let is_glob = kind == hir::UseKind::Glob; - - // Struct and variant constructors and proc macro stubs always show up alongside - // their definitions, we've already processed them so just discard these. - if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = path.res { - return; - } - - let attrs = self.cx.tcx.hir().attrs(item.hir_id()); + for &res in &path.res { + // Struct and variant constructors and proc macro stubs always show up alongside + // their definitions, we've already processed them so just discard these. + if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = res { + continue; + } - // If there was a private module in the current path then don't bother inlining - // anything as it will probably be stripped anyway. - if is_pub && self.inside_public_path { - let please_inline = attrs.iter().any(|item| match item.meta_item_list() { - Some(ref list) if item.has_name(sym::doc) => { - list.iter().any(|i| i.has_name(sym::inline)) + let attrs = self.cx.tcx.hir().attrs(item.hir_id()); + + // If there was a private module in the current path then don't bother inlining + // anything as it will probably be stripped anyway. + if is_pub && self.inside_public_path { + let please_inline = attrs.iter().any(|item| match item.meta_item_list() { + Some(ref list) if item.has_name(sym::doc) => { + list.iter().any(|i| i.has_name(sym::inline)) + } + _ => false, + }); + let is_glob = kind == hir::UseKind::Glob; + let ident = if is_glob { None } else { Some(name) }; + if self.maybe_inline_local( + item.hir_id(), + res, + ident, + is_glob, + om, + please_inline, + ) { + continue; } - _ => false, - }); - let ident = if is_glob { None } else { Some(name) }; - if self.maybe_inline_local( - item.hir_id(), - path.res, - ident, - is_glob, - om, - please_inline, - ) { - return; } - } - om.items.push((item, renamed, parent_id)) + om.items.push((item, renamed, parent_id)) + } } hir::ItemKind::Macro(ref macro_def, _) => { // `#[macro_export] macro_rules!` items are handled separately in `visit()`, diff --git a/src/tools/clippy/clippy_lints/src/disallowed_types.rs b/src/tools/clippy/clippy_lints/src/disallowed_types.rs index aee3d8c4f0852..1f56d0118a404 100644 --- a/src/tools/clippy/clippy_lints/src/disallowed_types.rs +++ b/src/tools/clippy/clippy_lints/src/disallowed_types.rs @@ -106,7 +106,9 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedTypes { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { if let ItemKind::Use(path, UseKind::Single) = &item.kind { - self.check_res_emit(cx, &path.res, item.span); + for res in &path.res { + self.check_res_emit(cx, res, item.span); + } } } diff --git a/src/tools/clippy/clippy_lints/src/macro_use.rs b/src/tools/clippy/clippy_lints/src/macro_use.rs index 594f6af76b3d8..e2e6a87a30151 100644 --- a/src/tools/clippy/clippy_lints/src/macro_use.rs +++ b/src/tools/clippy/clippy_lints/src/macro_use.rs @@ -94,7 +94,10 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports { let hir_id = item.hir_id(); let attrs = cx.tcx.hir().attrs(hir_id); if let Some(mac_attr) = attrs.iter().find(|attr| attr.has_name(sym::macro_use)); - if let Res::Def(DefKind::Mod, id) = path.res; + if let Some(id) = path.res.iter().find_map(|res| match res { + Res::Def(DefKind::Mod, id) => Some(id), + _ => None, + }); if !id.is_local(); then { for kid in cx.tcx.module_children(id).iter() { diff --git a/src/tools/clippy/clippy_lints/src/missing_enforced_import_rename.rs b/src/tools/clippy/clippy_lints/src/missing_enforced_import_rename.rs index 4712846939e6a..773174679dbdc 100644 --- a/src/tools/clippy/clippy_lints/src/missing_enforced_import_rename.rs +++ b/src/tools/clippy/clippy_lints/src/missing_enforced_import_rename.rs @@ -66,35 +66,38 @@ impl LateLintPass<'_> for ImportRename { } fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { - if_chain! { - if let ItemKind::Use(path, UseKind::Single) = &item.kind; - if let Res::Def(_, id) = path.res; - if let Some(name) = self.renames.get(&id); - // Remove semicolon since it is not present for nested imports - let span_without_semi = cx.sess().source_map().span_until_char(item.span, ';'); - if let Some(snip) = snippet_opt(cx, span_without_semi); - if let Some(import) = match snip.split_once(" as ") { - None => Some(snip.as_str()), - Some((import, rename)) => { - if rename.trim() == name.as_str() { - None - } else { - Some(import.trim()) + if let ItemKind::Use(path, UseKind::Single) = &item.kind { + for &res in &path.res { + if_chain! { + if let Res::Def(_, id) = res; + if let Some(name) = self.renames.get(&id); + // Remove semicolon since it is not present for nested imports + let span_without_semi = cx.sess().source_map().span_until_char(item.span, ';'); + if let Some(snip) = snippet_opt(cx, span_without_semi); + if let Some(import) = match snip.split_once(" as ") { + None => Some(snip.as_str()), + Some((import, rename)) => { + if rename.trim() == name.as_str() { + None + } else { + Some(import.trim()) + } + }, + }; + then { + span_lint_and_sugg( + cx, + MISSING_ENFORCED_IMPORT_RENAMES, + span_without_semi, + "this import should be renamed", + "try", + format!( + "{import} as {name}", + ), + Applicability::MachineApplicable, + ); } - }, - }; - then { - span_lint_and_sugg( - cx, - MISSING_ENFORCED_IMPORT_RENAMES, - span_without_semi, - "this import should be renamed", - "try", - format!( - "{import} as {name}", - ), - Applicability::MachineApplicable, - ); + } } } } diff --git a/src/tools/clippy/clippy_lints/src/redundant_pub_crate.rs b/src/tools/clippy/clippy_lints/src/redundant_pub_crate.rs index 833dc4913b469..d612d249c2f00 100644 --- a/src/tools/clippy/clippy_lints/src/redundant_pub_crate.rs +++ b/src/tools/clippy/clippy_lints/src/redundant_pub_crate.rs @@ -84,7 +84,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantPubCrate { fn is_not_macro_export<'tcx>(item: &'tcx Item<'tcx>) -> bool { if let ItemKind::Use(path, _) = item.kind { - if let Res::Def(DefKind::Macro(MacroKind::Bang), _) = path.res { + if path.res.iter().all(|res| matches!(res, Res::Def(DefKind::Macro(MacroKind::Bang), _))) { return false; } } else if let ItemKind::Macro(..) = item.kind { diff --git a/src/tools/clippy/clippy_lints/src/wildcard_imports.rs b/src/tools/clippy/clippy_lints/src/wildcard_imports.rs index be98344470b9c..e4d1ee195c4df 100644 --- a/src/tools/clippy/clippy_lints/src/wildcard_imports.rs +++ b/src/tools/clippy/clippy_lints/src/wildcard_imports.rs @@ -176,7 +176,8 @@ impl LateLintPass<'_> for WildcardImports { format!("{import_source_snippet}::{imports_string}") }; - let (lint, message) = if let Res::Def(DefKind::Enum, _) = use_path.res { + // Glob imports always have a single resolution. + let (lint, message) = if let Res::Def(DefKind::Enum, _) = use_path.res[0] { (ENUM_GLOB_USE, "usage of wildcard import for enum variants") } else { (WILDCARD_IMPORTS, "usage of wildcard import") From b32a4edb2078ca61ebcd9bad993905873f4a990a Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Thu, 1 Dec 2022 18:51:20 +0300 Subject: [PATCH 241/244] rustc_ast_lowering: Stop lowering imports into multiple items Lower them into a single item with multiple resolutions instead. This also allows to remove additional `NodId`s and `DefId`s related to those additional items. --- compiler/rustc_ast/src/ast.rs | 9 +- compiler/rustc_ast/src/mut_visit.rs | 6 +- compiler/rustc_ast/src/visit.rs | 2 +- compiler/rustc_ast_lowering/src/item.rs | 84 +++---------------- .../rustc_ast_pretty/src/pprust/state/item.rs | 2 +- .../src/assert/context.rs | 2 +- compiler/rustc_lint/src/unused.rs | 2 +- compiler/rustc_parse/src/parser/item.rs | 2 +- .../rustc_resolve/src/build_reduced_graph.rs | 15 +--- compiler/rustc_resolve/src/def_collector.rs | 8 -- .../src/effective_visibilities.rs | 29 ++----- compiler/rustc_resolve/src/imports.rs | 5 -- src/librustdoc/json/mod.rs | 2 +- src/test/ui/consts/miri_unleashed/tls.stderr | 4 +- .../print/generator-print-verbose-1.stderr | 8 +- .../self-outlives-lint.stderr | 22 ++--- src/test/ui/privacy/effective_visibilities.rs | 1 - .../ui/privacy/effective_visibilities.stderr | 8 +- src/test/ui/stats/hir-stats.stderr | 44 +++++----- .../src/single_component_path_imports.rs | 4 +- .../src/unnecessary_self_imports.rs | 2 +- .../src/unsafe_removed_from_name.rs | 4 +- .../clippy/clippy_utils/src/ast_utils.rs | 2 +- .../clippy/tests/ui/macro_use_imports.stderr | 8 +- src/tools/rustfmt/src/imports.rs | 2 +- 25 files changed, 79 insertions(+), 198 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 6a2f1f0c5749c..f2f8e1386a5c7 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2517,10 +2517,7 @@ pub struct Variant { #[derive(Clone, Encodable, Decodable, Debug)] pub enum UseTreeKind { /// `use prefix` or `use prefix as rename` - /// - /// The extra `NodeId`s are for HIR lowering, when additional statements are created for each - /// namespace. - Simple(Option, NodeId, NodeId), + Simple(Option), /// `use prefix::{...}` Nested(Vec<(UseTree, NodeId)>), /// `use prefix::*` @@ -2539,8 +2536,8 @@ pub struct UseTree { impl UseTree { pub fn ident(&self) -> Ident { match self.kind { - UseTreeKind::Simple(Some(rename), ..) => rename, - UseTreeKind::Simple(None, ..) => { + UseTreeKind::Simple(Some(rename)) => rename, + UseTreeKind::Simple(None) => { self.prefix.segments.last().expect("empty prefix in a simple import").ident } _ => panic!("`UseTree::ident` can only be used on a simple import"), diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index cb3c54fa03ce5..963e5a608a492 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -410,11 +410,7 @@ pub fn noop_visit_use_tree(use_tree: &mut UseTree, vis: &mut T) { let UseTree { prefix, kind, span } = use_tree; vis.visit_path(prefix); match kind { - UseTreeKind::Simple(rename, id1, id2) => { - visit_opt(rename, |rename| vis.visit_ident(rename)); - vis.visit_id(id1); - vis.visit_id(id2); - } + UseTreeKind::Simple(rename) => visit_opt(rename, |rename| vis.visit_ident(rename)), UseTreeKind::Nested(items) => { for (tree, id) in items { vis.visit_use_tree(tree); diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index c528118be0808..fe27d7fa8de17 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -439,7 +439,7 @@ pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) { pub fn walk_use_tree<'a, V: Visitor<'a>>(visitor: &mut V, use_tree: &'a UseTree, id: NodeId) { visitor.visit_path(&use_tree.prefix, id); match &use_tree.kind { - UseTreeKind::Simple(rename, ..) => { + UseTreeKind::Simple(rename) => { // The extra IDs are handled during HIR lowering. if let &Some(rename) = rename { visitor.visit_ident(rename); diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index b26b37b0466b1..f6275433fc51b 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -19,7 +19,6 @@ use rustc_span::symbol::{kw, sym, Ident}; use rustc_span::{Span, Symbol}; use rustc_target::spec::abi; use smallvec::{smallvec, SmallVec}; -use std::iter; use thin_vec::ThinVec; pub(super) struct ItemLowerer<'a, 'hir> { @@ -179,36 +178,22 @@ impl<'hir> LoweringContext<'_, 'hir> { let mut node_ids = smallvec![hir::ItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } }]; if let ItemKind::Use(use_tree) = &i.kind { - self.lower_item_id_use_tree(use_tree, i.id, &mut node_ids); + self.lower_item_id_use_tree(use_tree, &mut node_ids); } node_ids } - fn lower_item_id_use_tree( - &mut self, - tree: &UseTree, - base_id: NodeId, - vec: &mut SmallVec<[hir::ItemId; 1]>, - ) { + fn lower_item_id_use_tree(&mut self, tree: &UseTree, vec: &mut SmallVec<[hir::ItemId; 1]>) { match &tree.kind { UseTreeKind::Nested(nested_vec) => { for &(ref nested, id) in nested_vec { vec.push(hir::ItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(id) }, }); - self.lower_item_id_use_tree(nested, id, vec); - } - } - UseTreeKind::Glob => {} - UseTreeKind::Simple(_, id1, id2) => { - for (_, id) in - iter::zip(self.expect_full_res_from_use(base_id).skip(1), [*id1, *id2]) - { - vec.push(hir::ItemId { - owner_id: hir::OwnerId { def_id: self.local_def_id(id) }, - }); + self.lower_item_id_use_tree(nested, vec); } } + UseTreeKind::Simple(..) | UseTreeKind::Glob => {} } } @@ -489,7 +474,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let segments = prefix.segments.iter().chain(path.segments.iter()).cloned().collect(); match tree.kind { - UseTreeKind::Simple(rename, id1, id2) => { + UseTreeKind::Simple(rename) => { *ident = tree.ident(); // First, apply the prefix to the path. @@ -505,58 +490,9 @@ impl<'hir> LoweringContext<'_, 'hir> { } } - let mut resolutions = self.expect_full_res_from_use(id).fuse(); - // We want to return *something* from this function, so hold onto the first item - // for later. - let ret_res = smallvec![self.lower_res(resolutions.next().unwrap_or(Res::Err))]; - - // Here, we are looping over namespaces, if they exist for the definition - // being imported. We only handle type and value namespaces because we - // won't be dealing with macros in the rest of the compiler. - // Essentially a single `use` which imports two names is desugared into - // two imports. - for new_node_id in [id1, id2] { - let new_id = self.local_def_id(new_node_id); - let Some(res) = resolutions.next() else { - debug_assert!(self.children.iter().find(|(id, _)| id == &new_id).is_none()); - // Associate an HirId to both ids even if there is no resolution. - self.children.push(( - new_id, - hir::MaybeOwner::NonOwner(hir::HirId::make_owner(new_id))), - ); - continue; - }; - let ident = *ident; - let mut path = path.clone(); - for seg in &mut path.segments { - // Give the cloned segment the same resolution information - // as the old one (this is needed for stability checking). - let new_id = self.next_node_id(); - self.resolver.clone_res(seg.id, new_id); - seg.id = new_id; - } - let span = path.span; - - self.with_hir_id_owner(new_node_id, |this| { - let res = smallvec![this.lower_res(res)]; - let path = this.lower_use_path(res, &path, ParamMode::Explicit); - let kind = hir::ItemKind::Use(path, hir::UseKind::Single); - if let Some(attrs) = attrs { - this.attrs.insert(hir::ItemLocalId::new(0), attrs); - } - - let item = hir::Item { - owner_id: hir::OwnerId { def_id: new_id }, - ident: this.lower_ident(ident), - kind, - vis_span, - span: this.lower_span(span), - }; - hir::OwnerNode::Item(this.arena.alloc(item)) - }); - } - - let path = self.lower_use_path(ret_res, &path, ParamMode::Explicit); + let res = + self.expect_full_res_from_use(id).map(|res| self.lower_res(res)).collect(); + let path = self.lower_use_path(res, &path, ParamMode::Explicit); hir::ItemKind::Use(path, hir::UseKind::Single) } UseTreeKind::Glob => { @@ -633,8 +569,8 @@ impl<'hir> LoweringContext<'_, 'hir> { }); } - let res = self.expect_full_res_from_use(id).next().unwrap_or(Res::Err); - let res = smallvec![self.lower_res(res)]; + let res = + self.expect_full_res_from_use(id).map(|res| self.lower_res(res)).collect(); let path = self.lower_use_path(res, &prefix, ParamMode::Explicit); hir::ItemKind::Use(path, hir::UseKind::ListStem) } diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs index c52f15401abe3..e68a7b3f202b3 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/item.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs @@ -663,7 +663,7 @@ impl<'a> State<'a> { fn print_use_tree(&mut self, tree: &ast::UseTree) { match &tree.kind { - ast::UseTreeKind::Simple(rename, ..) => { + ast::UseTreeKind::Simple(rename) => { self.print_path(&tree.prefix, false, 0); if let &Some(rename) = rename { self.nbsp(); diff --git a/compiler/rustc_builtin_macros/src/assert/context.rs b/compiler/rustc_builtin_macros/src/assert/context.rs index bd415901ae34d..d82bc0453f572 100644 --- a/compiler/rustc_builtin_macros/src/assert/context.rs +++ b/compiler/rustc_builtin_macros/src/assert/context.rs @@ -106,7 +106,7 @@ impl<'cx, 'a> Context<'cx, 'a> { ( UseTree { prefix: this.cx.path(this.span, vec![Ident::with_dummy_span(sym)]), - kind: UseTreeKind::Simple(None, DUMMY_NODE_ID, DUMMY_NODE_ID), + kind: UseTreeKind::Simple(None), span: this.span, }, DUMMY_NODE_ID, diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index 88ad4c67d93e4..a7836ea8e7a4b 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -1264,7 +1264,7 @@ impl UnusedImportBraces { // Trigger the lint if the nested item is a non-self single item let node_name = match items[0].0.kind { - ast::UseTreeKind::Simple(rename, ..) => { + ast::UseTreeKind::Simple(rename) => { let orig_ident = items[0].0.prefix.segments.last().unwrap().ident; if orig_ident.name == kw::SelfLower { return; diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 767fb9378bef1..84c632199203a 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1012,7 +1012,7 @@ impl<'a> Parser<'a> { prefix.span = lo.to(self.prev_token.span); } - UseTreeKind::Simple(self.parse_rename()?, DUMMY_NODE_ID, DUMMY_NODE_ID) + UseTreeKind::Simple(self.parse_rename()?) } }; diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 91ac442431dfe..9c90d67aadf71 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -445,19 +445,13 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { prefix.is_empty() || prefix.len() == 1 && prefix[0].ident.name == kw::PathRoot }; match use_tree.kind { - ast::UseTreeKind::Simple(rename, id1, id2) => { + ast::UseTreeKind::Simple(rename) => { let mut ident = use_tree.ident(); let mut module_path = prefix; let mut source = module_path.pop().unwrap(); let mut type_ns_only = false; self.r.visibilities.insert(self.r.local_def_id(id), vis); - if id1 != ast::DUMMY_NODE_ID { - self.r.visibilities.insert(self.r.local_def_id(id1), vis); - } - if id2 != ast::DUMMY_NODE_ID { - self.r.visibilities.insert(self.r.local_def_id(id2), vis); - } if nested { // Correctly handle `self` @@ -565,7 +559,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { type_ns_only, nested, id, - additional_ids: (id1, id2), }; self.add_import(module_path, kind, use_tree.span, item, root_span, item.id, vis); @@ -621,11 +614,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { let new_span = prefix[prefix.len() - 1].ident.span; let tree = ast::UseTree { prefix: ast::Path::from_ident(Ident::new(kw::SelfLower, new_span)), - kind: ast::UseTreeKind::Simple( - Some(Ident::new(kw::Underscore, new_span)), - ast::DUMMY_NODE_ID, - ast::DUMMY_NODE_ID, - ), + kind: ast::UseTreeKind::Simple(Some(Ident::new(kw::Underscore, new_span))), span: use_tree.span, }; self.build_reduced_graph_for_use_tree( diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index bf2428e1731a6..2764a6c28a5e9 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -158,14 +158,6 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { fn visit_use_tree(&mut self, use_tree: &'a UseTree, id: NodeId, _nested: bool) { self.create_def(id, DefPathData::Use, use_tree.span); - match use_tree.kind { - UseTreeKind::Simple(_, id1, id2) => { - self.create_def(id1, DefPathData::Use, use_tree.prefix.span); - self.create_def(id2, DefPathData::Use, use_tree.prefix.span); - } - UseTreeKind::Glob => (), - UseTreeKind::Nested(..) => {} - } visit::walk_use_tree(self, use_tree, id); } diff --git a/compiler/rustc_resolve/src/effective_visibilities.rs b/compiler/rustc_resolve/src/effective_visibilities.rs index 3aa8d52db0381..85399385d1fd4 100644 --- a/compiler/rustc_resolve/src/effective_visibilities.rs +++ b/compiler/rustc_resolve/src/effective_visibilities.rs @@ -1,4 +1,4 @@ -use crate::{ImportKind, NameBinding, NameBindingKind, Resolver, ResolverTree}; +use crate::{NameBinding, NameBindingKind, Resolver, ResolverTree}; use rustc_ast::ast; use rustc_ast::visit; use rustc_ast::visit::Visitor; @@ -104,28 +104,11 @@ impl<'r, 'a> EffectiveVisibilitiesVisitor<'r, 'a> { for (binding, eff_vis) in visitor.import_effective_visibilities.iter() { let NameBindingKind::Import { import, .. } = binding.kind else { unreachable!() }; if let Some(node_id) = import.id() { - let mut update = |node_id| { - r.effective_visibilities.update_eff_vis( - r.local_def_id(node_id), - eff_vis, - ResolverTree(&r.definitions, &r.crate_loader), - ) - }; - update(node_id); - if let ImportKind::Single { additional_ids: (id1, id2), .. } = import.kind { - // In theory all the single import IDs have individual visibilities and - // effective visibilities, but in practice these IDs go straight to HIR - // where all their few uses assume that their (effective) visibility - // applies to the whole syntactic `use` item. So they all get the same - // value which is the maximum of all bindings. Maybe HIR for imports - // shouldn't use three IDs at all. - if id1 != ast::DUMMY_NODE_ID { - update(id1); - } - if id2 != ast::DUMMY_NODE_ID { - update(id2); - } - } + r.effective_visibilities.update_eff_vis( + r.local_def_id(node_id), + eff_vis, + ResolverTree(&r.definitions, &r.crate_loader), + ) } } diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index 2366b94732e0e..e6f4d7fcfcf06 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -56,9 +56,6 @@ pub enum ImportKind<'a> { /// If this is the import for `foo::bar::a`, we would have the ID of the `UseTree` /// for `a` in this field. id: NodeId, - /// Additional `NodeId`s allocated to a `ast::UseTree` for automatically generated `use` statement - /// (eg. implicit struct constructors) - additional_ids: (NodeId, NodeId), }, Glob { is_prelude: bool, @@ -88,7 +85,6 @@ impl<'a> std::fmt::Debug for ImportKind<'a> { ref type_ns_only, ref nested, ref id, - ref additional_ids, // Ignore the following to avoid an infinite loop while printing. source_bindings: _, target_bindings: _, @@ -99,7 +95,6 @@ impl<'a> std::fmt::Debug for ImportKind<'a> { .field("type_ns_only", type_ns_only) .field("nested", nested) .field("id", id) - .field("additional_ids", additional_ids) .finish_non_exhaustive(), Glob { ref is_prelude, ref max_vis, ref id } => f .debug_struct("Glob") diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index e4df1332521a7..83be1a16eb2c4 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -225,10 +225,10 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { types::ItemEnum::Function(_) | types::ItemEnum::Module(_) + | types::ItemEnum::Import(_) | types::ItemEnum::AssocConst { .. } | types::ItemEnum::AssocType { .. } => true, types::ItemEnum::ExternCrate { .. } - | types::ItemEnum::Import(_) | types::ItemEnum::StructField(_) | types::ItemEnum::Variant(_) | types::ItemEnum::TraitAlias(_) diff --git a/src/test/ui/consts/miri_unleashed/tls.stderr b/src/test/ui/consts/miri_unleashed/tls.stderr index 436c5112360e7..7aaeadd0403cb 100644 --- a/src/test/ui/consts/miri_unleashed/tls.stderr +++ b/src/test/ui/consts/miri_unleashed/tls.stderr @@ -2,13 +2,13 @@ error[E0080]: could not evaluate static initializer --> $DIR/tls.rs:11:25 | LL | unsafe { let _val = A; } - | ^ cannot access thread local static (DefId(0:6 ~ tls[78b0]::A)) + | ^ cannot access thread local static (DefId(0:4 ~ tls[78b0]::A)) error[E0080]: could not evaluate static initializer --> $DIR/tls.rs:18:26 | LL | unsafe { let _val = &A; } - | ^ cannot access thread local static (DefId(0:6 ~ tls[78b0]::A)) + | ^ cannot access thread local static (DefId(0:4 ~ tls[78b0]::A)) warning: skipping const checks | diff --git a/src/test/ui/generator/print/generator-print-verbose-1.stderr b/src/test/ui/generator/print/generator-print-verbose-1.stderr index 2e02078048027..ed0628bbbc3cc 100644 --- a/src/test/ui/generator/print/generator-print-verbose-1.stderr +++ b/src/test/ui/generator/print/generator-print-verbose-1.stderr @@ -9,7 +9,7 @@ note: generator is not `Send` as this value is used across a yield --> $DIR/generator-print-verbose-1.rs:35:9 | LL | let _non_send_gen = make_non_send_generator(); - | ------------- has type `Opaque(DefId(0:44 ~ generator_print_verbose_1[749a]::make_non_send_generator::{opaque#0}), [])` which is not `Send` + | ------------- has type `Opaque(DefId(0:34 ~ generator_print_verbose_1[749a]::make_non_send_generator::{opaque#0}), [])` which is not `Send` LL | yield; | ^^^^^ yield occurs here, with `_non_send_gen` maybe used later LL | }; @@ -35,17 +35,17 @@ note: required because it's used within this generator | LL | || { | ^^ -note: required because it appears within the type `Opaque(DefId(0:45 ~ generator_print_verbose_1[749a]::make_gen2::{opaque#0}), [std::sync::Arc>])` +note: required because it appears within the type `Opaque(DefId(0:35 ~ generator_print_verbose_1[749a]::make_gen2::{opaque#0}), [std::sync::Arc>])` --> $DIR/generator-print-verbose-1.rs:41:30 | LL | pub fn make_gen2(t: T) -> impl Generator { | ^^^^^^^^^^^^^^^^^^^^^^^^^^ -note: required because it appears within the type `Opaque(DefId(0:46 ~ generator_print_verbose_1[749a]::make_non_send_generator2::{opaque#0}), [])` +note: required because it appears within the type `Opaque(DefId(0:36 ~ generator_print_verbose_1[749a]::make_non_send_generator2::{opaque#0}), [])` --> $DIR/generator-print-verbose-1.rs:47:34 | LL | fn make_non_send_generator2() -> impl Generator>> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - = note: required because it captures the following types: `Opaque(DefId(0:46 ~ generator_print_verbose_1[749a]::make_non_send_generator2::{opaque#0}), [])`, `()` + = note: required because it captures the following types: `Opaque(DefId(0:36 ~ generator_print_verbose_1[749a]::make_non_send_generator2::{opaque#0}), [])`, `()` note: required because it's used within this generator --> $DIR/generator-print-verbose-1.rs:52:20 | diff --git a/src/test/ui/generic-associated-types/self-outlives-lint.stderr b/src/test/ui/generic-associated-types/self-outlives-lint.stderr index 58172bf06b512..9e9b2e18abeb8 100644 --- a/src/test/ui/generic-associated-types/self-outlives-lint.stderr +++ b/src/test/ui/generic-associated-types/self-outlives-lint.stderr @@ -108,17 +108,6 @@ LL | type Bar<'b>; = note: this bound is currently required to ensure that impls have maximum flexibility = note: we are soliciting feedback, see issue #87479 for more information -error: missing required bound on `Item` - --> $DIR/self-outlives-lint.rs:140:5 - | -LL | type Item<'a>; - | ^^^^^^^^^^^^^- - | | - | help: add the required where clause: `where Self: 'a` - | - = note: this bound is currently required to ensure that impls have maximum flexibility - = note: we are soliciting feedback, see issue #87479 for more information - error: missing required bound on `Iterator` --> $DIR/self-outlives-lint.rs:142:5 | @@ -130,6 +119,17 @@ LL | type Iterator<'a>: Iterator>; = note: this bound is currently required to ensure that impls have maximum flexibility = note: we are soliciting feedback, see issue #87479 for more information +error: missing required bound on `Item` + --> $DIR/self-outlives-lint.rs:140:5 + | +LL | type Item<'a>; + | ^^^^^^^^^^^^^- + | | + | help: add the required where clause: `where Self: 'a` + | + = note: this bound is currently required to ensure that impls have maximum flexibility + = note: we are soliciting feedback, see issue #87479 for more information + error: missing required bound on `Item` --> $DIR/self-outlives-lint.rs:148:5 | diff --git a/src/test/ui/privacy/effective_visibilities.rs b/src/test/ui/privacy/effective_visibilities.rs index 8d0602fa79f02..ff20e20d33288 100644 --- a/src/test/ui/privacy/effective_visibilities.rs +++ b/src/test/ui/privacy/effective_visibilities.rs @@ -72,6 +72,5 @@ mod half_public_import { #[rustc_effective_visibility] pub use half_public_import::HalfPublicImport; //~ ERROR Direct: pub, Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub - //~^ ERROR Direct: pub, Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub fn main() {} diff --git a/src/test/ui/privacy/effective_visibilities.stderr b/src/test/ui/privacy/effective_visibilities.stderr index 6a99afe64fee3..046b6095f4e79 100644 --- a/src/test/ui/privacy/effective_visibilities.stderr +++ b/src/test/ui/privacy/effective_visibilities.stderr @@ -124,12 +124,6 @@ error: Direct: pub, Reexported: pub, Reachable: pub, ReachableThroughImplTrait: LL | pub use half_public_import::HalfPublicImport; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: Direct: pub, Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub - --> $DIR/effective_visibilities.rs:74:9 - | -LL | pub use half_public_import::HalfPublicImport; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - error: Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub --> $DIR/effective_visibilities.rs:14:13 | @@ -142,5 +136,5 @@ error: Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImpl LL | type B; | ^^^^^^ -error: aborting due to 24 previous errors +error: aborting due to 23 previous errors diff --git a/src/test/ui/stats/hir-stats.stderr b/src/test/ui/stats/hir-stats.stderr index 2a0e9497a21fa..15900bef7f624 100644 --- a/src/test/ui/stats/hir-stats.stderr +++ b/src/test/ui/stats/hir-stats.stderr @@ -120,59 +120,59 @@ hir-stats Name Accumulated Size Count Item Size hir-stats ---------------------------------------------------------------- hir-stats ForeignItemRef 24 ( 0.3%) 1 24 hir-stats Lifetime 24 ( 0.3%) 1 24 -hir-stats Mod 32 ( 0.3%) 1 32 +hir-stats Mod 32 ( 0.4%) 1 32 hir-stats ExprField 40 ( 0.4%) 1 40 hir-stats TraitItemRef 56 ( 0.6%) 2 28 hir-stats Local 64 ( 0.7%) 1 64 hir-stats Param 64 ( 0.7%) 2 32 hir-stats InlineAsm 72 ( 0.8%) 1 72 hir-stats ImplItemRef 72 ( 0.8%) 2 36 -hir-stats Body 96 ( 1.0%) 3 32 -hir-stats FieldDef 96 ( 1.0%) 2 48 -hir-stats Arm 96 ( 1.0%) 2 48 -hir-stats Stmt 96 ( 1.0%) 3 32 -hir-stats - Local 32 ( 0.3%) 1 -hir-stats - Semi 32 ( 0.3%) 1 -hir-stats - Expr 32 ( 0.3%) 1 +hir-stats Body 96 ( 1.1%) 3 32 +hir-stats FieldDef 96 ( 1.1%) 2 48 +hir-stats Arm 96 ( 1.1%) 2 48 +hir-stats Stmt 96 ( 1.1%) 3 32 +hir-stats - Local 32 ( 0.4%) 1 +hir-stats - Semi 32 ( 0.4%) 1 +hir-stats - Expr 32 ( 0.4%) 1 hir-stats FnDecl 120 ( 1.3%) 3 40 hir-stats Attribute 128 ( 1.4%) 4 32 hir-stats GenericArg 128 ( 1.4%) 4 32 -hir-stats - Type 32 ( 0.3%) 1 -hir-stats - Lifetime 96 ( 1.0%) 3 +hir-stats - Type 32 ( 0.4%) 1 +hir-stats - Lifetime 96 ( 1.1%) 3 hir-stats GenericArgs 144 ( 1.6%) 3 48 hir-stats Variant 176 ( 1.9%) 2 88 hir-stats GenericBound 192 ( 2.1%) 4 48 hir-stats - Trait 192 ( 2.1%) 4 hir-stats WherePredicate 192 ( 2.1%) 3 64 hir-stats - BoundPredicate 192 ( 2.1%) 3 -hir-stats Block 288 ( 3.1%) 6 48 -hir-stats Pat 360 ( 3.9%) 5 72 +hir-stats Block 288 ( 3.2%) 6 48 +hir-stats Pat 360 ( 4.0%) 5 72 hir-stats - Wild 72 ( 0.8%) 1 hir-stats - Struct 72 ( 0.8%) 1 hir-stats - Binding 216 ( 2.4%) 3 hir-stats GenericParam 400 ( 4.4%) 5 80 -hir-stats Generics 560 ( 6.1%) 10 56 -hir-stats Ty 720 ( 7.9%) 15 48 +hir-stats Generics 560 ( 6.2%) 10 56 +hir-stats Ty 720 ( 8.0%) 15 48 hir-stats - Ptr 48 ( 0.5%) 1 hir-stats - Rptr 48 ( 0.5%) 1 -hir-stats - Path 624 ( 6.8%) 13 -hir-stats Expr 768 ( 8.4%) 12 64 +hir-stats - Path 624 ( 6.9%) 13 +hir-stats Expr 768 ( 8.5%) 12 64 hir-stats - Path 64 ( 0.7%) 1 hir-stats - Struct 64 ( 0.7%) 1 hir-stats - Match 64 ( 0.7%) 1 hir-stats - InlineAsm 64 ( 0.7%) 1 hir-stats - Lit 128 ( 1.4%) 2 hir-stats - Block 384 ( 4.2%) 6 -hir-stats Item 960 (10.5%) 12 80 +hir-stats Item 880 ( 9.7%) 11 80 hir-stats - Trait 80 ( 0.9%) 1 hir-stats - Enum 80 ( 0.9%) 1 hir-stats - ExternCrate 80 ( 0.9%) 1 hir-stats - ForeignMod 80 ( 0.9%) 1 hir-stats - Impl 80 ( 0.9%) 1 -hir-stats - Fn 160 ( 1.7%) 2 -hir-stats - Use 400 ( 4.4%) 5 -hir-stats Path 1_280 (14.0%) 32 40 -hir-stats PathSegment 1_920 (20.9%) 40 48 +hir-stats - Fn 160 ( 1.8%) 2 +hir-stats - Use 320 ( 3.5%) 4 +hir-stats Path 1_240 (13.7%) 31 40 +hir-stats PathSegment 1_920 (21.2%) 40 48 hir-stats ---------------------------------------------------------------- -hir-stats Total 9_168 +hir-stats Total 9_048 hir-stats diff --git a/src/tools/clippy/clippy_lints/src/single_component_path_imports.rs b/src/tools/clippy/clippy_lints/src/single_component_path_imports.rs index 2036e85db7e8c..d46f6a6352c63 100644 --- a/src/tools/clippy/clippy_lints/src/single_component_path_imports.rs +++ b/src/tools/clippy/clippy_lints/src/single_component_path_imports.rs @@ -149,7 +149,7 @@ impl SingleComponentPathImports { // keep track of `use some_module;` usages if segments.len() == 1 { - if let UseTreeKind::Simple(None, _, _) = use_tree.kind { + if let UseTreeKind::Simple(None) = use_tree.kind { let name = segments[0].ident.name; if !macros.contains(&name) { single_use_usages.push(SingleUse { @@ -169,7 +169,7 @@ impl SingleComponentPathImports { for tree in trees { let segments = &tree.0.prefix.segments; if segments.len() == 1 { - if let UseTreeKind::Simple(None, _, _) = tree.0.kind { + if let UseTreeKind::Simple(None) = tree.0.kind { let name = segments[0].ident.name; if !macros.contains(&name) { single_use_usages.push(SingleUse { diff --git a/src/tools/clippy/clippy_lints/src/unnecessary_self_imports.rs b/src/tools/clippy/clippy_lints/src/unnecessary_self_imports.rs index bc0dd263d88ab..397633f533b22 100644 --- a/src/tools/clippy/clippy_lints/src/unnecessary_self_imports.rs +++ b/src/tools/clippy/clippy_lints/src/unnecessary_self_imports.rs @@ -57,7 +57,7 @@ impl EarlyLintPass for UnnecessarySelfImports { format!( "{}{};", last_segment.ident, - if let UseTreeKind::Simple(Some(alias), ..) = self_tree.kind { format!(" as {alias}") } else { String::new() }, + if let UseTreeKind::Simple(Some(alias)) = self_tree.kind { format!(" as {alias}") } else { String::new() }, ), Applicability::MaybeIncorrect, ); diff --git a/src/tools/clippy/clippy_lints/src/unsafe_removed_from_name.rs b/src/tools/clippy/clippy_lints/src/unsafe_removed_from_name.rs index 952586527689a..7ee785804f0a5 100644 --- a/src/tools/clippy/clippy_lints/src/unsafe_removed_from_name.rs +++ b/src/tools/clippy/clippy_lints/src/unsafe_removed_from_name.rs @@ -39,7 +39,7 @@ impl EarlyLintPass for UnsafeNameRemoval { fn check_use_tree(use_tree: &UseTree, cx: &EarlyContext<'_>, span: Span) { match use_tree.kind { - UseTreeKind::Simple(Some(new_name), ..) => { + UseTreeKind::Simple(Some(new_name)) => { let old_name = use_tree .prefix .segments @@ -48,7 +48,7 @@ fn check_use_tree(use_tree: &UseTree, cx: &EarlyContext<'_>, span: Span) { .ident; unsafe_to_safe_check(old_name, new_name, cx, span); }, - UseTreeKind::Simple(None, ..) | UseTreeKind::Glob => {}, + UseTreeKind::Simple(None) | UseTreeKind::Glob => {}, UseTreeKind::Nested(ref nested_use_tree) => { for (use_tree, _) in nested_use_tree { check_use_tree(use_tree, cx, span); diff --git a/src/tools/clippy/clippy_utils/src/ast_utils.rs b/src/tools/clippy/clippy_utils/src/ast_utils.rs index 6bcf0bbd7eb75..49e5f283db089 100644 --- a/src/tools/clippy/clippy_utils/src/ast_utils.rs +++ b/src/tools/clippy/clippy_utils/src/ast_utils.rs @@ -566,7 +566,7 @@ pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool { use UseTreeKind::*; match (l, r) { (Glob, Glob) => true, - (Simple(l, _, _), Simple(r, _, _)) => both(l, r, |l, r| eq_id(*l, *r)), + (Simple(l), Simple(r)) => both(l, r, |l, r| eq_id(*l, *r)), (Nested(l), Nested(r)) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)), _ => false, } diff --git a/src/tools/clippy/tests/ui/macro_use_imports.stderr b/src/tools/clippy/tests/ui/macro_use_imports.stderr index bf7b6edd0e314..61843124ccd91 100644 --- a/src/tools/clippy/tests/ui/macro_use_imports.stderr +++ b/src/tools/clippy/tests/ui/macro_use_imports.stderr @@ -1,8 +1,8 @@ error: `macro_use` attributes are no longer needed in the Rust 2018 edition - --> $DIR/macro_use_imports.rs:23:5 + --> $DIR/macro_use_imports.rs:25:5 | LL | #[macro_use] - | ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::{inner::foofoo, inner::try_err};` + | ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::inner::nested::string_add;` | = note: `-D clippy::macro-use-imports` implied by `-D warnings` @@ -13,10 +13,10 @@ LL | #[macro_use] | ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mini_mac::ClippyMiniMacroTest;` error: `macro_use` attributes are no longer needed in the Rust 2018 edition - --> $DIR/macro_use_imports.rs:25:5 + --> $DIR/macro_use_imports.rs:23:5 | LL | #[macro_use] - | ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::inner::nested::string_add;` + | ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::{inner::foofoo, inner::try_err};` error: `macro_use` attributes are no longer needed in the Rust 2018 edition --> $DIR/macro_use_imports.rs:19:5 diff --git a/src/tools/rustfmt/src/imports.rs b/src/tools/rustfmt/src/imports.rs index b6530c69243ed..d9dc8d004aff4 100644 --- a/src/tools/rustfmt/src/imports.rs +++ b/src/tools/rustfmt/src/imports.rs @@ -490,7 +490,7 @@ impl UseTree { ); result.path.push(UseSegment { kind, version }); } - UseTreeKind::Simple(ref rename, ..) => { + UseTreeKind::Simple(ref rename) => { // If the path has leading double colons and is composed of only 2 segments, then we // bypass the call to path_to_imported_ident which would get only the ident and // lose the path root, e.g., `that` in `::that`. From 935def4df19d72bb9e7766a47de1621b4046d264 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 1 Dec 2022 11:31:44 -0700 Subject: [PATCH 242/244] rustdoc: use simpler CSS for setting the font on scraped examples --- src/librustdoc/html/static/css/rustdoc.css | 4 +--- src/test/rustdoc-gui/scrape-examples-fonts.goml | 8 ++++++++ .../src/scrape_examples/examples/check-many-1.rs | 3 +++ .../src/scrape_examples/examples/check-many-2.rs | 3 +++ .../src/scrape_examples/examples/check-many-3.rs | 3 +++ .../src/scrape_examples/examples/check-many-4.rs | 3 +++ .../src/scrape_examples/examples/check-many-5.rs | 3 +++ .../src/scrape_examples/examples/check-many-6.rs | 3 +++ .../src/scrape_examples/examples/check-many-7.rs | 3 +++ src/test/rustdoc-gui/src/scrape_examples/src/lib.rs | 2 ++ 10 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 src/test/rustdoc-gui/scrape-examples-fonts.goml create mode 100644 src/test/rustdoc-gui/src/scrape_examples/examples/check-many-1.rs create mode 100644 src/test/rustdoc-gui/src/scrape_examples/examples/check-many-2.rs create mode 100644 src/test/rustdoc-gui/src/scrape_examples/examples/check-many-3.rs create mode 100644 src/test/rustdoc-gui/src/scrape_examples/examples/check-many-4.rs create mode 100644 src/test/rustdoc-gui/src/scrape_examples/examples/check-many-5.rs create mode 100644 src/test/rustdoc-gui/src/scrape_examples/examples/check-many-6.rs create mode 100644 src/test/rustdoc-gui/src/scrape_examples/examples/check-many-7.rs diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 02a51312e8f84..055ac9fa505b2 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -197,9 +197,7 @@ a.srclink, #help-button > a, details.rustdoc-toggle.top-doc > summary, details.rustdoc-toggle.non-exhaustive > summary, -.scraped-example-title, -.more-examples-toggle summary, .more-examples-toggle .hide-more, -.example-links a, +.scraped-example-list, /* This selector is for the items listed in the "all items" page. */ ul.all-items { font-family: "Fira Sans", Arial, NanumBarunGothic, sans-serif; diff --git a/src/test/rustdoc-gui/scrape-examples-fonts.goml b/src/test/rustdoc-gui/scrape-examples-fonts.goml new file mode 100644 index 0000000000000..b7d7f4ccb4ae6 --- /dev/null +++ b/src/test/rustdoc-gui/scrape-examples-fonts.goml @@ -0,0 +1,8 @@ +goto: "file://" + |DOC_PATH| + "/scrape_examples/fn.test_many.html" + +store-value: (font, '"Fira Sans", Arial, NanumBarunGothic, sans-serif') + +wait-for-css: (".scraped-example-title", {"font-family": |font|}) +wait-for-css: (".more-examples-toggle summary", {"font-family": |font|}) +wait-for-css: (".more-examples-toggle .hide-more", {"font-family": |font|}) +wait-for-css: (".example-links a", {"font-family": |font|}) diff --git a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-1.rs b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-1.rs new file mode 100644 index 0000000000000..1d1bc5002aa8f --- /dev/null +++ b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-1.rs @@ -0,0 +1,3 @@ +fn main() { + scrape_examples::test_many(); +} diff --git a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-2.rs b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-2.rs new file mode 100644 index 0000000000000..1d1bc5002aa8f --- /dev/null +++ b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-2.rs @@ -0,0 +1,3 @@ +fn main() { + scrape_examples::test_many(); +} diff --git a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-3.rs b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-3.rs new file mode 100644 index 0000000000000..1d1bc5002aa8f --- /dev/null +++ b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-3.rs @@ -0,0 +1,3 @@ +fn main() { + scrape_examples::test_many(); +} diff --git a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-4.rs b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-4.rs new file mode 100644 index 0000000000000..1d1bc5002aa8f --- /dev/null +++ b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-4.rs @@ -0,0 +1,3 @@ +fn main() { + scrape_examples::test_many(); +} diff --git a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-5.rs b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-5.rs new file mode 100644 index 0000000000000..1d1bc5002aa8f --- /dev/null +++ b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-5.rs @@ -0,0 +1,3 @@ +fn main() { + scrape_examples::test_many(); +} diff --git a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-6.rs b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-6.rs new file mode 100644 index 0000000000000..1d1bc5002aa8f --- /dev/null +++ b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-6.rs @@ -0,0 +1,3 @@ +fn main() { + scrape_examples::test_many(); +} diff --git a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-7.rs b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-7.rs new file mode 100644 index 0000000000000..1d1bc5002aa8f --- /dev/null +++ b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-7.rs @@ -0,0 +1,3 @@ +fn main() { + scrape_examples::test_many(); +} diff --git a/src/test/rustdoc-gui/src/scrape_examples/src/lib.rs b/src/test/rustdoc-gui/src/scrape_examples/src/lib.rs index 6412de2c03afd..88b03cf26038d 100644 --- a/src/test/rustdoc-gui/src/scrape_examples/src/lib.rs +++ b/src/test/rustdoc-gui/src/scrape_examples/src/lib.rs @@ -5,3 +5,5 @@ /// test(); /// ``` pub fn test() {} + +pub fn test_many() {} From 80ab672b8611d34bf811d4498b9b9d116a085e7c Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 2 Dec 2022 18:14:53 +0100 Subject: [PATCH 243/244] Preparing for merge from rustc --- src/tools/miri/rust-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/miri/rust-version b/src/tools/miri/rust-version index 851ef39274094..0a6b9417cc2e2 100644 --- a/src/tools/miri/rust-version +++ b/src/tools/miri/rust-version @@ -1 +1 @@ -454784afba5bf35b5ff14ada0e31265ad1d75e73 +cef44f53034eac46be3a0e3eec7b2b3d4ef5140b From 29814f2e2ace29268f76b109022ea4fe26de6535 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 3 Dec 2022 13:24:04 +0100 Subject: [PATCH 244/244] clippy --- src/tools/miri/cargo-miri/src/phases.rs | 3 +- src/tools/miri/src/bin/miri.rs | 5 +-- .../miri/src/concurrency/vector_clock.rs | 36 +++++-------------- src/tools/miri/src/helpers.rs | 10 ++---- src/tools/miri/src/shims/foreign_items.rs | 3 +- 5 files changed, 15 insertions(+), 42 deletions(-) diff --git a/src/tools/miri/cargo-miri/src/phases.rs b/src/tools/miri/cargo-miri/src/phases.rs index 91945ad39f50b..2bffff4772270 100644 --- a/src/tools/miri/cargo-miri/src/phases.rs +++ b/src/tools/miri/cargo-miri/src/phases.rs @@ -486,8 +486,7 @@ pub fn phase_runner(mut binary_args: impl Iterator, phase: Runner continue; } else if verbose > 0 { eprintln!( - "[cargo-miri runner] Overwriting run-time env var {:?}={:?} with build-time value {:?}", - name, old_val, val + "[cargo-miri runner] Overwriting run-time env var {name:?}={old_val:?} with build-time value {val:?}" ); } } diff --git a/src/tools/miri/src/bin/miri.rs b/src/tools/miri/src/bin/miri.rs index 9ac04c4930f26..fce95b987f729 100644 --- a/src/tools/miri/src/bin/miri.rs +++ b/src/tools/miri/src/bin/miri.rs @@ -192,10 +192,7 @@ fn init_late_loggers(tcx: TyCtxt<'_>) { if log::Level::from_str(&var).is_ok() { env::set_var( "RUSTC_LOG", - format!( - "rustc_middle::mir::interpret={0},rustc_const_eval::interpret={0}", - var - ), + format!("rustc_middle::mir::interpret={var},rustc_const_eval::interpret={var}"), ); } else { env::set_var("RUSTC_LOG", &var); diff --git a/src/tools/miri/src/concurrency/vector_clock.rs b/src/tools/miri/src/concurrency/vector_clock.rs index e7e5b35ac2cd2..ba04991a5889d 100644 --- a/src/tools/miri/src/concurrency/vector_clock.rs +++ b/src/tools/miri/src/concurrency/vector_clock.rs @@ -404,67 +404,49 @@ mod tests { assert_eq!( alt_compare, o.map(Ordering::reverse), - "Invalid alt comparison\n l: {:?}\n r: {:?}", - l, - r + "Invalid alt comparison\n l: {l:?}\n r: {r:?}" ); //Test operators with faster implementations assert_eq!( matches!(compare, Some(Ordering::Less)), l < r, - "Invalid (<):\n l: {:?}\n r: {:?}", - l, - r + "Invalid (<):\n l: {l:?}\n r: {r:?}" ); assert_eq!( matches!(compare, Some(Ordering::Less) | Some(Ordering::Equal)), l <= r, - "Invalid (<=):\n l: {:?}\n r: {:?}", - l, - r + "Invalid (<=):\n l: {l:?}\n r: {r:?}" ); assert_eq!( matches!(compare, Some(Ordering::Greater)), l > r, - "Invalid (>):\n l: {:?}\n r: {:?}", - l, - r + "Invalid (>):\n l: {l:?}\n r: {r:?}" ); assert_eq!( matches!(compare, Some(Ordering::Greater) | Some(Ordering::Equal)), l >= r, - "Invalid (>=):\n l: {:?}\n r: {:?}", - l, - r + "Invalid (>=):\n l: {l:?}\n r: {r:?}" ); assert_eq!( matches!(alt_compare, Some(Ordering::Less)), r < l, - "Invalid alt (<):\n l: {:?}\n r: {:?}", - l, - r + "Invalid alt (<):\n l: {l:?}\n r: {r:?}" ); assert_eq!( matches!(alt_compare, Some(Ordering::Less) | Some(Ordering::Equal)), r <= l, - "Invalid alt (<=):\n l: {:?}\n r: {:?}", - l, - r + "Invalid alt (<=):\n l: {l:?}\n r: {r:?}" ); assert_eq!( matches!(alt_compare, Some(Ordering::Greater)), r > l, - "Invalid alt (>):\n l: {:?}\n r: {:?}", - l, - r + "Invalid alt (>):\n l: {l:?}\n r: {r:?}" ); assert_eq!( matches!(alt_compare, Some(Ordering::Greater) | Some(Ordering::Equal)), r >= l, - "Invalid alt (>=):\n l: {:?}\n r: {:?}", - l, - r + "Invalid alt (>=):\n l: {l:?}\n r: {r:?}" ); } } diff --git a/src/tools/miri/src/helpers.rs b/src/tools/miri/src/helpers.rs index 8f6a5fbc1f01c..7fb2539ca5a67 100644 --- a/src/tools/miri/src/helpers.rs +++ b/src/tools/miri/src/helpers.rs @@ -554,9 +554,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { assert_eq!( self.eval_context_ref().tcx.sess.target.os, target_os, - "`{}` is only available on the `{}` target OS", - name, - target_os, + "`{name}` is only available on the `{target_os}` target OS", ) } @@ -566,8 +564,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { fn assert_target_os_is_unix(&self, name: &str) { assert!( target_os_is_unix(self.eval_context_ref().tcx.sess.target.os.as_ref()), - "`{}` is only available for supported UNIX family targets", - name, + "`{name}` is only available for supported UNIX family targets", ); } @@ -1019,8 +1016,7 @@ where pub fn isolation_abort_error<'tcx>(name: &str) -> InterpResult<'tcx> { throw_machine_stop!(TerminationInfo::UnsupportedInIsolation(format!( - "{} not available when isolation is enabled", - name, + "{name} not available when isolation is enabled", ))) } diff --git a/src/tools/miri/src/shims/foreign_items.rs b/src/tools/miri/src/shims/foreign_items.rs index f72521f64adaf..8370e02b588af 100644 --- a/src/tools/miri/src/shims/foreign_items.rs +++ b/src/tools/miri/src/shims/foreign_items.rs @@ -299,8 +299,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { return Ok(Some(body)); } this.handle_unsupported(format!( - "can't call (diverging) foreign function: {}", - link_name + "can't call (diverging) foreign function: {link_name}" ))?; return Ok(None); }