Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filter for exhaustive switch expression #1472

Merged
merged 10 commits into from Jun 14, 2023

Conversation

Godin
Copy link
Member

@Godin Godin commented Jun 5, 2023

Before this change for the following Example.java

enum Example {
    A, B;

    static int example(Example e) {
        return switch (e) {
        case A -> 1;
        case B -> 2;
        };
    }

    public static void main(String[] args) {
        example(A);
        example(B);
    }
}

using

openjdk version "17.0.2" 2022-01-18 LTS
OpenJDK Runtime Environment Zulu17.32+13-CA (build 17.0.2+8-LTS)
OpenJDK 64-Bit Server VM Zulu17.32+13-CA (build 17.0.2+8-LTS, mixed mode, sharing)

execution of

javac Example.java -d classes
java -javaagent:jacocoagent.jar -cp classes Example
java -jar jacococli.jar report jacoco.exec --classfiles classes --sourcefiles . --html report

produces

before before

and after this change

after after

Fixes #771

@Godin Godin self-assigned this Jun 5, 2023
@Godin Godin added this to Implementation in Current work items via automation Jun 5, 2023
@Godin Godin added this to the 0.8.11 milestone Jun 5, 2023
@Godin Godin changed the title Add filter for exhaustive switch-expression Add filter for exhaustive switch expression Jun 5, 2023
@Godin Godin added this to Awaiting triage in Filtering via automation Jun 5, 2023
@Godin Godin moved this from Awaiting triage to In Progress in Filtering Jun 5, 2023
@Godin Godin marked this pull request as ready for review June 6, 2023 11:31
@Godin Godin requested a review from marchof June 6, 2023 11:31
@Godin Godin moved this from Implementation to Review in Current work items Jun 6, 2023
Copy link
Member

@marchof marchof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work again! I left some formal comments you might want to consider.

@marchof marchof merged commit 8271afb into jacoco:master Jun 14, 2023
23 checks passed
Filtering automation moved this from In Progress to Done Jun 14, 2023
Current work items automation moved this from Review to Done Jun 14, 2023
@Godin Godin deleted the ExhaustiveSwitchFilter branch June 14, 2023 21:25
@nioncode
Copy link

@Godin should this already handle multi-line switch expressions? I still get missed branch warnings in my code, where I have something like:

enum Example {
    A, B;

    static int example(Example e) {
        return switch (e) {
        case A -> {
            if (false) {
              throw new InvalidStateException();
            }
            yield 1
        };
        case B -> {
            if (false) {
              throw new InvalidStateException();
            }
            yield 2;
        };
    }

    public static void main(String[] args) {
        example(A);
        example(B);
    }
}

@Godin
Copy link
Member Author

Godin commented Apr 29, 2024

@nioncode for the following Example.java which is the same example as yours after fixing compilation errors

enum Example {
    A, B;

    static int example(Example e) {
        return switch (e) {
        case A -> {
            if (false) {
              throw new IllegalStateException();
            }
            yield 1;
        }
        case B -> {
            if (false) {
              throw new IllegalStateException();
            }
            yield 2;
        }
        };
    }

    public static void main(String[] args) {
        example(A);
        example(B);
    }
}

using

openjdk version "21" 2023-09-19
OpenJDK Runtime Environment (build 21+35-2513)
OpenJDK 64-Bit Server VM (build 21+35-2513, mixed mode, sharing)

execution of

javac Example.java -d classes
java -javaagent:jacoco-0.8.12/lib/jacocoagent.jar -cp classes Example
java -jar jacoco-0.8.12/lib/jacococli.jar report jacoco.exec --classfiles classes --sourcefiles . --html report

produces the following report

Screenshot 2024-04-29 at 11 38 05 Screenshot 2024-04-29 at 11 37 46

@nioncode
Copy link

This is true. Sorry, I ran this through gradle and it seems it does not pick up the latest version by default.

Just for others, if you use the jacoco plugin in gradle, you can force a specific version via:

plugins {
  id "jacoco" // note: don't specify a version here, specify it in the jacoco block below
}

jacoco {
  toolVersion = "0.8.12"
}

Everything works fine as is, sorry for the noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Filtering
  
Done
Development

Successfully merging this pull request may close these issues.

Add filter for exhaustive switch-expression
3 participants