Replies: 2 comments 1 reply
-
Look at what Alternatively, use IOperation callbacks, not syntax node callbacks. |
Beta Was this translation helpful? Give feedback.
1 reply
-
Why are you writing your own analyzer here, and not just using |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
What I'm trying to do
I’m working with a custom Roslyn analyzer in a C# project . The goal is to detect deprecated APIs marked with a custom
[DeprecatedApi]
attribute and flag their usage during development.We are using this to enforce newer API usages across our framework and platform code.
Here is the code for the analyser :
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
namespace Philips.AV.RoslynAnalyzers
{
[DiagnosticAnalyzer("C#", new string[] { })]
public class DeprecatedApiAnalyzerAnalyzer : DiagnosticAnalyzer
{
public const string DiagnosticId = "AV2002";
}
The Problem
Our analyzer fails to detect usage of a deprecated property when it’s accessed via a conditional access expression, like:
if (bitVol?.BitsCount > 0)
In this case, BitsCount is marked with [DeprecatedApi("Use GetBitsCount(token) instead")], but the analyzer doesn’t flag it.
an example code where it is not working is below :
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Serialization;
using GfnServices;
using Philips.AV.Framework.Common;
using Philips.Platform.DataModels;
using Philips.Platform.DataModels.Mathematics;
namespace Philips.AV.Framework.SuiteHosting
{
///
/// The statistics for a finding
///
[DataContract]
public class TissueStatistic
{
///
/// Volume of the given finding
///
[DataMember]
public double VolumeSize { get; set; }
#pragma warning disable PH2097 // Avoid empty statement blocks
#pragma warning restore PH2097 // Avoid empty statement blocks
#pragma warning disable PH2097 // Avoid empty statement blocks
if (peakSULValueCenter != null && peakSULStoredValue != double.MinValue)
{
// double peakSULModalityValue = rescale.ToRealValue(peakSULStoredValue);
// Point3D centerStack = new Point3D(peakSULValueCenter[0], peakSULValueCenter[1], peakSULValueCenter[2]);
// Point3D centerPhys = centerStack * vol.Box.InvTransform;
// if (stats.PeakSULModel != null)
// {
// stats.PeakSULModel.PeakSULCenterPhys = centerPhys;
// stats.PeakSULModel.PeakSULMeanValue = peakSULModalityValue;
// }
// else
// {
// stats.PeakSULModel = new PeakSULModel(centerPhys, peakSULModalityValue);
// }
}
#pragma warning restore PH2097 // Avoid empty statement blocks
stats.MeanValue = meanPixelValue;
stats.StdValue = stdPixelValue;
stats.MaxValue = maxPixelValue;
stats.MinValue = minPixelValue;
stats.Units = units;
#pragma warning disable PH2071
private static void ComputeDiameter(IVolume vol, BitVol bitVol, int meanX, int meanY, int meanZ, int minx, int miny, int minz, int maxx, int maxy, int maxz, bool onTumourDiameterCalculation, out double maxDiameter, out double maxSecondaryDiameter, out Point3D[] maxSecondaryDiameterPos, out double maxPerpendicularDiameter, out Point3D[] maxPerpendicularDiameterPos, out int indexMaxDiameterZ)
{
maxSecondaryDiameterPos = new Point3D[2];
maxPerpendicularDiameterPos = new Point3D[2];
#pragma warning restore PH2071
}
in line 173 if (bitVol?.BitsCount > 0) , BitsCount should have been flaged and and instead a suggestion should have shown saying - use instead GetBitsCount(readLockToken)
Could you tell me a possible fix ? Will be really helpful thankyou .
Beta Was this translation helpful? Give feedback.
All reactions