Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable resharper warnings that are dependent on build type

I'm using belt and suspenders type checking for potential null object problems. Resharper isn't playing nicely though. In a debug build it marks the if (button != null) check as always true and puts a warning marker in the sidebar. In a release build it shades the Debug.Assert as never used code, although at least it's smart enough not to clutter the sidebar this time.

I don't want to disable the always true/false resharper warning globally because it can indicate a problem in the code. At the same time having to clutter my code up with ReSharper disable/restore ConditionIsAlwaysTrueOrFalse comments every time I do a check is ugly.

Is there an option somewhere in ReSharper 5.1 to disable build type contingent behavior so that the if isn't marked in debug builds, without preventing the warning from being shown if an Assert isn't present?

//This should always work unless the columns are fiddled with.
LinkButton button = e.Row.Cells[5].FindControl( "linkButtonName" ) as LinkButton;

//if this isn't the case in a debug build have VS throw an error in the devs face
Debug.Assert(button != null);

//Don't let anything go boom in production if an error isn't caught in dev
if (button != null)
    button.Visible = ( schedule.CreatedBy == Authentification.GetLoggedInUser() );
like image 856
Dan Is Fiddling By Firelight Avatar asked Nov 16 '11 20:11

Dan Is Fiddling By Firelight


1 Answers

Not sure I agree with the design, but given what you want to accomplish try the following.

  1. Install Code Contracts from http://research.microsoft.com/en-us/projects/contracts/
  2. Rewrite your Debug.Asserts to Contract.Assert
  3. Then change your project properties to only check contracts in the debug build.

So it seems your problem is most easily solved by replacing debug.assert with the following:

  //Throw an error only if there is a problem with 
  Contract.Assert(button!=null);

However, I would probably change the design to make the work being done with the link button a method to the following assuming you may have other stuff going on with the link button.

So your code above would be:

    public void MyMethod(EventArgs e)
    {

            var button = e.Row.Cells[5].FindControl("linkButtonName") as LinkButton;
            SetButtonVisibility(button);
    }

    public void SetButtonVisibility(LinkButton button)
    {
        //The button is never null so its a contract
        Contract.Requires<ArgumentNullException>(button != null);

        button.Visible = (schedule.CreatedBy == Authentification.GetLoggedInUser());

    }

Hope that helps.

like image 59
CTierney Avatar answered Sep 18 '22 22:09

CTierney