Skip to content

Commit 2220806

Browse files
authored
Emit missing_const_for_fn for CONST_MUT_REFS (#13839)
1.83 stabilized `CONST_MUT_REFS`, also allowing for `const fn` to mutate through mutable references. This ensures `missing_const_for_fn` is emitted for those cases. changelog: [`missing_const_for_fn`]: Now suggests marking some functions taking mutable references `const`
2 parents 38828bf + f327640 commit 2220806

File tree

6 files changed

+36
-5
lines changed

6 files changed

+36
-5
lines changed

clippy_utils/src/msrvs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ macro_rules! msrv_aliases {
1818

1919
// names may refer to stabilized feature flags or library items
2020
msrv_aliases! {
21-
1,83,0 { CONST_EXTERN_FN, CONST_FLOAT_BITS_CONV, CONST_FLOAT_CLASSIFY, CONST_UNWRAP }
21+
1,83,0 { CONST_EXTERN_FN, CONST_FLOAT_BITS_CONV, CONST_FLOAT_CLASSIFY, CONST_MUT_REFS, CONST_UNWRAP }
2222
1,82,0 { IS_NONE_OR, REPEAT_N, RAW_REF_OP }
2323
1,81,0 { LINT_REASONS_STABILIZATION, ERROR_IN_CORE, EXPLICIT_SELF_TYPE_ELISION }
2424
1,80,0 { BOX_INTO_ITER }

clippy_utils/src/qualify_min_const_fn.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ pub fn is_min_const_fn<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, msrv: &Msrv)
2929
let def_id = body.source.def_id();
3030

3131
for local in &body.local_decls {
32-
check_ty(tcx, local.ty, local.source_info.span)?;
32+
check_ty(tcx, local.ty, local.source_info.span, msrv)?;
3333
}
3434
// impl trait is gone in MIR, so check the return type manually
3535
check_ty(
3636
tcx,
3737
tcx.fn_sig(def_id).instantiate_identity().output().skip_binder(),
3838
body.local_decls.iter().next().unwrap().source_info.span,
39+
msrv,
3940
)?;
4041

4142
for bb in &*body.basic_blocks {
@@ -51,7 +52,7 @@ pub fn is_min_const_fn<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, msrv: &Msrv)
5152
Ok(())
5253
}
5354

54-
fn check_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span) -> McfResult {
55+
fn check_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span, msrv: &Msrv) -> McfResult {
5556
for arg in ty.walk() {
5657
let ty = match arg.unpack() {
5758
GenericArgKind::Type(ty) => ty,
@@ -62,7 +63,7 @@ fn check_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span) -> McfResult {
6263
};
6364

6465
match ty.kind() {
65-
ty::Ref(_, _, hir::Mutability::Mut) => {
66+
ty::Ref(_, _, hir::Mutability::Mut) if !msrv.meets(msrvs::CONST_MUT_REFS) => {
6667
return Err((span, "mutable references in const fn are unstable".into()));
6768
},
6869
ty::Alias(ty::Opaque, ..) => return Err((span, "`impl Trait` in const fn is unstable".into())),

tests/ui/missing_const_for_fn/cant_be_const.rs

+6
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,9 @@ mod with_ty_alias {
220220
let _: Foo = 1;
221221
}
222222
}
223+
224+
// Do not lint because mutable references in const functions are unstable in 1.82
225+
#[clippy::msrv = "1.82"]
226+
fn mut_add(x: &mut i32) {
227+
*x += 1;
228+
}

tests/ui/missing_const_for_fn/could_be_const.fixed

+5
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,8 @@ mod extern_fn {
212212
const extern "system-unwind" fn system_unwind() {}
213213
//~^ ERROR: this could be a `const fn`
214214
}
215+
216+
const fn mut_add(x: &mut i32) {
217+
//~^ ERROR: this could be a `const fn`
218+
*x += 1;
219+
}

tests/ui/missing_const_for_fn/could_be_const.rs

+5
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,8 @@ mod extern_fn {
212212
extern "system-unwind" fn system_unwind() {}
213213
//~^ ERROR: this could be a `const fn`
214214
}
215+
216+
fn mut_add(x: &mut i32) {
217+
//~^ ERROR: this could be a `const fn`
218+
*x += 1;
219+
}

tests/ui/missing_const_for_fn/could_be_const.stderr

+15-1
Original file line numberDiff line numberDiff line change
@@ -316,5 +316,19 @@ help: make the function `const`
316316
LL | const extern "system-unwind" fn system_unwind() {}
317317
| +++++
318318

319-
error: aborting due to 24 previous errors
319+
error: this could be a `const fn`
320+
--> tests/ui/missing_const_for_fn/could_be_const.rs:216:1
321+
|
322+
LL | / fn mut_add(x: &mut i32) {
323+
LL | |
324+
LL | | *x += 1;
325+
LL | | }
326+
| |_^
327+
|
328+
help: make the function `const`
329+
|
330+
LL | const fn mut_add(x: &mut i32) {
331+
| +++++
332+
333+
error: aborting due to 25 previous errors
320334

0 commit comments

Comments
 (0)