The supreme court on Thursday struck down a Minnesota law that barred voters in the state from wearing a wide range of political hats, T-shirts and pins to the polls.

Minnesota had defended its law as a reasonable restriction that keeps order at polling places and prevents voter intimidation. But the justices ruled 7-2 that the state’s law is too broad, violating the free speech clause of the first amendment.

Chief Justice John Roberts wrote that “if a state wishes to set its polling places apart as areas free of partisan discord, it must employ a more discernible approach than the one Minnesota has offered here”.

Most states have laws restricting what voters can wear when they cast ballots, but Minnesota’s law was one of the broadest. It barred voters from casting a ballot while wearing clothing with the name of a candidate or political party. Also not allowed: clothing that references an issue on the ballot or promotes a group with recognizable political views. A National Rifle Association T-shirt or shirt with the text of the second amendment wouldn’t be allowed, for example, according to the lawyer who argued the case for the state.

Roberts noted that Minnesota, like other states, had sought to balance a voter’s ability to “engage in political discourse” with the ability to “exercise his civic duty in a setting removed from the clamor and din of electioneering”.

“While that choice is generally worthy of our respect, Minnesota has not supported its good intentions with a law capable of reasoned application,” he wrote.

It is unclear exactly how many states the ruling could affect beyond Minnesota. Both Minnesota and the group challenging the state’s law had said there are about 10 states with laws like Minnesota’s, though they disagreed significantly on which ones, agreeing only on Delaware, New Jersey, New York, Texas and Vermont.

The case before the supreme court dates back to 2010 and involves a dispute that began over Tea Party T-shirts and buttons with the words “Please ID Me”, a reference to legislation then under discussion in Minnesota that would have required residents to show photo identification to vote. The legislation ultimately did not become law.

Pointing to the state’s statute, Minnesota officials said before the election that neither the Tea Party T-shirts nor those buttons would be permitted at the polls. In response, a group of voters and organizations sued.

The supreme court has previously backed some restrictions on voters’ free speech rights at the polls. In 1992, the court upheld a Tennessee statute prohibiting the display or distribution of campaign materials within 100 feet of a polling place.

The case decided Thursday is 16-1435 Minnesota Voters Alliance v Mansky.