Bug 10351 - New Code Analysis rule to extract a group of similarly named integer-based constants into an enum
Summary: New Code Analysis rule to extract a group of similarly named integer-based co...
Status: RESOLVED NOT_ON_ROADMAP
Alias: None
Product: Xamarin Studio
Classification: Desktop
Component: Text Editor ()
Version: 4.0
Hardware: PC Windows
: Normal enhancement
Target Milestone: ---
Assignee: Mike Krüger
URL:
Depends on:
Blocks:
 
Reported: 2013-02-17 10:01 UTC by Jeffrey Stedfast
Modified: 2017-09-26 12:44 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NOT_ON_ROADMAP

Description Jeffrey Stedfast 2013-02-17 10:01:56 UTC
If you have something like:

public class MyClass {

    public const int FONT_WEIGHT_NORMAL = 0;
    public const int FONT_WEIGHT_SEMIBOLD = 1;
    public const int FONT_WEIGHT_BOLD = 2;

    ...
}

It would be really handy if Code Analysis offered to split those constants out into their own enum:

public enum FontWeight : int {
    Normal = 0,
    Semibold = 1,
    Bold = 2
}

(would be great if this worked for all integer primitives)

... and updated code using FONT_WEIGHT_* to use FontWeight.*


As an addendum to this, perhaps if it is a static class and contained nothing but those integer constants, perhaps just offer to replace the class with an enum of the same name.


I've been hacking on a project that was auto-converted from Java into C# and I find myself doing this type of refactoring a LOT.
Comment 1 Mike Krüger 2013-02-18 02:04:15 UTC
btw. when you do that a lot  - wouldn't it have been easier to write the refactoring instead ? :)
Comment 2 Jeffrey Stedfast 2013-02-18 06:48:57 UTC
I was pretty close to doing that ;-)
Comment 3 Luís Reis 2013-06-30 20:50:08 UTC
I've attempted to fix this with https://github.com/icsharpcode/NRefactory/pull/205

Note that my version will convert FONT_WEIGHT_* to ((int) FONT_WEIGHT.*):
It won't fix the name (but that part has an easy workaround because MonoDevelop already has a 'Rename' action) and it adds a type cast.
Comment 4 Jeffrey Stedfast 2013-07-01 08:49:44 UTC
awesome!
Comment 5 Marius Ungureanu 2017-09-26 12:44:07 UTC
This bug no longer applies to the product. This feature request should be upstreamed to Roslyn, as we take refactorings/issues from Roslyn now.