@@ -63,30 +63,54 @@ declare_clippy_lint! {
63
63
/// arguments but are not marked `unsafe`.
64
64
///
65
65
/// ### Why is this bad?
66
- /// The function should probably be marked `unsafe`, since
66
+ /// The function should almost definitely be marked `unsafe`, since
67
67
/// for an arbitrary raw pointer, there is no way of telling for sure if it is
68
68
/// valid.
69
69
///
70
+ /// In general, this lint should **never be disabled** unless it is definitely a
71
+ /// false positive (please submit an issue if so) since it breaks Rust's
72
+ /// soundness guarantees, directly exposing API users to potentially dangerous
73
+ /// program behavior. This is also true for internal APIs, as it is easy to leak
74
+ /// unsoundness.
75
+ ///
76
+ /// ### Context
77
+ /// In general, an `unsafe {...}` block is used to indicate that the code in that
78
+ /// section has been verified in some way that the compiler can not. For a
79
+ /// function that accepts a raw pointer then accesses the pointer's data, this is
80
+ /// generally impossible as the incoming pointer could point anywhere, valid or
81
+ /// not. So, the signature should be marked `unsafe fn`: this indicates that the
82
+ /// function's caller must provide some verification that the arguments it sends
83
+ /// are valid (and then call the function within an `unsafe` block).
84
+ ///
70
85
/// ### Known problems
71
86
/// * It does not check functions recursively so if the pointer is passed to a
72
87
/// private non-`unsafe` function which does the dereferencing, the lint won't
73
- /// trigger.
88
+ /// trigger (false negative) .
74
89
/// * It only checks for arguments whose type are raw pointers, not raw pointers
75
90
/// got from an argument in some other way (`fn foo(bar: &[*const u8])` or
76
- /// `some_argument.get_raw_ptr()`).
91
+ /// `some_argument.get_raw_ptr()`) (false negative) .
77
92
///
78
93
/// ### Example
79
94
/// ```rust,ignore
80
95
/// pub fn foo(x: *const u8) {
81
96
/// println!("{}", unsafe { *x });
82
97
/// }
98
+ ///
99
+ /// // this call "looks" safe but will segfault or worse!
100
+ /// // foo(invalid_ptr);
83
101
/// ```
84
102
///
85
103
/// Use instead:
86
104
/// ```rust,ignore
87
105
/// pub unsafe fn foo(x: *const u8) {
88
106
/// println!("{}", unsafe { *x });
89
107
/// }
108
+ ///
109
+ /// // this call would cause a compiler error
110
+ /// // foo(invalid_ptr);
111
+ ///
112
+ /// // sound call if the caller knows the pointer is valid
113
+ /// unsafe { foo(valid_ptr); }
90
114
/// ```
91
115
#[ clippy:: version = "pre 1.29.0" ]
92
116
pub NOT_UNSAFE_PTR_ARG_DEREF ,
0 commit comments