Recent

Author Topic: [solved] unused type declarations  (Read 1957 times)

speter

  • Hero Member
  • *****
  • Posts: 528
[solved] unused type declarations
« on: August 05, 2023, 02:08:22 am »
Is it possible to get a report (or compiler hints) on unused type declarations - like the compiler will mention local variables that are unused?

cheers
S.
« Last Edit: August 05, 2023, 06:57:44 am by speter »
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Thaddy

  • Hero Member
  • *****
  • Posts: 19143
  • Glad to be alive.
Re: unused type declarations
« Reply #1 on: August 05, 2023, 05:51:22 am »
No. It is also not necessary, because types that are not used are also not used by the compiler.
I assume you are under the impression that it leads to smaller code: it does not, it is simply omitted.
Someone else may use a type that you do not use, so never remove type declarations unless you are sure ithey no longer serve a purpose.
« Last Edit: August 05, 2023, 05:55:29 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

speter

  • Hero Member
  • *****
  • Posts: 528
Re: unused type declarations
« Reply #2 on: August 05, 2023, 06:57:19 am »
Thanks for the reply Thaddy.

I am currently working on some very old code and (simply) wanted to "clean it up" a bit by removing ancient unused stuff. :)

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

440bx

  • Hero Member
  • *****
  • Posts: 6480
Re: unused type declarations
« Reply #3 on: August 05, 2023, 07:38:11 am »
I am currently working on some very old code and (simply) wanted to "clean it up" a bit by removing ancient unused stuff. :)
For clean up purposes maybe just commenting out the data type(s) (preferably one by one) you suspect are not used and recompiling will cause the compiler to tell you if the type is used or not.

Given that FPC is a fairly quick compiler, using this "brute-force" approach, it should be possible to verify/check quite a few types for use/no-use in a relatively short period of time (maybe a few days at most for a mid-size project that uses a lot of types.)

HTH.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4715
  • I like bugs.
Re: unused type declarations
« Reply #4 on: August 05, 2023, 08:26:03 pm »
For clean up purposes maybe just commenting out the data type(s) (preferably one by one) you suspect are not used and recompiling will cause the compiler to tell you if the type is used or not.

Given that FPC is a fairly quick compiler, using this "brute-force" approach, it should be possible to verify/check quite a few types for use/no-use in a relatively short period of time (maybe a few days at most for a mid-size project that uses a lot of types.)
+1
"Find in Files" (Ctrl-Shift-F) in your project source directories is even faster. If a type is only defined but not found anywhere else, it can safely be removed (or commented out).
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12847
  • FPC developer.
Re: [solved] unused type declarations
« Reply #5 on: August 05, 2023, 09:51:46 pm »
Find in files does not handle ifdef. compiling is more reliable.

speter

  • Hero Member
  • *****
  • Posts: 528
Re: [solved] unused type declarations
« Reply #6 on: August 06, 2023, 02:12:36 am »
Thanks everyone. I ended up writing a little program (attached) to do it. It isn't that great (it finds lots of extra crap) but it did the job for me.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

PascalDragon

  • Hero Member
  • *****
  • Posts: 6395
  • Compiler Developer
Re: [solved] unused type declarations
« Reply #7 on: August 06, 2023, 12:27:07 pm »
Is it possible to get a report (or compiler hints) on unused type declarations - like the compiler will mention local variables that are unused?

If hints are enabled (-vh), then the compiler will issue a hint for unused types, but only if they are declared in the implementation section of a unit or in the program/library file. This is because units might be used by different programs that might use different subsets of types, thus the compiler won't issue messages for these as that might lead to you removing types (or variables, etc.) that are used by other programs.

 

TinyPortal © 2005-2018