mjson icon indicating copy to clipboard operation
mjson copied to clipboard

src/mjson.c: unused variable suspected by clang

Open git001 opened this issue 1 year ago • 2 comments

Clang creates a warning about unused variables which was discovered by HAPoxy community https://github.com/haproxy/haproxy/issues/1868

One suggestion is to create a pragma for clang https://github.com/haproxy/haproxy/issues/1868#issuecomment-1250049715
Another option is to add a new option like -D MJSON_DEBUG=1 and change the code accordingly.

Something like the code below instead of this block https://github.com/cesanta/mjson/blob/master/src/mjson.c#L186-L192.

static int plen1(const char *s) {
  int i = 0;
#if MJSON_DEBUG
  int n = 0;
#endif

  while (s[i] != '\0' && s[i] != '.' && s[i] != '[') {
    i += s[i] == '\\' ? 2 : 1;
#if MJSON_DEBUG
    n++;
#endif
  }

#if MJSON_DEBUG
  printf("PLEN1: s: [%s], [%.*s] => %d\n", s, i, s, n);
#endif

  return n;
}

As I would like to create a Pull Request to mjson and then add the new version to HAProxy please let me know what's your preferred way is to solve the clang warning.

git001 avatar Sep 18 '22 10:09 git001

this has raised just few days ago because is was detected by Fedora Rawhide which has switched to clang-15. I guess it is new rule in clang-15

chipitsine avatar Sep 18 '22 16:09 chipitsine

I stumbled also about this issue:

/myMPD/dist/mjson/mjson.c:195:14: error: variable 'n' set but not used [-Werror,-Wunused-but-set-variable]
  int i = 0, n = 0;
             ^
/myMPD/dist/mjson/mjson.c:743:17: error: variable 'n' set but not used [-Werror,-Wunused-but-set-variable]
  int sign = 1, n = 0;

jcorporation avatar Oct 22 '22 21:10 jcorporation