Skip to content

Commit b57d98b

Browse files
authored
Do not remove required parentheses in borrow_as_ptr suggestion (#13884)
Also, simplify boolean shortcut expression, and ensure that applicability is properly applied, as it was ignored and `MachineApplicable` was always used. changelog: [`borrow_as_ptr`]: do not remove required parentheses in autofix suggestion Close #13882
2 parents f5f1abd + eef47fc commit b57d98b

File tree

5 files changed

+47
-19
lines changed

5 files changed

+47
-19
lines changed

clippy_lints/src/casts/borrow_as_ptr.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::msrvs::Msrv;
3-
use clippy_utils::source::snippet_with_context;
3+
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
4+
use clippy_utils::sugg::has_enclosing_paren;
45
use clippy_utils::{is_lint_allowed, msrvs, std_or_core};
56
use rustc_errors::Applicability;
67
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, Ty, TyKind};
78
use rustc_lint::LateContext;
89
use rustc_middle::ty::adjustment::Adjust;
10+
use rustc_span::BytePos;
911

1012
use super::BORROW_AS_PTR;
1113

@@ -32,12 +34,21 @@ pub(super) fn check<'tcx>(
3234
return false;
3335
}
3436

35-
let suggestion = if msrv.meets(msrvs::RAW_REF_OP) {
37+
let (suggestion, span) = if msrv.meets(msrvs::RAW_REF_OP) {
3638
let operator_kind = match mutability {
3739
Mutability::Not => "const",
3840
Mutability::Mut => "mut",
3941
};
40-
format!("&raw {operator_kind} {snip}")
42+
// Make sure that the span to be replaced doesn't include parentheses, that could break the
43+
// suggestion.
44+
let span = if has_enclosing_paren(snippet_with_applicability(cx, expr.span, "", &mut app)) {
45+
expr.span
46+
.with_lo(expr.span.lo() + BytePos(1))
47+
.with_hi(expr.span.hi() - BytePos(1))
48+
} else {
49+
expr.span
50+
};
51+
(format!("&raw {operator_kind} {snip}"), span)
4152
} else {
4253
let Some(std_or_core) = std_or_core(cx) else {
4354
return false;
@@ -46,18 +57,10 @@ pub(super) fn check<'tcx>(
4657
Mutability::Not => "addr_of",
4758
Mutability::Mut => "addr_of_mut",
4859
};
49-
format!("{std_or_core}::ptr::{macro_name}!({snip})")
60+
(format!("{std_or_core}::ptr::{macro_name}!({snip})"), expr.span)
5061
};
5162

52-
span_lint_and_sugg(
53-
cx,
54-
BORROW_AS_PTR,
55-
expr.span,
56-
"borrow as raw pointer",
57-
"try",
58-
suggestion,
59-
Applicability::MachineApplicable,
60-
);
63+
span_lint_and_sugg(cx, BORROW_AS_PTR, span, "borrow as raw pointer", "try", suggestion, app);
6164
return true;
6265
}
6366
false

clippy_lints/src/casts/mod.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -836,11 +836,8 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
836836
as_underscore::check(cx, expr, cast_to_hir);
837837
as_pointer_underscore::check(cx, cast_to, cast_to_hir);
838838

839-
let was_borrow_as_ptr_emitted = if self.msrv.meets(msrvs::BORROW_AS_PTR) {
840-
borrow_as_ptr::check(cx, expr, cast_from_expr, cast_to_hir, &self.msrv)
841-
} else {
842-
false
843-
};
839+
let was_borrow_as_ptr_emitted = self.msrv.meets(msrvs::BORROW_AS_PTR)
840+
&& borrow_as_ptr::check(cx, expr, cast_from_expr, cast_to_hir, &self.msrv);
844841
if self.msrv.meets(msrvs::PTR_FROM_REF) && !was_borrow_as_ptr_emitted {
845842
ref_as_ptr::check(cx, expr, cast_from_expr, cast_to_hir);
846843
}

tests/ui/borrow_as_ptr.fixed

+8
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@ fn main() {
1616

1717
let mut val_mut = 1;
1818
let _p_mut = std::ptr::addr_of_mut!(val_mut);
19+
20+
let mut x: [i32; 2] = [42, 43];
21+
let _raw = std::ptr::addr_of_mut!(x[1]).wrapping_offset(-1);
22+
}
23+
24+
fn issue_13882() {
25+
let mut x: [i32; 2] = [42, 43];
26+
let _raw = (&raw mut x[1]).wrapping_offset(-1);
1927
}

tests/ui/borrow_as_ptr.rs

+8
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@ fn main() {
1616

1717
let mut val_mut = 1;
1818
let _p_mut = &mut val_mut as *mut i32;
19+
20+
let mut x: [i32; 2] = [42, 43];
21+
let _raw = (&mut x[1] as *mut i32).wrapping_offset(-1);
22+
}
23+
24+
fn issue_13882() {
25+
let mut x: [i32; 2] = [42, 43];
26+
let _raw = (&mut x[1] as *mut i32).wrapping_offset(-1);
1927
}

tests/ui/borrow_as_ptr.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,17 @@ error: borrow as raw pointer
1313
LL | let _p_mut = &mut val_mut as *mut i32;
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::addr_of_mut!(val_mut)`
1515

16-
error: aborting due to 2 previous errors
16+
error: borrow as raw pointer
17+
--> tests/ui/borrow_as_ptr.rs:21:16
18+
|
19+
LL | let _raw = (&mut x[1] as *mut i32).wrapping_offset(-1);
20+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::addr_of_mut!(x[1])`
21+
22+
error: borrow as raw pointer
23+
--> tests/ui/borrow_as_ptr.rs:26:17
24+
|
25+
LL | let _raw = (&mut x[1] as *mut i32).wrapping_offset(-1);
26+
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `&raw mut x[1]`
27+
28+
error: aborting due to 4 previous errors
1729

0 commit comments

Comments
 (0)