1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

Port illumos' shell linter improvements (#353)

This commit ports over two improvements to the shell linter from
illumos (original patch written by Andy Fiddaman). Links to the
relevant bug reports and the original patch:
https://www.illumos.org/issues/13601
https://www.illumos.org/issues/13631
c7b656fc71

The first improvement is to the lint warning for arithmetic
operators in [[ ... ]]. The ksh linter now suggests the correct
equivalent operator to use in ((...)). Example:
  $ ksh -nc '[[ 30 -gt 25 ]]'
  # Original warning
  warning: line 1: -gt within [[ ... ]] obsolete, use ((...))
  # New warning
  warning: line 1: [[ ... -gt ... ]] obsolete, use ((... > ...))

The second improvement pertains to variable expansion in arithmetic
expressions. The ksh linter now suggests referencing variable names
directly:
  $ ksh -nc 'integer foo=40; (($foo < 50 ))'
  # Old warning
  warning: line 1: variable expansion makes arithmetic evaluation less efficient
  # New warning
  warning: line 1: in '(($foo < 50))', using '$' is slower and can introduce rounding errors

src/cmd/ksh93/{data/lexstates,sh/lex,sh/parse}.c:
- Port the improved shell lint warnings from illumos to ksh93u+m.
- The original checks for arithmetic operators involved a bunch of
  if statements with inefficient calls to strcmp(3). These were
  replaced with a more efficient switch statement that avoids
  strcmp.
This commit is contained in:
Johnothan King 2021-11-30 12:26:17 -08:00 committed by Martijn Dekker
parent 370440473e
commit 6904585f49
4 changed files with 32 additions and 4 deletions

View file

@ -1389,7 +1389,32 @@ breakloop:
if(lp->lex.testop2)
{
if(lp->lexd.warn && (c&TEST_ARITH))
errormsg(SH_DICT,ERROR_warn(0),e_lexobsolete4,shp->inlineno,state);
{
char *alt;
switch(c)
{
case TEST_EQ:
alt = "=="; /* '-eq' --> '==' */
break;
case TEST_NE:
alt = "!="; /* '-ne' --> '!=' */
break;
case TEST_LT:
alt = "<"; /* '-lt' --> '<' */
break;
case TEST_GT:
alt = ">"; /* '-gt' --> '>' */
break;
case TEST_LE:
alt = "<="; /* '-le' --> '<=' */
break;
case TEST_GE:
alt = ">="; /* '-ge' --> '>=' */
break;
}
errormsg(SH_DICT, ERROR_warn(0), e_lexobsolete4,
shp->inlineno, state, alt);
}
if(c&TEST_STRCMP)
lp->lex.incase = 1;
else if(c==TEST_REP)