This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/87850] New: Add fix-it hint for "invalid conversion from 'X' to 'X*'"


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87850

            Bug ID: 87850
           Summary: Add fix-it hint for "invalid conversion from 'X' to
                    'X*'"
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: enhancement
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
                CC: dmalcolm at gcc dot gnu.org
  Target Milestone: ---

This fairly common mistake seems like an obvious candidate for a fix-it hint:

#include <pthread.h>
int main()
{
  pthread_key_t key;
  pthread_key_create(key, NULL);
}

Currently G++ says:

p.c: In function 'int main()':
p.c:5:22: error: invalid conversion from 'pthread_key_t' {aka 'unsigned int'}
to 'pthread_key_t*' {aka 'unsigned int*'} [-fpermissive]
    5 |   pthread_key_create(key, NULL);
      |                      ^~~
      |                      |
      |                      pthread_key_t {aka unsigned int}
In file included from p.c:1:
/usr/include/pthread.h:1112:47: note:   initializing argument 1 of 'int
pthread_key_create(pthread_key_t*, void (*)(void*))'
 1112 | extern int pthread_key_create (pthread_key_t *__key,
      |                                ~~~~~~~~~~~~~~~^~~~~


The obvious fix is to take the address of the variable i.e. &key

This should probably only be suggested when the argument is an lvalue
(otherwise you can't take its address) and when taking its address would give a
pointer of the correct type (so don't suggest taking the address of a const int
if the parameter wants a non-const int*).

Simplest form:

int foo(int*);

int main()
{
  int i = 0;
  foo(i);
}

fixit.cc: In function 'int main()':
fixit.cc:6:7: error: invalid conversion from 'int' to 'int*' [-fpermissive]
    6 |   foo(i);
      |       ^
      |       |
      |       int
fixit.cc:1:9: note:   initializing argument 1 of 'int foo(int*)'
    1 | int foo(int*);
      |         ^~~~

The fix-it would suggest inserting '&' before the parameter.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]