-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 #7726 - dswij:unseparated-literal-suffix, r=flip1995
Unseparated literal suffix Closes #7658 Since `literal_suffix` style is opinionated, we should disable by default and only enforce if it's stated as so. changelog: [`unseparated_literal_suffix`] is renamed to `literal_suffix`, adds a new configuration `literal-suffix-style` to enforce a certain style writing literal_suffix. Possible values for `literal-suffix-style`: `"separated"`, `"unseparated"`
- Loading branch information
Showing
10 changed files
with
151 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,18 @@ | ||
use super::MiscEarlyLints; | ||
use clippy_utils::diagnostics::span_lint; | ||
use rustc_ast::ast::{Expr, ExprKind, UnOp}; | ||
use rustc_lint::EarlyContext; | ||
|
||
use super::DOUBLE_NEG; | ||
|
||
pub(super) fn check(cx: &EarlyContext<'_>, expr: &Expr) { | ||
match expr.kind { | ||
ExprKind::Unary(UnOp::Neg, ref inner) => { | ||
if let ExprKind::Unary(UnOp::Neg, _) = inner.kind { | ||
span_lint( | ||
cx, | ||
DOUBLE_NEG, | ||
expr.span, | ||
"`--x` could be misinterpreted as pre-decrement by C programmers, is usually a no-op", | ||
); | ||
} | ||
}, | ||
ExprKind::Lit(ref lit) => MiscEarlyLints::check_lit(cx, lit), | ||
_ => (), | ||
if let ExprKind::Unary(UnOp::Neg, ref inner) = expr.kind { | ||
if let ExprKind::Unary(UnOp::Neg, _) = inner.kind { | ||
span_lint( | ||
cx, | ||
DOUBLE_NEG, | ||
expr.span, | ||
"`--x` could be misinterpreted as pre-decrement by C programmers, is usually a no-op", | ||
); | ||
} | ||
} | ||
} |
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,38 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use rustc_ast::ast::Lit; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::EarlyContext; | ||
|
||
use super::{SEPARATED_LITERAL_SUFFIX, UNSEPARATED_LITERAL_SUFFIX}; | ||
|
||
pub(super) fn check(cx: &EarlyContext<'_>, lit: &Lit, lit_snip: &str, suffix: &str, sugg_type: &str) { | ||
let maybe_last_sep_idx = if let Some(val) = lit_snip.len().checked_sub(suffix.len() + 1) { | ||
val | ||
} else { | ||
return; // It's useless so shouldn't lint. | ||
}; | ||
// Do not lint when literal is unsuffixed. | ||
if !suffix.is_empty() { | ||
if lit_snip.as_bytes()[maybe_last_sep_idx] == b'_' { | ||
span_lint_and_sugg( | ||
cx, | ||
SEPARATED_LITERAL_SUFFIX, | ||
lit.span, | ||
&format!("{} type suffix should not be separated by an underscore", sugg_type), | ||
"remove the underscore", | ||
format!("{}{}", &lit_snip[..maybe_last_sep_idx], suffix), | ||
Applicability::MachineApplicable, | ||
); | ||
} else { | ||
span_lint_and_sugg( | ||
cx, | ||
UNSEPARATED_LITERAL_SUFFIX, | ||
lit.span, | ||
&format!("{} type suffix should be separated by an underscore", sugg_type), | ||
"add an underscore", | ||
format!("{}_{}", &lit_snip[..=maybe_last_sep_idx], suffix), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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