plutoo / protobuf-csharp-port

Automatically exported from code.google.com/p/protobuf-csharp-port
Other
0 stars 0 forks source link

warning CS0219: The variable `size' is assigned but its value is never used #84

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
In generated code, a variable is created that is never used. This gives a 
warning.

public override void WriteTo(pb::ICodedOutputStream output) {
      int size = SerializedSize;

Original issue reported on code.google.com by garrynewman@gmail.com on 11 Apr 2014 at 10:17

GoogleCodeExporter commented 9 years ago
Interesting. There's a comment in MessageGenerator (just before we write that 
line):
"// Make sure we've computed the serialized length, so that packed fields are 
generated correctly."

Will need to investigate more closely. Aside from anything else, we could 
either suppress the warning or create a method to return the serialized size as 
well as just the property. (The reason for the variable is that a property 
access on its own isn't a valid statement.)

Original comment by jonathan.skeet on 11 Apr 2014 at 5:22

GoogleCodeExporter commented 9 years ago
This is indeed a required line/operation.  We should suppress this manually by 
adding 168 (219?) to the generated line:

#pragma warning disable 1591, 0612

Another possibility is to change it's usage from: 
      int size = SerializedSize;
to something like:
      if (SerializedSize < 0) { throw new ....Exception() }

In the mean time, you can add 168 to project's list of ignored warnings if you 
are seeing this warning.  I'm surprised you see this as the '#region Designer 
generated code' should disable warnings within the region.

What version of Visual Studio/compiler are you using?  As I do not see CS0219, 
but rather CS0168.

Original comment by Grig...@gmail.com on 15 Sep 2014 at 8:21

GoogleCodeExporter commented 9 years ago
Fixed by moving the logic of the SerializedSize getter into a new private 
method CalcSerializedSize().  This avoids the use of the fetching the property 
into an unused variable generating the error above.

Original comment by Grig...@gmail.com on 7 Feb 2015 at 9:36