Skip to content

Commit be8308f

Browse files
committed
make sure the msrv for const_ptr_deref is met when linting [missing_const_for_fn]
1 parent b2a7371 commit be8308f

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

clippy_config/src/msrvs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ msrv_aliases! {
2626
1,63,0 { ASSIGNING_CLONES }
2727
1,62,0 { BOOL_THEN_SOME, DEFAULT_ENUM_ATTRIBUTE }
2828
1,59,0 { THREAD_LOCAL_INITIALIZER_CAN_BE_MADE_CONST }
29-
1,58,0 { FORMAT_ARGS_CAPTURE, PATTERN_TRAIT_CHAR_ARRAY }
29+
1,58,0 { FORMAT_ARGS_CAPTURE, PATTERN_TRAIT_CHAR_ARRAY, CONST_RAW_PTR_DEREF }
3030
1,55,0 { SEEK_REWIND }
3131
1,54,0 { INTO_KEYS }
3232
1,53,0 { OR_PATTERNS, MANUAL_BITS, BTREE_MAP_RETAIN, BTREE_SET_RETAIN, ARRAY_INTO_ITERATOR }

clippy_lints/src/missing_const_for_fn.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
use std::ops::ControlFlow;
2+
13
use clippy_config::msrvs::{self, Msrv};
24
use clippy_utils::diagnostics::span_lint;
35
use clippy_utils::qualify_min_const_fn::is_min_const_fn;
46
use clippy_utils::ty::has_drop;
7+
use clippy_utils::visitors::for_each_expr;
58
use clippy_utils::{fn_has_unsatisfiable_preds, is_entrypoint_fn, is_from_proc_macro, trait_ref_of_method};
9+
use hir::UnOp;
610
use rustc_hir as hir;
711
use rustc_hir::def_id::CRATE_DEF_ID;
812
use rustc_hir::intravisit::FnKind;
9-
use rustc_hir::{Body, Constness, FnDecl, GenericParamKind};
13+
use rustc_hir::{Body, Constness, ExprKind, FnDecl, GenericParamKind};
1014
use rustc_lint::{LateContext, LateLintPass};
1115
use rustc_middle::lint::in_external_macro;
1216
use rustc_session::impl_lint_pass;
@@ -149,6 +153,11 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
149153
return;
150154
}
151155

156+
// Skip if there is any pointer deref ops in the body, and the msrv of that feature does not meet.
157+
if !self.msrv.meets(msrvs::CONST_RAW_PTR_DEREF) && has_raw_ptr_deref_in_body(cx, body) {
158+
return;
159+
}
160+
152161
let mir = cx.tcx.optimized_mir(def_id);
153162

154163
if let Err((span, err)) = is_min_const_fn(cx.tcx, mir, &self.msrv) {
@@ -176,3 +185,16 @@ fn method_accepts_droppable(cx: &LateContext<'_>, def_id: LocalDefId) -> bool {
176185
fn already_const(header: hir::FnHeader) -> bool {
177186
header.constness == Constness::Const
178187
}
188+
189+
fn has_raw_ptr_deref_in_body(cx: &LateContext<'_>, body: &Body<'_>) -> bool {
190+
for_each_expr(body.value, |expr| {
191+
if let ExprKind::Unary(UnOp::Deref, oprand) = expr.kind
192+
&& cx.typeck_results().expr_ty(oprand).is_unsafe_ptr()
193+
{
194+
ControlFlow::Break(())
195+
} else {
196+
ControlFlow::Continue(())
197+
}
198+
})
199+
.is_some()
200+
}

tests/ui/missing_const_for_fn/could_be_const.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,24 @@ impl const Drop for D {
113113
// Lint this, since it can be dropped in const contexts
114114
// FIXME(effects)
115115
fn d(this: D) {}
116+
117+
#[clippy::msrv = "1.58"]
118+
mod const_ptr_deref_supported {
119+
struct Foo(*const u8);
120+
impl Foo {
121+
fn can_be_const(self) -> usize {
122+
//~^ ERROR: this could be a `const fn`
123+
unsafe { *self.0 as usize }
124+
}
125+
}
126+
}
127+
128+
#[clippy::msrv = "1.57"]
129+
mod const_ptr_deref_not_supported {
130+
struct Foo(*const u8);
131+
impl Foo {
132+
fn cannot_be_const(self) -> usize {
133+
unsafe { *self.0 as usize }
134+
}
135+
}
136+
}

tests/ui/missing_const_for_fn/could_be_const.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,14 @@ LL | | 46
102102
LL | | }
103103
| |_^
104104

105-
error: aborting due to 11 previous errors
105+
error: this could be a `const fn`
106+
--> tests/ui/missing_const_for_fn/could_be_const.rs:121:9
107+
|
108+
LL | / fn can_be_const(self) -> usize {
109+
LL | |
110+
LL | | unsafe { *self.0 as usize }
111+
LL | | }
112+
| |_________^
113+
114+
error: aborting due to 12 previous errors
106115

0 commit comments

Comments
 (0)