Skip to content

Commit eda3e64

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 d2f67a8 commit eda3e64

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

clippy_lints/src/format_args.rs

+16-13
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

+1
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 }

0 commit comments

Comments
 (0)