Wiki

Ticket #279 (assigned enhancement)

Opened 13 years ago

Last modified 13 years ago

Warn about unused method's argument

Reported by: maboiteaspam Owned by: Charles
Priority: trivial Milestone:
Component: Cobra Compiler Version: 0.8.0
Keywords: Cc:

Description

Detect unused method arguments within his body
Define a new arg modifier that is allowing to declare unused parameter

class Example
	def looks_like_a_mistake( param )
		print 'Hello world ! i did not use provided argument'
		# must print a warning

	def looks_like_ok( param as unused String )
		print 'Hello world ! i did not use provided argument'
		# is ok, as arg in declared unused

	def is_also_ok_as_abstract( ) is abstract
		# is ok as method this is an abstract method

	def passed_method_are_also_ok( )
		pass
		# this is ok as there is only one statement, and this is a PassStmt

The warning should read:

The value of parameter "[param.name]" is never used in [.parentBox.name].[.name].
#BackEndObjC/ObjcGenerator.cobra(247): warning: The value of parameter "cw" is never used in SomeClass.SomeMethod.

Concerning current patch
- Provided patch does skip abstract method, empty method, pass'ed method,
- They are some unexpected result when logging operations, i don't really know what it is for now

    : (empty)(1): warning: The value of parameter "value" is never used in IExpr.type.set.
   : (empty)(1): warning: The value of parameter "value" is never used in IExpr.receiverType.set.
   : (empty)(1): warning: The value of parameter "value" is never used in IExpr.contextType.set

- I found what looks like a false/positive in

: Source/Cobra.Lang/CobraFrame.cobra(13): warning: The value of parameter "args" is never used in ['public'] CobraFrame.cue.init.

where code source is like this

namespace Cobra.Lang

	class CobraFrame implements HasAppendNonPropertyKeyValues
	
			....
		var _args as Object[]
			....
		
		cue init(declClassName as String, methodName as String, fileName as String, lineNum as int, args as vari Object)
			"""
			args should have the arg names embedded: "x", x, "y", y
			"""
			....
			_args = sharp'(object[])args.Clone()'
			....

		get args from var
#...

- For now there is 123 parameters detected as unused, they was 300 at beginning.

Attachments

diff-unused-01.patch Download (3.5 KB) - added by maboiteaspam 13 years ago.

Change History

Changed 13 years ago by maboiteaspam

From  http://cobra-language.com/forums/viewtopic.php?f=4&t=801#p4121

==
One more thing: We have a lot of keywords already, so I may tweak your patch to make the word "unused" into what the C# compiler guys call a "virtual keyword", meaning one that it is only considered a keyword in certain circumstances.

Basically, this would be done by removing "unused" from KeywordSpecs? and improving CobraParser?.paramDecl to look for "unused" as an ID (identifier) after AS. ==

- Actual patch is not providing such functionnality for now.

Changed 13 years ago by maboiteaspam

Changed 13 years ago by Charles

  • status changed from new to assigned
  • owner set to Charles

In the future, include test cases with the patch. They are located next to Source in Tests.

Changed 13 years ago by Charles

Bump.

Changed 13 years ago by Charles

It's not clear to me why a method body of pass would exempt the method from unused paramater warnings.

The patch seems to only contain files is Source\ and not in subdirectories such as Cobra.Lang\ nor in ..\Tests\.

There are a lot of new warnings when compiling the standard library such as through:

cobra -ert:yes Misc/hello.cobra

I applied the patch, tweaked the logic and changed usused to a "virtual keyword". However, I commented out the actual check until this can be done properly/completely.

See changeset:2584.

Note: See TracTickets for help on using tickets.