JDK-8043784 : use (void) instead of unused variable
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 9
  • Priority: P5
  • Status: Closed
  • Resolution: Won't Fix
  • Submitted: 2014-05-22
  • Updated: 2018-05-24
  • Resolved: 2015-02-12
Related Reports
Relates :  
Description
$ ack -A1 "Make an unused local variable to avoid warning" src/
src/share/vm/prims/jvm.cpp
2814:    // Make an unused local variable to avoid warning from gcc 4.x compiler.
2815-    size_t count = ::write(defaultStream::output_fd(), s, (int)strlen(s));

src/share/vm/utilities/ostream.cpp
549:    // Make an unused local variable to avoid warning from gcc 4.x compiler.
550-    size_t count = fwrite(s, 1, len, _file);
--
599:    // Make an unused local variable to avoid warning from gcc 4.x compiler.
600-    size_t count = ::write(_fd, s, (int)len);

Comments
One way, without introducing a variable, is to: if (::write(defaultStream::output_fd(), s, (int)strlen(s)) < 0) { /* do nothing */ } but that maybe more expensive instruction wise, although you would hope a compiler would recognize the empty if statement and optimize the if statement's conditional away. At least it makes it a bit more clearer that the vm is choosing to do nothing if ::write() fails.
04-06-2014

Yeah, unfortunately. In that case, feel free to close this enhancement.
03-06-2014

My understanding is that the functions have to be declared with the warn_unused_result attribute for gcc to complain about an unused result. And google also seems to indicate that the (void) cast trick does not work when these warnings are enabled. Really all potentially failing system calls should be called in a macro that ignores error in product builds and either warns or aborts in debug builds. Of course each call should be examined to see if ignoring errors was actually right in the first place. Or use something like the assert_status macro that is used with pthread calls. It's all very messy.
03-06-2014

Coleen thinks void doesn't work either, so these had to return something to an unused variable.
02-06-2014

Yeah, that's what I thought too but looking at the file after JDK-6781583: http://hg.openjdk.java.net/jdk7/hotspot-rt/hotspot/file/2328d1d3f8cf/src/share/vm/utilities/ostream.cpp#l302 there is an fclose at line 312 and fflush at line 318 which both have a return value but GCC apparently didn't complain about these. Puzzled.
02-06-2014

IIRC gcc only added the warning when you ignored the return value of library calls - and probably only libc library calls at that.
30-05-2014

Looking at JDK-6781583 which introduced the unused variables I wonder why GCC complained about these but not others.
30-05-2014

Well, I see a benefit. First it's plain stupid and second some compilers might complain about unused variables with the right warnings turned on.
30-05-2014

We don't see the benefits of making this change. Closing as WNF
29-05-2014