Warning Level

This is a discussion on Warning Level within the C# Programming forums, part of the General Programming Boards category; I had this code Code: string name = "ABCDEF"; name.Replace("A", "B"); which does nothing. The compiler thought doesn't gave me ...

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    Warning Level

    I had this code

    Code:
    string name = "ABCDEF";
    name.Replace("A", "B");
    which does nothing. The compiler thought doesn't gave me a warning, something equivalent to the "statement with no effect" you get from GCC.
    The warning level is at maximum.

    Is there another way to set this on? Is there a reason that the compiler doesn't detect that the statement does nothing?

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by C_ntua View Post
    Is there a reason that the compiler doesn't detect that the statement does nothing?
    Just taking a swing at it, I'd guess it's because .Net doesn't support method-level const. Because you have no way to tell the compiler that a given method does not change the object's state, it can't warn you.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, you can tell it by just telling it
    I mean, why not have a keyword that specifies that a function doesn't have side effects so you can enable the compiler to give warnings and perform optimizations? Even if you don't make it "public", at least it should exist for the .NET library itself. I mean it tells you about other less important things, like the method is obsolete. You can mark it as not having side-effects which is more important.
    a) It helps you avoid mistakes
    b) It can make optimizations
    c) It stacks really well with immutable strings since these kind of mistakes are very common and expected to be. Since C# will use the .NET library heavily which will be using string heavily it kind of makes sense.

    I don't know, it just can of doesn't make sense for me.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    I agree. I was a bit disappointed that I wasn't able to use method-level const in C#.

    The compiler tells you about obsoleteness with an attribute. I don't know much about attributes, but there may be a way to check for method-level const in your own code using attributes.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,475
    Nah. There's nothing to warn about. The return value is simply being discarded. This follows the same behavior in C++.

    Code:
    struct foo {
    	foo() { val = 0; }
    	int val;
    };
    
    foo bar() {
    	foo f;
    	return f;
    }
    
    int main() {
    	bar();   // NO WARNING
    	return 0;
    }
    EDIT: After rereading your initial post, a few clarifications:

    which does nothing. The compiler thought doesn't gave me a warning, something equivalent to the "statement with no effect" you get from GCC.
    The statement does something. Internally it creates a copy of your string and performs the replacement operation. It then returns the new string. It just so happens your are not assigning that. There's no chance this could be ever reported as "statement with no effect". I think your confusion stems from your misunderstanding of that GCC warning message. I'm a little rough around the edges regarding GCC. But here it is:

    That particular message you mention, I think, exists in a different context than that of your code. It's controlled by -Wunused-value. This is different than the much more appropriate -Wunused-result. This last one indeed reports when the caller of a function does not use its return value, which is precisely what is happening with your code above.

    Personally, I find -Wunused-result a double-edged blade. I'm not a fan of this type of reporting. Preferably I'd leave it off by default after a -Wall. The problem is that, it is common for functions to return control flags that may or may not be used by the caller, depending on the function usage. I do define such functions occasionally and I'm never happy about adding the __attribute__ keyword to avoid the reporting. Personal Rule Nº 5: My code should reflect the business logic and I shall refrain from introducing compiler gadgetry.

    But I guess you can always argue they should at least give you the option (after all that's what GCC does; it gives us the option). Fair enough. But it's probably a little more complicated that that. C# and the .Net introduce Reflection. With it, it is virtually impossible to provide something in C# equivalent to -Wunused (-Wunused-function, -Wunused-variable, probably even -Wunused-value and -Wunused-parameter) since types usage can be hidden behind the encapsulated content. But -Wunused-result would be possible, no doubt. But not having anything else to keep it company, this would become a one item warning class. Probably not an high priority because of that.
    Last edited by Mario F.; 07-12-2010 at 10:28 PM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Yes, because you don't mark the function in any way. Consider this
    Code:
    #include <math.h>
    
    double bar(int a) {
    	return pow(a,a);
    }
    
    int main() {
    	bar(10);              // NO WARNING
    	pow(10,10);      //WARNING
    	return 0;
    }
    compiled in G++ or GCC. It seems that pow() is marked to give a warning. How, I don't know. I believe because they are inlined, so the compiler ends up with something like "result;" which does nothing.

    Whatever policy GCC/G++ uses, C# uses a different one, since you can mark functions with attributes and give warning like a method being obsolete. It just seems strange having that kind of attribute and not something for strings where you know that users will often mistake them.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,475
    pow() is probably declared as:

    Code:
    double pow (double, int) __attribute__ ((warn_unused_result));
    That's the only way in GCC you can get such a warning (and -Wunused-result must be set, which is by default). Your function, on the other hand has no such attribute set. So, no warning. If you add that attribute to your function, you'll get the warning.
    Last edited by Mario F.; 07-13-2010 at 07:09 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21