Skip to content

Commit d54fc50

Browse files
committed
Implement has_string_formatting
1 parent 032ae17 commit d54fc50

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
@@ -3,7 +3,6 @@
33
#![deny(clippy::missing_docs_in_private_items)]
44

55
use crate::{is_expn_of, last_path_segment, match_def_path, paths};
6-
use bitflags::bitflags;
76
use if_chain::if_chain;
87
use rustc_ast::ast::{self, LitKind};
98
use rustc_hir as hir;
@@ -571,9 +570,10 @@ impl FormatArgsExpn<'tcx> {
571570
}
572571
}
573572

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

579579
/// Returns an iterator over `FormatArgsArg`.
@@ -657,16 +657,6 @@ pub struct FormatArgsArg<'tcx> {
657657
fmt: Option<&'tcx Expr<'tcx>>,
658658
}
659659

660-
bitflags! {
661-
pub struct Formatting: u32 {
662-
const FILL = 0b0000_0001;
663-
const ALIGN = 0b0000_0010;
664-
const FLAGS = 0b0000_0100;
665-
const PRECISION = 0b0000_1000;
666-
const WIDTH = 0b0001_0000;
667-
}
668-
}
669-
670660
impl<'tcx> FormatArgsArg<'tcx> {
671661
/// An element of `value_args` according to `position`
672662
pub fn value(&self) -> &'tcx Expr<'tcx> {
@@ -683,12 +673,10 @@ impl<'tcx> FormatArgsArg<'tcx> {
683673
self.fmt
684674
}
685675

686-
/// Returns true if any formatting parameters are used like `{:+2}` instead of just `{}`. Note
687-
/// that the check is performed using the logical OR of the flags. So, for example,
688-
/// `has_formatting(Formatting:all())` checks whether any (not all) formatting types are
689-
/// used.
676+
/// Returns true if any formatting parameters are used that would have an effect on strings,
677+
/// like `{:+2}` instead of just `{}`.
690678
#[allow(clippy::nonminimal_bool)]
691-
pub fn has_formatting(&self, formatting: Formatting) -> bool {
679+
pub fn has_string_formatting(&self) -> bool {
692680
self.fmt().map_or(false, |fmt| {
693681
// `!` because these conditions check that `self` is unformatted.
694682
!if_chain! {
@@ -708,27 +696,11 @@ impl<'tcx> FormatArgsArg<'tcx> {
708696
let _ = assert_eq!(precision_field.ident.name, sym::precision);
709697
let _ = assert_eq!(width_field.ident.name, sym::width);
710698

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

729702
if let ExprKind::Path(ref width_qpath) = width_field.expr.kind;
730-
if !formatting.contains(Formatting::WIDTH)
731-
|| last_path_segment(width_qpath).ident.name == sym::Implied;
703+
if last_path_segment(width_qpath).ident.name == sym::Implied;
732704

733705
then { true } else { false }
734706
}

0 commit comments

Comments
 (0)