Ask a questionAsk a question
 

AnswerCustom rule to check if we always use the return value

  • Thursday, November 05, 2009 7:59 AMsrodrigu85 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Dear all,
    I'm trying to do a custom code analysis rule that would detect if all methods that returns a certain type use that return value.
    For eg, I've a custom class called "MyPersonalStatusCode".
    Some of my methods return that object:
    public MyPersonalStatusCode MyPublicMethod (){};

    I want to raise a warning if I do:

    // some code
    MyPublicMethod ();

    instead of:
    MyPersonalStatusCode  myreturnValue = MyPublicMethod ();

    I've already managed to find the methods that returns my class, but I dont know how to check if the return value is used or not.
    Any pointers that could lead me to a solution?

    Thank you very much,

Answers

  • Thursday, November 05, 2009 12:57 PMNicole Calinoiu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    There is already a built-in rule (Usage.DoNotIgnoreMethodResults) that checks that method return values that meet certain criteria are used.  If you take a look at the rule implementation in Reflector, you should get a pretty good idea of how to implement your rule.  If you're still not sure, give a shout back here...

All Replies

  • Thursday, November 05, 2009 12:57 PMNicole Calinoiu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    There is already a built-in rule (Usage.DoNotIgnoreMethodResults) that checks that method return values that meet certain criteria are used.  If you take a look at the rule implementation in Reflector, you should get a pretty good idea of how to implement your rule.  If you're still not sure, give a shout back here...
  • Wednesday, November 11, 2009 2:02 AMRoahn LuoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello srodrigu85,

    Have you got an idea on the solution? Code Analysis works on the MSIL code, I think we need to be familiar with the MSIL instructions for the method before checking it. And I agree with Nicole, if you will, you could use Microsoft built-in rules for code analysis (please make sure we checked the rule before running code anaysis in Visual Studio). If you don't mind, I'm going to mark Nicole's reply as answer. Please unmark it if you have anything unclear or questions.

    Thanks
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
  • Wednesday, November 11, 2009 7:40 AMsrodrigu85 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi all,
    yes, actually Nicole did provide me the way for the solution. After reading the reverse engineer code with reflector of the warning CA1806, I could create my custom rule as desired.
    I still think the description of the rule ("CA1806: Do not ignore method results") is wrong. It looks like from the description it checks that all the return values for all the methods are checked. After reading the respective code, it only do check this for 3 types of methods.

    I've created two rules, one for checking my personal return type, another to actually check that ALL the return values are actually checked. 
  • Wednesday, November 11, 2009 12:52 PMNicole Calinoiu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Rules names often do not reflect exactly what a rule does, largely because a more complete description would simply be too long.  The rule documentation should be your reference for what is verified by a rule, not its name.  (Sometimes even the documentation description leaves a bit to be desired, in which case a bit of Reflector spelunking may be necessary.)