42School / norminette

Official 42 norminette
MIT License
893 stars 132 forks source link

Unrecognized return #435

Open andreyvdl opened 10 months ago

andreyvdl commented 10 months ago

Describe the bug Can't recognize return line on a simple main.

Erroneous code

#include <stdio.h>

int     main(void)
{
        struct
        {
                int x;
        }       test;

        test.x = 42;
        printf("%d\n", test.x);
        return (0);
}

Additional infos

Additional context I was testing if the norm detects anonymous structs as norm error, but it can't recognize the return line. image

NiumXp commented 10 months ago

This is a known bug, you can see the same behavior in #322 (closed), IsDeclaration is greedy when """parsing""" the tokens.

If you use -dd flag, you can see what I'm saying about IsDeclaration:

[...]
test.c - IsDeclaration In "Function" from "GlobalScope" line 5":
                <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <STRUCT> <NEWLINE>
                <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <LBRACE> <NEWLINE>
                <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <INT> <SPACE> <IDENTIFIER=x> <SEMI_COLON> <NEWLINE>
test.c - IsBlockEnd In "Function" from "GlobalScope" line 8":
                <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <RBRACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> 
test.c - IsVarDeclaration In "GlobalScope" from "None" line 8":
                <IDENTIFIER=test> <SEMI_COLON> 
[An empty line in line 8? wat]
test.c - IsEmptyLine In "GlobalScope" from "None" line 8":
                <NEWLINE>
test.c - IsEmptyLine In "GlobalScope" from "None" line 9":
                <NEWLINE>
test.c - IsAssignation In "GlobalScope" from "None" line 10":
                <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <SPACE> <IDENTIFIER=test> <DOT> <IDENTIFIER=x> <SPACE> <ASSIGN> <SPACE> <CONSTANT=42> <SEMI_COLON> <NEWLINE>
[...]

Note that the struct you wrote is represented by IsDeclaration, IsBlockEnd and IsVarDeclaration. When norminette sees an IsBlockEnd, it unwraps the scope, this means that after declaring the struct, norminette understands it to be in the global scope and because of this, IsExpressionStatement (return) is not reached (since returns can only appears in a Function or a ControlStructure scope), resulting in the error.

When I first saw this bug (in the issue I mentioned), I noticed another one regarding scopes (softly rewriting the fragmented CheckSpacing), and looking at some project repositories like minishell and web server, I noticed that these two bugs (note that when trying to solve one of them we would probably be forced by the code to solve the other together) cannot be fixed without breaking several tests and projects already written.

matthieu42Network commented 10 months ago

Hello, after some discussion with the pedago, with thought it should not be allowed to declare struct inside function. And in the PDF it is mentioned that: You cannot declare a structure in a .c file.

andreyvdl commented 10 months ago

if we should not declare a struct in .c files, then this is also wrong:

struct s_test {
    int a;
};

int main(void)
{
    struct s_test   test;

    test.a = 42;
    return (test.a);
}

I tested this on the campus PC so i will repass the specs

OS: Ubuntu 20.04.5 LTS python3 --version: Python 3.8.10 norminette -v: norminette 3.3.52

but the norminette doesn't detect: Screenshot from 2023-09-19 15-18-26

matthieu42Network commented 10 months ago

Seems like this rules were never implemented in the norminette :(