Skip to content

Commit d6aef18

Browse files
authored
more pedantic and nursery clippy (#222)
* more pedantic and nursery clippy * mark methods as must_use or ignore if has side effect * format code
1 parent 52b4e2a commit d6aef18

File tree

14 files changed

+69
-42
lines changed

14 files changed

+69
-42
lines changed

examples/data_type.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static MY_REDIS_TYPE: RedisType = RedisType::new(
3737
);
3838

3939
unsafe extern "C" fn free(value: *mut c_void) {
40-
Box::from_raw(value as *mut MyType);
40+
Box::from_raw(value.cast::<MyType>());
4141
}
4242

4343
fn alloc_set(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
@@ -49,19 +49,15 @@ fn alloc_set(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
4949

5050
let key = ctx.open_key_writable(&key);
5151

52-
match key.get_value::<MyType>(&MY_REDIS_TYPE)? {
53-
Some(value) => {
54-
value.data = "B".repeat(size as usize);
55-
}
56-
None => {
57-
let value = MyType {
58-
data: "A".repeat(size as usize),
59-
};
60-
61-
key.set_value(&MY_REDIS_TYPE, value)?;
62-
}
63-
}
52+
if let Some(value) = key.get_value::<MyType>(&MY_REDIS_TYPE)? {
53+
value.data = "B".repeat(size as usize);
54+
} else {
55+
let value = MyType {
56+
data: "A".repeat(size as usize),
57+
};
6458

59+
key.set_value(&MY_REDIS_TYPE, value)?;
60+
}
6561
Ok(size.into())
6662
}
6763

examples/hello.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn hello_mul(_: &Context, args: Vec<RedisString>) -> RedisResult {
2525
}
2626

2727
fn hello_err(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
28-
if args.len() < 1 {
28+
if args.is_empty() {
2929
return Err(RedisError::WrongArity);
3030
}
3131

src/context/blocked.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ impl Drop for BlockedClient {
1717
}
1818

1919
impl Context {
20+
#[must_use]
2021
pub fn block_client(&self) -> BlockedClient {
2122
let blocked_client = unsafe {
2223
raw::RedisModule_BlockClient.unwrap()(

src/context/info.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ impl ServerInfo {
2929
}
3030

3131
impl Context {
32+
#[must_use]
3233
pub fn server_info(&self, section: &str) -> ServerInfo {
3334
let section = CString::new(section).unwrap();
3435
let server_info = unsafe {

src/context/mod.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ use crate::{RedisError, RedisResult, RedisString, RedisValue};
1212
mod timer;
1313

1414
#[cfg(feature = "experimental-api")]
15-
pub(crate) mod thread_safe;
15+
pub mod thread_safe;
1616

1717
#[cfg(feature = "experimental-api")]
18-
pub(crate) mod blocked;
18+
pub mod blocked;
1919

20-
pub(crate) mod info;
20+
pub mod info;
2121

2222
/// `Context` is a structure that's designed to give us a high-level interface to
2323
/// the Redis module API by abstracting away the raw C FFI calls.
@@ -26,11 +26,12 @@ pub struct Context {
2626
}
2727

2828
impl Context {
29-
pub fn new(ctx: *mut raw::RedisModuleCtx) -> Self {
29+
pub const fn new(ctx: *mut raw::RedisModuleCtx) -> Self {
3030
Self { ctx }
3131
}
3232

33-
pub fn dummy() -> Self {
33+
#[must_use]
34+
pub const fn dummy() -> Self {
3435
Self {
3536
ctx: ptr::null_mut(),
3637
}
@@ -68,6 +69,7 @@ impl Context {
6869
/// # Panics
6970
///
7071
/// Will panic if `RedisModule_IsKeysPositionRequest` is missing in redismodule.h
72+
#[must_use]
7173
pub fn is_keys_position_request(&self) -> bool {
7274
// We want this to be available in tests where we don't have an actual Redis to call
7375
if cfg!(feature = "test") {
@@ -127,7 +129,7 @@ impl Context {
127129
for i in 0..length {
128130
vec.push(Self::parse_call_reply(raw::call_reply_array_element(
129131
reply, i,
130-
))?)
132+
))?);
131133
}
132134
Ok(RedisValue::Array(vec))
133135
}
@@ -137,6 +139,7 @@ impl Context {
137139
}
138140
}
139141

142+
#[must_use]
140143
pub fn str_as_legal_resp_string(s: &str) -> CString {
141144
CString::new(
142145
s.chars()
@@ -149,11 +152,13 @@ impl Context {
149152
.unwrap()
150153
}
151154

155+
#[allow(clippy::must_use_candidate)]
152156
pub fn reply_simple_string(&self, s: &str) -> raw::Status {
153157
let msg = Self::str_as_legal_resp_string(s);
154158
unsafe { raw::RedisModule_ReplyWithSimpleString.unwrap()(self.ctx, msg.as_ptr()).into() }
155159
}
156160

161+
#[allow(clippy::must_use_candidate)]
157162
pub fn reply_error_string(&self, s: &str) -> raw::Status {
158163
let msg = Self::str_as_legal_resp_string(s);
159164
unsafe { raw::RedisModule_ReplyWithError.unwrap()(self.ctx, msg.as_ptr()).into() }
@@ -162,6 +167,7 @@ impl Context {
162167
/// # Panics
163168
///
164169
/// Will panic if methods used are missing in redismodule.h
170+
#[allow(clippy::must_use_candidate)]
165171
pub fn reply(&self, r: RedisResult) -> raw::Status {
166172
match r {
167173
Ok(RedisValue::Integer(v)) => unsafe {
@@ -185,7 +191,7 @@ impl Context {
185191
Ok(RedisValue::BulkString(s)) => unsafe {
186192
raw::RedisModule_ReplyWithStringBuffer.unwrap()(
187193
self.ctx,
188-
s.as_ptr() as *const c_char,
194+
s.as_ptr().cast::<c_char>(),
189195
s.len() as usize,
190196
)
191197
.into()
@@ -198,7 +204,7 @@ impl Context {
198204
Ok(RedisValue::StringBuffer(s)) => unsafe {
199205
raw::RedisModule_ReplyWithStringBuffer.unwrap()(
200206
self.ctx,
201-
s.as_ptr() as *const c_char,
207+
s.as_ptr().cast::<c_char>(),
202208
s.len() as usize,
203209
)
204210
.into()
@@ -243,10 +249,12 @@ impl Context {
243249
}
244250
}
245251

252+
#[must_use]
246253
pub fn open_key(&self, key: &RedisString) -> RedisKey {
247254
RedisKey::open(self.ctx, key)
248255
}
249256

257+
#[must_use]
250258
pub fn open_key_writable(&self, key: &RedisString) -> RedisKeyWritable {
251259
RedisKeyWritable::open(self.ctx, key)
252260
}
@@ -255,11 +263,13 @@ impl Context {
255263
raw::replicate_verbatim(self.ctx);
256264
}
257265

266+
#[must_use]
258267
pub fn create_string(&self, s: &str) -> RedisString {
259268
RedisString::create(self.ctx, s)
260269
}
261270

262-
pub fn get_raw(&self) -> *mut raw::RedisModuleCtx {
271+
#[must_use]
272+
pub const fn get_raw(&self) -> *mut raw::RedisModuleCtx {
263273
self.ctx
264274
}
265275

@@ -274,6 +284,7 @@ impl Context {
274284
}
275285

276286
#[cfg(feature = "experimental-api")]
287+
#[allow(clippy::must_use_candidate)]
277288
pub fn notify_keyspace_event(
278289
&self,
279290
event_type: raw::NotifyEvent,
@@ -283,7 +294,7 @@ impl Context {
283294
unsafe { raw::notify_keyspace_event(self.ctx, event_type, event, keyname) }
284295
}
285296

286-
/// Returns the redis version either by calling RedisModule_GetServerVersion API,
297+
/// Returns the redis version either by calling ``RedisModule_GetServerVersion`` API,
287298
/// Or if it is not available, by calling "info server" API and parsing the reply
288299
pub fn get_redis_version(&self) -> Result<Version, RedisError> {
289300
self.get_redis_version_internal(false)
@@ -332,18 +343,21 @@ pub struct InfoContext {
332343
}
333344

334345
impl InfoContext {
335-
pub fn new(ctx: *mut raw::RedisModuleInfoCtx) -> Self {
346+
pub const fn new(ctx: *mut raw::RedisModuleInfoCtx) -> Self {
336347
Self { ctx }
337348
}
338349

350+
#[allow(clippy::must_use_candidate)]
339351
pub fn add_info_section(&self, name: Option<&str>) -> Status {
340352
add_info_section(self.ctx, name)
341353
}
342354

355+
#[allow(clippy::must_use_candidate)]
343356
pub fn add_info_field_str(&self, name: &str, content: &str) -> Status {
344357
add_info_field_str(self.ctx, name, content)
345358
}
346359

360+
#[allow(clippy::must_use_candidate)]
347361
pub fn add_info_field_long_long(&self, name: &str, value: c_longlong) -> Status {
348362
add_info_field_long_long(self.ctx, name, value)
349363
}

src/context/thread_safe.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl Deref for ContextGuard {
2222
}
2323
}
2424

25-
/// A ThreadSafeContext can either be bound to a blocked client, or detached from any client.
25+
/// A ``ThreadSafeContext`` can either be bound to a blocked client, or detached from any client.
2626
pub struct DetachedFromClient;
2727

2828
pub struct ThreadSafeContext<B: Send> {
@@ -37,9 +37,10 @@ unsafe impl<B: Send> Send for ThreadSafeContext<B> {}
3737
unsafe impl<B: Send> Sync for ThreadSafeContext<B> {}
3838

3939
impl ThreadSafeContext<DetachedFromClient> {
40+
#[must_use]
4041
pub fn new() -> Self {
4142
let ctx = unsafe { raw::RedisModule_GetThreadSafeContext.unwrap()(ptr::null_mut()) };
42-
ThreadSafeContext {
43+
Self {
4344
ctx,
4445
blocked_client: DetachedFromClient,
4546
}
@@ -53,16 +54,18 @@ impl Default for ThreadSafeContext<DetachedFromClient> {
5354
}
5455

5556
impl ThreadSafeContext<BlockedClient> {
57+
#[must_use]
5658
pub fn with_blocked_client(blocked_client: BlockedClient) -> Self {
5759
let ctx = unsafe { raw::RedisModule_GetThreadSafeContext.unwrap()(blocked_client.inner) };
58-
ThreadSafeContext {
60+
Self {
5961
ctx,
6062
blocked_client,
6163
}
6264
}
6365

6466
/// The Redis modules API does not require locking for `Reply` functions,
6567
/// so we pass through its functionality directly.
68+
#[allow(clippy::must_use_candidate)]
6669
pub fn reply(&self, r: RedisResult) -> raw::Status {
6770
let ctx = Context::new(self.ctx);
6871
ctx.reply(r)

src/context/timer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl Context {
2727
data: T,
2828
) -> RedisModuleTimerID
2929
where
30-
F: FnOnce(&Context, T),
30+
F: FnOnce(&Self, T),
3131
{
3232
let cb_data = CallbackData { data, callback };
3333

@@ -46,7 +46,7 @@ impl Context {
4646
.try_into()
4747
.expect("Value must fit in 64 bits"),
4848
Some(raw_callback::<F, T>),
49-
data as *mut c_void,
49+
data.cast::<c_void>(),
5050
)
5151
}
5252
}
@@ -98,7 +98,7 @@ impl Context {
9898
}
9999

100100
// Cast the *mut c_void supplied by the Redis API to a raw pointer of our custom type.
101-
let data = data as *mut T;
101+
let data = data.cast::<T>();
102102

103103
// Dereference the raw pointer (we know this is safe, since Redis should return our
104104
// original pointer which we know to be good) and turn it into a safe reference
@@ -110,7 +110,7 @@ impl Context {
110110

111111
fn take_data<T>(data: *mut c_void) -> T {
112112
// Cast the *mut c_void supplied by the Redis API to a raw pointer of our custom type.
113-
let data = data as *mut T;
113+
let data = data.cast::<T>();
114114

115115
// Take back ownership of the original boxed data, so we can unbox it safely.
116116
// If we don't do this, the data's memory will be leaked.

src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub enum Error {
1111
}
1212

1313
impl Error {
14+
#[must_use]
1415
pub fn generic(message: &str) -> Self {
1516
Self::Generic(GenericError::new(message))
1617
}
@@ -66,6 +67,7 @@ pub struct GenericError {
6667
}
6768

6869
impl GenericError {
70+
#[must_use]
6971
pub fn new(message: &str) -> Self {
7072
Self {
7173
message: String::from(message),

src/key.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,12 @@ impl RedisKeyWritable {
160160
Ok(Some(read_key(self.key_inner)?))
161161
}
162162

163+
#[allow(clippy::must_use_candidate)]
163164
pub fn hash_set(&self, field: &str, value: RedisString) -> raw::Status {
164165
raw::hash_set(self.key_inner, field, value.inner)
165166
}
166167

168+
#[allow(clippy::must_use_candidate)]
167169
pub fn hash_del(&self, field: &str) -> raw::Status {
168170
raw::hash_del(self.key_inner, field)
169171
}
@@ -191,11 +193,13 @@ impl RedisKeyWritable {
191193
}
192194

193195
// `list_push_head` inserts the specified element at the head of the list stored at this key.
196+
#[allow(clippy::must_use_candidate)]
194197
pub fn list_push_head(&self, element: RedisString) -> raw::Status {
195198
raw::list_push(self.key_inner, raw::Where::ListHead, element.inner)
196199
}
197200

198201
// `list_push_tail` inserts the specified element at the tail of the list stored at this key.
202+
#[allow(clippy::must_use_candidate)]
199203
pub fn list_push_tail(&self, element: RedisString) -> raw::Status {
200204
raw::list_push(self.key_inner, raw::Where::ListTail, element.inner)
201205
}
@@ -204,6 +208,7 @@ impl RedisKeyWritable {
204208
// Returns None when:
205209
// 1. The list is empty.
206210
// 2. The key is not a list.
211+
#[allow(clippy::must_use_candidate)]
207212
pub fn list_pop_head(&self) -> Option<RedisString> {
208213
let ptr = raw::list_pop(self.key_inner, raw::Where::ListHead);
209214

@@ -267,6 +272,7 @@ impl RedisKeyWritable {
267272
/// # Panics
268273
///
269274
/// Will panic if `RedisModule_KeyType` is missing in redismodule.h
275+
#[must_use]
270276
pub fn key_type(&self) -> raw::KeyType {
271277
unsafe { raw::RedisModule_KeyType.unwrap()(self.key_inner) }.into()
272278
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub use crate::redismodule::*;
3636
use backtrace::Backtrace;
3737

3838
/// Ideally this would be `#[cfg(not(test))]`, but that doesn't work:
39-
/// https://github.com/rust-lang/rust/issues/59168#issuecomment-472653680
39+
/// [59168#issuecomment-472653680](https://github.com/rust-lang/rust/issues/59168#issuecomment-472653680)
4040
/// The workaround is to use the `test` feature instead.
4141
#[cfg(not(feature = "test"))]
4242
#[global_allocator]

0 commit comments

Comments
 (0)