Java: an enhanced `for (T x : xs)` loop is not counted (cx, ccx, nest, ev and the qd kinds built on them)

Aperta Adatta ai principianti
#871 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
88/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp, java
Ambito
devtools, tooling

Direzione di ricerca

Inizia da src/ingest_metrics.h, in particolare da isDecisionType, cc_isNestingControl ed ev_ctrlKindFor; l’issue individua il tipo di nodo Java mancante in ciascun elenco. Leggi i fixture delle metriche corrispondenti in test/metricscheck.sh e test/essentialcxcheck.sh, poi aggiungi casi abbinati per indice e ciclo enhanced. Il lavoro è terminato quando i valori cx, ccx, nest ed ev corrispondono e entrambi i controlli passano; incrementa la versione del parser ed esegui bash test/qschemetripcheck.sh.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug good first issue help wanted

Claim it in this issue first. Size: XS. Depends on: nothing. Language: Java. Good for a first contribution: the fix is one node-type name added to three lists in one file, and the test is a twin fixture.

The problem, in user terms

for (T x : xs) is how most Java code loops. ripwire's metrics walk does not know the node enhanced_for_statement, so the loop adds nothing to cyclomatic complexity (cx), cognitive complexity (ccx), nesting (nest) or essential complexity (ev). The --quality-delta kinds built on nesting (nesting, complexity, tangle, and the deep / humps facts) read idiomatic Java as flatter than it is. The same loop written with an index is counted.

Fixture Probe.java (two pairs, each pair the same shape):

import java.util.List;

class Probe {
    static int indexFor(int[] xs, int[] ys) {
        int hits = 0;
        for (int i = 0; i < xs.length; i++) {
            for (int j = 0; j < ys.length; j++) {
                if (xs[i] == ys[j]) {
                    hits++;
                }
            }
        }
        return hits;
    }

    static int enhancedFor(List<Integer> xs, List<Integer> ys) {
        int hits = 0;
        for (int x : xs) {
            for (int y : ys) {
                if (x == y) {
                    hits++;
                }
            }
        }
        return hits;
    }

    static int indexBreak(int[] xs, boolean on) {
        int s = 0;
        for (int i = 0; i < xs.length; i++) {
            if (on) {
                if (xs[i] == 3) {
                    break;
                }
                s += xs[i];
            }
        }
        return s;
    }

    static int enhancedBreak(List<Integer> xs, boolean on) {
        int s = 0;
        for (int x : xs) {
            if (on) {
                if (x == 3) {
                    break;
                }
                s += x;
            }
        }
        return s;
    }
}

On main (e94757295), with the other attributes cut for reading:

ripwire . --no-cache --metrics
<s t="method" n="indexFor"      cx="4" ccx="6" nest="3"
<s t="method" n="enhancedFor"   cx="2" ccx="1" nest="1"
<s t="method" n="indexBreak"    cx="4" ccx="6" nest="3" ev="4" ev_floor="1" ev_why="loop-escape:1"
<s t="method" n="enhancedBreak" cx="3" ccx="3" nest="2"

Expected: each enhanced* row prints exactly what its index* twin prints. The grammar has the node: ripwire . --match='(enhanced_for_statement) @l' finds all three enhanced loops. Only the metric lists miss it.

Where to look

All in src/ingest_metrics.h. grep -n enhanced_for_statement src/ingest_metrics.h prints nothing today.

  • isDecisionType (line 32): the cyclomatic decision list. for_statement is at line 35.
  • cc_isNestingControl (line 107): the nesting list. for_statement is at line 110.
  • ev_ctrlKindFor (line 1573): the loop list for ev, lines 1588-1597. Without it a break inside an enhanced for finds no loop below the function, and the floor rule marks nothing.

Other files already know the node, so the spelling is proven: src/ingest_binds.h:3170, src/slice.h:832.

Suggested approach

Add kindIs( t, "enhanced_for_statement" ) next to for_statement in all three lists. No language gate is needed: of the vendored grammars only Java's has that node name (grep -l '"enhanced_for_statement"' third_party/deps/*/src/parser.c prints only third_party/deps/java/src/parser.c).

The test to add (red first)

  • Add the four functions above to the Java part of test/metricscheck.sh (it already writes Java fixtures, see its Calc.java arm), or to a new small Java arm there. Assert each enhanced* row equals its index* twin on cx, ccx, nest and ev. Show the arm red on main first.
  • Add enhancedBreak to the Java section of test/essentialcxcheck.sh (javaLabelled lives there) with its ev value.
  • The metrics are cached: bump kParserVer in src/ingest_cache.h:409 and its mirror kIngestParserVerMirror in src/quality.h:3335 to any number above main's. Run bash test/qschemetripcheck.sh.

Acceptance criteria

  • The four rows above print twin-equal numbers.
  • bash test/metricscheck.sh "$PWD/build/ripwire" and bash test/essentialcxcheck.sh "$PWD/build/ripwire" pass, with the new arms red on main.
  • bash test/langidentitycheck.sh origin/main HEAD --langs=java,kt,cs,py,cpp: every language except Java is SAME. Java DIFFs only on metric attributes of functions that contain an enhanced for, waived by --intended rows naming the new arm. If you have a real Java repository, add --repo=java:<your clone> and say how many functions moved.
  • ripwire . --quality-delta=origin/main..HEAD gates 0.

Size

XS: three one-line additions, one fixture arm, one version bump.

How to verify locally

cmake -S . -B build && cmake --build build -j      # plain build: no build type
mkdir -p /tmp/efor && cp Probe.java /tmp/efor/ && ./build/ripwire /tmp/efor --no-cache --metrics
bash test/metricscheck.sh "$PWD/build/ripwire"
bash test/essentialcxcheck.sh "$PWD/build/ripwire"
bash test/qschemetripcheck.sh
bash test/langidentitycheck.sh origin/main HEAD --langs=java,kt,cs,py,cpp
./build/ripwire . --quality-delta=origin/main..HEAD --legend=compact

Not in scope

Java stream chains (xs.stream().filter(...)) and lambdas: they are not loops for cx by design. Kotlin's for (x in xs) is already counted (for_statement).


Claiming it only takes a comment here. Every contributor is credited by handle in the CHANGELOG entry and the release notes. Thank you!

Lingua principale
C++
Stelle
2.6k
Fork
182
Merge medio
21h 12m
PR unite (30g)
262

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di redhat-et/ripwire

Tutte le issue di redhat-et/ripwire

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.