Bug 10731 - Auto-Fix regarding method declaration: "Convert to <BaseType>"
Summary: Auto-Fix regarding method declaration: "Convert to <BaseType>"
Status: RESOLVED FIXED
Alias: None
Product: Xamarin Studio
Classification: Desktop
Component: C# Binding ()
Version: 4.0
Hardware: PC Linux
: Normal enhancement
Target Milestone: ---
Assignee: Mike Krüger
URL:
Depends on:
Blocks:
 
Reported: 2013-02-27 08:12 UTC by Ciprian Khlud
Modified: 2013-05-02 05:55 UTC (History)
1 user (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 FIXED

Description Ciprian Khlud 2013-02-27 08:12:54 UTC
Description of Problem:
Auto-Fix should hint some fixes:

Steps to reproduce the problem:
1. Create a console application
2. Paste this code as starting point:
using System;

namespace TestConsole
{
    class MainClass
    {
         public bool AreAllNumbersPrime (List<int> items)
         {
             bool result = true;

             foreach (var item in items) {
                if (item < 4)
                     continue;
                if(item%2==0)
                 {
                     result = false;
                     break;
                 }
                for (var i = 3; i < item / 2; i += 2)
                     if (item % i == 0)
                     {
                     result = false;
                     break;
                      }
            }

                        Console.WriteLine(result?"All numers are prime":"Some
numbers are not prime");
            return result;
        }

        public static void Main (string[] args)
        {
            var items = new List<int>{2,3,5,7,11,13,17,19,23,29,37};
            var main = new MainClass ();
            var result = main.AreAllNumbersPrime (items);

        }
    }
}
3. Set the cursor to the word: IList; for line: public bool AreAllNumbersPrime (List<int> items) 
4. Press Alt+Enter to show quick fixes
5. Select the first option

Expected result:
 It would be great to notice that is used just IEnumerable part of IList, so it
can be changed to IEnumerable<int> (foreach is using the IEnumerable
interface). Making your code "IEnumerable aware" makes it very easily later to
be mixed with Linq (Linq generates IEnumerable by default)
- - "Convert to IEnumerable": all variables inside AreAllNumbersPrime are not
using this pointer, so make it static would make sense (static code gives some
warranties in code too, so is better to have as much code as possible as
static).

Actual result:
Nothing appears
Comment 1 Ciprian Khlud 2013-02-27 08:22:38 UTC
Sorry, in the original bug and here too I copy/pasted something about static method. Is just about using base class only.
There is a note that Linq uses IEnumerable by default so this hinting will simplify in many cases working with Linq. 
So (maybe as a separate bug), using arrays like int[] as parameters, can be checked if are IEnumerable and suggest to use IEnumerable<int> instead of int[].
Comment 2 Ciprian Khlud 2013-04-16 16:44:50 UTC
CanBeDemoted issue doesn't work against generic types and the logic is a bit branchy, so I made a separate logic for IEnumerable/ICollection that works better in most cases.

https://github.com/ciplogic/NRefactory/commit/0d391896fb82c0c466552f48d495878a3ced4055
Comment 3 Mike Krüger 2013-05-02 05:55:25 UTC
Thanks for the fix