forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#5894 - tmiasko:self-assignment, r=Manishearth
Warn about explicit self-assignment Warn about assignments where left-hand side place expression is the same as right-hand side value expression. For example, warn about assignment in: ```rust pub struct Event { id: usize, x: i32, y: i32, } pub fn copy_position(a: &mut Event, b: &Event) { a.x = b.x; a.y = a.y; } ``` changelog: New lint `self_assignment`, checks for explicit self-assignments.
- Loading branch information
Showing
17 changed files
with
257 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use crate::utils::{eq_expr_value, snippet, span_lint}; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// **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:** | ||
/// | ||
/// ```rust | ||
/// struct Event { | ||
/// id: usize, | ||
/// x: i32, | ||
/// y: i32, | ||
/// } | ||
/// | ||
/// fn copy_position(a: &mut Event, b: &Event) { | ||
/// a.x = b.x; | ||
/// a.y = a.y; | ||
/// } | ||
/// ``` | ||
pub SELF_ASSIGNMENT, | ||
correctness, | ||
"explicit self-assignment" | ||
} | ||
|
||
declare_lint_pass!(SelfAssignment => [SELF_ASSIGNMENT]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for SelfAssignment { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { | ||
if let ExprKind::Assign(lhs, rhs, _) = &expr.kind { | ||
if eq_expr_value(cx, lhs, rhs) { | ||
let lhs = snippet(cx, lhs.span, "<lhs>"); | ||
let rhs = snippet(cx, rhs.span, "<rhs>"); | ||
span_lint( | ||
cx, | ||
SELF_ASSIGNMENT, | ||
expr.span, | ||
&format!("self-assignment of `{}` to `{}`", rhs, lhs), | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.