Skip to content

Commit e048ae0

Browse files
clippy: add safety comment for unwrap calls
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent 12505dc commit e048ae0

File tree

6 files changed

+30
-8
lines changed

6 files changed

+30
-8
lines changed

plugin/src/commands/builtin.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,30 @@ impl<T: Clone> RPCCommand<T> for ManifestRPC {
5151
#[derive(Clone)]
5252
/// Type to define the init method and its attributes, used in plugin
5353
pub struct InitRPC<T: 'static + Clone> {
54+
#[allow(clippy::type_complexity)]
5455
pub(crate) on_init: Option<Arc<dyn Fn(&mut Plugin<T>) -> Value>>,
5556
}
5657

5758
impl<T: Clone> InitRPC<T> {
5859
fn parse_option(&self, plugin: &mut Plugin<T>, options: &HashMap<String, serde_json::Value>) {
5960
for option_name in options.keys() {
61+
// SAFETY: We are iterating over the key this never None
62+
#[allow(clippy::unwrap_used)]
6063
let option = options.get(option_name).unwrap();
61-
plugin.option.get_mut(option_name).unwrap().value = Some(option.to_owned());
64+
// SAFETY: we put them into it so it is safe to unwrap.
65+
// If we panic this mean that there is a bug
66+
#[allow(clippy::unwrap_used)]
67+
let opt = plugin.option.get_mut(option_name).unwrap();
68+
opt.value = Some(option.to_owned());
6269
}
6370
}
6471
}
6572

6673
impl<T: Clone> RPCCommand<T> for InitRPC<T> {
6774
fn call<'c>(&self, plugin: &mut Plugin<T>, request: Value) -> Result<Value, PluginError> {
6875
let mut response = init_payload();
76+
// SAFETY: Shouwl be valid json so should be safe to unwrap
77+
#[allow(clippy::unwrap_used)]
6978
let init: InitConf = serde_json::from_value(request.to_owned()).unwrap();
7079
plugin.configuration = Some(init.configuration);
7180
self.parse_option(plugin, &init.options);

plugin/src/commands/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::errors::PluginError;
1515
/// in contrast, it is more complex but the plugin_macros package will help to simplify the API.
1616
pub trait RPCCommand<T: Clone>: RPCCommandClone<T> {
1717
/// call is a generic method that it is used to simulate the callback.
18-
fn call<'c>(
18+
fn call(
1919
&self,
2020
_: &mut Plugin<T>,
2121
_: serde_json::Value,

plugin/src/io.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ impl AsyncIO {
3030
Ok(())
3131
}
3232

33+
#[allow(clippy::wrong_self_convention)]
3334
pub fn into_loop<F>(&mut self, mut async_callback: F) -> io::Result<()>
3435
where
3536
F: FnMut(String) -> Option<String>,

plugin/src/plugin.rs

+15
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ impl<'a, T: 'a + Clone> Plugin<T> {
126126
params: payload,
127127
};
128128
crate::poll_loop!({
129+
// SAFETY: it is valid json and if we panic there is a buf somewhere
130+
#[allow(clippy::unwrap_used)]
129131
writer.write_all(serde_json::to_string(&request).unwrap().as_bytes())
130132
});
131133
crate::poll_loop!({ writer.flush() });
@@ -142,8 +144,12 @@ impl<'a, T: 'a + Clone> Plugin<T> {
142144
) -> &mut Self {
143145
let def_val = match opt_type {
144146
"flag" | "bool" => {
147+
// FIXME: remove unwrap and return the error
148+
#[allow(clippy::unwrap_used)]
145149
def_val.map(|val| serde_json::json!(val.parse::<bool>().unwrap()))
146150
}
151+
// FIXME: remove unwrap and return the error
152+
#[allow(clippy::unwrap_used)]
147153
"int" => def_val.map(|val| serde_json::json!(val.parse::<i64>().unwrap())),
148154
"string" => def_val.map(|val| serde_json::json!(val)),
149155
_ => unreachable!("{opt_type} not supported"),
@@ -212,6 +218,9 @@ impl<'a, T: 'a + Clone> Plugin<T> {
212218
}
213219

214220
fn handle_notification(&'a mut self, name: &str, params: serde_json::Value) {
221+
// SAFETY: we register the notification and if we do not have inside the map
222+
// this is a bug.
223+
#[allow(clippy::unwrap_used)]
215224
let notification = self.rpc_notification.get(name).unwrap().clone();
216225
notification.call_void(self, &params);
217226
}
@@ -253,6 +262,8 @@ impl<'a, T: 'a + Clone> Plugin<T> {
253262
match result {
254263
Ok(json_resp) => response["result"] = json_resp.to_owned(),
255264
Err(json_err) => {
265+
// SAFETY: should be valud json
266+
#[allow(clippy::unwrap_used)]
256267
let err_resp = serde_json::to_value(json_err).unwrap();
257268
response["error"] = err_resp;
258269
}
@@ -281,6 +292,8 @@ impl<'a, T: 'a + Clone> Plugin<T> {
281292
asyncio.into_loop(|buffer| {
282293
#[cfg(feature = "log")]
283294
log::info!("looping around the string: {buffer}");
295+
// SAFETY: should be valud json
296+
#[allow(clippy::unwrap_used)]
284297
let request: Request<serde_json::Value> = serde_json::from_str(&buffer).unwrap();
285298
if let Some(id) = request.id {
286299
// when the id is specified this is a RPC or Hook, so we need to return a response
@@ -293,6 +306,8 @@ impl<'a, T: 'a + Clone> Plugin<T> {
293306
request.method,
294307
rpc_response
295308
);
309+
// SAFETY: should be valud json
310+
#[allow(clippy::unwrap_used)]
296311
Some(serde_json::to_string(&rpc_response).unwrap())
297312
} else {
298313
// in case of the id is None, we are receiving the notification, so the server is not

plugin_macros/src/plugin.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use kproc_parser::kproc_macros::KTokenStream;
55
use kproc_parser::proc_macro::{TokenStream, TokenTree};
66
use kproc_parser::{build_error, check, trace};
77

8-
#[derive(Debug)]
9-
#[derive(Default)]
8+
#[derive(Debug, Default)]
109
pub struct PluginDeclaration {
1110
pub state: Option<String>,
1211
pub dynamic: Option<TokenTree>,
@@ -59,8 +58,6 @@ impl std::fmt::Display for PluginDeclaration {
5958
}
6059
}
6160

62-
63-
6461
/// proc macro syntax is something like this
6562
///
6663
/// ```ignore

tests/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ pub mod fixtures {
3636
pub fn lightningd() -> cln::Node {
3737
init();
3838
let pwd = std::env::var("PWD").unwrap();
39-
39+
4040
async_run!(cln::Node::with_params(&format!("--developer --plugin={pwd}/target/debug/examples/foo_plugin --plugin={pwd}/target/debug/examples/macros_ex"), "regtest")).unwrap()
4141
}
4242

4343
#[fixture]
4444
pub fn lightningd_second() -> cln::Node {
4545
init();
46-
46+
4747
async_run!(cln::Node::with_params("--developer", "regtest")).unwrap()
4848
}
4949
}

0 commit comments

Comments
 (0)