Skip to content

Commit 53a1ff7

Browse files
committed
Check os_str_display MSRV instead of feature
This feature was stabilized, so the FormatArgs lints should check if the MSRV of the stabilization is met, rather than checking if the feature is enabled.
1 parent 02e812a commit 53a1ff7

File tree

6 files changed

+54
-34
lines changed

6 files changed

+54
-34
lines changed

clippy_lints/src/format_args.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_ast::{
1515
FormatArgPosition, FormatArgPositionKind, FormatArgsPiece, FormatArgumentKind, FormatCount, FormatOptions,
1616
FormatPlaceholder, FormatTrait,
1717
};
18+
use rustc_attr_parsing::RustcVersion;
1819
use rustc_data_structures::fx::FxHashMap;
1920
use rustc_errors::Applicability;
2021
use rustc_errors::SuggestionStyle::{CompletelyHidden, ShowCode};
@@ -206,17 +207,17 @@ pub struct FormatArgs<'tcx> {
206207
format_args: FormatArgsStorage,
207208
msrv: Msrv,
208209
ignore_mixed: bool,
209-
ty_feature_map: FxHashMap<Ty<'tcx>, Option<Symbol>>,
210+
ty_msrv_map: FxHashMap<Ty<'tcx>, Option<RustcVersion>>,
210211
}
211212

212213
impl<'tcx> FormatArgs<'tcx> {
213214
pub fn new(tcx: TyCtxt<'tcx>, conf: &'static Conf, format_args: FormatArgsStorage) -> Self {
214-
let ty_feature_map = make_ty_feature_map(tcx);
215+
let ty_msrv_map = make_ty_msrv_map(tcx);
215216
Self {
216217
format_args,
217218
msrv: conf.msrv.clone(),
218219
ignore_mixed: conf.allow_mixed_uninlined_format_args,
219-
ty_feature_map,
220+
ty_msrv_map,
220221
}
221222
}
222223
}
@@ -233,7 +234,8 @@ impl<'tcx> LateLintPass<'tcx> for FormatArgs<'tcx> {
233234
macro_call: &macro_call,
234235
format_args,
235236
ignore_mixed: self.ignore_mixed,
236-
ty_feature_map: &self.ty_feature_map,
237+
msrv: &self.msrv,
238+
ty_msrv_map: &self.ty_msrv_map,
237239
};
238240

239241
linter.check_templates();
@@ -253,7 +255,8 @@ struct FormatArgsExpr<'a, 'tcx> {
253255
macro_call: &'a MacroCall,
254256
format_args: &'a rustc_ast::FormatArgs,
255257
ignore_mixed: bool,
256-
ty_feature_map: &'a FxHashMap<Ty<'tcx>, Option<Symbol>>,
258+
msrv: &'a Msrv,
259+
ty_msrv_map: &'a FxHashMap<Ty<'tcx>, Option<RustcVersion>>,
257260
}
258261

259262
impl<'tcx> FormatArgsExpr<'_, 'tcx> {
@@ -538,19 +541,19 @@ impl<'tcx> FormatArgsExpr<'_, 'tcx> {
538541
fn can_display_format(&self, ty: Ty<'tcx>) -> bool {
539542
let ty = ty.peel_refs();
540543

541-
if let Some(feature) = self.ty_feature_map.get(&ty)
542-
&& feature.is_none_or(|feature| self.cx.tcx.features().enabled(feature))
544+
if let Some(msrv) = self.ty_msrv_map.get(&ty)
545+
&& msrv.is_none_or(|msrv| self.msrv.meets(msrv))
543546
{
544547
return true;
545548
}
546549

547-
// Even if `ty` is not in `self.ty_feature_map`, check whether `ty` implements `Deref` with
548-
// a `Target` that is in `self.ty_feature_map`.
550+
// Even if `ty` is not in `self.ty_msrv_map`, check whether `ty` implements `Deref` with
551+
// a `Target` that is in `self.ty_msrv_map`.
549552
if let Some(deref_trait_id) = self.cx.tcx.lang_items().deref_trait()
550553
&& implements_trait(self.cx, ty, deref_trait_id, &[])
551554
&& let Some(target_ty) = self.cx.get_associated_type(ty, deref_trait_id, "Target")
552-
&& let Some(feature) = self.ty_feature_map.get(&target_ty)
553-
&& feature.is_none_or(|feature| self.cx.tcx.features().enabled(feature))
555+
&& let Some(msrv) = self.ty_msrv_map.get(&target_ty)
556+
&& msrv.is_none_or(|msrv| self.msrv.meets(msrv))
554557
{
555558
return true;
556559
}
@@ -559,8 +562,8 @@ impl<'tcx> FormatArgsExpr<'_, 'tcx> {
559562
}
560563
}
561564

562-
fn make_ty_feature_map(tcx: TyCtxt<'_>) -> FxHashMap<Ty<'_>, Option<Symbol>> {
563-
[(sym::OsStr, Some(Symbol::intern("os_str_display"))), (sym::Path, None)]
565+
fn make_ty_msrv_map(tcx: TyCtxt<'_>) -> FxHashMap<Ty<'_>, Option<RustcVersion>> {
566+
[(sym::OsStr, Some(msrvs::OS_STR_DISPLAY)), (sym::Path, None)]
564567
.into_iter()
565568
.filter_map(|(name, feature)| {
566569
tcx.get_diagnostic_item(name).map(|def_id| {

clippy_utils/src/msrvs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ macro_rules! msrv_aliases {
1919

2020
// names may refer to stabilized feature flags or library items
2121
msrv_aliases! {
22+
1,87,0 { OS_STR_DISPLAY }
2223
1,84,0 { CONST_OPTION_AS_SLICE }
2324
1,83,0 { CONST_EXTERN_FN, CONST_FLOAT_BITS_CONV, CONST_FLOAT_CLASSIFY, CONST_MUT_REFS, CONST_UNWRAP }
2425
1,82,0 { IS_NONE_OR, REPEAT_N, RAW_REF_OP }

tests/ui/unnecessary_os_str_debug_formatting.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(os_str_display)]
21
#![warn(clippy::unnecessary_debug_formatting)]
32

43
use std::ffi::{OsStr, OsString};

tests/ui/unnecessary_os_str_debug_formatting.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unnecessary `Debug` formatting in `println!` args
2-
--> tests/ui/unnecessary_os_str_debug_formatting.rs:15:22
2+
--> tests/ui/unnecessary_os_str_debug_formatting.rs:14:22
33
|
44
LL | println!("{:?}", os_str);
55
| ^^^^^^
@@ -10,7 +10,7 @@ LL | println!("{:?}", os_str);
1010
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_debug_formatting)]`
1111

1212
error: unnecessary `Debug` formatting in `println!` args
13-
--> tests/ui/unnecessary_os_str_debug_formatting.rs:16:22
13+
--> tests/ui/unnecessary_os_str_debug_formatting.rs:15:22
1414
|
1515
LL | println!("{:?}", os_string);
1616
| ^^^^^^^^^
@@ -19,7 +19,7 @@ LL | println!("{:?}", os_string);
1919
= note: switching to `Display` formatting will change how the value is shown; escaped characters will no longer be escaped and surrounding quotes will be removed
2020

2121
error: unnecessary `Debug` formatting in `println!` args
22-
--> tests/ui/unnecessary_os_str_debug_formatting.rs:18:16
22+
--> tests/ui/unnecessary_os_str_debug_formatting.rs:17:16
2323
|
2424
LL | println!("{os_str:?}");
2525
| ^^^^^^
@@ -28,7 +28,7 @@ LL | println!("{os_str:?}");
2828
= note: switching to `Display` formatting will change how the value is shown; escaped characters will no longer be escaped and surrounding quotes will be removed
2929

3030
error: unnecessary `Debug` formatting in `println!` args
31-
--> tests/ui/unnecessary_os_str_debug_formatting.rs:19:16
31+
--> tests/ui/unnecessary_os_str_debug_formatting.rs:18:16
3232
|
3333
LL | println!("{os_string:?}");
3434
| ^^^^^^^^^
@@ -37,7 +37,7 @@ LL | println!("{os_string:?}");
3737
= note: switching to `Display` formatting will change how the value is shown; escaped characters will no longer be escaped and surrounding quotes will be removed
3838

3939
error: unnecessary `Debug` formatting in `format!` args
40-
--> tests/ui/unnecessary_os_str_debug_formatting.rs:21:37
40+
--> tests/ui/unnecessary_os_str_debug_formatting.rs:20:37
4141
|
4242
LL | let _: String = format!("{:?}", os_str);
4343
| ^^^^^^
@@ -46,7 +46,7 @@ LL | let _: String = format!("{:?}", os_str);
4646
= note: switching to `Display` formatting will change how the value is shown; escaped characters will no longer be escaped and surrounding quotes will be removed
4747

4848
error: unnecessary `Debug` formatting in `format!` args
49-
--> tests/ui/unnecessary_os_str_debug_formatting.rs:22:37
49+
--> tests/ui/unnecessary_os_str_debug_formatting.rs:21:37
5050
|
5151
LL | let _: String = format!("{:?}", os_string);
5252
| ^^^^^^^^^

tests/ui/unnecessary_path_debug_formatting.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ fn main() {
2525
println!("{}", path.display());
2626
println!("{}", path_buf.display());
2727

28-
// should not fire because feature `os_str_display` is not enabled
29-
println!("{:?}", os_str);
30-
println!("{:?}", os_string);
31-
3228
// positive tests
29+
println!("{:?}", os_str); //~ unnecessary_debug_formatting
30+
println!("{:?}", os_string); //~ unnecessary_debug_formatting
31+
3332
println!("{:?}", path); //~ unnecessary_debug_formatting
3433
println!("{:?}", path_buf); //~ unnecessary_debug_formatting
3534

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
error: unnecessary `Debug` formatting in `println!` args
2-
--> tests/ui/unnecessary_path_debug_formatting.rs:33:22
2+
--> tests/ui/unnecessary_path_debug_formatting.rs:29:22
3+
|
4+
LL | println!("{:?}", os_str);
5+
| ^^^^^^
6+
|
7+
= help: use `Display` formatting and change this to `os_str.display()`
8+
= note: switching to `Display` formatting will change how the value is shown; escaped characters will no longer be escaped and surrounding quotes will be removed
9+
= note: `-D clippy::unnecessary-debug-formatting` implied by `-D warnings`
10+
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_debug_formatting)]`
11+
12+
error: unnecessary `Debug` formatting in `println!` args
13+
--> tests/ui/unnecessary_path_debug_formatting.rs:30:22
14+
|
15+
LL | println!("{:?}", os_string);
16+
| ^^^^^^^^^
17+
|
18+
= help: use `Display` formatting and change this to `os_string.display()`
19+
= note: switching to `Display` formatting will change how the value is shown; escaped characters will no longer be escaped and surrounding quotes will be removed
20+
21+
error: unnecessary `Debug` formatting in `println!` args
22+
--> tests/ui/unnecessary_path_debug_formatting.rs:32:22
323
|
424
LL | println!("{:?}", path);
525
| ^^^^
626
|
727
= help: use `Display` formatting and change this to `path.display()`
828
= note: switching to `Display` formatting will change how the value is shown; escaped characters will no longer be escaped and surrounding quotes will be removed
9-
= note: `-D clippy::unnecessary-debug-formatting` implied by `-D warnings`
10-
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_debug_formatting)]`
1129

1230
error: unnecessary `Debug` formatting in `println!` args
13-
--> tests/ui/unnecessary_path_debug_formatting.rs:34:22
31+
--> tests/ui/unnecessary_path_debug_formatting.rs:33:22
1432
|
1533
LL | println!("{:?}", path_buf);
1634
| ^^^^^^^^
@@ -19,7 +37,7 @@ LL | println!("{:?}", path_buf);
1937
= note: switching to `Display` formatting will change how the value is shown; escaped characters will no longer be escaped and surrounding quotes will be removed
2038

2139
error: unnecessary `Debug` formatting in `println!` args
22-
--> tests/ui/unnecessary_path_debug_formatting.rs:36:16
40+
--> tests/ui/unnecessary_path_debug_formatting.rs:35:16
2341
|
2442
LL | println!("{path:?}");
2543
| ^^^^
@@ -28,7 +46,7 @@ LL | println!("{path:?}");
2846
= note: switching to `Display` formatting will change how the value is shown; escaped characters will no longer be escaped and surrounding quotes will be removed
2947

3048
error: unnecessary `Debug` formatting in `println!` args
31-
--> tests/ui/unnecessary_path_debug_formatting.rs:37:16
49+
--> tests/ui/unnecessary_path_debug_formatting.rs:36:16
3250
|
3351
LL | println!("{path_buf:?}");
3452
| ^^^^^^^^
@@ -37,7 +55,7 @@ LL | println!("{path_buf:?}");
3755
= note: switching to `Display` formatting will change how the value is shown; escaped characters will no longer be escaped and surrounding quotes will be removed
3856

3957
error: unnecessary `Debug` formatting in `format!` args
40-
--> tests/ui/unnecessary_path_debug_formatting.rs:39:37
58+
--> tests/ui/unnecessary_path_debug_formatting.rs:38:37
4159
|
4260
LL | let _: String = format!("{:?}", path);
4361
| ^^^^
@@ -46,7 +64,7 @@ LL | let _: String = format!("{:?}", path);
4664
= note: switching to `Display` formatting will change how the value is shown; escaped characters will no longer be escaped and surrounding quotes will be removed
4765

4866
error: unnecessary `Debug` formatting in `format!` args
49-
--> tests/ui/unnecessary_path_debug_formatting.rs:40:37
67+
--> tests/ui/unnecessary_path_debug_formatting.rs:39:37
5068
|
5169
LL | let _: String = format!("{:?}", path_buf);
5270
| ^^^^^^^^
@@ -55,13 +73,13 @@ LL | let _: String = format!("{:?}", path_buf);
5573
= note: switching to `Display` formatting will change how the value is shown; escaped characters will no longer be escaped and surrounding quotes will be removed
5674

5775
error: unnecessary `Debug` formatting in `println!` args
58-
--> tests/ui/unnecessary_path_debug_formatting.rs:43:22
76+
--> tests/ui/unnecessary_path_debug_formatting.rs:42:22
5977
|
6078
LL | println!("{:?}", &*deref_path);
6179
| ^^^^^^^^^^^^
6280
|
6381
= help: use `Display` formatting and change this to `&*deref_path.display()`
6482
= note: switching to `Display` formatting will change how the value is shown; escaped characters will no longer be escaped and surrounding quotes will be removed
6583

66-
error: aborting due to 7 previous errors
84+
error: aborting due to 9 previous errors
6785

0 commit comments

Comments
 (0)