fn call
has a cyclomatic complexity of 79 with "critical" risk141 self.api_impl.poll_ready(cx)
142 }
143
144 fn call(&mut self, req: (Request<Body>, C)) -> Self::Future {145 async fn run<T, C>(
146 mut api_impl: T,
147 req: (Request<Body>, C),
fn load_section_data_with_skel_value
has a cyclomatic complexity of 31 with "very-high" risk 43 vec}
44 };
45}
46pub(crate) fn load_section_data_with_skel_value( 47 btf: &Btf,
48 section: &DataSectionMeta,
49 buffer: &mut [u8],
fn load_and_attach
has a cyclomatic complexity of 27 with "very-high" risk 52 /// load and attach the ebpf program to the kernel to run the ebpf program
53 /// if the ebpf program has maps to export to user space, you need to call
54 /// the wait and export.
55 pub fn load_and_attach(mut self) -> Result<BpfSkeleton> { 56 // This function differs from the C++ version `int bpf_skeleton::load_and_attach_prog(void)`
57 // Because we put the call to bpf_object_open in `BpfSkeletonBuilder::build`
58 // So Here are just the calls to load and attach
fn wait_and_poll_to_handler_with_multiple_exporter
has a cyclomatic complexity of 27 with "very-high" risk211 }
212 /// Start poll with each map corresponding to a different exporter
213 /// The function `exporter_provider` should return the ExportFormatType, EventHandler, and UserContext(if applies) for the given map name (If you want to set the exporter)
214 pub fn wait_and_poll_to_handler_with_multiple_exporter(215 &self,
216 exporter_provider: impl Fn(
217 &str,
A function with high cyclomatic complexity can be hard to understand and maintain. Cyclomatic complexity is a software metric that measures the number of independent paths through a function. A higher cyclomatic complexity indicates that the function has more decision points and is more complex.
Functions with high cyclomatic complexity are more likely to have bugs and be harder to test. They may lead to reduced code maintainability and increased development time.
To reduce the cyclomatic complexity of a function, you can:
use anyhow::{anyhow, Context, Result};
use std::io::{Lines, StdinLock};
trait InputHandle {
type Input<'a>
where
Self: 'a;
fn get_input<'a>(&self) -> Self::Input<'a>;
fn process_input<'a, T>(&self, f: impl FnOnce(Self::Input<'a>) -> Result<T>) -> Result<T>
where
Self: 'a;
}
struct StdioInput;
impl InputHandle for StdioInput {
type Input<'a> = Lines<StdinLock<'a>>;
fn get_input<'a>(&self) -> Self::Input<'a> {
std::io::stdin().lines()
}
fn process_input<'a, T>(&self, f: impl FnOnce(Self::Input<'a>) -> Result<T>) -> Result<T>
where
Self: 'a,
{
f(self.get_input())
}
}
fn main() -> Result<()> {
// cc = 1 (default)
let index: usize = StdioInput.process_input(|mut input| {
// cc = 3 (?, ?)
str::parse(input.next().context("no input from stdin")??.as_str()).context("parse failed")
})?; // cc = 4 (?)
// cc = 5 (for)
for i in 0..index {
let fizzbuzz = match i % 15 {
// cc = 6 (match_arm 0)
0 => "fizzbuzz".into(),
// cc = 9 (match_arm 3 | 6 | 9)
3 | 6 | 9 | 12 => "fizz".into(),
// cc = 11 (match_arm 5 | 10)
5 | 10 => "buzz".into(),
x => format!("{x}"),
};
println!("{i}: {fizzbuzz}");
}
Ok(())
}
use anyhow::{anyhow, Context, Result};
use std::io::{Lines, StdinLock};
trait InputHandle {
type Input<'a>
where
Self: 'a;
fn get_input<'a>(&self) -> Self::Input<'a>;
fn process_input<'a, T>(&self, f: impl FnOnce(Self::Input<'a>) -> Result<T>) -> Result<T>
where
Self: 'a;
}
struct StdioInput;
impl InputHandle for StdioInput {
type Input<'a> = Lines<StdinLock<'a>>;
fn get_input<'a>(&self) -> Self::Input<'a> {
std::io::stdin().lines()
}
fn process_input<'a, T>(&self, f: impl FnOnce(Self::Input<'a>) -> Result<T>) -> Result<T>
where
Self: 'a,
{
f(self.get_input())
}
}
fn fizzbuzz(u: usize) -> String {
// cc = 1 (default)
match u % 15 {
// cc = 2 (match_arm 0)
0 => "fizzbuzz".into(),
// cc = 9 (match_arm 3 | 6 | 9)
3 | 6 | 9 | 12 => "fizz".into(),
// cc = 11 (match_arm 5 | 10)
5 | 10 => "buzz".into(),
x => format!("{x}"),
}
} // total cc =
fn main() -> Result<()> {
// cc = 1 (default)
let index: usize = StdioInput.process_input(|mut input| {
// cc = 3 (?, ?)
str::parse(input.next().context("no input from stdin")??.as_str()).context("parse failed")
})?; // cc = 4 (?)
// cc = 5 (for)
for i in 0..index {
let val = fizzbuzz(i);
println!("{i}: {val}");
}
Ok(())
}
Cyclomatic complexity threshold can be configured using the
cyclomatic_complexity_threshold
(docs) in the
.deepsource.toml
config file.
Configuring this is optional. If you don't provide a value, the Analyzer will
raise issues for functions with complexity higher than the default threshold,
which is high
(only raise issues for >25) for the Rust Analyzer.
Here's the mapping of the risk category to the cyclomatic complexity score to help you configure this better:
Risk category | Cyclomatic complexity range | Recommended action |
---|---|---|
low | 1-5 | No action needed. |
medium | 6-15 | Review and monitor. |
high | 16-25 | Review and refactor. Recommended to add comments if the function is absolutely needed to be kept as it is. |
very-high. | 26-50 | Refactor to reduce the complexity. |
critical | >50 | Must refactor this. This can make the code untestable and very difficult to understand. |