1

In my project I have a simple function that calculates the length of an iterable (as I don't think there is an easy way to get it? No .size() or .length() is accepted?) Here is the code:

public int getIterableSize(Iterable<User> users){
        int size = 0;

        for(User user : users){
            size++;
        }

        return size;
    }

I also use Sonarqube to keep my code quality and I get the following code smell about this function:

Remove this unused "user" local variable.

There must be an easy way to get rid of this right? Maybe an alternative for the for loop, maybe a different function provided by iterable?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Pim_vh
  • 143
  • 1
  • 14

1 Answers1

2

You can annotate it with @SuppressWarnings("unused").

Either the method:

@SuppressWarnings("unused")
public int getIterableSize(Iterable<User> users){
    int size = 0;

    for(User user : users){
        size++;
    }

    return size;
}

Or the variable:

public int getIterableSize(Iterable<User> users){
    int size = 0;

    for(@SuppressWarnings("unused") User user : users){
        size++;
    }

    return size;
}

Various IDEs can automatically offer both of these fixes.

tevemadar
  • 12,389
  • 3
  • 21
  • 49