Skip to content

Commit

Permalink
fix: #407
Browse files Browse the repository at this point in the history
  • Loading branch information
terryyin committed Feb 7, 2025
1 parent a0fc857 commit 2229380
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
1 change: 1 addition & 0 deletions lizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ def __init__(self, name, filename, start_line=0, ccn=1):
self.fan_in = 0
self.fan_out = 0
self.general_fan_out = 0
self.max_nesting_depth = 0 # Initialize max_nesting_depth to 0

@property
def name_in_space(self):
Expand Down
113 changes: 112 additions & 1 deletion prompt-history.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,115 @@ test_record_compact_constructor
test_enum_declaration_with_methods
test_local_class_inside_method
test_switch_expression_with_yield
test_pattern_matching_instanceof
test_pattern_matching_instanceof

---------

Got the following issue. Please:

1. find where is the right place to add a new unit test to reproduce the issue
2. add a new unit test
3. run all test to see if the test really fails
4. fix the issue until all test pass
5. clean up
6. run all test at least to confirm things are still working.

-------------
The issue:

I am using lizard in a tool I manage called Statick. When we run lizard we use the -w flag by default, and we are seeing lizard crash. I am seeing the same behavior when running the lizard tool on its own. This behavior does not happen in v1.17.10, but it does occur in 1.17.17. I have narrowed it down to a change between v1.17.14 and v1.17.15, persisting on through v1.17.17.

A minimal example is to use the C file at the bottom of this post as input. Save it as test.c. Then run

lizard test.c
That runs fine. Now add the -w flag. This crashes with Traceback

$ lizard --version
1.17.17
$ lizard -w test.c
Traceback (most recent call last):
File "/home/thomas/.local/bin/lizard", line 8, in <module>
sys.exit(main())
File "/home/thomas/.local/lib/python3.10/site-packages/lizard.py", line 1079, in main
warning_count = printer(result, options, schema, AllResult)
File "/home/thomas/.local/lib/python3.10/site-packages/lizard.py", line 855, in print_clang_style_warning
print(scheme.clang_warning_format().format(f=warning))
AttributeError: 'FunctionInfo' object has no attribute 'max_nesting_depth'
In v1.17.14 there is no problem and I see the warning.

$ lizard --version
1.17.14
$ lizard -w test.c
test.c:2: warning: func has 52 NLOC, 16 CCN, 143 token, 0 PARAM, 69 length
The test.c file I am using is

#include <stdlib.h>
void func()
{
int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o;

if (a)
{
// Do nothing.
}
else if (b)
{
// Do nothing.
}
else if (c)
{
// Do nothing.
}
else if (d)
{
// Do nothing.
}
else if (e)
{
// Do nothing.
}
else if (f)
{
// Do nothing.
}
else if (g)
{
// Do nothing.
}
else if (h)
{
// Do nothing.
}
else if (i)
{
// Do nothing.
}
else if (j)
{
// Do nothing.
}
else if (k)
{
// Do nothing.
}
else if (l)
{
// Do nothing.
}
else if (m)
{
// Do nothing.
}
else if (n)
{
// Do nothing.
}
else if (o)
{
// Do nothing.
}
else
{
// Do nothing.
}
}
9 changes: 9 additions & 0 deletions test/testOutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ def test_sort_warning_with_generator(self):
self.option.sorting = ['cyclomatic_complexity']
print_warnings(self.option, self.scheme, (x for x in []))

def test_warning_when_max_nesting_depth_missing(self):
self.foo.cyclomatic_complexity = 30
# Intentionally not setting max_nesting_depth
fileSummary = FileInformation("FILENAME", 123, [self.foo])
scheme = OutputScheme([Ext()])
count = print_clang_style_warning([fileSummary], self.option, scheme, None)
self.assertIn("FILENAME:100: warning: foo has 1 NLOC, 30 CCN, 1 token, 0 PARAM, 1 length", sys.stdout.stream)
self.assertEqual(1, count)


class TestFileInformationOutput(StreamStdoutTestCase):

Expand Down

0 comments on commit 2229380

Please sign in to comment.