Rust: cyclomatic and cognitive values that differ from McCabe and the Cognitive Complexity whitepaper

Aberta
#1,249 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
4/5
Tempo estimado
3-5 dias
Facilidade para iniciantes
48/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
rust
Domínio
devtools, testing

Direção de pesquisa

Start by reading how rust-code-analysis-cli 0.0.25 computes cyclomatic and cognitive metrics for Rust control flow, then reproduce the four self-contained snippets from the issue. Compare each result with the cited McCabe and Cognitive Complexity rules to locate the relevant analysis and tests. Done means all reported metric values match the stated expectations.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

Version: rust-code-analysis-cli 0.0.25 (crates.io), and master at 37e5d83c056c. Same results on both.

Four small cases (five departures), each a self-contained file. The expected values come from McCabe (1976) for cyclomatic and from G. Ann Campbell's Cognitive Complexity whitepaper (SonarSource, v1.7) for cognitive.

1. match counts every arm, including the last (the whitepaper's getWords example, written with match):

fn get_words(number: u32) -> &'static str {
    match number {
        1 => "one",
        2 => "a couple",
        3 => "a few",
        _ => "lots",
    }
}

Cyclomatic: expected 4 (a four-way branch is three decisions; the whitepaper gives 4 for its switch version), actual 5. Cognitive 1 is as expected.

2. A bare loop counts as a decision, and is not counted for cognitive:

fn f(mut n: u32) -> u32 {
    loop {
        n += 1;
        if n > 10 {
            break;
        }
    }
    n
}

Cyclomatic: expected 2 (loop has no predicate; the if is the only decision), actual 3. Cognitive: expected 3 (the whitepaper adds +1 for the loop and +2 for the if nested in it), actual 1.

3. let ... else is not a decision:

fn first(v: &[i32]) -> i32 {
    let Some(x) = v.first() else { return 0; };
    *x
}

Cyclomatic: expected 2 (the pattern either matches, or the diverging else runs), actual 1.

4. The whitepaper's mixed-operator example:

fn mixed(a: bool, b: bool, c: bool, d: bool, e: bool, g: bool) -> i32 {
    if a && b && c || d || e && g {
        return 1;
    }
    0
}

Cognitive: expected 4 (the whitepaper's own worked example: +1 for the if, +1 for each of the three operator sequences), actual 5. Cyclomatic 7 is as expected.

I'm happy to split this into separate issues if that's easier to track.

This report was drafted with AI assistance; I ran the repro and checked the numbers myself.

Linguagem predominante
Rust
Estrelas
469
Forks
74
Merge médio
17h 16min
PRs com merge (30d)
10

Preparar o ambiente

Este projeto não oferece contêiner de desenvolvimento, Dockerfile nem guia de contribuição, então a configuração fica por sua conta: comece pelo README e veja nosso guia da primeira contribuição para os passos gerais.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de mozilla/rust-code-analysis

Todas as issues de mozilla/rust-code-analysis

Issues semelhantes

Mais issues de Rust

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.