The United Kingdom is entitled to withhold child benefit and child tax credits from EU migrants who do not have the right to reside in the UK, European judges ruled today.

Dismissing an action by the European Commission against the UK, the Court of Justice of the European Union said that the UK was not acting contrary to the terms of European legislation by introducing the "right to reside" test. Further, although there was an element of indirect discrimination in its legislation which required claimants to possess a right to reside in the United Kingdom, it was justified on the basis of "protecting" the state's finances.

The ruling relates to conditions introduced in 2004 on the right of migrants from the EU and European Economic Area who are "economically inactive" and their family members to claim certain benefits.

The Commission claimed that by making the benefits subject to a "right to reside" test as well as the habitual residence test in EU Regulation 883/2004, which deals with social security rights of nationals of other EU member states, the UK was imposing an additional condition for which no provision was made in the regulation. Alternatively, the UK legislation was discriminatory on its effect.

Dismissing the main complaint, the court said that the regulation was not intended to lay down the conditions creating the right to social security benefits, and it was in principle for the legislation of each member state to lay down those conditions.

Regarding the alternative case of discrimination the court stated: "it is clear from the court’s case law that the need to protect the finances of the host member state justifies in principle the possibility of checking whether residence is lawful when a social benefit is granted in particular to persons from other member states who are not economically active, as such grant could have consequences for the overall level of assistance which may be accorded by that state".

The checks on right of residence were not carried out systematically (which would have been illegal), but only in specific cases, and the Commission "has not provided evidence or arguments showing that such checking does not satisfy the conditions of proportionality, that it is not appropriate for securing the attainment of the objective of protecting public finances or that it goes beyond what is necessary to attain that objective".

The ruling comes with predictions of a close result in the EU referendum campaign. A decision against the UK would have been seized on by the Leave campaign, and the UK Government will be relieved at the outcome.

Click here to view the judgment of the court.