dawnbeen / c_formatter_42

C language formatter for 42 norminette
GNU General Public License v3.0
165 stars 17 forks source link

IndexError: list index out of range (preprocessor_directive.py) #46

Closed younesaassila closed 1 year ago

younesaassila commented 1 year ago

Been getting this error since the last update (this is taken from the VS Code console):

Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/mnt/nfs/homes/yaassila/.local/lib/python3.10/site-packages/c_formatter_42/__main__.py", line 63, in <module>
    main()
  File "/mnt/nfs/homes/yaassila/.local/lib/python3.10/site-packages/c_formatter_42/__main__.py", line 43, in main
    print(run_all(content), end="")
  File "/mnt/nfs/homes/yaassila/.local/lib/python3.10/site-packages/c_formatter_42/run.py", line 30, in run_all
    content = preprocessor_directive(content)
  File "/mnt/nfs/homes/yaassila/.local/lib/python3.10/site-packages/c_formatter_42/formatters/preprocessor_directive.py", line 38, in preprocessor_directive
    lastline_index = idented[-1][0]
IndexError: list index out of range

42-c-format: Please read README and check requirements.

File contents:

int main(int argc, char*argv[]){
    return 0;
}

The issue is solved if at least one pre-processor directive is present. Seems like the issue is that indented might be empty, and the code doesn't check for that. So indented[-1] fails.

Would also be nice to add a new test case for pre-processor directives formatter: if no directives are present, do nothing and shouldn't fail

keyhr commented 1 year ago

Fixed with #47.