keepsimple1 / mdns-sd

Rust library for mDNS based Service Discovery
Apache License 2.0
88 stars 38 forks source link

Refactor functions warned by clippy::cognitive_complexity lint #204

Closed keepsimple1 closed 1 month ago

keepsimple1 commented 2 months ago
          > > * Add `allow(clippy::cognitive_complexity)` for the `service_daemon` module, [the lint](https://rust-lang.github.io/rust-clippy/master/index.html#/cognitive_complexity) rates the complexity of a function and warns if it is exceeded, in this case 2 functions pass the default threshold, the solution to this is to make it smaller by refactoring, splitting it into multiple functions etc (or maybe not possible in some cases), I've added this as solving it would be outside of the PR's scope, so lets make CI pass.

It's cool to learn about this (for me). Although I opted not to mandate cognitive_complexity lint, I'm curious what the two functions are. (A quick local run didn't turn up such warnings unless I missed them).

Yeah, very interesting to see that clippy nursery suggests splitting big chunks of code/complexity.

    Checking mdns-sd v0.11.0 (/Users/cosminpatrinjan/Github/_contributing/mdns-sd)
error: the function has a cognitive complexity of (50/25)
   --> src/service_daemon.rs:624:8
    |
624 |     fn exec_command(zc: &mut Zeroconf, command: Command, repeating: bool) {
    |        ^^^^^^^^^^^^
    |
    = help: you could split it up into multiple smaller functions
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cognitive_complexity
    = note: `-D clippy::cognitive-complexity` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(clippy::cognitive_complexity)]`

error: the function has a cognitive complexity of (26/25)
    --> src/service_daemon.rs:1970:8
     |
1970 |     fn handle_query(&mut self, msg: DnsIncoming, ip: &IpAddr) {
     |        ^^^^^^^^^^^^
     |
     = help: you could split it up into multiple smaller functions
     = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cognitive_complexity

error: could not compile `mdns-sd` (lib) due to 2 previous errors

Originally posted by @CosminPerRam in https://github.com/keepsimple1/mdns-sd/issues/202#issuecomment-2084783071

keepsimple1 commented 1 month ago

I took a look at the lint. I think exec_command is indeed due for refactoring, and handle_query seems to be okay for now. Opened PR #215 to fix exec_command.

keepsimple1 commented 1 month ago

done.