Support for StyleCop rule SA1101 (add "this.")?

Hi,

I am currently evaluating R# and noticed that even though the rule to always add "this." for access to items being a member of the class (SA1101) seems rather simple, support in R# does apparently not exist. It doesn't offer an auto-fix and especially is not able to add "this." in code cleanups. Can this possibly be added? Would really be an additional time-saver.

Regards,
Florian

---
Original message URL: http://devnet.jetbrains.net/message/5484229#5484229

0
11 comments

Just noticed this also applies to SA1126.

0

Hello Florian,

Could you please describe if you are using standalone ReSharper, or do you experience this issue with StyleCop plugin for ReSharper as well?

Thank you!

0

Hi,

I am using the StyleCop plugin. I already reported this suggestion here (or rather, added to the discussion) https://stylecop.codeplex.com/discussions/358010.

According to the developer, this is not safely possible, but he also failed to provide a sample proving this statement to this date :(

Regards,
Florian

0

Well, I beleive this issue is not addressed to ReSharper, since it is a StyleCop rule and its feature.
Meanwhile, I'll try to ask some of my collegues for a good example when this use case can be dangerous.

Thanks.

0

Florian,

We've come up to an example, where local variable Foo and private filed Foo are used in the code. By default, if you use 'this.a', private filed is used, while simply calling 'a' is going to use local variable insted.
Hope this helps.

0

No, this sample does not apply. Consider this simple class:

public partial class EditDialog : Form
{
    public EditDialog()
    {
        InitializeComponent();
        AcceptButton = bOK;
        CancelButton = bCancel;
    }

    public EditDialog(Button AcceptButton)
    {
        InitializeComponent();
        AcceptButton = bOK;
        CancelButton = bCancel;
    }
}


In the first case, StyleCop complains about a missing "this" (SA1126) for the AcceptButton property. Using or omitting "this" here does not make a code difference.

In the second case, using or omitting "this" would make a huge difference, but StyleCop does in fact not even complain, because it refers to the local parameter which does not require "this". So whenever there is a local variable with the same name, StyleCop is not involved anyway.

0

I'm not sure about StyleCop since we are developing ReSharper, and StyleCop is a 3rd-party extension...

Consider the following example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication4
{
  class Program
  {
    private int a = 15;
    static void Main(string[] args)
    {
      var p = new Program();
      p.Foo();
    }

    private void Foo()
    {
      var a = 3;
      Console.WriteLine(a);
      Console.ReadKey();
    }
  }
}



In this case, using this qualifier on 'a' symbol will show different results (depending on the usage of either field a or local var a).

Thanks.
0

Yes, it makes a difference, but StyleCop won't complain because this code does not violate the SA1126 rule.

I'm not sure where the responsibilities in regard to this feature are. I suppose the StyleCop developer needs to implement this, but so far does not agree to my complaint.

0

After reading a similar discussion about the issue, it seems like StyleCop does not have enough information about the code to determine whether or not a static method has been called, and thus cannot decide whether to fix the issue with "this" or a class name. This could apply for example if Equals() is called, which could either be the static "bool Equals(object objA, object objB)" or non-static method "Equals(object obj)".

So it would be up to Resharper to implement this, since it has more knowledge about the code. So would it be possible to add a code-cleanup option that simply adds all "this." prefixes whereever they are missing?

0

Hi Florian,

I've talked to the corresponding developer, and he ensured me that this kind of analysis is very specific and not really neccesary in most scenarios, so we are not planning to implement it in ReSharper itself. However, it can be done via ReSharper plugin (we have all needed APIs for it), so basically it is a feature request for a StyleCop.

Thanks.

0

OK, thanks for clarification.

0

Please sign in to leave a comment.