# Style checking with the MetOffice stylist tool

**URL:** <https://fortran-lang.discourse.group/t/style-checking-with-the-metoffice-stylist-tool/2960>\
**Category:** Uncategorized\
**Created:** [March 11, 2022, 8:05pm UTC](https://fortran-lang.discourse.group/t/style-checking-with-the-metoffice-stylist-tool/2960 "2022-03-11T20:05:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [March 11, 2022, 8:05pm UTC](https://fortran-lang.discourse.group/t/style-checking-with-the-metoffice-stylist-tool/2960/1 "2022-03-11T20:05:11Z")

</div>

The UK Met Office is developing a Python tool called [stylist](https://github.com/MetOffice/stylist) that warns about specified defects in Fortran code. As an example of its capabilities, for the configuration file config.ini

```auto
[file-pipe]
x90=fortran:pfp:fpp

[style.multiple]
rules = FortranCharacterset, MissingOnly, MissingImplicit, MissingPointerInit, KindPattern(integer=r'.*_jazz', real=r'.*dp')

```

and code bad\_style.f90

```auto
module m
contains
function sum_mat(x) result(xsum)
real*8, intent(in) :: x(:,:)
xsum = 0.0
do i=1,size(x,1)
   do j=1,size(x,2)
      xsum = xsum + x(i,j)
   end do
end do
end function sum_mat
end module m
!
program main
use m
real*8 :: x(2,3)
integer, pointer :: p
	x = 10.0
print*,sum_mat(x)
allocate(p)
p = 5
print*,"p=",p
end program main

```

running `stylist -verbose -configuration config.ini bad_style.f90` gives output

```auto
Examining: bad_style.f90
Style: Style
Rule: FortranCharacterset - Failed
Rule: MissingOnly - Failed
Rule: MissingImplicit - Failed
Rule: MissingPointerInit - Failed
Rule: KindPattern - Failed
bad_style.f90: 1: Module 'm' is missing an implicit statement
bad_style.f90: 4: Kind '*8' found for real variable 'x(:, :)' does not fit the pattern /.*dp/.
bad_style.f90: 14: Program 'main' is missing an implicit statement
bad_style.f90: 15: Usage of "m" without "only" clause.
bad_style.f90: 16: Kind '*8' found for real variable 'x(2, 3)' does not fit the pattern /.*dp/.
bad_style.f90: 17: Declaration of pointer "p" without initialisation.
bad_style.f90: 18: Found character '\t' not in Fortran character set
Found 7 issues

```

and for the improved code good\_style.f90

```auto
module m
implicit none
integer, parameter :: dp = kind(1.0d0)  
contains
function sum_mat(x) result(xsum)
real(kind=dp), intent(in) :: x(:,:)
real(kind=dp) :: xsum
integer :: i,j
xsum = 0.0
do i=1,size(x,1)
   do j=1,size(x,2)
      xsum = xsum + x(i,j)
   end do
end do
end function sum_mat
end module m
!
program main
use m, only: dp, sum_mat
implicit none
real(kind=dp) :: x(2,3)
integer, pointer :: p => null()
x = 10.0
print*,sum_mat(x)
allocate(p)
p = 5
print*,"p=",p
end program main

```

the output of `stylist -verbose -configuration config.ini good_style.f90` is

```auto
Examining: good_style.f90
Style: Style
Rule: FortranCharacterset - Passed
Rule: MissingOnly - Passed
Rule: MissingImplicit - Passed
Rule: MissingPointerInit - Passed
Rule: KindPattern - Passed
Found 0 issue

```

I had some trouble installing stylist on Windows (although it should work) but found it easy to install it on WSL2.

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [March 13, 2022, 1:51pm UTC](https://fortran-lang.discourse.group/t/style-checking-with-the-metoffice-stylist-tool/2960/2 "2022-03-13T13:51:27Z")

</div>

> [@Beliavsky](#):
>
> ```auto
> x = 10.0
> 
> ```

One more warning I’d add personally is missing kind specifier in real literal.

It would be nice to see stylist supported as an extension to fpm. The [`extra`](https://fpm.fortran-lang.org/en/spec/manifest.html#additional-free-data-field) section can be used to store settings which are passed to stylist.
