@@ -91,6 +91,7 @@ mod or_fun_call;
91
91
mod or_then_unwrap;
92
92
mod path_buf_push_overwrite;
93
93
mod path_ends_with_ext;
94
+ mod ptr_offset_with_cast;
94
95
mod range_zip_with_len;
95
96
mod read_line_without_trim;
96
97
mod readonly_write_lock;
@@ -1725,6 +1726,43 @@ declare_clippy_lint! {
1725
1726
"Check for offset calculations on raw pointers to zero-sized types"
1726
1727
}
1727
1728
1729
+ declare_clippy_lint ! {
1730
+ /// ### What it does
1731
+ /// Checks for usage of the `offset` pointer method with a `usize` casted to an
1732
+ /// `isize`.
1733
+ ///
1734
+ /// ### Why is this bad?
1735
+ /// If we’re always increasing the pointer address, we can avoid the numeric
1736
+ /// cast by using the `add` method instead.
1737
+ ///
1738
+ /// ### Example
1739
+ /// ```no_run
1740
+ /// let vec = vec![b'a', b'b', b'c'];
1741
+ /// let ptr = vec.as_ptr();
1742
+ /// let offset = 1_usize;
1743
+ ///
1744
+ /// unsafe {
1745
+ /// ptr.offset(offset as isize);
1746
+ /// }
1747
+ /// ```
1748
+ ///
1749
+ /// Could be written:
1750
+ ///
1751
+ /// ```no_run
1752
+ /// let vec = vec![b'a', b'b', b'c'];
1753
+ /// let ptr = vec.as_ptr();
1754
+ /// let offset = 1_usize;
1755
+ ///
1756
+ /// unsafe {
1757
+ /// ptr.add(offset);
1758
+ /// }
1759
+ /// ```
1760
+ #[ clippy:: version = "1.30.0" ]
1761
+ pub PTR_OFFSET_WITH_CAST ,
1762
+ complexity,
1763
+ "unneeded pointer offset cast"
1764
+ }
1765
+
1728
1766
declare_clippy_lint ! {
1729
1767
/// ### What it does
1730
1768
/// Checks for `FileType::is_file()`.
@@ -4665,6 +4703,7 @@ impl_lint_pass!(Methods => [
4665
4703
UNINIT_ASSUMED_INIT ,
4666
4704
MANUAL_SATURATING_ARITHMETIC ,
4667
4705
ZST_OFFSET ,
4706
+ PTR_OFFSET_WITH_CAST ,
4668
4707
FILETYPE_IS_FILE ,
4669
4708
OPTION_AS_REF_DEREF ,
4670
4709
UNNECESSARY_LAZY_EVALUATIONS ,
@@ -4960,10 +4999,7 @@ impl Methods {
4960
4999
// Handle method calls whose receiver and arguments may not come from expansion
4961
5000
if let Some ( ( name, recv, args, span, call_span) ) = method_call ( expr) {
4962
5001
match ( name, args) {
4963
- (
4964
- sym:: add | sym:: offset | sym:: sub | sym:: wrapping_offset | sym:: wrapping_add | sym:: wrapping_sub,
4965
- [ _arg] ,
4966
- ) => {
5002
+ ( sym:: add | sym:: sub | sym:: wrapping_add | sym:: wrapping_sub, [ _arg] ) => {
4967
5003
zst_offset:: check ( cx, expr, recv) ;
4968
5004
} ,
4969
5005
( sym:: all, [ arg] ) => {
@@ -5334,6 +5370,11 @@ impl Methods {
5334
5370
} ,
5335
5371
_ => iter_nth_zero:: check ( cx, expr, recv, n_arg) ,
5336
5372
} ,
5373
+ ( sym:: offset | sym:: wrapping_offset, [ arg] ) => {
5374
+ zst_offset:: check ( cx, expr, recv) ;
5375
+
5376
+ ptr_offset_with_cast:: check ( cx, name, expr, recv, arg, self . msrv ) ;
5377
+ } ,
5337
5378
( sym:: ok_or_else, [ arg] ) => {
5338
5379
unnecessary_lazy_eval:: check ( cx, expr, recv, arg, "ok_or" ) ;
5339
5380
} ,
0 commit comments