C# DependencyProperty Flagged by S4275 (Getters and setters should access the expected fields)

Environment:

  • SonarQube 8.3.0
  • C# Code Quality and Security 8.6.1

I have some dependency properties implemented based on Microsoft’s recommendation: https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/how-to-implement-a-dependency-property

These properties are being flagged with S4275. This makes sense based on the rule’s description because the DependecyProperty fields aren’t private and the naming pattern doesn’t match. Is my best option for resolving this issue to use #pragma warning disable S4275 and #pragma warning restore S4275 around the properties?

public static readonly DependencyProperty ToolTipProperty = DependencyProperty.Register("ToolTip", typeof(string), typeof(MyObject), new PropertyMetadata(new PropertyChangedCallback(ToolTipPropertyChanged)));

public string ToolTip
{
    get
    {
        return (string)GetValue(ToolTipProperty);
    }
    set
    {
        SetValue(ToolTipProperty, value);
    }
}

hi @ckdrc, welcome to our community!

You can mark this issue as FP inside SQ. Or as Won’t Fix. And it won’t be counted anymore in the technical debt of the project.

We’ll think if we can avoid to raise this in the rule, as it seems quite a common scenario for WPF programming.

LE: https://github.com/SonarSource/sonar-dotnet/issues/3442

Coming back, @ckdrc, I cannot reproduce the issue.

this is my attempt to reproduce:

    public class Repro3442 : System.Windows.Controls.Primitives.ButtonBase
    {
        public static readonly DependencyProperty IsSpinningProperty = DependencyProperty.Register("IsSpinning", typeof(Boolean),typeof(Repro3442));
        public bool IsSpinning
        {
            get { return (bool)GetValue(IsSpinningProperty); }
            set { SetValue(IsSpinningProperty, value); }
        }
    }

Can you provide more code from your reproducer, please?

This should be a more complete example of what’s triggering the issue for me.

public partial class StatusLight : UserControl
{
	public static readonly DependencyProperty BackgroundColorProperty =
		DependencyProperty.Register(
		"BackgroundColor",
		typeof(Color),
		typeof(StatusLight),
		new PropertyMetadata(Colors.Red, new PropertyChangedCallback(StatusLight.BackgroundColorPropertyChanged)));

	public StatusLight()
	{
		this.InitializeComponent();
		this.backgroundColor.Color = this.BackgroundColor;
	}

	public Color BackgroundColor
	{
		get
		{
			return (Color)GetValue(BackgroundColorProperty);
		}

		set
		{
			this.SetValue(BackgroundColorProperty, value);
		}
	}

	private static void BackgroundColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
	{
		StatusLight led = (StatusLight)d;
		led.backgroundColor.Color = (Color)e.NewValue;
	}
}

Sorry… I cannot seem to repro.

First, GetValue and SetValue should be part of System.Windows.DependencyObject, but System.Windows.Forms. UserControl doesn’t seem to inherit from that class. So where are SetValue and GetValue defined in your example?

Can you please try to make a reproducer project and analyze it on SonarCloud? It’s free for open source projects. Having it on SonarCloud will be a clear reproducer.

I’m not entirely sure where SetValue and GetValue are defined. I was able to reproduce the issue here https://sonarcloud.io/project/issues?id=ckerkhoff_SonarQubeTesting&open=AXNYX2vcN7LHxP31iBZ_&types=BUG

1 Like

thanks!