21
21
22
22
import org .sonar .check .Rule ;
23
23
import org .sonar .plugins .python .api .PythonSubscriptionCheck ;
24
+ import org .sonar .plugins .python .api .quickfix .PythonQuickFix ;
24
25
import org .sonar .plugins .python .api .tree .AssertStatement ;
26
+ import org .sonar .plugins .python .api .tree .Token ;
25
27
import org .sonar .plugins .python .api .tree .Tree ;
26
28
import org .sonar .plugins .python .api .tree .Tuple ;
27
- import org .sonar .plugins .python .api .quickfix .PythonQuickFix ;
28
29
import org .sonar .python .quickfix .TextEditUtils ;
29
30
30
31
@ Rule (key = "S5905" )
@@ -43,17 +44,22 @@ public void initialize(Context context) {
43
44
44
45
var issue = ctx .addIssue (tuple , MESSAGE );
45
46
46
- if (tuple . leftParenthesis () != null && tuple . rightParenthesis () != null ) {
47
- // defensive condition
48
- issue . addQuickFix ( PythonQuickFix .newQuickFix (QUICK_FIX_MESSAGE )
47
+ if (isSingletonTupleWithParenthesis ( tuple ) ) {
48
+ Token comma = tuple . commas (). get ( 0 );
49
+ var quickfixBuilder = PythonQuickFix .newQuickFix (QUICK_FIX_MESSAGE )
49
50
.addTextEdit (TextEditUtils .remove (tuple .leftParenthesis ()))
50
- .addTextEdit (TextEditUtils .remove (tuple .rightParenthesis ()))
51
- .build ());
51
+ .addTextEdit (TextEditUtils .replaceRange (comma , tuple .rightParenthesis (), "" ));
52
+
53
+ issue .addQuickFix (quickfixBuilder .build ());
52
54
}
53
55
}
54
56
});
55
57
}
56
58
59
+ private static boolean isSingletonTupleWithParenthesis (Tuple tuple ) {
60
+ return tuple .leftParenthesis () != null && tuple .rightParenthesis () != null && tuple .elements ().size () == 1 ;
61
+ }
62
+
57
63
@ Override
58
64
public CheckScope scope () {
59
65
return CheckScope .ALL ;
0 commit comments