Olympic champion Cesar Cielo took "sufficient precautions" over the use of food supplements which resulted in him failing a doping test, the Court of Arbitration for Sport said on Friday.
The Court was giving its reasons for clearing the Brazilian to compete at the world swimming championships in Shanghai this week.
Sport's highest tribunal said the use of food supplements by athletes was "generally risky" but it ruled against world swimming body FINA's appeal against the decision by the Brazilian swimming authorities to let Cielo off with a warning.
"The CAS panel recognized that the use of food supplements by athletes was generally risky, but that, in the present case, the athletes took sufficient precautions to reduce their fault or negligence to the minimum," said CAS.
Cielo, 24, and three team mates had tested positive for the banned diuretic furosemide in May but their national governing body (CBDA) decided against banning them.
Cielo, who won gold in the 50 and 100 meters freestyle at the 2009 Rome world championships and is the Olympic 50 meters freestyle champion, said the positive test had been caused by a supplement he took regularly that had become contaminated.
CAS last week confirmed the CBDA decision regarding Cielo, allowing him to compete. He won the 50 meters butterfly title on Monday and is in the 50 freestyle final on Saturday.
The court gave similar rulings in the cases of Henrique Barbosa and Nicolas Dos Santos but banned Vinicius Waked for a year as it was a second doping offence in his case
CAS said in its ruling that Cielo's doctor had prescribed caffeine capsules since 2009 and they had been manufactured by the same pharmacy since then.
The pharmacy that prepared the capsules admitted that on the same day it had also made up, for other clients, several prescriptions for the treatment of heart disease, an ingredient of which was furosemide.
CAS added that FINA had accepted that the furosemide was not intended to enhance the performance of the swimmers or mask the use of a performance-enhancing substances.
FINA argued that the mistake was serious enough for a three-month suspension in Cielo's case.