How to find tasks without effect?

In the process of automatically renaming many variables in a large project, I may have created many of these things:

class Foo {
    int Par;
    void Bar(int Par) {
        Par = Par;       // Nonsense
    }
};

Now I need to identify these locations in order to fix them. For instance. in "this-> Par = Par;". Unfortunately, the Visual C ++ Compiler does not give me any comments about this even with all the warnings. I remember once there was a warning about this. He said that “Code has no effect” or something else. But this seems to be possible because some people have used this practice to avoid the "unreferenced parameter" warnings. Is there a way to activate this warning? Does GCC warn? Any idea?

+5
source share
5 answers

Several compilers can generate warnings about this:

  • GCC and Clang can warn about this if you add a parameter -Wshadow. (In particular, although they do not warn of meaningless assignment, they warn of a local variable Parthat obscures the member variable Par— you may or may not like this.)
  • Embarcadero C ++ Builder does not warn that it is Par = Paruseless, but it can warn that it is Parnot used after its appointment, which should meet your needs.

I suspect that a tool like PC-Lint can also identify such code.

Another solution is to mark your parameters as const:

class Foo {
    int Par;
    void Bar(const int Par) {
        Par = Par;       // Compiler error!
    }
};

const pass-by-value , .cpp, .h. , :

// foo.h
class Foo {
    int Par;
    void Bar(int Par);
};

// foo.cpp
void Foo::Bar(const int Par) { ... }
+6

kaptnole, . :

^ [\ s\] * ([A-Za-Z_0-9]) [\ s\] = [\ s\]\1 [\ s\]; [\ s\] * $

, :
http://msdn.microsoft.com/en-us/library/2k3te2cs%28VS.80%29.aspx
... ( Perl!).


perl :

perl -n -e'/^[\s\t]*([a-zA-Z_0-9]*)[\s\t]*=[\s\t]*\1[\s\t]*;[\s\t]*$/&&print "$. $_"' test_me && echo

, , :

hi = hi; 
hi= hi ;
  hi=hi   ;

....

xxxxx@yyyy% perl -n -e'/[\s\t]*([a-zA-Z_0-9]*)[\s\t]*=[\s\t]*\1[\s\t]*;[\s\t]*$/&&print "$. $_"' test_me && echo
1 hi = hi; 
2 hi= hi ;
3   hi=hi   ;
xxxxx@yyyy%

, Awk, , , Awk !: (

, Perl script ... !


1

, main "-pedantic" "-Wall" gcc g++ ... .

:

int main (int argc, char *argv[]) {
  int bob=5;
  bob=bob;
  return 0;
}

2

, perl script , . , ( ).

, "-Wshadow" gcc/g++ .

const . , , const

.

void hello_world_print_numbers(int number_1, int number_2, int number_3) {
   ...
}

- , , :

void hello_world_print_numbers(const int number_1, const int number_2, const int number_3) {
   ...
}

... , ( , !).

void hello_world_print_numbers(const int * number_1, const int * number_2, const int * number_3) {
   ...
}

3

^ . , my_class->name=name;. RC. . RC!

+4

, , - ( "m_" - ). , .

+1

, , , , . ## ##

Foo.h :

class Foo {
    int Par;
    void Bar(int Par) {
        Par = Par;       // Nonsense
        Par=Par;       // Nonsense
        Par  =  Par;       // Nonsense
        Par  =  Par   ;       // Nonsense

        this->x = x;  // Do not match this
    }
};

Perl Script, match.pl

#!/usr/bin/perl
use strict;

my $filename = $ARGV[0];
open(my $F, $ARGV[0]) || die("Cannot open file: $filename");
print "Procesing File: $filename\n";

my $lineNum = 0;
while (<$F>)
{
    $lineNum++;

    chomp;
    my $line = $_; 
    if ($line =~ /(?:^|\s+)(\w+?)\s*=\s*\1\s*;/)
    {
        print "\t$filename:$lineNum: $line\n";
    }
}

.

%> ./match.pl Foo.h 
    Procesing File: Foo.h
    Foo.h:4:         Par = Par;       // Nonsense
    Foo.h:5:         Par=Par;       // Nonsense
    Foo.h:6:         Par  =  Par;       // Nonsense
    Foo.h:7:         Par  =  Par   ;       // Nonsense

Linux ( , Windows ):

%> find *.cpp *.h -exec ./match.pl {} \; 
Procesing File: test.cpp
Procesing File: test2.cpp
Procesing File: test3.cpp
Procesing File: Foo.h
    Foo.h:4:         Par = Par;       // Nonsense
    Foo.h:5:         Par=Par;       // Nonsense
    Foo.h:6:         Par  =  Par;       // Nonsense
    Foo.h:7:         Par  =  Par   ;       // Nonsense
+1

I am thinking of writing a script to browse files and detect strings containing a pattern

exp=exp;

ignoring all spaces in the line.

0
source

All Articles