1

I tried searching through internet , but not able to get anything.

I have a method with a Boolean default parameter , i want to find places where they are specifically sending value other than the default value with a compiler warning.

public example(SomeObject value , IsChecked = false )

How to find places , where they send IsChecked as true without giving find all references.

Cannot specify the whole method as obsolete , Need to find way to specify parameter value as obsolete ( only primitive parameter and static values , not using any run time evaluated variables also ).

Sarav
  • 139
  • 1
  • 13
  • do you try to Ctrl+F? – vasily.sib May 22 '18 at 06:41
  • :| , I want to know other than manual ways . Like to generate compiler warning or any such thing which can force the VS build to identify for me – Sarav May 22 '18 at 06:42
  • 6
    You can change the datatype of parameter, try to recompile. it will show error at all the places where the parameter was passed – kashi_rock May 22 '18 at 06:42
  • Possible duplicate of [Custom Compiler Warnings](https://stackoverflow.com/questions/154109/custom-compiler-warnings) – Zero May 22 '18 at 06:42
  • @Zero thanks for pointing me out on one of possible solution to write code analyzer using Roselyn . I will try that. – Sarav May 22 '18 at 06:45
  • 1
    if you only need to do it once, do what @kashi_rock suggested. Otherwise look into [custom roslyn analyzers](https://github.com/dotnet/roslyn/wiki/Getting-Started-Writing-a-Custom-Analyzer-&-Code-Fix). – Christian.K May 22 '18 at 06:49
  • @Christian.K I will try the roselyn analyzer and update/close the question possibly . Thanks – Sarav May 22 '18 at 06:51
  • @saravanakumarv so, rather than manually finding method usage in code, you prefer write your own code analyzer applicable for only this single task? Are you sure? May be you'll better log something to System.Diagnostics.Debug at runtime? Or add a comment – vasily.sib May 22 '18 at 06:53
  • @vasily.sib i prefer to , so that i can re-use the same for any other such tasks in future. – Sarav May 22 '18 at 06:55
  • Do you also want to find places where they pass in the the value of a variable that also happens to be `true`? This sounds like an X/Y problem, **why** do you want to find these places? If the method is meant to only allow `false`, why not go with the answer below using `ObsoleteAttribute` and create an overload without the parameter? – Lasse V. Karlsen May 22 '18 at 06:55
  • @LasseVågsætherKarlsen using this flag , we do some validations and identified a issue in quite some place it is set to true and lead to data integrity failures . So i need to find places , where they set it without validating beforehand. – Sarav May 22 '18 at 06:59
  • Then it sounds like you still want to send `true` in those cases, in which case a compiler error would be counter-productive. My advice is to go with the obsolete attribute below, change the signature and force everybody over on the new signature after a code review. – Lasse V. Karlsen May 22 '18 at 07:15
  • @saravanakumarv: Mind you that "re-use" is not really a take away here. Detecting the actual situation you want to find is the bulk work you do in an analyzer - all the other stuff (like reporting warnings/errors, etc.) is a given by the framework. Consider only writing an analyzer if you only want to find _this specific_ issue continuously. – Christian.K May 22 '18 at 07:34

1 Answers1

2

You can create a second method without the IsChecked parameter and then mark the old method as obsolete, like this:

[Obsolete("Using IsChecked is obsolete")]
public example(SomeObject value , IsChecked = false )
{
}

public example(SomeObject value )
{
}

then if you use the old method, you'll get a warning that it's obsolete.

Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
  • This will show warning for all the places where it is used , right ? I need only place where they set it as True . The problem is in the production code , they send false for the default param in lot of places . So not able to use the above solution . – Sarav May 22 '18 at 06:53
  • Unfortunately, then you don't have an easy to use solution. You could create an analyzer, or if you're using ReSharper you might be able to use search by pattern to identify the places but you need an analyzer for the compiler error. – Lasse V. Karlsen May 22 '18 at 06:54
  • 2
    I think what you want can't be done. What if they use a variable instead of a constant? There is no way to know what is sent at compile time. – Hans Kilian May 22 '18 at 06:56
  • @LasseVågsætherKarlsen how do i use the resharper pattern search for such finds ? i never used such feature. – Sarav May 22 '18 at 06:56
  • You can write the code using an example of what you want, with placeholders for the parameters, so you could write something like `.Example($expression$, true)` and specify that `$expression$` is an expression of type `SomeObject` or a descendant. You'll find the search function on the ReSharper menu. – Lasse V. Karlsen May 22 '18 at 06:57
  • I would consider specifying in the message what to use instead, that's probably the most important thing when getting an obsolete warning or error message. – Lasse V. Karlsen May 22 '18 at 06:58
  • Thanks, @LasseVågsætherKarlsen :) – Hans Kilian May 22 '18 at 06:58