@@ -91,6 +91,7 @@ mod or_fun_call;
9191mod or_then_unwrap;
9292mod path_buf_push_overwrite;
9393mod path_ends_with_ext;
94+ mod ptr_offset_with_cast;
9495mod range_zip_with_len;
9596mod read_line_without_trim;
9697mod readonly_write_lock;
@@ -1725,6 +1726,43 @@ declare_clippy_lint! {
17251726 "Check for offset calculations on raw pointers to zero-sized types"
17261727}
17271728
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+
17281766declare_clippy_lint ! {
17291767 /// ### What it does
17301768 /// Checks for `FileType::is_file()`.
@@ -4665,6 +4703,7 @@ impl_lint_pass!(Methods => [
46654703 UNINIT_ASSUMED_INIT ,
46664704 MANUAL_SATURATING_ARITHMETIC ,
46674705 ZST_OFFSET ,
4706+ PTR_OFFSET_WITH_CAST ,
46684707 FILETYPE_IS_FILE ,
46694708 OPTION_AS_REF_DEREF ,
46704709 UNNECESSARY_LAZY_EVALUATIONS ,
@@ -4960,12 +4999,14 @@ impl Methods {
49604999 // Handle method calls whose receiver and arguments may not come from expansion
49615000 if let Some ( ( name, recv, args, span, call_span) ) = method_call ( expr) {
49625001 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] ) => {
49675003 zst_offset:: check ( cx, expr, recv) ;
49685004 } ,
5005+ ( sym:: offset | sym:: wrapping_offset, [ arg] ) => {
5006+ zst_offset:: check ( cx, expr, recv) ;
5007+
5008+ ptr_offset_with_cast:: check ( cx, name, expr, recv, arg) ;
5009+ } ,
49695010 ( sym:: all, [ arg] ) => {
49705011 unused_enumerate_index:: check ( cx, expr, recv, arg) ;
49715012 needless_character_iteration:: check ( cx, expr, recv, arg, true ) ;
0 commit comments