Marking an Unused Block Variable

Marking an unused block variable

A * means "all remaining parameters". An _ is just another variable name, although it is a bit special. So they are different, for example the following does not make sense:

[[1, 2, 3], [4, 5, 6]].each{|*, x, *| p x}  # Syntax error

Indeed, how is Ruby supposed to know if the first star should get 0, 1 or 2 of the values (and the reverse)?

There are very few cases where you want to use a star to ignore parameters. An example would be if you only want to use the last of a variable number of parameters:

[[1], [2, 3], [4, 5, 6]].each{|*, last| p last}  # => prints 1, 3 and 6

Ruby allows you to not give a name to the "rest" of the parameters, but you can use _:

[[1], [2, 3], [4, 5, 6]].each{|*_, last| p last}  # => prints 1, 3 and 6

Typically, the number of parameters is known and your best choice is to use a _:

[[1, 2, 3], [4, 5, 6]].each{|_, mid, _| p mid}  # prints 2 and 5

Note that you could leave the last paramater unnamed too (like you can when using a *), although it is less obvious:

[[1, 2, 3], [4, 5, 6]].each{|_, mid, | p mid}  # prints 2 and 5

Now _ is the designated variable name to use when you don't want to use a value. It is a special variable name for two reasons:

  1. Ruby won't complain if you don't use it (if warnings are on)
  2. Ruby will allow you to repeat it in the argument list.

Example of point 1:

> ruby -w -e "def foo; x = 42; end; foo"
-e:1: warning: assigned but unused variable - x

> ruby -w -e "def foo; _ = 42; end; foo"
no warning

Example of point 2:

[[1, 2, 3], [4, 5, 6]].each{|unused, mid, unused| p mid}
# => SyntaxError: (irb):23: duplicated argument name

[[1, 2, 3], [4, 5, 6]].each{|_, mid, _| p mid}
# => prints 2 and 5

Finally, as @DigitalRoss notes, _ holds the last result in irb

Update: In Ruby 2.0, you can use any variable starting with _ to signify it is unused. This way the variable name can be more explicit about what is being ignored:

_scheme, _domain, port, _url = parse_some_url
# ... do something with port

How do I best silence a warning about unused variables?

You can put it in "(void)var;" expression (does nothing) so that a compiler sees it is used. This is portable between compilers.

E.g.

void foo(int param1, int param2)
{
(void)param2;
bar(param1);
}

Or,

#define UNUSED(expr) do { (void)(expr); } while (0)
...

void foo(int param1, int param2)
{
UNUSED(param2);
bar(param1);
}

How can I suppress unused parameter warnings in C?

I usually write a macro like this:

#define UNUSED(x) (void)(x)

You can use this macro for all your unused parameters. (Note that this works on any compiler.)

For example:

void f(int x) {
UNUSED(x);
...
}

JSLint message: Unused variables

Try:

var items = "<option selected></option>";
/*jslint unparam: true*/
$.each(data, function (i, item) {
items += "<option value='" + item.Value + "'>" + item.Text + "</option>";
});
/*jslint unparam: false*/ // so that you still get warnings from other functions

How can I get rid of an unused variable warning in Xcode?

I'm unsure if it's still supported in the new LLVM compiler, but GCC has an "unused" attribute you can use to suppress that warning:

BOOL saved __attribute__((unused)) = [moc save:&error];

Alternatively (in case LLVM doesn't support the above), you could split the variable declaration into a separate line, guaranteeing that the variable would be "used" whether the macro expands or not:

BOOL saved = NO;
saved = [moc save:&error];

Unused variable warning with static NSInteger, but not with NSString

A primitive variable is just a memory block allocated in a static memory part and initialized by the compiler. The string object, however, is a variable initialized at runtime (in startup, probably), so the compiler adds an implicit call to the constructor and uses the variable as a parameter for that call. So the variable is being used.

The _unused item of the structure is IMHO not a directive, but just a member variable, probably it is added for better alignment (fills the object size to a round size).

Why does my IDE mark this variable as unused?

Because you don't need the line

$freeSlots = 0; //Unused local variable 'freeSlots'. The value of the variable is overwritten immediately.

As the IDE tells this variable will be overwritten immediately by this line:

$freeSlots = (5 - $startSlot) + ($endSlot - 1);

So the first line is in fact a line that is completely unnecessary and can be deleted. You never use $freeSlots, you're redefining it.

To clarify the scope:

$freeSlots = 0; //Unused local variable 'freeSlots'. The value of the variable is overwritten immediately.

if (strtotime($endDate) === strtotime($startDate))
{
return $endSlot - $startSlot;
}

$freeSlots = (5 - $startSlot) + ($endSlot - 1);
$newTime = strtotime('+1 day', $newTime);

if (date("Y-m-d", $newTime) === $endDate)
{
return $freeSlots;
}

do
{
if (!(date('N', $newTime) >= 6))
{
$freeSlots += 4;
}
$newTime = strtotime('+1 day', $newTime);
} while (date("Y-m-d", $newTime) !== $endDate);

return $freeSlots; //but is clearly used here

The code above does the EXACT same as your code does. You don't need to use the else. If the condition is true, you're returining a value. Else you continue on. No need for the else. And so it's the same scope.



Related Topics



Leave a reply



Submit