Skip to content

Commit

Permalink
Auto merge of #7726 - dswij:unseparated-literal-suffix, r=flip1995
Browse files Browse the repository at this point in the history
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
bors committed Nov 2, 2021
2 parents 4e355eb + bb1cf72 commit 38d8025
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 59 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3123,6 +3123,7 @@ Released 2018-09-13
[`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
[`semicolon_if_nothing_returned`]: https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned
[`separated_literal_suffix`]: https://rust-lang.github.io/rust-clippy/master/index.html#separated_literal_suffix
[`serde_api_misuse`]: https://rust-lang.github.io/rust-clippy/master/index.html#serde_api_misuse
[`shadow_reuse`]: https://rust-lang.github.io/rust-clippy/master/index.html#shadow_reuse
[`shadow_same`]: https://rust-lang.github.io/rust-clippy/master/index.html#shadow_same
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/lib.register_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ store.register_lints(&[
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,
Expand Down
1 change: 0 additions & 1 deletion clippy_lints/src/lib.register_pedantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ store.register_group(true, "clippy::pedantic", Some("clippy_pedantic"), vec![
LintId::of(methods::MAP_UNWRAP_OR),
LintId::of(misc::FLOAT_CMP),
LintId::of(misc::USED_UNDERSCORE_BINDING),
LintId::of(misc_early::UNSEPARATED_LITERAL_SUFFIX),
LintId::of(mut_mut::MUT_MUT),
LintId::of(needless_bitwise_bool::NEEDLESS_BITWISE_BOOL),
LintId::of(needless_borrow::REF_BINDING_TO_REFERENCE),
Expand Down
2 changes: 2 additions & 0 deletions clippy_lints/src/lib.register_restriction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ store.register_group(true, "clippy::restriction", Some("clippy_restriction"), ve
LintId::of(methods::GET_UNWRAP),
LintId::of(methods::UNWRAP_USED),
LintId::of(misc::FLOAT_CMP_CONST),
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),
Expand Down
23 changes: 9 additions & 14 deletions clippy_lints/src/misc_early/double_neg.rs
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",
);
}
}
}
38 changes: 38 additions & 0 deletions clippy_lints/src/misc_early/literal_suffix.rs
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,
);
}
}
}
41 changes: 35 additions & 6 deletions clippy_lints/src/misc_early/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
mod builtin_type_shadow;
mod double_neg;
mod literal_suffix;
mod mixed_case_hex_literals;
mod redundant_pattern;
mod unneeded_field_pattern;
mod unneeded_wildcard_pattern;
mod unseparated_literal_suffix;
mod zero_prefixed_literal;

use clippy_utils::diagnostics::span_lint;
use clippy_utils::source::snippet_opt;
use rustc_ast::ast::{Expr, Generics, Lit, LitFloatType, LitIntType, LitKind, NodeId, Pat, PatKind};
use rustc_ast::ast::{Expr, ExprKind, Generics, Lit, LitFloatType, LitIntType, LitKind, NodeId, Pat, PatKind};
use rustc_ast::visit::FnKind;
use rustc_data_structures::fx::FxHashMap;
use rustc_lint::{EarlyContext, EarlyLintPass};
Expand Down Expand Up @@ -115,9 +115,11 @@ declare_clippy_lint! {
/// ### 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?
/// It is much less readable.
/// Suffix style should be consistent.
///
/// ### Example
/// ```rust
Expand All @@ -128,10 +130,32 @@ declare_clippy_lint! {
/// let y = 123832_i32;
/// ```
pub UNSEPARATED_LITERAL_SUFFIX,
pedantic,
restriction,
"literals whose suffix is not separated by an underscore"
}

declare_clippy_lint! {
/// ### 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
/// ```rust
/// // Bad
/// let y = 123832_i32;
///
/// // Good
/// let y = 123832i32;
/// ```
pub SEPARATED_LITERAL_SUFFIX,
restriction,
"literals whose suffix is separated by an underscore"
}

declare_clippy_lint! {
/// ### What it does
/// Warns if an integral constant literal starts with `0`.
Expand Down Expand Up @@ -260,6 +284,7 @@ declare_lint_pass!(MiscEarlyLints => [
DOUBLE_NEG,
MIXED_CASE_HEX_LITERALS,
UNSEPARATED_LITERAL_SUFFIX,
SEPARATED_LITERAL_SUFFIX,
ZERO_PREFIXED_LITERAL,
BUILTIN_TYPE_SHADOW,
REDUNDANT_PATTERN,
Expand Down Expand Up @@ -310,6 +335,10 @@ impl EarlyLintPass for MiscEarlyLints {
if in_external_macro(cx.sess, expr.span) {
return;
}

if let ExprKind::Lit(ref lit) = expr.kind {
MiscEarlyLints::check_lit(cx, lit);
}
double_neg::check(cx, expr);
}
}
Expand All @@ -332,7 +361,7 @@ impl MiscEarlyLints {
LitIntType::Unsigned(ty) => ty.name_str(),
LitIntType::Unsuffixed => "",
};
unseparated_literal_suffix::check(cx, lit, &lit_snip, suffix, "integer");
literal_suffix::check(cx, lit, &lit_snip, suffix, "integer");
if lit_snip.starts_with("0x") {
mixed_case_hex_literals::check(cx, lit, suffix, &lit_snip);
} else if lit_snip.starts_with("0b") || lit_snip.starts_with("0o") {
Expand All @@ -342,7 +371,7 @@ impl MiscEarlyLints {
}
} else if let LitKind::Float(_, LitFloatType::Suffixed(float_ty)) = lit.kind {
let suffix = float_ty.name_str();
unseparated_literal_suffix::check(cx, lit, &lit_snip, suffix, "float");
literal_suffix::check(cx, lit, &lit_snip, suffix, "float");
}
}
}
26 changes: 0 additions & 26 deletions clippy_lints/src/misc_early/unseparated_literal_suffix.rs

This file was deleted.

3 changes: 2 additions & 1 deletion tests/ui/literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

#![warn(clippy::mixed_case_hex_literals)]
#![warn(clippy::zero_prefixed_literal)]
#![allow(clippy::unseparated_literal_suffix)]
#![warn(clippy::unseparated_literal_suffix)]
#![warn(clippy::separated_literal_suffix)]
#![allow(dead_code)]

fn main() {
Expand Down
74 changes: 63 additions & 11 deletions tests/ui/literals.stderr
Original file line number Diff line number Diff line change
@@ -1,25 +1,65 @@
error: integer type suffix should not be separated by an underscore
--> $DIR/literals.rs:12:15
|
LL | let ok4 = 0xab_cd_i32;
| ^^^^^^^^^^^ help: remove the underscore: `0xab_cdi32`
|
= note: `-D clippy::separated-literal-suffix` implied by `-D warnings`

error: integer type suffix should not be separated by an underscore
--> $DIR/literals.rs:13:15
|
LL | let ok5 = 0xAB_CD_u32;
| ^^^^^^^^^^^ help: remove the underscore: `0xAB_CDu32`

error: integer type suffix should not be separated by an underscore
--> $DIR/literals.rs:14:15
|
LL | let ok5 = 0xAB_CD_isize;
| ^^^^^^^^^^^^^ help: remove the underscore: `0xAB_CDisize`

error: inconsistent casing in hexadecimal literal
--> $DIR/literals.rs:14:17
--> $DIR/literals.rs:15:17
|
LL | let fail1 = 0xabCD;
| ^^^^^^
|
= note: `-D clippy::mixed-case-hex-literals` implied by `-D warnings`

error: integer type suffix should not be separated by an underscore
--> $DIR/literals.rs:16:17
|
LL | let fail2 = 0xabCD_u32;
| ^^^^^^^^^^ help: remove the underscore: `0xabCDu32`

error: inconsistent casing in hexadecimal literal
--> $DIR/literals.rs:15:17
--> $DIR/literals.rs:16:17
|
LL | let fail2 = 0xabCD_u32;
| ^^^^^^^^^^

error: integer type suffix should not be separated by an underscore
--> $DIR/literals.rs:17:17
|
LL | let fail2 = 0xabCD_isize;
| ^^^^^^^^^^^^ help: remove the underscore: `0xabCDisize`

error: inconsistent casing in hexadecimal literal
--> $DIR/literals.rs:16:17
--> $DIR/literals.rs:17:17
|
LL | let fail2 = 0xabCD_isize;
| ^^^^^^^^^^^^

error: integer type suffix should be separated by an underscore
--> $DIR/literals.rs:18:27
|
LL | let fail_multi_zero = 000_123usize;
| ^^^^^^^^^^^^ help: add an underscore: `000_123_usize`
|
= note: `-D clippy::unseparated-literal-suffix` implied by `-D warnings`

error: this is a decimal constant
--> $DIR/literals.rs:17:27
--> $DIR/literals.rs:18:27
|
LL | let fail_multi_zero = 000_123usize;
| ^^^^^^^^^^^^
Expand All @@ -34,8 +74,14 @@ help: if you mean to use an octal constant, use `0o`
LL | let fail_multi_zero = 0o123usize;
| ~~~~~~~~~~

error: integer type suffix should not be separated by an underscore
--> $DIR/literals.rs:21:16
|
LL | let ok10 = 0_i64;
| ^^^^^ help: remove the underscore: `0i64`

error: this is a decimal constant
--> $DIR/literals.rs:21:17
--> $DIR/literals.rs:22:17
|
LL | let fail8 = 0123;
| ^^^^
Expand All @@ -49,39 +95,45 @@ help: if you mean to use an octal constant, use `0o`
LL | let fail8 = 0o123;
| ~~~~~

error: integer type suffix should not be separated by an underscore
--> $DIR/literals.rs:31:16
|
LL | let ok17 = 0x123_4567_8901_usize;
| ^^^^^^^^^^^^^^^^^^^^^ help: remove the underscore: `0x123_4567_8901usize`

error: digits grouped inconsistently by underscores
--> $DIR/literals.rs:33:18
--> $DIR/literals.rs:34:18
|
LL | let fail19 = 12_3456_21;
| ^^^^^^^^^^ help: consider: `12_345_621`
|
= note: `-D clippy::inconsistent-digit-grouping` implied by `-D warnings`

error: digits grouped inconsistently by underscores
--> $DIR/literals.rs:34:18
--> $DIR/literals.rs:35:18
|
LL | let fail22 = 3__4___23;
| ^^^^^^^^^ help: consider: `3_423`

error: digits grouped inconsistently by underscores
--> $DIR/literals.rs:35:18
--> $DIR/literals.rs:36:18
|
LL | let fail23 = 3__16___23;
| ^^^^^^^^^^ help: consider: `31_623`

error: digits of hex or binary literal not grouped by four
--> $DIR/literals.rs:37:18
--> $DIR/literals.rs:38:18
|
LL | let fail24 = 0xAB_ABC_AB;
| ^^^^^^^^^^^ help: consider: `0x0ABA_BCAB`
|
= note: `-D clippy::unusual-byte-groupings` implied by `-D warnings`

error: digits of hex or binary literal not grouped by four
--> $DIR/literals.rs:38:18
--> $DIR/literals.rs:39:18
|
LL | let fail25 = 0b01_100_101;
| ^^^^^^^^^^^^ help: consider: `0b0110_0101`

error: aborting due to 10 previous errors
error: aborting due to 18 previous errors

0 comments on commit 38d8025

Please sign in to comment.