Skip to content

Formatter removes semicolon after variable initializer at class level (C#) #1333

Closed

Description

@mihaipopescu
namespace Unity
{
    public class Class
    {
        // doesn't work because ; gets removed but
        public static readonly Class A = new Class() { name = "A", id = 1 };
        // works and ; doesn't get removed
        public static readonly Class B = new Class { name = "B", id = 2 };
    }
}

Expected versus output diff:

diff --git a/Users/mihai/Development/external/uncrustify/tests/output/staging/60016-UNI-11662.cs b/Users/mihai/Development/external/uncrustify/tests/results/staging/60016-UNI-11662.cs
index 9a49fdb..9288ee8 100644
--- a/Users/mihai/Development/external/uncrustify/tests/output/staging/60016-UNI-11662.cs
+++ b/Users/mihai/Development/external/uncrustify/tests/results/staging/60016-UNI-11662.cs
@@ -1,10 +1,10 @@
 namespace Unity
 {
     public class Class
     {
         // doesn't work because ; gets removed but
-        public static readonly Class A = new Class() { name = "A", id = 1 };
+        public static readonly Class A = new Class() { name = "A", id = 1 }
         // works and ; doesn't get removed
         public static readonly Class B = new Class { name = "B", id = 2 };
     }
 }

Debug support files:

60016-UNI-11662.cs
60016-UNI-11662.cs.log
60016-UNI-11662.cs.unc
Uncrustify.CSharp.cfg
UNI-11662.cs

Disclaimer:

Note: This is a generated issue from our internal issue tracker.
Expect a dry description which could be insufficient to properly understand the issue.
On the other hand, the support files should be sufficient to easily reproduce and debug the issue,
but please comment if it's still unclear and I will get back with clarifications.
See main discussion thread here #1223.

Activity

  1. mihaipopescu commented on Aug 31, 2017

    @mihaipopescu
    CollaboratorAuthor

    This is high-priority on our side as it's actually breaking the code.

  2. self-assigned this
    on Aug 31, 2017
  3. mihaipopescu commented on Sep 1, 2017

    @mihaipopescu
    CollaboratorAuthor

    As probably you already may have noticed the responsible option is mod_remove_extra_semicolon.

  4. guy-maurel commented on Sep 1, 2017

    @guy-maurel
    Contributor

    I can reproduce...

  5. guy-maurel commented on Sep 10, 2017

    @guy-maurel
    Contributor

    The remove is caused by the option: mod_remove_extra_semicolon = true.
    Using the command option -p, one can see:

     FUNC_CLASS_DEF[                        NONE][ 46/ 46/ 51/  1]    Class   
           FPAREN_OPEN[  FUNC_CLASS_DEF][ 51/ 51/ 52/  0]             (    
         FPAREN_CLOSE[  FUNC_CLASS_DEF][ 52/ 52/ 53/  0]               )
    

    I don't have any compiler for C#. Sorry. I don't have experience about C#.
    Is the classification as FUNC_CLASS_DEF correct?
    Could you modify the source to a cpp, and complete it to be compilable?

  6. mihaipopescu commented on Sep 12, 2017

    @mihaipopescu
    CollaboratorAuthor

    Definitely that's not a class function definition but an initialized static member.

    It's kind of hard to try to reproduce the same behavior in C++ but we did saw it in something similar in our C++/CLI code as well but it might be a totally different issue:

    IIterable<int>^ Class::Foo(int first, int second)
    {
    	auto array = ref new Platform::Array<int>
    	{
    		first,
    		second
    	};
    	return ref new Iterable<int>(array);
    }

    The semicolon after return statement was removed.

  7. guy-maurel commented on Sep 12, 2017

    @guy-maurel
    Contributor

    Could you push a more complete source, which could be compiled?

  8. mihaipopescu commented on Oct 2, 2017

    @mihaipopescu
    CollaboratorAuthor

    Why do you need it to be compilable ? The snippets are syntactically correct and uncrustify should be able to format them just like that.

  9. guy-maurel commented on Oct 2, 2017

    @guy-maurel
    Contributor

    I don't need!
    I wish it!
    Using the clang compiler I can see what it does.

  10. changed the title [-]Formatter removes semicolon after variable initializer at class level[/-] [+]Formatter removes semicolon after variable initializer at class level (C#)[/+] on Oct 3, 2017
  11. mihaipopescu commented on Oct 3, 2017

    @mihaipopescu
    CollaboratorAuthor

    I said.. it's very hard to try to come up with a C++ equivalent as this is a C# bug (the tag you put is wrong).

    You can see the correct tokenization if you look at the second line where the semicolon is not removed.

  12. removed their assignment
    on Oct 3, 2017
  13. DieHertz commented on Oct 6, 2017

    @DieHertz

    For C++ return {}; transforms into return {}
    Using a binary distribution from Ubuntu 16.04.

  14. gmaurel commented on Jan 24, 2018

    @gmaurel
    Collaborator

    Fixed at e8dd4d0, Dec 31 12:38:29 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions