Skip to content

Commit 2f5583b

Browse files
committed
Implement has_string_formatting
1 parent 8d83e25 commit 2f5583b

File tree

4 files changed

+13
-42
lines changed

4 files changed

+13
-42
lines changed

clippy_lints/src/format.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::higher::{FormatExpn, Formatting};
2+
use clippy_utils::higher::FormatExpn;
33
use clippy_utils::source::{snippet_opt, snippet_with_applicability};
44
use clippy_utils::sugg::Sugg;
55
use if_chain::if_chain;
@@ -69,7 +69,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessFormat {
6969
_ => false,
7070
};
7171
if format_args.args().all(|arg| arg.is_display());
72-
if !format_args.has_formatting(Formatting::PRECISION | Formatting::WIDTH);
72+
if !format_args.has_string_formatting();
7373
then {
7474
let is_new_string = match value.kind {
7575
ExprKind::Binary(..) => true,

clippy_lints/src/format_args.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
2-
use clippy_utils::higher::{FormatArgsArg, FormatArgsExpn, FormatExpn, Formatting};
2+
use clippy_utils::higher::{FormatArgsArg, FormatArgsExpn, FormatExpn};
33
use clippy_utils::source::snippet_opt;
44
use clippy_utils::ty::implements_trait;
55
use clippy_utils::{get_trait_def_id, match_def_path, paths};
@@ -85,7 +85,7 @@ where
8585
if call_site.from_expansion();
8686
let expn_data = call_site.ctxt().outer_expn_data();
8787
if let ExpnKind::Macro(_, name) = expn_data.kind;
88-
if !format_args.has_formatting(Formatting::all());
88+
if !format_args.has_string_formatting();
8989
then {
9090
for (i, arg) in format_args.args().enumerate() {
9191
if !arg.is_display() {

clippy_utils/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ edition = "2021"
55
publish = false
66

77
[dependencies]
8-
bitflags = "1.2"
98
if_chain = "1.0"
109
rustc-semver = "1.1"
1110

clippy_utils/src/higher.rs

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use crate::ty::is_type_diagnostic_item;
66
use crate::{is_expn_of, last_path_segment, match_def_path, paths};
7-
use bitflags::bitflags;
87
use if_chain::if_chain;
98
use rustc_ast::ast::{self, LitKind};
109
use rustc_hir as hir;
@@ -574,9 +573,10 @@ impl FormatArgsExpn<'tcx> {
574573
}
575574
}
576575

577-
/// Returns true if any argument has the given formatting types.
578-
pub fn has_formatting(&self, formatting: Formatting) -> bool {
579-
self.args().any(|arg| arg.has_formatting(formatting))
576+
/// Returns true if any argument uses formatting parameters that would have an effect on
577+
/// strings.
578+
pub fn has_string_formatting(&self) -> bool {
579+
self.args().any(|arg| arg.has_string_formatting())
580580
}
581581

582582
/// Returns an iterator over `FormatArgsArg`.
@@ -660,16 +660,6 @@ pub struct FormatArgsArg<'tcx> {
660660
fmt: Option<&'tcx Expr<'tcx>>,
661661
}
662662

663-
bitflags! {
664-
pub struct Formatting: u32 {
665-
const FILL = 0b0000_0001;
666-
const ALIGN = 0b0000_0010;
667-
const FLAGS = 0b0000_0100;
668-
const PRECISION = 0b0000_1000;
669-
const WIDTH = 0b0001_0000;
670-
}
671-
}
672-
673663
impl<'tcx> FormatArgsArg<'tcx> {
674664
/// An element of `value_args` according to `position`
675665
pub fn value(&self) -> &'tcx Expr<'tcx> {
@@ -686,12 +676,10 @@ impl<'tcx> FormatArgsArg<'tcx> {
686676
self.fmt
687677
}
688678

689-
/// Returns true if any formatting parameters are used like `{:+2}` instead of just `{}`. Note
690-
/// that the check is performed using the logical OR of the flags. So, for example,
691-
/// `has_formatting(Formatting:all())` checks whether any (not all) formatting types are
692-
/// used.
679+
/// Returns true if any formatting parameters are used that would have an effect on strings,
680+
/// like `{:+2}` instead of just `{}`.
693681
#[allow(clippy::nonminimal_bool)]
694-
pub fn has_formatting(&self, formatting: Formatting) -> bool {
682+
pub fn has_string_formatting(&self) -> bool {
695683
self.fmt().map_or(false, |fmt| {
696684
// `!` because these conditions check that `self` is unformatted.
697685
!if_chain! {
@@ -711,27 +699,11 @@ impl<'tcx> FormatArgsArg<'tcx> {
711699
let _ = assert_eq!(precision_field.ident.name, sym::precision);
712700
let _ = assert_eq!(width_field.ident.name, sym::width);
713701

714-
if let ExprKind::Lit(lit) = &fill_field.expr.kind;
715-
if let LitKind::Char(char) = lit.node;
716-
if !formatting.contains(Formatting::FILL)
717-
|| char == ' ';
718-
719-
if let ExprKind::Path(ref align_path) = align_field.expr.kind;
720-
if !formatting.contains(Formatting::ALIGN)
721-
|| last_path_segment(align_path).ident.name == sym::Unknown;
722-
723-
if let ExprKind::Lit(lit) = &flags_field.expr.kind;
724-
if let LitKind::Int(u128, _) = lit.node;
725-
if !formatting.contains(Formatting::FLAGS)
726-
|| u128 == 0;
727-
728702
if let ExprKind::Path(ref precision_path) = precision_field.expr.kind;
729-
if !formatting.contains(Formatting::PRECISION)
730-
|| last_path_segment(precision_path).ident.name == sym::Implied;
703+
if last_path_segment(precision_path).ident.name == sym::Implied;
731704

732705
if let ExprKind::Path(ref width_qpath) = width_field.expr.kind;
733-
if !formatting.contains(Formatting::WIDTH)
734-
|| last_path_segment(width_qpath).ident.name == sym::Implied;
706+
if last_path_segment(width_qpath).ident.name == sym::Implied;
735707

736708
then { true } else { false }
737709
}

0 commit comments

Comments
 (0)